index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. function formatDate(time) {
  2. var date = new Date(time);
  3.   var YY = date.getFullYear() + '-';
  4.   var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  5.   var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
  6.   var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  7.   var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  8.   var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  9.   return YY + MM + DD +" "+hh + mm + ss;
  10. }
  11. // 查看详细数据
  12. function bindQueryDataDetailEvent() {
  13. var $queryData = $(".queryData");
  14. $queryData.unbind("click");
  15. $queryData.click(function () {
  16. var json = $(this).parent().find("div").text();
  17. var html = '<div class="row driver_break_word">' + json + '</div>';
  18. BootstrapDialog.show({
  19. title: "注意信息安全",
  20. type: BootstrapDialog.TYPE_INFO,
  21. message: html,
  22. size: BootstrapDialog.SIZE_NORMAL,
  23. buttons: [{
  24. label: "关闭",
  25. action: function (dialog) {
  26. dialog.close();
  27. }
  28. }]
  29. });
  30. });
  31. }
  32. // 查看详细数据日志
  33. function bindQueryErrorDetailEvent() {
  34. var $queryData = $(".queryError");
  35. $queryData.unbind("click");
  36. $queryData.click(function () {
  37. var json = $(this).text();
  38. var html = '<div class="row driver_break_word">' + json + '</div>';
  39. BootstrapDialog.show({
  40. title: "异常详细",
  41. type: BootstrapDialog.TYPE_INFO,
  42. message: html,
  43. size: BootstrapDialog.SIZE_NORMAL,
  44. buttons: [{
  45. label: "关闭",
  46. action: function (dialog) {
  47. dialog.close();
  48. }
  49. }]
  50. });
  51. });
  52. }
  53. // 清空数据
  54. function bindClearEvent($btn, $title, $msg, $url){
  55. $btn.click(function () {
  56. var $id = $(this).attr("id");
  57. var data = {"id": $id};
  58. BootstrapDialog.show({
  59. title: "警告",
  60. type: BootstrapDialog.TYPE_DANGER,
  61. message: $title,
  62. size: BootstrapDialog.SIZE_NORMAL,
  63. buttons: [{
  64. label: "确定",
  65. action: function (dialog) {
  66. doPoster($url, data, function (data) {
  67. if (data.success == true) {
  68. bootGrowl($msg, "success");
  69. doLoader('/monitor?id=' + $id);
  70. } else {
  71. bootGrowl(data.resultValue, "danger");
  72. }
  73. });
  74. dialog.close();
  75. }
  76. }, {
  77. label: "取消",
  78. action: function (dialog) {
  79. dialog.close();
  80. }
  81. }]
  82. });
  83. });
  84. }
  85. // 显示更多
  86. function showMore($this, $url, $params, $call){
  87. $params.pageNum = parseInt($this.attr("num")) + 1;
  88. $params.pageSize = 10;
  89. doGetter($url, $params, function (data) {
  90. if (data.success == true) {
  91. if(data.resultValue.data.length > 0){
  92. $this.attr("num", $params.pageNum);
  93. }
  94. $call(data.resultValue);
  95. } else {
  96. bootGrowl(data.resultValue, "danger");
  97. }
  98. });
  99. }
  100. // 查看数据
  101. function bindQueryDataEvent() {
  102. $("#queryDataBtn").click(function () {
  103. var keyword = $("#searchDataKeyword").val();
  104. var id = $("#searchMetaData").select2("val");
  105. var success = $("#searchDataSuccess").select2("val");
  106. doGetter('/monitor/queryData', {"error": keyword, "success": success, "id" : id, "pageNum" : 1, "pageSize" : 10}, function (data) {
  107. if (data.success == true) {
  108. refreshDataList(data.resultValue);
  109. } else {
  110. bootGrowl(data.resultValue, "danger");
  111. }
  112. });
  113. });
  114. }
  115. function bindQueryDataMoreEvent() {
  116. $("#queryDataMore").click(function () {
  117. var keyword = $("#searchDataKeyword").val();
  118. var id = $("#searchMetaData").select2("val");
  119. var success = $("#searchDataSuccess").select2("val");
  120. showMore($(this), '/monitor/queryData', {"error": keyword, "success": success, "id" : id}, function(resultValue){
  121. refreshDataList(resultValue, true)
  122. });
  123. });
  124. }
  125. function refreshDataList(resultValue, append){
  126. var $dataList = $("#dataList");
  127. var $dataTotal = $("#dataTotal");
  128. var html = showData($dataList, resultValue.data, append);
  129. if(append){
  130. $dataList.append(html);
  131. }else{
  132. $dataList.html(html);
  133. $("#queryDataMore").attr("num", 1);
  134. }
  135. $dataTotal.html(resultValue.total);
  136. bindQueryDataDetailEvent();
  137. bindQueryErrorDetailEvent();
  138. }
  139. function showData($dataList, arr, append){
  140. var html = '';
  141. var size = arr.length;
  142. if(size > 0){
  143. var start = append ? $dataList.find("tr").size() : 0;
  144. for(i = 0; i < size; i++) {
  145. html += '<tr>';
  146. html += '<td>' + (start + i + 1) + '</td>';
  147. html += '<td>' + arr[i].event + '</td>';
  148. html += '<td>' + (arr[i].success ? '<span class="label label-success">成功</span>' : '<span class="label label-warning">失败</span>') + '</td>';
  149. html += '<td style="max-width:100px;" class="dbsyncer_over_hidden"><a href="javascript:;" class="dbsyncer_pointer queryError">' + arr[i].error + '</a></td>';
  150. html += '<td>' + formatDate(arr[i].createTime) + '</td>';
  151. html += '<td><a href="javascript:;" class="label label-info queryData">查看数据</a><div class="hidden">' + arr[i].json + '</div></td>';
  152. html += '</tr>';
  153. }
  154. }
  155. return html;
  156. }
  157. // 查看日志
  158. function bindQueryLogEvent() {
  159. $("#queryLogBtn").click(function () {
  160. var keyword = $("#searchLogKeyword").val();
  161. doGetter('/monitor/queryLog', {"json": keyword, "pageNum" : 1, "pageSize" : 10}, function (data) {
  162. if (data.success == true) {
  163. refreshLogList(data.resultValue);
  164. } else {
  165. bootGrowl(data.resultValue, "danger");
  166. }
  167. });
  168. });
  169. }
  170. function bindQueryLogMoreEvent() {
  171. $("#queryLogMore").click(function () {
  172. var keyword = $("#searchLogKeyword").val();
  173. showMore($(this), '/monitor/queryLog', {"json": keyword}, function(resultValue){
  174. refreshLogList(resultValue, true)
  175. });
  176. });
  177. }
  178. function refreshLogList(resultValue, append){
  179. var $logList = $("#logList");
  180. var $logTotal = $("#logTotal");
  181. var html = showLog($logList, resultValue.data, append);
  182. if(append){
  183. $logList.append(html);
  184. }else{
  185. $logList.html(html);
  186. $("#queryLogMore").attr("num", 1);
  187. }
  188. $logTotal.html(resultValue.total);
  189. }
  190. function showLog($logList, arr, append){
  191. var size = arr.length;
  192. var html = '';
  193. if(size > 0){
  194. var start = append ? $logList.find("tr").size() : 0;
  195. for(i = 0; i < size; i++) {
  196. html += '<tr>';
  197. html += '<td>' + (start + i + 1) + '</td>';
  198. html += '<td>' + arr[i].json + '</td>';
  199. html += '<td>' + formatDate(arr[i].createTime) + '</td>';
  200. html += '</tr>';
  201. }
  202. }
  203. return html;
  204. }
  205. // 堆积数据
  206. function showQueueChart(queueUp, queueCapacity){
  207. var option={
  208. title:{
  209. text:"堆积数据",
  210. x:'center',
  211. y: 'top'
  212. },
  213. tooltip : {
  214. formatter: "{a}: {c}",
  215. position: 'top'
  216. },
  217. series: [
  218. {
  219. name: "待处理",
  220. animation: true,
  221. type: 'gauge',
  222. min: 0,
  223. max: queueCapacity,
  224. splitNumber: 5,
  225. axisLine: { // 坐标轴线
  226. lineStyle: { // 属性lineStyle控制线条样式
  227. color: [[0.1, '#5cb85c'], [0.3, '#5bc0de'],[0.8, '#f0ad4e'],[1, '#d9534f']],
  228. width: 10
  229. }
  230. },
  231. axisTick: { // 坐标轴小标记
  232. length: 15, // 属性length控制线长
  233. lineStyle: { // 属性lineStyle控制线条样式
  234. color: 'auto'
  235. }
  236. },
  237. splitLine: { // 分隔线
  238. length: 20, // 属性length控制线长
  239. lineStyle: { // 属性lineStyle(详见lineStyle)控制线条样式
  240. color: 'auto'
  241. }
  242. },
  243. detail: {fontSize:12, offsetCenter:[0,'65%']},
  244. data: [{value: queueUp, name: ''}]
  245. }
  246. ]
  247. };
  248. echarts.init(document.getElementById('queueChart')).setOption(option);
  249. }
  250. // 事件分类
  251. function showEventChart(ins, upd, del){
  252. var option = {
  253. title: {
  254. text: '事件分类',
  255. left: 'center'
  256. },
  257. tooltip: {
  258. trigger: 'item'
  259. },
  260. legend: {
  261. orient: 'vertical',
  262. left: 'left',
  263. },
  264. series: [
  265. {
  266. name: '事件',
  267. type: 'pie',
  268. radius: '50%',
  269. data: [
  270. {value:upd, name:'更新'},
  271. {value:ins, name:'插入'},
  272. {value:del, name:'删除'}
  273. ],
  274. emphasis: {
  275. itemStyle: {
  276. shadowBlur: 10,
  277. shadowOffsetX: 0,
  278. shadowColor: 'rgba(0, 0, 0, 0.5)'
  279. }
  280. }
  281. }
  282. ]
  283. };
  284. echarts.init(document.getElementById('eventChart')).setOption(option);
  285. $("#insertSpan").html(ins);
  286. $("#updateSpan").html(upd);
  287. $("#deleteSpan").html(del);
  288. }
  289. // 统计成功失败
  290. function showTotalChart(success, fail){
  291. var option = {
  292. title: {
  293. text: '已完成数据',
  294. left: 'center'
  295. },
  296. tooltip: {
  297. trigger: 'item'
  298. },
  299. legend: {
  300. orient: 'vertical',
  301. left: 'left',
  302. },
  303. series: [
  304. {
  305. name: '已完成',
  306. type: 'pie',
  307. radius: '50%',
  308. data: [
  309. {value:success, name:'成功'},
  310. {value:fail, name:'失败'}
  311. ],
  312. emphasis: {
  313. itemStyle: {
  314. shadowBlur: 10,
  315. shadowOffsetX: 0,
  316. shadowColor: 'rgba(0, 0, 0, 0.5)'
  317. }
  318. }
  319. }
  320. ]
  321. };
  322. echarts.init(document.getElementById('totalChart')).setOption(option);
  323. $("#totalSpan").html(success + fail);
  324. $("#successSpan").html(success);
  325. $("#failSpan").html(fail);
  326. }
  327. // CPU历史
  328. function showCpuChart(cpu){
  329. var option = {
  330. title : {
  331. show:true,
  332. text: 'CPU(%)',
  333. x:'center',
  334. y: 'bottom'
  335. },
  336. tooltip : {
  337. trigger: 'item',
  338. formatter: "{b} : {c}%"
  339. },
  340. xAxis: {
  341. type: 'category',
  342. data: cpu.name
  343. },
  344. yAxis: {
  345. type: 'value'
  346. },
  347. series: [{
  348. data: cpu.value,
  349. type: 'line'
  350. }]
  351. };
  352. echarts.init(document.getElementById('cpuChart')).setOption(option);
  353. }
  354. // 内存历史
  355. function showMemoryChart(memory){
  356. var option = {
  357. title : {
  358. show:true,
  359. text: '内存(MB)',
  360. x:'center',
  361. y: 'bottom'
  362. },
  363. tooltip : {
  364. trigger: 'item',
  365. formatter: "{b} : {c}MB"
  366. },
  367. xAxis: {
  368. type: 'category',
  369. boundaryGap: false,
  370. data: memory.name
  371. },
  372. yAxis: {
  373. type: 'value'
  374. },
  375. series: [{
  376. data: memory.value,
  377. type: 'line',
  378. areaStyle: {}
  379. }]
  380. };
  381. echarts.init(document.getElementById('memoryChart')).setOption(option);
  382. }
  383. // 指标列表
  384. function showMetricTable(metrics){
  385. var html = '';
  386. $.each(metrics, function(i) {
  387. html += '<tr>';
  388. html += ' <td style="width:5%;">'+ (i + 1) +'</td>';
  389. html += ' <td>'+ '['+ metrics[i].group + ']' + metrics[i].metricName +'</td>';
  390. html += ' <td>'+ metrics[i].detail +'</td>';
  391. html += '</tr>';
  392. });
  393. $("#metricTable").html(html);
  394. }
  395. // 显示图表信息
  396. function showChartTable(){
  397. doGetWithoutLoading("/monitor/queryAppReportMetric",{}, function (data) {
  398. if (data.success == true) {
  399. var report = data.resultValue;
  400. showTotalChart(report.success, report.fail);
  401. showEventChart(report.insert, report.update, report.delete);
  402. showQueueChart(report.queueUp, report.queueCapacity);
  403. showCpuChart(report.cpu);
  404. showMemoryChart(report.memory);
  405. showMetricTable(report.metrics);
  406. } else {
  407. bootGrowl(data.resultValue, "danger");
  408. }
  409. });
  410. }
  411. // 创建定时器
  412. function createTimer(){
  413. clearInterval(timer);
  414. showChartTable();
  415. doGetWithoutLoading("/monitor/getRefreshInterval",{}, function (data) {
  416. if (data.success == true) {
  417. timer = setInterval(function(){
  418. showChartTable();
  419. }, data.resultValue * 1000);
  420. } else {
  421. bootGrowl(data.resultValue, "danger");
  422. }
  423. });
  424. }
  425. $(function () {
  426. // 初始化select2插件
  427. $(".select-control").select2({
  428. width: "100%",
  429. theme: "classic"
  430. });
  431. // 连接类型切换事件
  432. $("#searchMetaData").change(function () {
  433. $("#queryDataBtn").click();
  434. });
  435. // 数据状态切换事件
  436. $("#searchDataSuccess").change(function () {
  437. $("#queryDataBtn").click();
  438. });
  439. bindQueryLogEvent();
  440. bindQueryLogMoreEvent();
  441. bindQueryDataEvent();
  442. bindQueryDataMoreEvent();
  443. bindQueryDataDetailEvent();
  444. bindQueryErrorDetailEvent();
  445. bindClearEvent($(".clearDataBtn"), "确认清空数据?", "清空数据成功!", "/monitor/clearData");
  446. bindClearEvent($(".clearLogBtn"), "确认清空日志?", "清空日志成功!", "/monitor/clearLog");
  447. createTimer();
  448. // 绑定回车事件
  449. $("#searchDataKeyword").keydown(function (e) {
  450. if (e.which == 13) {
  451. $("#queryDataBtn").trigger("click");
  452. }
  453. });
  454. $("#searchLogKeyword").keydown(function (e) {
  455. if (e.which == 13) {
  456. $("#queryLogBtn").trigger("click");
  457. }
  458. });
  459. });