|
| 1 | +const { Category } = require('@material-ui/icons'); |
| 2 | +const si = require('systeminformation'); |
| 3 | + |
| 4 | +const healthHelpers = {}; |
| 5 | + |
| 6 | +/* This object contains all systeminformation methods, metric names, and corresponding points */ |
| 7 | + |
| 8 | +const collectedMetrics = { |
| 9 | + cpu: { |
| 10 | + speed_in_GHz: 'speed', |
| 11 | + speedMax_in_GHz: 'speedMax', |
| 12 | + num_of_cores: 'cores', |
| 13 | + num_of_processors: 'processors', |
| 14 | + 'cache.l1d in bytes': 'cache.l1d', |
| 15 | + 'cache.l1i in bytes': 'cache.l1i', |
| 16 | + 'cache.l2 in bytes': 'cache.l2', |
| 17 | + 'cache.l3 in bytes': 'cache.l3', |
| 18 | + }, |
| 19 | + cpuCurrentSpeed: { |
| 20 | + average_CPU_speed_in_GHz: 'avg', |
| 21 | + minimum_CPU_speed_in_GHz: 'min', |
| 22 | + maximum_CPU_speed_in_GHz: 'max', |
| 23 | + }, |
| 24 | + cpuTemperature: { |
| 25 | + average_temperature: 'main', |
| 26 | + max_temperature: 'max', |
| 27 | + }, |
| 28 | + currentLoad: { |
| 29 | + average_CPU_load_percent: 'avg', |
| 30 | + current_CPU_load_percent: 'currentLoad', |
| 31 | + current_CPU_load_user_percent: 'currentLoadUser', |
| 32 | + current_CPU_load__system_percent: 'currentLoadSystem', |
| 33 | + current_CPU_load_nice_percent: 'currentLoadNice', |
| 34 | + current_CPU_load_idle_percent: 'currentLoadIdle', |
| 35 | + current_CPU_load_raw_ticks: 'rawCurrentLoad', |
| 36 | + }, |
| 37 | + mem: { |
| 38 | + totalmemory_in_bytes: 'total', |
| 39 | + freememory_in_bytes: 'free', |
| 40 | + usedmemory_in_bytes: 'used', |
| 41 | + activememory_in_bytes: 'active', |
| 42 | + buffers_plus_cache_in_bytes: 'buffcache', |
| 43 | + available_memory: 'available', |
| 44 | + }, |
| 45 | + processes: { |
| 46 | + totalprocesses: 'all', |
| 47 | + blockedprocesses: 'blocked', |
| 48 | + runningprocesses: 'running', |
| 49 | + sleepingprocesses: 'sleeping', |
| 50 | + }, |
| 51 | + inetLatency: 'all data collected', |
| 52 | +}; |
| 53 | +const type = 'cpu'; |
| 54 | +const element = 'speed_in_GHz'; |
| 55 | + |
| 56 | +console.log(collectedMetrics[type][element]); |
| 57 | + |
| 58 | +//value: collectedMetrics[type][element]] |
| 59 | + |
| 60 | +healthHelpers.collectHealthData = () => { |
| 61 | + const healthDataCollection = []; |
| 62 | + const time = Date.now(); |
| 63 | + |
| 64 | + si.cpu() |
| 65 | + .then(data => { |
| 66 | + const siMethodName = 'cpu'; |
| 67 | + for (let metricName in collectedMetrics[siMethodName]) { |
| 68 | + healthDataCollection.push({ |
| 69 | + metric: metricName, |
| 70 | + value: data[collectedMetrics[siMethodName][metricName]], |
| 71 | + category: 'CPU', |
| 72 | + time, |
| 73 | + }); |
| 74 | + } |
| 75 | + }) |
| 76 | + .catch(err => { |
| 77 | + if (err) { |
| 78 | + throw err; |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + si.cpuCurrentSpeed() |
| 83 | + .then(data => { |
| 84 | + const siMethodName = 'cpuCurrentSpeed'; |
| 85 | + for (let metricName in collectedMetrics[siMethodName]) { |
| 86 | + healthDataCollection.push({ |
| 87 | + metric: metricName, |
| 88 | + value: data[collectedMetrics[siMethodName][metricName]], |
| 89 | + category: 'CPU', |
| 90 | + time, |
| 91 | + }); |
| 92 | + } |
| 93 | + }) |
| 94 | + .catch(err => { |
| 95 | + if (err) { |
| 96 | + throw err; |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + si.cpuTemperature() |
| 101 | + .then(data => { |
| 102 | + const siMethodName = 'cpuTemperature'; |
| 103 | + for (let metricName in collectedMetrics[siMethodName]) { |
| 104 | + healthDataCollection.push({ |
| 105 | + metric: metricName, |
| 106 | + value: data[collectedMetrics[siMethodName][metricName]], |
| 107 | + category: 'CPU', |
| 108 | + time, |
| 109 | + }); |
| 110 | + } |
| 111 | + }) |
| 112 | + .catch(err => { |
| 113 | + if (err) { |
| 114 | + throw err; |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + si.currentLoad() |
| 119 | + .then(data => { |
| 120 | + const siMethodName = 'currentLoad'; |
| 121 | + for (let metricName in collectedMetrics[siMethodName]) { |
| 122 | + healthDataCollection.push({ |
| 123 | + metric: metricName, |
| 124 | + value: data[collectedMetrics[siMethodName][element]], |
| 125 | + category: 'CPU', |
| 126 | + time, |
| 127 | + }); |
| 128 | + } |
| 129 | + }) |
| 130 | + .catch(err => { |
| 131 | + if (err) { |
| 132 | + throw err; |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + si.mem() |
| 137 | + .then(data => { |
| 138 | + const siMethodName = 'mem'; |
| 139 | + for (let metricName in collectedMetrics[siMethodName]) { |
| 140 | + healthDataCollection.push({ |
| 141 | + metric: metricName, |
| 142 | + value: data[collectedMetrics[siMethodName][element]], |
| 143 | + category: 'Memory', |
| 144 | + time, |
| 145 | + }); |
| 146 | + } |
| 147 | + }) |
| 148 | + .catch(err => { |
| 149 | + if (err) { |
| 150 | + throw err; |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + si.processes() |
| 155 | + .then(data => { |
| 156 | + const siMethodName = 'processes'; |
| 157 | + for (let metricName in collectedMetrics[siMethodName]) { |
| 158 | + healthDataCollection.push({ |
| 159 | + metric: metricName, |
| 160 | + value: data[collectedMetrics[siMethodName][element]], |
| 161 | + category: 'Processes', |
| 162 | + time, |
| 163 | + }); |
| 164 | + } |
| 165 | + }) |
| 166 | + .catch(err => { |
| 167 | + if (err) { |
| 168 | + throw err; |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + si.inetLatency() |
| 173 | + .then(data => { |
| 174 | + const siMethodName = 'inetLatency'; |
| 175 | + healthDataCollection.push({ |
| 176 | + metric: latency, |
| 177 | + value: data, |
| 178 | + category: 'Memory', |
| 179 | + time, |
| 180 | + }); |
| 181 | + }) |
| 182 | + .catch(err => { |
| 183 | + if (err) { |
| 184 | + throw err; |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | + // Return a promise that resolves to an array of all of the data points unnested |
| 189 | + return ( |
| 190 | + Promise.all(healthDataCollection) |
| 191 | + // Remove any empty strings, NaN, or "NaN" from values prevent database errors |
| 192 | + .then(array => |
| 193 | + array.filter(metric => { |
| 194 | + if (isNaN(metric.value) || metric.value === 'NaN' || metric.value === '') return false; |
| 195 | + else return true; |
| 196 | + }) |
| 197 | + ) |
| 198 | + ); |
| 199 | +}; |
0 commit comments