monitor-edit.component.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { Component, OnInit } from '@angular/core';
  2. import { FormGroup } from '@angular/forms';
  3. import { ActivatedRoute, ParamMap, Router } from '@angular/router';
  4. import { TitleService } from '@delon/theme';
  5. import { NzNotificationService } from 'ng-zorro-antd/notification';
  6. import { throwError } from 'rxjs';
  7. import { switchMap } from 'rxjs/operators';
  8. import { Message } from '../../../pojo/Message';
  9. import { Monitor } from '../../../pojo/Monitor';
  10. import { Param } from '../../../pojo/Param';
  11. import { ParamDefine } from '../../../pojo/ParamDefine';
  12. import { AppDefineService } from '../../../service/app-define.service';
  13. import { MonitorService } from '../../../service/monitor.service';
  14. @Component({
  15. selector: 'app-monitor-modify',
  16. templateUrl: './monitor-edit.component.html',
  17. styles: []
  18. })
  19. export class MonitorEditComponent implements OnInit {
  20. constructor(
  21. private appDefineSvc: AppDefineService,
  22. private monitorSvc: MonitorService,
  23. private route: ActivatedRoute,
  24. private router: Router,
  25. private titleSvc: TitleService,
  26. private notifySvc: NzNotificationService
  27. ) {}
  28. paramDefines!: ParamDefine[];
  29. params!: Param[];
  30. paramValueMap = new Map<String, Param>();
  31. monitor = new Monitor();
  32. profileForm: FormGroup = new FormGroup({});
  33. detected: boolean = true;
  34. passwordVisible: boolean = false;
  35. isSpinning: boolean = false;
  36. ngOnInit(): void {
  37. this.route.paramMap
  38. .pipe(
  39. switchMap((paramMap: ParamMap) => {
  40. this.isSpinning = false;
  41. this.passwordVisible = false;
  42. let id = paramMap.get('monitorId');
  43. this.monitor.id = Number(id);
  44. // 查询监控信息
  45. return this.monitorSvc.getMonitor(this.monitor.id);
  46. })
  47. )
  48. .pipe(
  49. switchMap((message: Message<any>) => {
  50. if (message.code === 0) {
  51. this.monitor = message.data.monitor;
  52. this.titleSvc.setTitleByI18n(`monitor.app.${this.monitor.app}`);
  53. if (message.data.params != null) {
  54. message.data.params.forEach((item: Param) => {
  55. this.paramValueMap.set(item.field, item);
  56. });
  57. }
  58. this.params = message.data.params;
  59. this.detected = message.data.detected ? message.data.detected : true;
  60. } else {
  61. console.warn(message.msg);
  62. this.notifySvc.error('查询异常,此监控不存在', message.msg);
  63. return throwError('查询此监控异常');
  64. }
  65. return this.appDefineSvc.getAppParamsDefine(this.monitor.app);
  66. })
  67. )
  68. .subscribe(message => {
  69. if (message.code === 0) {
  70. this.paramDefines = message.data;
  71. this.params = [];
  72. this.paramDefines.forEach(define => {
  73. let param = this.paramValueMap.get(define.field);
  74. if (param === undefined) {
  75. param = new Param();
  76. param.field = define.field;
  77. param.type = define.type === 'number' ? 0 : 1;
  78. if (define.type === 'boolean') {
  79. param.value = false;
  80. }
  81. if (param.field === 'host') {
  82. param.value = this.monitor.host;
  83. }
  84. } else {
  85. if (define.type === 'boolean') {
  86. if (param.value != null) {
  87. param.value = param.value.toLowerCase() == 'true';
  88. } else {
  89. param.value = false;
  90. }
  91. }
  92. }
  93. this.params.push(param);
  94. });
  95. } else {
  96. console.warn(message.msg);
  97. }
  98. });
  99. }
  100. onSubmit() {
  101. // todo 暂时单独设置host属性值
  102. this.params.forEach(param => {
  103. if (param.field === 'host') {
  104. param.value = this.monitor.host;
  105. }
  106. });
  107. let addMonitor = {
  108. detected: this.detected,
  109. monitor: this.monitor,
  110. params: this.params
  111. };
  112. this.isSpinning = true;
  113. this.monitorSvc.editMonitor(addMonitor).subscribe(
  114. message => {
  115. this.isSpinning = false;
  116. if (message.code === 0) {
  117. this.notifySvc.success('修改监控成功', '');
  118. this.router.navigateByUrl(`/monitors?app=${this.monitor.app}`);
  119. } else {
  120. this.notifySvc.error('修改监控失败', message.msg);
  121. }
  122. },
  123. error => {
  124. this.isSpinning = false;
  125. this.notifySvc.error('修改监控失败', error.error.msg);
  126. }
  127. );
  128. }
  129. onDetect() {
  130. // todo 暂时单独设置host属性值
  131. this.params.forEach(param => {
  132. if (param.field === 'host') {
  133. param.value = this.monitor.host;
  134. }
  135. });
  136. let detectMonitor = {
  137. detected: this.detected,
  138. monitor: this.monitor,
  139. params: this.params
  140. };
  141. this.isSpinning = true;
  142. this.monitorSvc.detectMonitor(detectMonitor).subscribe(
  143. message => {
  144. this.isSpinning = false;
  145. if (message.code === 0) {
  146. this.notifySvc.success('探测成功', '');
  147. } else {
  148. this.notifySvc.error('探测失败', message.msg);
  149. }
  150. },
  151. error => {
  152. this.isSpinning = false;
  153. this.notifySvc.error('探测异常', error.error.msg);
  154. }
  155. );
  156. }
  157. onCancel() {
  158. let app = this.monitor.app;
  159. app = app ? app : '';
  160. this.router.navigateByUrl(`/monitors?app=${app}`);
  161. }
  162. }