-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
480 lines (458 loc) · 15 KB
/
script.js
File metadata and controls
480 lines (458 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
(function () {
'use strict';
const sleep = (delay) => new Promise(resolve => setTimeout(resolve, delay));
const formatDuration = (x) => {
const minutes = Math.trunc(x / 60) % 60;
const hours = Math.trunc(x / (60 * 60)) % 24;
const days = Math.trunc(x / (60 * 60 * 24));
return `${days}d ${hours}h ${minutes}m`;
};
const xAxisRangeMinutes = 10;
const xAxisRangeMilliseconds = xAxisRangeMinutes * 60 * 1000;
const commonChartOptions = {
showArea: true,
showLine: true,
showPoint: false,
axisX: {
type: Chartist.FixedScaleAxis,
divisor: xAxisRangeMinutes,
labelInterpolationFnc: (value) => {
const delta = (Date.now() - value) / (60 * 1000);
return `${delta.toFixed(0)}m`;
},
offset: 15
},
axisY: {
offset: 33,
labelOffset: { x: 4, y: 2 }
},
chartPadding: {
top: 15,
right: 3,
bottom: 0,
left: 0
}
};
// Sadly Chartist doesn't support relative units, so padding must be set at specific resolutions to accomodate labels that are sized in ems.
// Up to 4k should be enough. Note that these are scaled by the device's DPI, so the last one is suitable for a 4k display at 100% or an 8k display at 200%.
const commonResponsiveOption = [
['(min-height: 600px)', {
chartPadding: {
top: 19,
right: 3,
bottom: 4,
left: 10
}
}],
['(min-height: 768px)', {
chartPadding: {
top: 24,
right: 4,
bottom: 9,
left: 22
}
}],
['(min-height: 900px)', {
chartPadding: {
top: 34,
right: 6,
bottom: 19,
left: 42
}
}],
['(min-height: 1080px)', {
chartPadding: {
top: 45,
right: 9,
bottom: 30,
left: 68
}
}],
['(min-height: 1440px)', {
chartPadding: {
top: 60,
right: 12,
bottom: 45,
left: 100
}
}],
['(min-height: 2160px)', {
chartPadding: {
top: 90,
right: 18,
bottom: 75,
left: 165
}
}]
];
const percentChartOptions = Object.assign({}, commonChartOptions, {
axisY: Object.assign({
onlyInteger: true,
high: 100,
low: 0
}, commonChartOptions.axisY)
});
const logDataRateChartOptions = Object.assign({}, commonChartOptions, {
axisY: Object.assign({
type: Chartist.FixedScaleAxis,
onlyInteger: true,
labelInterpolationFnc: (value) => {
if (value % 3 > 0)
return '';
const engOom = Math.trunc(value / 3);
const units = [
'B/s',
'kB/s',
'MB/s',
'GB/s',
'TB/s' // Eventually!
];
return units[engOom];
},
low: 0,
ticks: [0,3,6,9,12]
}, commonChartOptions.axisY)
});
const trimSeries = (series, minTime) => {
for (const s of series) {
while (s.length && s[0].x < minTime) {
s.shift();
}
}
};
const cpuData = { series: [] };
const cpuOptions = Object.assign({}, percentChartOptions);
const cpuChart = new Chartist.Line('#cpu .chart', cpuData, cpuOptions, commonResponsiveOption);
const updateCpuChart = async () => {
const response = await fetch('processor_utilization');
const data = await response.json();
const nowMilliseconds = Date.now();
const seriesSum = (previous, current, index) => {
if (index >= cpuData.series.length)
cpuData.series[index] = [];
const total = previous + (current / data.length);
cpuData.series[index].push({x: nowMilliseconds, y: total});
return total;
};
// The last element is the total, which isn't needed.
data.pop();
// By sorting the data, overlap between series on the charts will be minimized, making it easier to spot high single-thread use.
data.sort((e1, e2) => e1 - e2).reduce(seriesSum, 0);
cpuOptions.axisX.high = nowMilliseconds;
cpuOptions.axisX.low = cpuOptions.axisX.high - xAxisRangeMilliseconds;
trimSeries(cpuData.series, cpuOptions.axisX.low);
cpuChart.update(cpuData, cpuOptions);
};
// From https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-physicalmemoryarray
const eccType = [
'Reserved',
'Other',
'Unknown',
'None',
'Parity',
'Single-bit ECC',
'Multi-bit ECC',
'CRC'
];
// From https://docs.microsoft.com/en-gb/windows/win32/cimwin32prov/win32-physicalmemory
// Note that DDR5 is missing as of 2022-02-12
const memoryType = [
'Unknown',
'Other',
'DRAM',
'Synchronous DRAM',
'Cache DRAM',
'EDO',
'EDRAM',
'VRAM',
'SRAM',
'RAM',
'ROM',
'Flash',
'EEPROM',
'FEPROM',
'EPROM',
'CDRAM',
'3DRAM',
'SDRAM',
'SGRAM',
'RDRAM',
'DDR',
'DDR2',
'DDR2 FB-DIMM',
'Invalid', // 23 is missing from the list in Microsoft's docs.
'DDR3',
'FBD2',
'DDR4'
];
const memData = { series: [[],[]] };
const memOptions = Object.assign({
plugins: [Chartist.plugins.legend({ legendNames: ['Page', 'RAM'] })]
}, percentChartOptions);
const memChart = new Chartist.Line('#mem .chart', memData, memOptions, commonResponsiveOption);
const updateMemChart = async () => {
const response = await fetch('memory_utilization');
const data = await response.json();
const nowMilliseconds = Date.now();
const physicalUsage = 1 - (data.FreePhysicalMemory / data.TotalVisibleMemorySize);
// Technically this is everything except RAM, not just the page file, but it seems reasonable to assume that the only memory available is physical RAM + page file.
const pageSize = data.TotalVirtualMemorySize - data.TotalVisibleMemorySize;
const pageUsage = 1 - (data.FreeSpaceInPagingFiles / pageSize);
memData.series[0].push({x: nowMilliseconds, y: pageUsage * 100});
memData.series[1].push({x: nowMilliseconds, y: physicalUsage * 100});
memOptions.axisX.high = nowMilliseconds;
memOptions.axisX.low = memOptions.axisX.high - xAxisRangeMilliseconds;
trimSeries(memData.series, memOptions.axisX.low);
memChart.update(memData, memOptions);
};
const netData = { series: [[],[]] };
const netOptions = Object.assign({
plugins: [Chartist.plugins.legend({ legendNames: ['Rx', 'Tx'] })]
}, logDataRateChartOptions);
const netChart = new Chartist.Line('#net .chart', netData, netOptions, commonResponsiveOption);
const updateNetChart = async () => {
const response = await fetch('network_perf');
const data = await response.json();
const nowMilliseconds = Date.now();
netData.series[0].push({x: nowMilliseconds, y: Math.max(Math.log10(data.BytesReceivedPersec), 0)});
netData.series[1].push({x: nowMilliseconds, y: Math.max(Math.log10(data.BytesSentPersec), 0)});
netOptions.axisX.high = nowMilliseconds;
netOptions.axisX.low = netOptions.axisX.high - xAxisRangeMilliseconds;
trimSeries(netData.series, netOptions.axisX.low);
netChart.update(netData, netOptions);
};
const diskData = [
{ series: [[],[]] },
{ series: [[],[]] }
];
const diskOptions = [
Object.assign({}, logDataRateChartOptions, {
axisY: Object.assign({}, logDataRateChartOptions.axisY, { high: 10 }),
plugins: [Chartist.plugins.legend({ legendNames: ['Read', 'Write'] })]
}),
Object.assign({}, logDataRateChartOptions, {
axisY: Object.assign({}, logDataRateChartOptions.axisY, { high: 10 }),
plugins: [Chartist.plugins.legend({ legendNames: ['Read', 'Write'] })]
})
];
const diskChart = [
new Chartist.Line('#disk-0 .chart', diskData[0], diskOptions[0], commonResponsiveOption),
new Chartist.Line('#disk-1 .chart', diskData[1], diskOptions[1], commonResponsiveOption)
];
const updateDiskCharts = async () => {
const response = await fetch('logical_disk_perf');
const data = await response.json();
const nowMilliseconds = Date.now();
const updateDiskChart = (index) => {
diskData[index].series[0].push({x: nowMilliseconds, y: Math.max(Math.log10(data[index].DiskReadBytesPersec), 0)});
diskData[index].series[1].push({x: nowMilliseconds, y: Math.max(Math.log10(data[index].DiskWriteBytesPersec), 0)});
diskOptions[index].axisX.high = nowMilliseconds;
diskOptions[index].axisX.low = diskOptions[index].axisX.high - xAxisRangeMilliseconds;
trimSeries(diskData[index].series, diskOptions[index].axisX.low);
diskChart[index].update(diskData[index], diskOptions[index]);
};
updateDiskChart(0);
updateDiskChart(1);
};
const diskSpaceData = [
{ series: [[]] },
{ series: [[]] }
];
const diskSpaceOptions = {
axisX: {
offset: 15
},
axisY: {
low: 0,
high: 100,
offset: 5,
onlyInteger: true,
showLabel: false
},
chartPadding: commonChartOptions.chartPadding
};
const diskSpaceResponsiveOptions = [
['(min-height: 600px)', {
axisX: { offset: 19 },
axisY: { offset: 7 }
}],
['(min-height: 768px)', {
axisX: { offset: 24 },
axisY: { offset: 8 }
}],
['(min-height: 900px)', {
axisX: { offset: 34 },
axisY: { offset: 12 }
}],
['(min-height: 1080px)', {
axisX: { offset: 45 },
axisY: { offset: 15 }
}],
['(min-height: 1440px)', {
axisX: { offset: 60 },
axisY: { offset: 20 }
}],
['(min-height: 2160px)', {
axisX: { offset: 90 },
axisY: { offset: 30 }
}]
];
const diskSpaceChart = [
new Chartist.Bar('#disk-0 .side-chart', diskSpaceData[0], diskSpaceOptions, diskSpaceResponsiveOptions),
new Chartist.Bar('#disk-1 .side-chart', diskSpaceData[1], diskSpaceOptions, diskSpaceResponsiveOptions)
];
const updateTime = () => {
const now = new Date();
const format = { weekday: 'short', day: 'numeric', month: 'short', year: 'numeric' };
const formatter = new Intl.DateTimeFormat('en-GB', format);
const parts = formatter.formatToParts(now);
const formattedDate = parts.map(({type, value}) => {
switch (type) {
case 'day': return value + (value % 10 > 3 || (value > 10 && value < 14) ? 'th' : ['th','st','nd','rd'][value % 10])
case 'literal': return [',', ' '].includes(value[0]) ? '' : value;
default: return value;
}
}).filter(value => value !== '').join(' ');
document.querySelector('#sys .date td').textContent = formattedDate;
document.querySelector('#sys .time td').textContent = now.toLocaleTimeString([], { hour12: false });
};
const updateUptime = async () => {
try {
const response = await fetch('up_time');
const data = await response.json();
document.querySelector('#sys .uptime td').textContent = formatDuration(data.SystemUpTime);
} catch (e) {
console.log(e);
}
};
const updateTimes = async (delay) => {
await Promise.all([updateUptime(), updateTime(), sleep(delay)]);
for (let i = 0; i < 60; i++) {
await sleep(delay);
updateTime();
}
setTimeout(updateTimes, 0, delay);
};
const updateCharts = async (delay) => {
await Promise.all([(async () => {
try {
await updateCpuChart();
await updateMemChart();
await updateNetChart();
await updateDiskCharts();
} catch (e) {
// Catch inside Promise.all so it always waits for the sleep instead of failing fast.
// Otherwise it would retry too fast on error.
console.log(e);
}
})(), sleep(delay)]);
setTimeout(updateCharts, 0, delay);
};
const updateMachineName = async () => {
const td = document.querySelector('#sys .name td');
try {
const response = await fetch('machine_name');
const data = await response.json();
td.textContent = data.Name;
} catch (e) {
td.textContent = '';
console.log(e);
}
};
const updateLocalIpAddress = async () => {
const td = document.querySelector('#sys .local td');
try {
const response = await fetch('local_ip_address');
const data = await response.json();
// To trigger the CSS animation when the address changes, the content must first be set to empty
// and then left like that long enough for the :empty selector to pick it up.
if (td.textContent != data.IPAddress) {
td.textContent = '';
}
setTimeout(() => td.textContent = data.IPAddress, 0);
} catch (e) {
td.textContent = '';
console.log(e);
}
};
const updateLocalNetProperties = async (delay) => {
await Promise.all([(async () => {
await updateMachineName();
await updateLocalIpAddress();
})(), sleep(delay)]);
setTimeout(updateLocalNetProperties, 0, delay);
};
const updateCpuTitle = async () => {
const response = await fetch('processor_name');
const data = await response.json();
document.querySelector('#cpu .chart .chart-title').textContent = data.Name;
};
const updateMemTitle = async () => {
const response = await fetch('physical_memory');
const data = await response.json();
document.querySelector('#mem .chart .chart-title').textContent = `${data.Capacity / (1024 * 1024 * 1024)}GB ${memoryType[data.SMBIOSMemoryType]} ${data.ConfiguredClockSpeed}MHz ${eccType[data.MemoryErrorCorrection]}`;
};
const updateDiskTitles = async () => {
const response = await fetch('logical_disk');
const data = await response.json();
const updateDiskDetails = (index) => {
document.querySelector(`#disk-${index} .chart .chart-title`).textContent = `${data[index].Name} ${Math.round(data[index].Size / (1024 * 1024 * 1024))}GB ${data[index].Caption}`;
diskSpaceData[index].series[0][0] = (1 - (data[index].FreeSpace / data[index].Size)) * 100;
diskSpaceChart[index].update(diskSpaceData[index], diskSpaceOptions);
};
updateDiskDetails(0);
updateDiskDetails(1);
};
const updateNetTitle = async () => {
const response = await fetch('network');
const data = await response.json();
document.querySelector('#net .chart .chart-title').textContent = `${data.CurrentBandwidth / (1000 * 1000 * 1000)}Gbps ${data.Name}`;
const bandwidthBytesPerSecond = data.CurrentBandwidth / 8;
netOptions.axisY.high = Math.ceil(Math.log10(bandwidthBytesPerSecond));
netOptions.axisY.divisor = Math.trunc(netOptions.axisY.high / 3);
}
const updateTitles = async (delay) => {
await Promise.all([(async () => {
try {
// Some of thse are unlikely to change while the machine is running, but update them all periodically anyway.
await updateCpuTitle();
await updateMemTitle();
await updateDiskTitles();
await updateNetTitle();
} catch (e) {
console.log(e);
}
})(), sleep(delay)]);
setTimeout(updateTitles, 0, delay);
};
const updatePublicIpAddress = async (delay) => {
await Promise.all([(async () => {
const td = document.querySelector('#sys .public td');
try {
const response = await fetch('public_ip_address');
const data = await response.json();
td.textContent = data.IPAddress;
} catch(e) {
td.textContent = '';
console.log(e);
}
})(), sleep(delay)]);
setTimeout(updatePublicIpAddress, 0, delay);
};
const hideCursorWhenInactive = (delay) => {
let timer = 0;
const start = () => {
clearTimeout(timer);
document.body.classList.remove('inactive');
timer = setTimeout(() => document.body.classList.add('inactive'), delay);
};
timer = start();
window.addEventListener('mousemove', start);
};
updateTimes(1000);
updateCharts(4 * 1000);
updateLocalNetProperties(61 * 1000);
updateTitles(97 * 1000);
updatePublicIpAddress(8 * 60 * 1000);
hideCursorWhenInactive(10 * 1000);
}());