monitor-data-chart.component.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import { Component, Input, OnInit } from '@angular/core';
  2. import { EChartsOption } from 'echarts';
  3. import { finalize } from 'rxjs/operators';
  4. import { MonitorService } from '../../../service/monitor.service';
  5. @Component({
  6. selector: 'app-monitor-data-chart',
  7. templateUrl: './monitor-data-chart.component.html',
  8. styles: []
  9. })
  10. export class MonitorDataChartComponent implements OnInit {
  11. @Input()
  12. get monitorId(): number {
  13. return this._monitorId;
  14. }
  15. set monitorId(monitorId: number) {
  16. this._monitorId = monitorId;
  17. }
  18. private _monitorId!: number;
  19. @Input()
  20. app!: string;
  21. @Input()
  22. metrics!: string;
  23. @Input()
  24. metric!: string;
  25. @Input()
  26. unit!: string;
  27. eChartOption!: EChartsOption;
  28. lineHistoryTheme!: EChartsOption;
  29. loading: boolean = true;
  30. echartsInstance!: any;
  31. // 查询历史数据时间段 默认最近6小时
  32. timePeriod: string = '6h';
  33. constructor(private monitorSvc: MonitorService) {}
  34. ngOnInit(): void {
  35. this.lineHistoryTheme = {
  36. title: {
  37. text: `${this.metrics}.${this.metric}`,
  38. textStyle: {
  39. fontSize: 16,
  40. fontFamily: 'monospace',
  41. textShadowOffsetX: 10
  42. }
  43. },
  44. toolbox: {
  45. show: true,
  46. orient: 'vertical',
  47. feature: {
  48. dataZoom: {
  49. yAxisIndex: 'none',
  50. title: {
  51. zoom: '区域缩放',
  52. back: '缩放还原'
  53. },
  54. emphasis: {
  55. iconStyle: {
  56. textPosition: 'left'
  57. }
  58. }
  59. },
  60. saveAsImage: {
  61. title: '保存图片',
  62. emphasis: {
  63. iconStyle: {
  64. textPosition: 'left'
  65. }
  66. }
  67. },
  68. myPeriod1h: {
  69. show: true,
  70. title: '查询近1小时',
  71. icon: 'path://M827.871087 196.128913C743.498468 111.756293 631.321596 65.290005 512 65.290005c-119.319549 0-231.499491 46.465265-315.871087 130.837884S65.290005 392.680451 65.290005 512s46.465265 231.499491 130.837884 315.871087 196.551538 130.837884 315.871087 130.837884c119.321596 0 231.499491-46.465265 315.871087-130.837884S958.708971 631.319549 958.708971 512 912.243707 280.500509 827.871087 196.128913zM531.556405 917.246651l0-74.145697c0-11.31572-9.174963-20.491707-20.491707-20.491707-11.316743 0-20.491707 9.174963-20.491707 20.491707l0 74.059739C283.276738 906.322857 116.693746 739.164766 106.755396 531.634176l72.351841 0c11.31572 0 20.491707-9.174963 20.491707-20.491707 0-11.31572-9.174963-20.491707-20.491707-20.491707l-72.273047 0c10.769274-206.737528 177.01253-373.005342 383.740848-383.813502l0 72.346725c0 11.316743 9.174963 20.491707 20.491707 20.491707 11.31572 0 20.491707-9.17394 20.491707-20.491707L531.558451 106.752326c207.593012 9.901511 374.807385 176.539762 385.609405 383.89946l-74.142627 0c-11.316743 0-20.491707 9.174963-20.491707 20.491707 0 11.316743 9.174963 20.491707 20.491707 20.491707l74.220399 0C907.275555 739.78796 739.720422 907.317511 531.556405 917.246651z;M532.098757 503.118726 532.098757 258.240529c0-11.316743-9.174963-20.491707-20.491707-20.491707-11.31572 0-20.491707 9.17394-20.491707 20.491707l0 254.66612c0 7.858992 4.429893 14.677281 10.924817 18.114566L693.447539 722.42757c4.002151 4.000104 9.245572 6.001691 14.490016 6.001691s10.487865-2.001587 14.490016-6.001691c8.002254-8.002254 8.002254-20.977777 0-28.980032L532.098757 503.118726z',
  72. emphasis: {
  73. iconStyle: {
  74. textPosition: 'left'
  75. }
  76. },
  77. onclick: () => {
  78. this.loadData('1h');
  79. }
  80. },
  81. myPeriod6h: {
  82. show: true,
  83. title: '查询近6小时',
  84. icon: 'path://M827.871087 196.128913C743.498468 111.756293 631.321596 65.290005 512 65.290005c-119.319549 0-231.499491 46.465265-315.871087 130.837884S65.290005 392.680451 65.290005 512s46.465265 231.499491 130.837884 315.871087 196.551538 130.837884 315.871087 130.837884c119.321596 0 231.499491-46.465265 315.871087-130.837884S958.708971 631.319549 958.708971 512 912.243707 280.500509 827.871087 196.128913zM531.556405 917.246651l0-74.145697c0-11.31572-9.174963-20.491707-20.491707-20.491707-11.316743 0-20.491707 9.174963-20.491707 20.491707l0 74.059739C283.276738 906.322857 116.693746 739.164766 106.755396 531.634176l72.351841 0c11.31572 0 20.491707-9.174963 20.491707-20.491707 0-11.31572-9.174963-20.491707-20.491707-20.491707l-72.273047 0c10.769274-206.737528 177.01253-373.005342 383.740848-383.813502l0 72.346725c0 11.316743 9.174963 20.491707 20.491707 20.491707 11.31572 0 20.491707-9.17394 20.491707-20.491707L531.558451 106.752326c207.593012 9.901511 374.807385 176.539762 385.609405 383.89946l-74.142627 0c-11.316743 0-20.491707 9.174963-20.491707 20.491707 0 11.316743 9.174963 20.491707 20.491707 20.491707l74.220399 0C907.275555 739.78796 739.720422 907.317511 531.556405 917.246651z;M532.098757 503.118726 532.098757 258.240529c0-11.316743-9.174963-20.491707-20.491707-20.491707-11.31572 0-20.491707 9.17394-20.491707 20.491707l0 254.66612c0 7.858992 4.429893 14.677281 10.924817 18.114566L693.447539 722.42757c4.002151 4.000104 9.245572 6.001691 14.490016 6.001691s10.487865-2.001587 14.490016-6.001691c8.002254-8.002254 8.002254-20.977777 0-28.980032L532.098757 503.118726z',
  85. emphasis: {
  86. iconStyle: {
  87. textPosition: 'left'
  88. }
  89. },
  90. onclick: () => {
  91. this.loadData('6h');
  92. }
  93. },
  94. myPeriod1d: {
  95. show: true,
  96. title: '查询近1天',
  97. icon: 'path://M827.871087 196.128913C743.498468 111.756293 631.321596 65.290005 512 65.290005c-119.319549 0-231.499491 46.465265-315.871087 130.837884S65.290005 392.680451 65.290005 512s46.465265 231.499491 130.837884 315.871087 196.551538 130.837884 315.871087 130.837884c119.321596 0 231.499491-46.465265 315.871087-130.837884S958.708971 631.319549 958.708971 512 912.243707 280.500509 827.871087 196.128913zM531.556405 917.246651l0-74.145697c0-11.31572-9.174963-20.491707-20.491707-20.491707-11.316743 0-20.491707 9.174963-20.491707 20.491707l0 74.059739C283.276738 906.322857 116.693746 739.164766 106.755396 531.634176l72.351841 0c11.31572 0 20.491707-9.174963 20.491707-20.491707 0-11.31572-9.174963-20.491707-20.491707-20.491707l-72.273047 0c10.769274-206.737528 177.01253-373.005342 383.740848-383.813502l0 72.346725c0 11.316743 9.174963 20.491707 20.491707 20.491707 11.31572 0 20.491707-9.17394 20.491707-20.491707L531.558451 106.752326c207.593012 9.901511 374.807385 176.539762 385.609405 383.89946l-74.142627 0c-11.316743 0-20.491707 9.174963-20.491707 20.491707 0 11.316743 9.174963 20.491707 20.491707 20.491707l74.220399 0C907.275555 739.78796 739.720422 907.317511 531.556405 917.246651z;M532.098757 503.118726 532.098757 258.240529c0-11.316743-9.174963-20.491707-20.491707-20.491707-11.31572 0-20.491707 9.17394-20.491707 20.491707l0 254.66612c0 7.858992 4.429893 14.677281 10.924817 18.114566L693.447539 722.42757c4.002151 4.000104 9.245572 6.001691 14.490016 6.001691s10.487865-2.001587 14.490016-6.001691c8.002254-8.002254 8.002254-20.977777 0-28.980032L532.098757 503.118726z',
  98. emphasis: {
  99. iconStyle: {
  100. textPosition: 'left'
  101. }
  102. },
  103. onclick: () => {
  104. this.loadData('1d');
  105. }
  106. },
  107. myPeriod1w: {
  108. show: true,
  109. title: '查询近1周',
  110. icon: 'path://M827.871087 196.128913C743.498468 111.756293 631.321596 65.290005 512 65.290005c-119.319549 0-231.499491 46.465265-315.871087 130.837884S65.290005 392.680451 65.290005 512s46.465265 231.499491 130.837884 315.871087 196.551538 130.837884 315.871087 130.837884c119.321596 0 231.499491-46.465265 315.871087-130.837884S958.708971 631.319549 958.708971 512 912.243707 280.500509 827.871087 196.128913zM531.556405 917.246651l0-74.145697c0-11.31572-9.174963-20.491707-20.491707-20.491707-11.316743 0-20.491707 9.174963-20.491707 20.491707l0 74.059739C283.276738 906.322857 116.693746 739.164766 106.755396 531.634176l72.351841 0c11.31572 0 20.491707-9.174963 20.491707-20.491707 0-11.31572-9.174963-20.491707-20.491707-20.491707l-72.273047 0c10.769274-206.737528 177.01253-373.005342 383.740848-383.813502l0 72.346725c0 11.316743 9.174963 20.491707 20.491707 20.491707 11.31572 0 20.491707-9.17394 20.491707-20.491707L531.558451 106.752326c207.593012 9.901511 374.807385 176.539762 385.609405 383.89946l-74.142627 0c-11.316743 0-20.491707 9.174963-20.491707 20.491707 0 11.316743 9.174963 20.491707 20.491707 20.491707l74.220399 0C907.275555 739.78796 739.720422 907.317511 531.556405 917.246651z;M532.098757 503.118726 532.098757 258.240529c0-11.316743-9.174963-20.491707-20.491707-20.491707-11.31572 0-20.491707 9.17394-20.491707 20.491707l0 254.66612c0 7.858992 4.429893 14.677281 10.924817 18.114566L693.447539 722.42757c4.002151 4.000104 9.245572 6.001691 14.490016 6.001691s10.487865-2.001587 14.490016-6.001691c8.002254-8.002254 8.002254-20.977777 0-28.980032L532.098757 503.118726z',
  111. emphasis: {
  112. iconStyle: {
  113. textPosition: 'left'
  114. }
  115. },
  116. onclick: () => {
  117. this.loadData('1w');
  118. }
  119. },
  120. myPeriod4w: {
  121. show: true,
  122. title: '查询近1月',
  123. icon: 'path://M827.871087 196.128913C743.498468 111.756293 631.321596 65.290005 512 65.290005c-119.319549 0-231.499491 46.465265-315.871087 130.837884S65.290005 392.680451 65.290005 512s46.465265 231.499491 130.837884 315.871087 196.551538 130.837884 315.871087 130.837884c119.321596 0 231.499491-46.465265 315.871087-130.837884S958.708971 631.319549 958.708971 512 912.243707 280.500509 827.871087 196.128913zM531.556405 917.246651l0-74.145697c0-11.31572-9.174963-20.491707-20.491707-20.491707-11.316743 0-20.491707 9.174963-20.491707 20.491707l0 74.059739C283.276738 906.322857 116.693746 739.164766 106.755396 531.634176l72.351841 0c11.31572 0 20.491707-9.174963 20.491707-20.491707 0-11.31572-9.174963-20.491707-20.491707-20.491707l-72.273047 0c10.769274-206.737528 177.01253-373.005342 383.740848-383.813502l0 72.346725c0 11.316743 9.174963 20.491707 20.491707 20.491707 11.31572 0 20.491707-9.17394 20.491707-20.491707L531.558451 106.752326c207.593012 9.901511 374.807385 176.539762 385.609405 383.89946l-74.142627 0c-11.316743 0-20.491707 9.174963-20.491707 20.491707 0 11.316743 9.174963 20.491707 20.491707 20.491707l74.220399 0C907.275555 739.78796 739.720422 907.317511 531.556405 917.246651z;M532.098757 503.118726 532.098757 258.240529c0-11.316743-9.174963-20.491707-20.491707-20.491707-11.31572 0-20.491707 9.17394-20.491707 20.491707l0 254.66612c0 7.858992 4.429893 14.677281 10.924817 18.114566L693.447539 722.42757c4.002151 4.000104 9.245572 6.001691 14.490016 6.001691s10.487865-2.001587 14.490016-6.001691c8.002254-8.002254 8.002254-20.977777 0-28.980032L532.098757 503.118726z',
  124. emphasis: {
  125. iconStyle: {
  126. textPosition: 'left'
  127. }
  128. },
  129. onclick: () => {
  130. this.loadData('4w');
  131. }
  132. },
  133. myRefresh: {
  134. show: true,
  135. title: '刷新',
  136. icon: 'path://M663.881 339.763l274.021-0.742 0.058-13.271 0.699 0c-0.204-0.48-0.495-0.945-0.699-1.426L938.658 65l-23.776 0.044L914.3 280.41C835.9 151.374 694.321 65 532.342 65c-246.869 0-447 200.132-447 447 0 246.84 200.131 447 447 447 180.343 0 335.657-106.919 406.316-260.75l-33.176 0C836.948 835.027 695.456 929.2 532.342 929.2c-230.048 0-417.2-187.152-417.2-417.2s187.152-417.2 417.2-417.2c158.895 0 297.068 89.487 367.466 220.547l-235.868 0.64L663.881 339.763z',
  137. emphasis: {
  138. iconStyle: {
  139. textPosition: 'left'
  140. }
  141. },
  142. onclick: () => {
  143. this.loadData();
  144. }
  145. }
  146. }
  147. },
  148. tooltip: {
  149. trigger: 'axis',
  150. formatter: function (params: any) {
  151. let time: number = params[0].value[0];
  152. var date = new Date(time);
  153. let seriesName = params[0].seriesName;
  154. let month = (date.getMonth() + 1).toString().padStart(2, '0');
  155. let day = date.getDate().toString().padStart(2, '0');
  156. if (seriesName == null || seriesName == 'NULL') {
  157. return `${date.getFullYear()}/${month}/${day} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()} -- ${
  158. params[0].value[1]
  159. }`;
  160. } else {
  161. return `${seriesName} ${date.getFullYear()}/${month}/${day} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()} -- ${
  162. params[0].value[1]
  163. }`;
  164. }
  165. }
  166. },
  167. xAxis: {
  168. type: 'time',
  169. splitLine: {
  170. show: false
  171. }
  172. },
  173. yAxis: {
  174. type: 'value',
  175. boundaryGap: [0, '100%'],
  176. splitLine: {
  177. show: true
  178. }
  179. }
  180. };
  181. if (this.unit != undefined || this.unit != null) {
  182. // @ts-ignore
  183. this.lineHistoryTheme.title?.subtext = `单位 ${this.unit}`;
  184. }
  185. this.loadData();
  186. }
  187. loadData(timePeriod?: string) {
  188. if (timePeriod != undefined) {
  189. this.timePeriod = timePeriod;
  190. }
  191. // 读取指标历史数据
  192. this.loading = true;
  193. let metricData$ = this.monitorSvc
  194. .getMonitorMetricHistoryData(this.monitorId, this.app, this.metrics, this.metric, this.timePeriod, false)
  195. .pipe(
  196. finalize(() => {
  197. this.loading = false;
  198. metricData$.unsubscribe();
  199. })
  200. )
  201. .subscribe(
  202. message => {
  203. if (message.code === 0 && message.data.values != undefined) {
  204. let values: Record<string, any> = message.data.values;
  205. let legend: string[] = [];
  206. Object.keys(values).forEach(key => {
  207. legend.push(key);
  208. });
  209. if (legend.length > 1) {
  210. this.lineHistoryTheme.legend = {
  211. orient: 'vertical',
  212. data: legend
  213. };
  214. }
  215. Object.keys(values).forEach(key => {
  216. let seriesData: Array<{ value: any }> = [];
  217. values[key].forEach((item: { time: number; origin: any }) => {
  218. seriesData.push({
  219. value: [item.time, item.origin]
  220. });
  221. });
  222. this.lineHistoryTheme.series = [];
  223. this.lineHistoryTheme.series.push({
  224. name: key,
  225. type: 'line',
  226. smooth: true,
  227. showSymbol: false,
  228. data: seriesData
  229. });
  230. this.eChartOption = this.lineHistoryTheme;
  231. if (this.echartsInstance != undefined) {
  232. this.echartsInstance.setOption(this.eChartOption, {
  233. replaceMerge: ['xAxis', 'yAxis', 'series']
  234. });
  235. }
  236. });
  237. } else {
  238. this.eChartOption = this.lineHistoryTheme;
  239. this.eChartOption.title = {
  240. text: `${this.metrics}.${this.metric}` + '\n\n\n' + '暂无数据',
  241. textStyle: {
  242. fontSize: 16,
  243. fontFamily: 'monospace',
  244. textShadowOffsetX: 10
  245. },
  246. left: 'center',
  247. top: 'center'
  248. };
  249. if (this.echartsInstance != undefined) {
  250. this.echartsInstance.setOption(this.eChartOption, {
  251. replaceMerge: ['title']
  252. });
  253. }
  254. }
  255. },
  256. error => {
  257. console.error(error.msg);
  258. }
  259. );
  260. }
  261. onChartInit(ec: any) {
  262. this.echartsInstance = ec;
  263. }
  264. }