Skip to content

Commit ad3d862

Browse files
author
Lucienne Seidler
committed
finalizad health helpers
1 parent bee0155 commit ad3d862

File tree

2 files changed

+182
-391
lines changed

2 files changed

+182
-391
lines changed

chronos_npm_package/controllers/healthHelpers.js

Lines changed: 182 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -2,211 +2,201 @@ const si = require('systeminformation');
22

33
const healthHelpers = {};
44

5-
/*
6-
This method returns an promise that resolves to an array of
7-
si data points.
8-
*/
5+
/* This object contains all systeminformation methods, metric names, and corresponding points */
6+
7+
const collectedMetrics = {
8+
cpu: {
9+
speed_in_GHz: 'speed',
10+
speedMax_in_GHz: 'speedMax',
11+
num_of_cores: 'cores',
12+
num_of_processors: 'processors',
13+
'cache.l1d in bytes': 'cache.l1d',
14+
'cache.l1i in bytes': 'cache.l1i',
15+
'cache.l2 in bytes': 'cache.l2',
16+
'cache.l3 in bytes': 'cache.l3',
17+
},
18+
cpuCurrentSpeed: {
19+
average_CPU_speed_in_GHz: 'avg',
20+
minimum_CPU_speed_in_GHz: 'min',
21+
maximum_CPU_speed_in_GHz: 'max',
22+
},
23+
cpuTemperature: {
24+
average_temperature: 'main',
25+
max_temperature: 'max',
26+
},
27+
currentLoad: {
28+
average_CPU_load_percent: 'avg',
29+
current_CPU_load_percent: 'currentLoad',
30+
current_CPU_load_user_percent: 'currentLoadUser',
31+
current_CPU_load__system_percent: 'currentLoadSystem',
32+
current_CPU_load_nice_percent: 'currentLoadNice',
33+
current_CPU_load_idle_percent: 'currentLoadIdle',
34+
current_CPU_load_raw_ticks: 'rawCurrentLoad',
35+
},
36+
mem: {
37+
totalmemory_in_bytes: 'total',
38+
freememory_in_bytes: 'free',
39+
usedmemory_in_bytes: 'used',
40+
activememory_in_bytes: 'active',
41+
buffers_plus_cache_in_bytes: 'buffcache',
42+
available_memory: 'available',
43+
},
44+
processes: {
45+
totalprocesses: 'all',
46+
blockedprocesses: 'blocked',
47+
runningprocesses: 'running',
48+
sleepingprocesses: 'sleeping',
49+
},
50+
inetLatency: 'all data collected',
51+
};
952

