monitor-new.component.ts 4.6 KB

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