monitor-edit.component.ts 5.0 KB

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