monitor-edit.component.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. advancedParamDefines!: ParamDefine[];
  31. advancedParams!: Param[];
  32. paramValueMap = new Map<String, Param>();
  33. monitor = new Monitor();
  34. profileForm: FormGroup = new FormGroup({});
  35. detected: boolean = true;
  36. passwordVisible: boolean = false;
  37. isSpinning: boolean = false;
  38. ngOnInit(): void {
  39. this.route.paramMap
  40. .pipe(
  41. switchMap((paramMap: ParamMap) => {
  42. this.isSpinning = false;
  43. this.passwordVisible = false;
  44. let id = paramMap.get('monitorId');
  45. this.monitor.id = Number(id);
  46. // 查询监控信息
  47. return this.monitorSvc.getMonitor(this.monitor.id);
  48. })
  49. )
  50. .pipe(
  51. switchMap((message: Message<any>) => {
  52. if (message.code === 0) {
  53. this.monitor = message.data.monitor;
  54. this.titleSvc.setTitleByI18n(`monitor.app.${this.monitor.app}`);
  55. if (message.data.params != null) {
  56. message.data.params.forEach((item: Param) => {
  57. this.paramValueMap.set(item.field, item);
  58. });
  59. }
  60. this.detected = message.data.detected ? message.data.detected : true;
  61. } else {
  62. console.warn(message.msg);
  63. this.notifySvc.error('查询异常,此监控不存在', message.msg);
  64. return throwError('查询此监控异常');
  65. }
  66. return this.appDefineSvc.getAppParamsDefine(this.monitor.app);
  67. })
  68. )
  69. .subscribe(message => {
  70. if (message.code === 0) {
  71. this.params = [];
  72. this.advancedParams = [];
  73. this.paramDefines = [];
  74. this.advancedParamDefines = [];
  75. message.data.forEach(define => {
  76. let param = this.paramValueMap.get(define.field);
  77. if (param === undefined) {
  78. param = new Param();
  79. param.field = define.field;
  80. if (define.type === 'number') {
  81. param.type = 0;
  82. } else if (define.type === 'key-value') {
  83. param.type = 3;
  84. } else {
  85. param.type = 1;
  86. }
  87. if (define.type === 'boolean') {
  88. param.value = false;
  89. }
  90. if (param.field === 'host') {
  91. param.value = this.monitor.host;
  92. }
  93. } else {
  94. if (define.type === 'boolean') {
  95. if (param.value != null) {
  96. param.value = param.value.toLowerCase() == 'true';
  97. } else {
  98. param.value = false;
  99. }
  100. }
  101. }
  102. if (define.hide) {
  103. this.advancedParams.push(param);
  104. this.advancedParamDefines.push(define);
  105. } else {
  106. this.params.push(param);
  107. this.paramDefines.push(define);
  108. }
  109. });
  110. } else {
  111. console.warn(message.msg);
  112. }
  113. });
  114. }
  115. onParamBooleanChanged(booleanValue: boolean, field: string) {
  116. // 对SSL的端口联动处理, 不开启SSL默认80端口,开启SSL默认443
  117. if (field === 'ssl') {
  118. this.params.forEach(param => {
  119. if (param.field === 'port') {
  120. if (booleanValue) {
  121. param.value = '443';
  122. } else {
  123. param.value = '80';
  124. }
  125. }
  126. });
  127. }
  128. }
  129. onSubmit(formGroup: FormGroup) {
  130. if (formGroup.invalid) {
  131. Object.values(formGroup.controls).forEach(control => {
  132. if (control.invalid) {
  133. control.markAsDirty();
  134. control.updateValueAndValidity({ onlySelf: true });
  135. }
  136. });
  137. return;
  138. }
  139. this.monitor.host = this.monitor.host.trim();
  140. this.monitor.name = this.monitor.name.trim();
  141. // todo 暂时单独设置host属性值
  142. this.params.forEach(param => {
  143. if (param.field === 'host') {
  144. param.value = this.monitor.host;
  145. }
  146. if (param.value != null && typeof param.value == 'string') {
  147. param.value = (param.value as string).trim();
  148. }
  149. });
  150. this.advancedParams.forEach(param => {
  151. if (param.value != null && typeof param.value == 'string') {
  152. param.value = (param.value as string).trim();
  153. }
  154. });
  155. let addMonitor = {
  156. detected: this.detected,
  157. monitor: this.monitor,
  158. params: this.params.concat(this.advancedParams)
  159. };
  160. this.isSpinning = true;
  161. this.monitorSvc.editMonitor(addMonitor).subscribe(
  162. message => {
  163. this.isSpinning = false;
  164. if (message.code === 0) {
  165. this.notifySvc.success('修改监控成功', '');
  166. this.router.navigateByUrl(`/monitors?app=${this.monitor.app}`);
  167. } else {
  168. this.notifySvc.error('修改监控失败', message.msg);
  169. }
  170. },
  171. error => {
  172. this.isSpinning = false;
  173. this.notifySvc.error('修改监控失败', error.error.msg);
  174. }
  175. );
  176. }
  177. onDetect(formGroup: FormGroup) {
  178. if (formGroup.invalid) {
  179. Object.values(formGroup.controls).forEach(control => {
  180. if (control.invalid) {
  181. control.markAsDirty();
  182. control.updateValueAndValidity({ onlySelf: true });
  183. }
  184. });
  185. return;
  186. }
  187. this.monitor.host = this.monitor.host.trim();
  188. this.monitor.name = this.monitor.name.trim();
  189. // todo 暂时单独设置host属性值
  190. this.params.forEach(param => {
  191. if (param.field === 'host') {
  192. param.value = this.monitor.host;
  193. }
  194. if (param.value != null && typeof param.value == 'string') {
  195. param.value = (param.value as string).trim();
  196. }
  197. });
  198. this.advancedParams.forEach(param => {
  199. if (param.value != null && typeof param.value == 'string') {
  200. param.value = (param.value as string).trim();
  201. }
  202. });
  203. let detectMonitor = {
  204. detected: this.detected,
  205. monitor: this.monitor,
  206. params: this.params.concat(this.advancedParams)
  207. };
  208. this.isSpinning = true;
  209. this.monitorSvc.detectMonitor(detectMonitor).subscribe(
  210. message => {
  211. this.isSpinning = false;
  212. if (message.code === 0) {
  213. this.notifySvc.success('探测成功', '');
  214. } else {
  215. this.notifySvc.error('探测失败', message.msg);
  216. }
  217. },
  218. error => {
  219. this.isSpinning = false;
  220. this.notifySvc.error('探测异常', error.error.msg);
  221. }
  222. );
  223. }
  224. onCancel() {
  225. let app = this.monitor.app;
  226. app = app ? app : '';
  227. this.router.navigateByUrl(`/monitors?app=${app}`);
  228. }
  229. }