monitor-new.component.ts 4.2 KB

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