monitor.service.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { HttpClient, HttpParams } from '@angular/common/http';
  2. import { Injectable } from '@angular/core';
  3. import { Observable } from 'rxjs';
  4. import { Message } from '../pojo/Message';
  5. import { Monitor } from '../pojo/Monitor';
  6. import { Page } from '../pojo/Page';
  7. const monitor_uri = '/monitor';
  8. const monitors_uri = '/monitors';
  9. const detect_monitor_uri = '/monitor/detect';
  10. const manage_monitors_uri = '/monitors/manage';
  11. const summary_uri = '/summary';
  12. @Injectable({
  13. providedIn: 'root'
  14. })
  15. export class MonitorService {
  16. constructor(private http: HttpClient) {}
  17. public newMonitor(body: any): Observable<Message<any>> {
  18. return this.http.post<Message<any>>(monitor_uri, body);
  19. }
  20. public editMonitor(body: any): Observable<Message<any>> {
  21. return this.http.put<Message<any>>(monitor_uri, body);
  22. }
  23. public deleteMonitor(monitorId: number): Observable<Message<any>> {
  24. return this.http.delete<Message<any>>(`${monitor_uri}/${monitorId}`);
  25. }
  26. public deleteMonitors(monitorIds: Set<number>): Observable<Message<any>> {
  27. let httpParams = new HttpParams();
  28. monitorIds.forEach(monitorId => {
  29. // 注意HttpParams是不可变对象 需要保存append后返回的对象为最新对象
  30. // append方法可以叠加同一key, set方法会把key之前的值覆盖只留一个key-value
  31. httpParams = httpParams.append('ids', monitorId);
  32. });
  33. const options = { params: httpParams };
  34. return this.http.delete<Message<any>>(monitors_uri, options);
  35. }
  36. public cancelManageMonitors(monitorIds: Set<number>): Observable<Message<any>> {
  37. let httpParams = new HttpParams();
  38. monitorIds.forEach(monitorId => {
  39. // 注意HttpParams是不可变对象 需要保存append后返回的对象为最新对象
  40. // append方法可以叠加同一key, set方法会把key之前的值覆盖只留一个key-value
  41. httpParams = httpParams.append('ids', monitorId);
  42. });
  43. const options = { params: httpParams };
  44. return this.http.delete<Message<any>>(manage_monitors_uri, options);
  45. }
  46. public enableManageMonitors(monitorIds: Set<number>): Observable<Message<any>> {
  47. let httpParams = new HttpParams();
  48. monitorIds.forEach(monitorId => {
  49. httpParams = httpParams.append('ids', monitorId);
  50. });
  51. const options = { params: httpParams };
  52. return this.http.get<Message<any>>(manage_monitors_uri, options);
  53. }
  54. public detectMonitor(body: any): Observable<Message<any>> {
  55. return this.http.post<Message<any>>(detect_monitor_uri, body);
  56. }
  57. public getMonitor(monitorId: number): Observable<Message<any>> {
  58. return this.http.get<Message<any>>(`${monitor_uri}/${monitorId}`);
  59. }
  60. public getMonitorsByApp(app: string): Observable<Message<Monitor[]>> {
  61. return this.http.get<Message<Monitor[]>>(`${monitors_uri}/${app}`);
  62. }
  63. public getMonitors(app: string, pageIndex: number, pageSize: number): Observable<Message<Page<Monitor>>> {
  64. app = app.trim();
  65. pageIndex = pageIndex ? pageIndex : 0;
  66. pageSize = pageSize ? pageSize : 8;
  67. // 注意HttpParams是不可变对象 需要保存set后返回的对象为最新对象
  68. let httpParams = new HttpParams();
  69. httpParams = httpParams.appendAll({
  70. app: app,
  71. pageIndex: pageIndex,
  72. pageSize: pageSize
  73. });
  74. const options = { params: httpParams };
  75. return this.http.get<Message<Page<Monitor>>>(monitors_uri, options);
  76. }
  77. public searchMonitors(monitorName: string, monitorHost: string, pageIndex: number, pageSize: number): Observable<Message<Page<Monitor>>> {
  78. pageIndex = pageIndex ? pageIndex : 0;
  79. pageSize = pageSize ? pageSize : 8;
  80. // 注意HttpParams是不可变对象 需要保存set后返回的对象为最新对象
  81. let httpParams = new HttpParams();
  82. httpParams = httpParams.appendAll({
  83. name: monitorName,
  84. host: monitorHost,
  85. pageIndex: pageIndex,
  86. pageSize: pageSize
  87. });
  88. const options = { params: httpParams };
  89. return this.http.get<Message<Page<Monitor>>>(monitors_uri, options);
  90. }
  91. public getMonitorMetricsData(monitorId: number, metrics: string): Observable<Message<any>> {
  92. return this.http.get<Message<any>>(`/monitor/${monitorId}/metrics/${metrics}`);
  93. }
  94. public getAppsMonitorSummary(): Observable<Message<any>> {
  95. return this.http.get<Message<any>>(summary_uri);
  96. }
  97. }