metric.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. var app = angular.module('sentinelDashboardApp');
  2. app.controller('MetricCtl', ['$scope', '$stateParams', 'MetricService', '$interval', '$timeout',
  3. function ($scope, $stateParams, MetricService, $interval, $timeout) {
  4. $scope.charts = [];
  5. $scope.endTime = new Date();
  6. $scope.startTime = new Date();
  7. $scope.startTime.setMinutes($scope.endTime.getMinutes() - 30);
  8. $scope.startTimeFmt = formatDate($scope.startTime);
  9. $scope.endTimeFmt = formatDate($scope.endTime);
  10. function formatDate(date) {
  11. return moment(date).format('YYYY/MM/DD HH:mm:ss');
  12. }
  13. $scope.changeStartTime = function (startTime) {
  14. $scope.startTime = new Date(startTime);
  15. $scope.startTimeFmt = formatDate(startTime);
  16. };
  17. $scope.changeEndTime = function (endTime) {
  18. $scope.endTime = new Date(endTime);
  19. $scope.endTimeFmt = formatDate(endTime);
  20. };
  21. $scope.app = $stateParams.app;
  22. // 数据自动刷新频率
  23. var DATA_REFRESH_INTERVAL = 1000 * 10;
  24. $scope.servicePageConfig = {
  25. pageSize: 6,
  26. currentPageIndex: 1,
  27. totalPage: 1,
  28. totalCount: 0,
  29. };
  30. $scope.servicesChartConfigs = [];
  31. $scope.pageChanged = function (newPageNumber) {
  32. $scope.servicePageConfig.currentPageIndex = newPageNumber;
  33. reInitIdentityDatas();
  34. };
  35. var searchT;
  36. $scope.searchService = function () {
  37. $timeout.cancel(searchT);
  38. searchT = $timeout(function () {
  39. reInitIdentityDatas();
  40. }, 600);
  41. }
  42. var intervalId;
  43. reInitIdentityDatas();
  44. function reInitIdentityDatas() {
  45. $interval.cancel(intervalId);
  46. queryIdentityDatas();
  47. intervalId = $interval(function () {
  48. queryIdentityDatas();
  49. }, DATA_REFRESH_INTERVAL);
  50. };
  51. $scope.$on('$destroy', function () {
  52. $interval.cancel(intervalId);
  53. });
  54. $scope.initAllChart = function () {
  55. //revoke useless charts positively
  56. while($scope.charts.length > 0) {
  57. let chart = $scope.charts.pop();
  58. chart.destroy();
  59. }
  60. $.each($scope.metrics, function (idx, metric) {
  61. if (idx == $scope.metrics.length - 1) {
  62. return;
  63. }
  64. const chart = new G2.Chart({
  65. container: 'chart' + idx,
  66. forceFit: true,
  67. width: 100,
  68. height: 250,
  69. padding: [10, 30, 70, 50]
  70. });
  71. $scope.charts.push(chart);
  72. var maxQps = 0;
  73. for (var i in metric.data) {
  74. var item = metric.data[i];
  75. if (item.passQps > maxQps) {
  76. maxQps = item.passQps;
  77. }
  78. if (item.blockQps > maxQps) {
  79. maxQps = item.blockQps;
  80. }
  81. }
  82. chart.source(metric.data);
  83. chart.scale('timestamp', {
  84. type: 'time',
  85. mask: 'YYYY-MM-DD HH:mm:ss'
  86. });
  87. chart.scale('passQps', {
  88. min: 0,
  89. max: maxQps,
  90. fine: true,
  91. alias: '通过 QPS'
  92. // max: 10
  93. });
  94. chart.scale('blockQps', {
  95. min: 0,
  96. max: maxQps,
  97. fine: true,
  98. alias: '拒绝 QPS',
  99. });
  100. chart.scale('rt', {
  101. min: 0,
  102. fine: true,
  103. });
  104. chart.axis('rt', {
  105. grid: null,
  106. label: null
  107. });
  108. chart.axis('blockQps', {
  109. grid: null,
  110. label: null
  111. });
  112. chart.axis('timestamp', {
  113. label: {
  114. textStyle: {
  115. textAlign: 'center', // 文本对齐方向,可取值为: start center end
  116. fill: '#404040', // 文本的颜色
  117. fontSize: '11', // 文本大小
  118. //textBaseline: 'top', // 文本基准线,可取 top middle bottom,默认为middle
  119. },
  120. autoRotate: false,
  121. formatter: function (text, item, index) {
  122. return text.substring(11, 11 + 5);
  123. }
  124. }
  125. });
  126. chart.legend({
  127. custom: true,
  128. position: 'bottom',
  129. allowAllCanceled: true,
  130. itemFormatter: function (val) {
  131. if ('passQps' === val) {
  132. return '通过 QPS';
  133. }
  134. if ('blockQps' === val) {
  135. return '拒绝 QPS';
  136. }
  137. return val;
  138. },
  139. items: [
  140. { value: 'passQps', marker: { symbol: 'hyphen', stroke: 'green', radius: 5, lineWidth: 2 } },
  141. { value: 'blockQps', marker: { symbol: 'hyphen', stroke: 'blue', radius: 5, lineWidth: 2 } },
  142. //{ value: 'rt', marker: {symbol: 'hyphen', stroke: 'gray', radius: 5, lineWidth: 2} },
  143. ],
  144. onClick: function (ev) {
  145. const item = ev.item;
  146. const value = item.value;
  147. const checked = ev.checked;
  148. const geoms = chart.getAllGeoms();
  149. for (var i = 0; i < geoms.length; i++) {
  150. const geom = geoms[i];
  151. if (geom.getYScale().field === value) {
  152. if (checked) {
  153. geom.show();
  154. } else {
  155. geom.hide();
  156. }
  157. }
  158. }
  159. }
  160. });
  161. chart.line().position('timestamp*passQps').size(1).color('green').shape('smooth');
  162. chart.line().position('timestamp*blockQps').size(1).color('blue').shape('smooth');
  163. //chart.line().position('timestamp*rt').size(1).color('gray').shape('smooth');
  164. G2.track(false);
  165. chart.render();
  166. });
  167. };
  168. $scope.metrics = [];
  169. $scope.emptyObjs = [];
  170. function queryIdentityDatas() {
  171. var params = {
  172. app: $scope.app,
  173. pageIndex: $scope.servicePageConfig.currentPageIndex,
  174. pageSize: $scope.servicePageConfig.pageSize,
  175. desc: $scope.isDescOrder,
  176. searchKey: $scope.serviceQuery
  177. };
  178. MetricService.queryAppSortedIdentities(params).success(function (data) {
  179. $scope.metrics = [];
  180. $scope.emptyObjs = [];
  181. if (data.code === 0 && data.data) {
  182. var metricsObj = data.data.metric;
  183. var identityNames = Object.keys(metricsObj);
  184. if (identityNames.length < 1) {
  185. $scope.emptyServices = true;
  186. } else {
  187. $scope.emptyServices = false;
  188. }
  189. $scope.servicePageConfig.totalPage = data.data.totalPage;
  190. $scope.servicePageConfig.pageSize = data.data.pageSize;
  191. var totalCount = data.data.totalCount;
  192. $scope.servicePageConfig.totalCount = totalCount;
  193. for (i = 0; i < totalCount; i++) {
  194. $scope.emptyObjs.push({});
  195. }
  196. $.each(identityNames, function (idx, identityName) {
  197. var identityDatas = metricsObj[identityName];
  198. var metrics = {};
  199. metrics.resource = identityName;
  200. // metrics.data = identityDatas;
  201. metrics.data = fillZeros(identityDatas);
  202. metrics.shortData = lastOfArray(identityDatas, 6);
  203. $scope.metrics.push(metrics);
  204. });
  205. // push an empty element in the last, for ng-init reasons.
  206. $scope.metrics.push([]);
  207. } else {
  208. $scope.emptyServices = true;
  209. console.log(data.msg);
  210. }
  211. });
  212. };
  213. function fillZeros(metricData) {
  214. if (!metricData || metricData.length == 0) {
  215. return [];
  216. }
  217. var filledData = [];
  218. filledData.push(metricData[0]);
  219. var lastTime = metricData[0].timestamp / 1000;
  220. for (var i = 1; i < metricData.length; i++) {
  221. var curTime = metricData[i].timestamp / 1000;
  222. if (curTime > lastTime + 1) {
  223. for (var j = lastTime + 1; j < curTime; j++) {
  224. filledData.push({
  225. "timestamp": j * 1000,
  226. "passQps": 0,
  227. "blockQps": 0,
  228. "successQps": 0,
  229. "exception": 0,
  230. "rt": 0,
  231. "count": 0
  232. })
  233. }
  234. }
  235. filledData.push(metricData[i]);
  236. lastTime = curTime;
  237. }
  238. return filledData;
  239. }
  240. function lastOfArray(arr, n) {
  241. if (!arr.length) {
  242. return [];
  243. }
  244. var rs = [];
  245. for (i = 0; i < n && i < arr.length; i++) {
  246. rs.push(arr[arr.length - 1 - i]);
  247. }
  248. return rs;
  249. }
  250. $scope.isDescOrder = true;
  251. $scope.setDescOrder = function () {
  252. $scope.isDescOrder = true;
  253. reInitIdentityDatas();
  254. }
  255. $scope.setAscOrder = function () {
  256. $scope.isDescOrder = false;
  257. reInitIdentityDatas();
  258. }
  259. }]);