monitor-edit.component.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. if (define.type === 'number') {
  78. param.type = 0;
  79. } else if (define.type === 'key-value') {
  80. param.type = 3;
  81. } else {
  82. param.type = 1;
  83. }
  84. if (define.type === 'boolean') {
  85. param.value = false;
  86. }
  87. if (param.field === 'host') {
  88. param.value = this.monitor.host;
  89. }
  90. } else {
  91. if (define.type === 'boolean') {
  92. if (param.value != null) {
  93. param.value = param.value.toLowerCase() == 'true';
  94. } else {
  95. param.value = false;
  96. }
  97. }
  98. }
  99. this.params.push(param);
  100. });
  101. } else {
  102. console.warn(message.msg);
  103. }
  104. });
  105. }
  106. onSubmit(formGroup: FormGroup) {
  107. if (formGroup.invalid) {
  108. Object.values(formGroup.controls).forEach(control => {
  109. if (control.invalid) {
  110. control.markAsDirty();
  111. control.updateValueAndValidity({ onlySelf: true });
  112. }
  113. });
  114. return;
  115. }
  116. this.monitor.host = this.monitor.host.trim();
  117. this.monitor.name = this.monitor.name.trim();
  118. // todo 暂时单独设置host属性值
  119. this.params.forEach(param => {
  120. if (param.field === 'host') {
  121. param.value = this.monitor.host;
  122. }
  123. if (param.value != null && typeof param.value == 'string') {
  124. param.value = (param.value as string).trim();
  125. }
  126. });
  127. let addMonitor = {
  128. detected: this.detected,
  129. monitor: this.monitor,
  130. params: this.params
  131. };
  132. this.isSpinning = true;
  133. this.monitorSvc.editMonitor(addMonitor).subscribe(
  134. message => {
  135. this.isSpinning = false;
  136. if (message.code === 0) {
  137. this.notifySvc.success('修改监控成功', '');
  138. this.router.navigateByUrl(`/monitors?app=${this.monitor.app}`);
  139. } else {
  140. this.notifySvc.error('修改监控失败', message.msg);
  141. }
  142. },
  143. error => {
  144. this.isSpinning = false;
  145. this.notifySvc.error('修改监控失败', error.error.msg);
  146. }
  147. );
  148. }
  149. onDetect(formGroup: FormGroup) {
  150. if (formGroup.invalid) {
  151. Object.values(formGroup.controls).forEach(control => {
  152. if (control.invalid) {
  153. control.markAsDirty();
  154. control.updateValueAndValidity({ onlySelf: true });
  155. }
  156. });
  157. return;
  158. }
  159. this.monitor.host = this.monitor.host.trim();
  160. this.monitor.name = this.monitor.name.trim();
  161. // todo 暂时单独设置host属性值
  162. this.params.forEach(param => {
  163. if (param.field === 'host') {
  164. param.value = this.monitor.host;
  165. }
  166. if (param.value != null && typeof param.value == 'string') {
  167. param.value = (param.value as string).trim();
  168. }
  169. });
  170. let detectMonitor = {
  171. detected: this.detected,
  172. monitor: this.monitor,
  173. params: this.params
  174. };
  175. this.isSpinning = true;
  176. this.monitorSvc.detectMonitor(detectMonitor).subscribe(
  177. message => {
  178. this.isSpinning = false;
  179. if (message.code === 0) {
  180. this.notifySvc.success('探测成功', '');
  181. } else {
  182. this.notifySvc.error('探测失败', message.msg);
  183. }
  184. },
  185. error => {
  186. this.isSpinning = false;
  187. this.notifySvc.error('探测异常', error.error.msg);
  188. }
  189. );
  190. }
  191. onCancel() {
  192. let app = this.monitor.app;
  193. app = app ? app : '';
  194. this.router.navigateByUrl(`/monitors?app=${app}`);
  195. }
  196. }