|
| 1 | +class Cpuinfo { |
| 2 | + constructor(parentId) { |
| 3 | + if (!parentId) throw "Missing parameters"; |
| 4 | + |
| 5 | + this.si = require("systeminformation"); |
| 6 | + |
| 7 | + // Create initial DOM |
| 8 | + this.parent = document.getElementById(parentId); |
| 9 | + this.parent.innerHTML += `<div id="mod_cpuinfo"> |
| 10 | + <div id="mod_cpuinfo_innercontainer"></div> |
| 11 | + </div>`; |
| 12 | + this.container = document.getElementById("mod_cpuinfo_innercontainer"); |
| 13 | + |
| 14 | + // Init Smoothie |
| 15 | + let TimeSeries = require("smoothie").TimeSeries; |
| 16 | + let SmoothieChart = require("smoothie").SmoothieChart; |
| 17 | + |
| 18 | + this.series = []; |
| 19 | + this.charts = []; |
| 20 | + this.si.cpu((data) => { |
| 21 | + let createCoresDOM = () => { |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + let timeId = setTimeout(reject, 1000); // Fail after 1s |
| 24 | + for (var i = 0; i < data.cores; i++) { |
| 25 | + // Create DOM |
| 26 | + this.container.innerHTML += `<div> |
| 27 | + <h1>CORE <em>#<span>${i}</span></em><br><i>% USED</i></h1> |
| 28 | + <canvas id="mod_cpuinfo_canvas_${i}" height="60"></canvas> |
| 29 | + </div>`; |
| 30 | + |
| 31 | + if (i === data.cores-1) { |
| 32 | + clearTimeout(timeId); // Clear fail timer |
| 33 | + resolve(true); |
| 34 | + } |
| 35 | + } |
| 36 | + }); |
| 37 | + }; |
| 38 | + async function waitForCoresDOM() { |
| 39 | + let result = createCoresDOM(); |
| 40 | + return await result; |
| 41 | + } |
| 42 | + waitForCoresDOM().then(() => { |
| 43 | + for (var i = 0; i < data.cores; i++) { |
| 44 | + // Create TimeSeries |
| 45 | + this.series.push(new TimeSeries()); |
| 46 | + |
| 47 | + // Create charts |
| 48 | + let serie = this.series[i]; |
| 49 | + let chart = new SmoothieChart({ |
| 50 | + limitFPS: 30, |
| 51 | + responsive: true, |
| 52 | + millisPerPixel: 50, |
| 53 | + grid:{ |
| 54 | + fillStyle:'transparent', |
| 55 | + strokeStyle:'transparent', |
| 56 | + verticalSections:0, |
| 57 | + borderVisible:false |
| 58 | + }, |
| 59 | + labels:{ |
| 60 | + disabled: true |
| 61 | + }, |
| 62 | + yRangeFunction: () => { |
| 63 | + return {min:0,max:100}; |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + chart.addTimeSeries(serie, {lineWidth:1.7,strokeStyle:'#bee6c1'}); |
| 68 | + chart.streamTo(document.getElementById(`mod_cpuinfo_canvas_${i}`), 500); |
| 69 | + |
| 70 | + this.charts.push(chart); |
| 71 | + } |
| 72 | + |
| 73 | + // Init updater |
| 74 | + this.updateInfo(); |
| 75 | + this.infoUpdater = setInterval(() => { |
| 76 | + this.updateInfo(); |
| 77 | + }, 500); |
| 78 | + }, () => { |
| 79 | + console.error("[mod_cpuinfo]: Error when creating DOM."); |
| 80 | + }); |
| 81 | + }); |
| 82 | + } |
| 83 | + updateInfo() { |
| 84 | + this.si.currentLoad((data) => { |
| 85 | + data.cpus.forEach((e, i) => { |
| 86 | + this.series[i].append(new Date().getTime(), e.load); |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +module.exports = { |
| 93 | + Cpuinfo |
| 94 | +}; |
0 commit comments