1053
healthHelpers.collectHealthData = () => {
11-
// Create an array to hold the promises returned by si
12-
const promises = [];
13-
// for use with every object
54+
const healthDataCollection = [];
1455
const time = Date.now();
15-
// Fill the array of promises
16-
promises.push(
17-
si
18-
.cpu()
19-
.then(data => ({
20-
speed_in_GHz: data.speed,
21-
speedMax_in_GHz: data.speedMax,
22-
num_of_cores: data.cores,
23-
num_of_processors: data.processors,
24-
'cache.l1d in bytes': data.cache.l1d,
25-
'cache.l1i in bytes': data.cache.l1i,
26-
'cache.l2 in bytes': data.cache.l2,
27-
'cache.l3 in bytes': data.cache.l3,
28-
}))
29-
.then(data => {
30-
const cpuMetrics = [];
31-
for (const metric in data) {
32-
cpuMetrics.push({
33-
metric,
34-
value: data[metric],
35-
category: 'CPU',
36-
time,
37-
});
38-
}
39-
return cpuMetrics;
40-
})
41-
.catch(err => {
42-
if (err) {
43-
throw err;
44-
}
45-
})
46-
);
47-
promises.push(
48-
si
49-
.cpuCurrentSpeed()
50-
.then(data => ({
51-
average_CPU_speed_in_GHz: data.avg,
52-
minimum_CPU_speed_in_GHz: data.min,
53-
maximum_CPU_speed_in_GHz: data.max,
54-
}))
55-
.then(data => {
56-
const cpuSpeedMetrics = [];
57-
for (const metric in data) {
58-
cpuSpeedMetrics.push({
59-
metric,
60-
value: data[metric],
61-
category: 'CPU',
62-
time,
63-
});
64-
}
65-
return cpuSpeedMetrics;
66-
})
67-
.catch(err => {
68-
if (err) {
69-
throw err;
70-
}
71-
})
72-
);
7356

74-
promises.push(
75-
si
76-
.cpuTemperature()
77-
.then(data => ({
78-
average_temperature: data.main,
79-
max_temperature: data.max,
80-
}))
81-
.then(data => {
82-
const cpuTemperatureMetrics = [];
83-
for (const metric in data) {
84-
cpuTemperatureMetrics.push({
85-
metric,
86-
value: data[metric],
87-
category: 'CPU',
88-
time,
89-
});
90-
}
91-
return cpuTemperatureMetrics;
92-
})
93-
.catch(err => {
94-
if (err) {
95-
throw err;
96-
}
97-
})
98-
);
57+
console.log('in health helpers 2');
9958

100-
promises.push(
101-
si
102-
.currentLoad()
103-
.then(data => ({
104-
average_CPU_load_percent: data.avg,
105-
current_CPU_load_percent: data.currentLoad,
106-
current_CPU_load_user_percent: data.currentLoadUser,
107-
current_CPU_load__system_percent: data.currentLoadSystem,
108-
current_CPU_load_nice_percent: data.currentLoadNice,
109-
current_CPU_load_idle_percent: data.currentLoadIdle,
110-
current_CPU_load_raw_ticks: data.rawCurrentLoad,
111-
}))
112-
.then(data => {
113-
const cpuLoadMetrics = [];
114-
for (const metric in data) {
115-
cpuLoadMetrics.push({
116-
metric,
117-
value: data[metric],
118-
category: 'CPU',
119-
time,
120-
});
121-
}
122-
return cpuLoadMetrics;
123-
})
124-
.catch(err => {
59+
si.cpu()
60+
.then(data => {
61+
const siMethodName = 'cpu';
62+
for (let metricName in collectedMetrics[siMethodName]) {
63+
healthDataCollection.push({
64+
metric: metricName,
65+
value: data[collectedMetrics[siMethodName][metricName]],
66+
category: 'CPU',
67+
time,
68+
});
69+
}
70+
})
71+
.catch(err => {
72+
if (err) {
12573
throw err;
126-
})
127-
);
74+
}
75+
});
12876

129-
promises.push(
130-
si
131-
.mem()
132-
.then(data => ({
133-
totalmemory_in_bytes: data.total,
134-
freememory_in_bytes: data.free,
135-
usedmemory_in_bytes: data.used,
136-
activememory_in_bytes: data.active,
137-
buffers_plus_cache_in_bytes: data.buffcache,
138-
available_memory: data.available,
139-
}))
140-
.then(data => {
141-
const memMetrics = [];
142-
for (const metric in data) {
143-
memMetrics.push({
144-
metric,
145-
value: data[metric],
146-
category: 'Memory',
147-
time,
148-
});
149-
}
150-
return memMetrics;
151-
})
152-
.catch(err => {
153-
if (err) {
154-
throw err;
155-
}
156-
})
157-
);
77+
si.cpuCurrentSpeed()
78+
.then(data => {
79+
const siMethodName = 'cpuCurrentSpeed';
80+
for (let metricName in collectedMetrics[siMethodName]) {
81+
healthDataCollection.push({
82+
metric: metricName,
83+
value: data[collectedMetrics[siMethodName][metricName]],
84+
category: 'CPU',
85+
time,
86+
});
87+
}
88+
})
89+
.catch(err => {
90+
if (err) {
91+
throw err;
92+
}
93+
});
15894

159-
promises.push(
160-
si
161-
.processes()
162-
.then(data => ({
163-
totalprocesses: data.all,
164-
blockedprocesses: data.blocked,
165-
runningprocesses: data.running,
166-
sleepingprocesses: data.sleeping,
167-
}))
168-
.then(data => {
169-
const processMetrics = [];
170-
for (const metric in data) {
171-
processMetrics.push({
172-
metric,
173-
value: data[metric],
174-
category: 'Processes',
175-
time,
176-
});
177-
}
178-
return processMetrics;
179-
})
180-
.catch(err => {
181-
if (err) {
182-
throw err;
183-
}
184-
})
185-
);
95+
si.cpuTemperature()
96+
.then(data => {
97+
const siMethodName = 'cpuTemperature';
98+
for (let metricName in collectedMetrics[siMethodName]) {
99+
healthDataCollection.push({
100+
metric: metricName,
101+
value: data[collectedMetrics[siMethodName][metricName]],
102+
category: 'CPU',
103+
time,
104+
});
105+
}
106+
})
107+
.catch(err => {
108+
if (err) {
109+
throw err;
110+
}
111+
});
186112

187-
promises.push(
188-
si
189-
.inetLatency()
190-
.then(data => ({
113+
si.currentLoad()
114+
.then(data => {
115+
const siMethodName = 'currentLoad';
116+
for (let metricName in collectedMetrics[siMethodName]) {
117+
healthDataCollection.push({
118+
metric: metricName,
119+
value: data[collectedMetrics[siMethodName][metricName]],
120+
category: 'CPU',
121+
time,
122+
});
123+
}
124+
})
125+
.catch(err => {
126+
if (err) {
127+
throw err;
128+
}
129+
});
130+
131+
si.mem()
132+
.then(data => {
133+
const siMethodName = 'mem';
134+
for (let metricName in collectedMetrics[siMethodName]) {
135+
healthDataCollection.push({
136+
metric: metricName,
137+
value: data[collectedMetrics[siMethodName][metricName]],
138+
category: 'Memory',
139+
time,
140+
});
141+
}
142+
})
143+
.catch(err => {
144+
if (err) {
145+
throw err;
146+
}
147+
});
148+
149+
si.processes()
150+
.then(data => {
151+
const siMethodName = 'processes';
152+
for (let metricName in collectedMetrics[siMethodName]) {
153+
healthDataCollection.push({
154+
metric: metricName,
155+
value: data[collectedMetrics[siMethodName][metricName]],
156+
category: 'Processes',
157+
time,
158+
});
159+
}
160+
})
161+
.catch(err => {
162+
if (err) {
163+
throw err;
164+
}
165+
});
166+
167+
si.inetLatency()
168+
.then(data => {
169+
const siMethodName = 'inetLatency';
170+
healthDataCollection.push({
191171
metric: 'latency',
192172
value: data,
193-
category: 'Latency',
173+
category: 'Memory',
194174
time,
195-
}))
196-
.catch(err => {
197-
if (err) {
198-
throw err;
199-
}
200-
})
201-
);
175+
});
176+
})
177+
.catch(err => {
178+
if (err) {
179+
throw err;
180+
}
181+
});
182+
202183
// Return a promise that resolves to an array of all of the data points unnested
203-
return Promise.all(promises)
204-
.then(array => array.flat())
205-
// Remove any empty strings, NaN, or "NaN" from values prevent database errors
206-
.then((array) => array.filter((a) => {
207-
if (isNaN(a.value) || a.value === 'NaN' || a.value === '') return false;
208-
else return true;
209-
}));
184+
return (
185+
Promise.all(healthDataCollection)
186+
// Remove any empty strings, NaN, or "NaN" from values prevent database errors
187+
.then(array =>
188+
array.filter(metric => {
189+
if (
190+
isNaN(metric.value) ||
191+
metric.value === 'NaN' ||
192+
metric.value === '' ||
193+
metric.value === null
194+
)
195+
return false;
196+
else return true;
197+
})
198+
)
199+
);
210200
};
211201

212202
module.exports = healthHelpers;

0 commit comments

Comments
 (0)