forked from GitSquared/edex-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathramwatcher.class.js
More file actions
81 lines (69 loc) · 3.15 KB
/
Copy pathramwatcher.class.js
File metadata and controls
81 lines (69 loc) · 3.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
class RAMwatcher {
constructor(parentId) {
if (!parentId) throw "Missing parameters";
// Create DOM
this.parent = document.getElementById(parentId);
let modExtContainer = document.createElement("div");
let ramwatcherDOM = `<div id="mod_ramwatcher_inner">
<h1>MEMORY<i id="mod_ramwatcher_info"></i></h1>
<div id="mod_ramwatcher_pointmap">`;
for (var i = 0; i < 440; i++) {
ramwatcherDOM += `<div class="mod_ramwatcher_point free"></div>`;
}
ramwatcherDOM += `</div>
</div>`;
modExtContainer.innerHTML = ramwatcherDOM;
modExtContainer.setAttribute("id", "mod_ramwatcher");
this.parent.append(modExtContainer);
this.points = Array.from(document.querySelectorAll("div.mod_ramwatcher_point"));
this.shuffleArray(this.points);
// Init updaters
this.updateInfo();
this.infoUpdater = setInterval(() => {
this.updateInfo();
}, 1500);
}
updateInfo() {
window.si.mem().then((data) => {
let total = data.free+data.used;
let free = data.free;
let available = data.used-data.active;
let active = data.active;
if (process.platform === "win32") available = data.available;
if (free+available+active !== total && process.platform !== "win32") throw("RAM Watcher Error: Bad memory values");
if (free+data.used !== total && process.platform === "win32") console.warn("RAM Watcher Error: Bad memory values");
// Convert the data for the 440-points grid
active = Math.round((440*active)/total);
available = Math.round((440*available)/total);
// Update grid
this.points.slice(0, active).forEach((domPoint) => {
if (domPoint.attributes.class.value !== "mod_ramwatcher_point active") {
domPoint.setAttribute("class", "mod_ramwatcher_point active");
}
});
this.points.slice(active, available).forEach((domPoint) => {
if (domPoint.attributes.class.value !== "mod_ramwatcher_point available") {
domPoint.setAttribute("class", "mod_ramwatcher_point available");
}
});
this.points.slice(available, this.points.length).forEach((domPoint) => {
if (domPoint.attributes.class.value !== "mod_ramwatcher_point free") {
domPoint.setAttribute("class", "mod_ramwatcher_point free");
}
});
// Update info text
let totalGiB = Math.round((total/1073742000)*2)/2; // 1073742000 bytes = 1 Gibibyte (GiB)
let usedGiB = Math.round((data.active/1073742000)*2)/2;
document.getElementById("mod_ramwatcher_info").innerText = `USING ${usedGiB} OUT OF ${totalGiB} GiB`;
});
}
shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
}
module.exports = {
RAMwatcher
};