monitor.service.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Injectable } from '@angular/core';
  2. import {HttpClient, HttpParams} from "@angular/common/http";
  3. import {Observable} from "rxjs";
  4. import {Message} from "../pojo/Message";
  5. import {Page} from "../pojo/Page";
  6. import {Monitor} from "../pojo/Monitor";
  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 getMonitorMetricData(monitorId: number, metric: string) : Observable<Message<any>> {
  78. return this.http.get<Message<any>>(`/monitors/${monitorId}/metrics/${metric}`);
  79. }
  80. public getAppsMonitorSummary() : Observable<Message<any>> {
  81. return this.http.get<Message<any>>(summary_uri);
  82. }
  83. }