dashboard.ui.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. let progressTemplate = '<div class="progress rounded-lg w-40 h-2 mr-2 inline-block" style="width:120px;" title="{tip}"><div class="progress-bar {color}" style="width: {rate};"></div></div>';
  2. let statusTemplate = '<span id="instance-status-{id}" data-status="{status}" class="{class}">{text}</span>';
  3. let cpuCircle, memoryCircle, cpuRate, memoryRate, cpuAnimationId, memoryAnimationId, cpuRateStart = 0, memoryRateStart = 0;
  4. window.renderInstanceList = function (result, {col, row, value})
  5. {
  6. if(col.name === 'status')
  7. {
  8. var statusClass = value == 'running' ? 'text-success' : '';
  9. result[0] = {html: statusTemplate.replace('{id}', row.data.id).replace('{status}', row.data.status).replace('{text}', result[0]).replace('{class}', statusClass)};
  10. return result;
  11. }
  12. if(col.name === 'cpu')
  13. {
  14. var rowHtml = progressTemplate.replace('{color}', row.data.cpu.color).replace('{tip}', row.data.cpu.tip).replace('{rate}', row.data.cpu.rate)
  15. result[0] = {html: rowHtml + '<span class="text-gray">' + row.data.cpu.rate + '</span>'};
  16. return result;
  17. }
  18. if(col.name === 'mem')
  19. {
  20. var rowHtml = progressTemplate.replace('{color}', row.data.mem.color).replace('{tip}', row.data.mem.tip).replace('{rate}', row.data.mem.rate)
  21. result[0] = {html: rowHtml + '<span class="text-gray">' + row.data.mem.usage + '/' + row.data.mem.limit + '</span>'};
  22. return result;
  23. }
  24. return result;
  25. }
  26. $(function()
  27. {
  28. if(typeof timer !== 'undefined') clearInterval(timer);
  29. loadCneStatistic();
  30. if(instanceIdList.length === 0) return;
  31. if(inQuickon) timer = setInterval(refreshStatus, 5000);
  32. });
  33. window.onPageUnmount = function()
  34. {
  35. if(typeof timer !== 'undefined') clearInterval(timer);
  36. }
  37. function loadCneStatistic()
  38. {
  39. toggleLoading('#cpu-circle', true);
  40. toggleLoading('#memory-circle', true);
  41. toggleLoading('#status-icon', true);
  42. toggleLoading('.cpu-rate', true);
  43. toggleLoading('.cpu-memory', true);
  44. toggleLoading('#cne-statistic .cne-status', true);
  45. toggleLoading('#cne-statistic .node-quantity', true);
  46. $.ajaxSubmit({
  47. url: $.createLink('system', 'ajaxCneMetrics'),
  48. onComplete:function(res)
  49. {
  50. if(res.result === 'success')
  51. {
  52. let cpuInfo = res.data.cpuInfo, memoryInfo = res.data.memoryInfo, cpuLoading = true, memoryLoading = true;
  53. cpuInfo.tip = cpuInfo.tip.substring(cpuInfo.tip.indexOf('=') + 1).trim();
  54. memoryInfo.tip = memoryInfo.tip.substring(memoryInfo.tip.indexOf('=') + 1).trim();
  55. cpuRate = cpuInfo.rate;
  56. memoryRate = memoryInfo.rate;
  57. if(typeof $('#cpu-circle').zui() === 'undefined')
  58. {
  59. $('#cpu-circle div').remove();
  60. }
  61. else
  62. {
  63. cpuLoading = false;
  64. $('#cpu-circle').remove();
  65. $('.cpu-circle').prepend('<div id="cpu-circle" class="relative"><span class="absolute text-lg" style="transform: translate(-50%, -50%); top: 50%; left: 50%; white-space: nowrap;"><i class="icon icon-cpu mr-1" style="font-size: 20px; color: gray;"></i>' + cpuUsage + '</span></div>')
  66. }
  67. if(typeof $('#memory-circle').zui() === 'undefined')
  68. {
  69. $('#memory-circle div').remove();
  70. }
  71. else
  72. {
  73. memoryLoading = false;
  74. $('#memory-circle').remove();
  75. $('.memory-circle').prepend('<div id="memory-circle" class="relative"><span class="absolute text-lg" style="transform: translate(-50%, -50%); top: 50%; left: 50%; white-space: nowrap;"><i class="icon icon-memory mr-1" style="font-size: 20px; color: gray;"></i>' + memUsage + '</span></div>')
  76. }
  77. cpuCircle = new zui.ProgressCircle('#cpu-circle', {percent: 0, size: 160, circleColor: cpuInfo.color, circleWidth: 8, text: ''});
  78. memoryCircle = new zui.ProgressCircle('#memory-circle', {percent: 0, size: 160, circleColor: memoryInfo.color, circleWidth: 8, text: ''});
  79. $('#status-icon').replaceWith('<i class="icon icon-exclamation-pure app-status-circle icon-' + statusIcons[res.data.status] + ' status-' + res.data.status + ' " style="font-size: 30px;"></i>');
  80. $('#cne-statistic .cne-status').text(statusList[res.data.status]);
  81. const nodeQuantity = res.data.node_count == res.data.ready_count ? res.data.ready_count : res.data.ready_count + '/' + res.data.node_count;
  82. $('#cne-statistic .node-quantity').text(nodeQuantity);
  83. const nodeError = res.data.nodeError;
  84. if(!nodeError)
  85. {
  86. $('#cne-statistic .node-quantity-help').addClass('hidden');
  87. }
  88. else
  89. {
  90. $('#cne-statistic .node-quantity-help').removeClass('hidden').attr('title', nodeError);
  91. }
  92. $('#cne-statistic .icon-cpu').css('color' , cpuInfo.color);
  93. $('#cne-statistic .icon-memory').css('color' , memoryInfo.color);
  94. $('.cpu-rate').html(cpuInfo.rate + '<span class="text-xl ml-1">%</span>');
  95. $('.memory-rate').html(memoryInfo.rate + '<span class="text-xl ml-1">%</span>');
  96. let element = document.querySelector(".cpu-rate"),textNode = document.createTextNode(cpuInfo.tip);
  97. element.parentNode.insertBefore(textNode, element.nextSibling);
  98. element = document.querySelector(".memory-rate");
  99. textNode = document.createTextNode(memoryInfo.tip);
  100. element.parentNode.insertBefore(textNode, element.nextSibling);
  101. toggleLoading('#cne-statistic .cne-status', false);
  102. toggleLoading('#cne-statistic .node-quantity', false);
  103. if(cpuLoading) toggleLoading('#cpu-circle', false);
  104. if(memoryLoading) toggleLoading('#memory-circle', false);
  105. toggleLoading('.cpu-rate', false);
  106. toggleLoading('.cpu-memory', false);
  107. loadProgressCircle();
  108. }
  109. else
  110. {
  111. toggleLoading('#cpu-circle', false);
  112. toggleLoading('#memory-circle', false);
  113. toggleLoading('#status-icon', false);
  114. toggleLoading('.cpu-rate', false);
  115. toggleLoading('.cpu-memory', false);
  116. toggleLoading('#cne-statistic .cne-status', false);
  117. toggleLoading('#cne-statistic .node-quantity', false);
  118. }
  119. }
  120. });
  121. }
  122. function loadProgressCircle()
  123. {
  124. loadCpuProgressCircle();
  125. loadMemoryProgressCircle();
  126. }
  127. function loadCpuProgressCircle()
  128. {
  129. if(++cpuRateStart >= Math.floor(cpuRate))
  130. {
  131. cpuCircle.render({percent: cpuRate});
  132. cancelAnimationFrame(cpuAnimationId);
  133. }
  134. else if(cpuRateStart < Math.floor(cpuRate))
  135. {
  136. cpuCircle.render({percent: cpuRateStart});
  137. cpuAnimationId = requestAnimationFrame(loadCpuProgressCircle);
  138. }
  139. }
  140. function loadMemoryProgressCircle()
  141. {
  142. if(++memoryRateStart >= Math.floor(memoryRate))
  143. {
  144. memoryCircle.render({percent: memoryRate});
  145. cancelAnimationFrame(memoryAnimationId);
  146. }
  147. else if(memoryRateStart < Math.floor(memoryRate))
  148. {
  149. memoryCircle.render({percent: memoryRateStart});
  150. memoryAnimationId = requestAnimationFrame(loadMemoryProgressCircle);
  151. }
  152. }
  153. function refreshStatus()
  154. {
  155. const postData = new FormData();
  156. if(instanceIdList.length > 0)
  157. {
  158. instanceIdList.forEach(function(id){postData.append('idList[]', id)});
  159. }
  160. $.ajaxSubmit({
  161. url: $.createLink('instance', 'ajaxStatus'),
  162. method: 'POST',
  163. data: postData,
  164. onComplete: function(res)
  165. {
  166. if(res.result === 'success')
  167. {
  168. $.each(res.data, function(index, instance)
  169. {
  170. if($("#instance-status-" + instance.id).data('status') != instance.status)
  171. {
  172. loadTable();
  173. return;
  174. }
  175. });
  176. }
  177. }
  178. });
  179. }