let progressTemplate = '
'; let statusTemplate = '{text}'; let cpuCircle, memoryCircle, cpuRate, memoryRate, cpuAnimationId, memoryAnimationId, cpuRateStart = 0, memoryRateStart = 0; window.renderInstanceList = function (result, {col, row, value}) { if(col.name === 'status') { var statusClass = value == 'running' ? 'text-success' : ''; result[0] = {html: statusTemplate.replace('{id}', row.data.id).replace('{status}', row.data.status).replace('{text}', result[0]).replace('{class}', statusClass)}; return result; } if(col.name === 'cpu') { var rowHtml = progressTemplate.replace('{color}', row.data.cpu.color).replace('{tip}', row.data.cpu.tip).replace('{rate}', row.data.cpu.rate) result[0] = {html: rowHtml + '' + row.data.cpu.rate + ''}; return result; } if(col.name === 'mem') { var rowHtml = progressTemplate.replace('{color}', row.data.mem.color).replace('{tip}', row.data.mem.tip).replace('{rate}', row.data.mem.rate) result[0] = {html: rowHtml + '' + row.data.mem.usage + '/' + row.data.mem.limit + ''}; return result; } return result; } $(function() { if(typeof timer !== 'undefined') clearInterval(timer); loadCneStatistic(); if(instanceIdList.length === 0) return; if(inQuickon) timer = setInterval(refreshStatus, 5000); }); window.onPageUnmount = function() { if(typeof timer !== 'undefined') clearInterval(timer); } function loadCneStatistic() { toggleLoading('#cpu-circle', true); toggleLoading('#memory-circle', true); toggleLoading('#status-icon', true); toggleLoading('.cpu-rate', true); toggleLoading('.cpu-memory', true); toggleLoading('#cne-statistic .cne-status', true); toggleLoading('#cne-statistic .node-quantity', true); $.ajaxSubmit({ url: $.createLink('system', 'ajaxCneMetrics'), onComplete:function(res) { if(res.result === 'success') { let cpuInfo = res.data.cpuInfo, memoryInfo = res.data.memoryInfo, cpuLoading = true, memoryLoading = true; cpuInfo.tip = cpuInfo.tip.substring(cpuInfo.tip.indexOf('=') + 1).trim(); memoryInfo.tip = memoryInfo.tip.substring(memoryInfo.tip.indexOf('=') + 1).trim(); cpuRate = cpuInfo.rate; memoryRate = memoryInfo.rate; if(typeof $('#cpu-circle').zui() === 'undefined') { $('#cpu-circle div').remove(); } else { cpuLoading = false; $('#cpu-circle').remove(); $('.cpu-circle').prepend('
' + cpuUsage + '
') } if(typeof $('#memory-circle').zui() === 'undefined') { $('#memory-circle div').remove(); } else { memoryLoading = false; $('#memory-circle').remove(); $('.memory-circle').prepend('
' + memUsage + '
') } cpuCircle = new zui.ProgressCircle('#cpu-circle', {percent: 0, size: 160, circleColor: cpuInfo.color, circleWidth: 8, text: ''}); memoryCircle = new zui.ProgressCircle('#memory-circle', {percent: 0, size: 160, circleColor: memoryInfo.color, circleWidth: 8, text: ''}); $('#status-icon').replaceWith(''); $('#cne-statistic .cne-status').text(statusList[res.data.status]); const nodeQuantity = res.data.node_count == res.data.ready_count ? res.data.ready_count : res.data.ready_count + '/' + res.data.node_count; $('#cne-statistic .node-quantity').text(nodeQuantity); const nodeError = res.data.nodeError; if(!nodeError) { $('#cne-statistic .node-quantity-help').addClass('hidden'); } else { $('#cne-statistic .node-quantity-help').removeClass('hidden').attr('title', nodeError); } $('#cne-statistic .icon-cpu').css('color' , cpuInfo.color); $('#cne-statistic .icon-memory').css('color' , memoryInfo.color); $('.cpu-rate').html(cpuInfo.rate + '%'); $('.memory-rate').html(memoryInfo.rate + '%'); let element = document.querySelector(".cpu-rate"),textNode = document.createTextNode(cpuInfo.tip); element.parentNode.insertBefore(textNode, element.nextSibling); element = document.querySelector(".memory-rate"); textNode = document.createTextNode(memoryInfo.tip); element.parentNode.insertBefore(textNode, element.nextSibling); toggleLoading('#cne-statistic .cne-status', false); toggleLoading('#cne-statistic .node-quantity', false); if(cpuLoading) toggleLoading('#cpu-circle', false); if(memoryLoading) toggleLoading('#memory-circle', false); toggleLoading('.cpu-rate', false); toggleLoading('.cpu-memory', false); loadProgressCircle(); } else { toggleLoading('#cpu-circle', false); toggleLoading('#memory-circle', false); toggleLoading('#status-icon', false); toggleLoading('.cpu-rate', false); toggleLoading('.cpu-memory', false); toggleLoading('#cne-statistic .cne-status', false); toggleLoading('#cne-statistic .node-quantity', false); } } }); } function loadProgressCircle() { loadCpuProgressCircle(); loadMemoryProgressCircle(); } function loadCpuProgressCircle() { if(++cpuRateStart >= Math.floor(cpuRate)) { cpuCircle.render({percent: cpuRate}); cancelAnimationFrame(cpuAnimationId); } else if(cpuRateStart < Math.floor(cpuRate)) { cpuCircle.render({percent: cpuRateStart}); cpuAnimationId = requestAnimationFrame(loadCpuProgressCircle); } } function loadMemoryProgressCircle() { if(++memoryRateStart >= Math.floor(memoryRate)) { memoryCircle.render({percent: memoryRate}); cancelAnimationFrame(memoryAnimationId); } else if(memoryRateStart < Math.floor(memoryRate)) { memoryCircle.render({percent: memoryRateStart}); memoryAnimationId = requestAnimationFrame(loadMemoryProgressCircle); } } function refreshStatus() { const postData = new FormData(); if(instanceIdList.length > 0) { instanceIdList.forEach(function(id){postData.append('idList[]', id)}); } $.ajaxSubmit({ url: $.createLink('instance', 'ajaxStatus'), method: 'POST', data: postData, onComplete: function(res) { if(res.result === 'success') { $.each(res.data, function(index, instance) { if($("#instance-status-" + instance.id).data('status') != instance.status) { loadTable(); return; } }); } } }); }