-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstats.js
More file actions
165 lines (146 loc) · 4.64 KB
/
stats.js
File metadata and controls
165 lines (146 loc) · 4.64 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
const { monitorEventLoopDelay } = require('perf_hooks');
const {Utilities} = require("./library/utilities");
const escape = require('escape-html');
class ServerStats {
started = false;
requestCount = 0;
staticRequestCount = 0;
requestTime = 0;
// Collect metrics every 10 minutes
intervalMs = 10 * 60 * 1000;
history = [];
requestCountSnapshot = 0;
startMem = 0;
startTime = Date.now();
timer;
cachingModules = [];
taskMap = new Map();
constructor() {
this.timer = setInterval(() => {
this.recordMetrics();
}, this.intervalMs);
}
recordMetrics() {
if (this.started) {
const now = Date.now();
const currentMem = process.memoryUsage().heapUsed;
const combinedCount = this.requestCount + this.staticRequestCount;
const requestsDelta = combinedCount - this.requestCountSnapshot;
const requestsTat = requestsDelta > 0 ? this.requestTime / requestsDelta : 0;
const minutesSinceStart = this.history.length > 1
? this.intervalMs / 60000
: (now - this.startTime) / 60000;
const requestsPerMin = minutesSinceStart > 0 ? requestsDelta / minutesSinceStart : 0;
const currentCpu = this.readSystemCpu();
const idleDelta = currentCpu.idle - this.lastUsage.idle;
const totalDelta = currentCpu.total - this.lastUsage.total;
const percent = totalDelta > 0 ? 100 * (1 - idleDelta / totalDelta) : 0;
const loopDelay = this.eventLoopMonitor.mean / 1e6;
let cacheCount = 0;
for (let m of this.cachingModules) {
cacheCount = cacheCount + m.cacheCount();
}
this.history.push({time: now, mem: currentMem - this.startMem, rpm: requestsPerMin, tat: requestsTat, cpu: percent, block: loopDelay, cache : cacheCount});
this.eventLoopMonitor.reset();
this.requestCountSnapshot = combinedCount;
this.requestTime = 0;
this.lastTime = now;
this.lastUsage = currentCpu;
// Prune old data (keep 24 hours)
const cutoff = now - (24 * 60 * 60 * 1000); // 24 hours ago
this.history = this.history.filter(m => m.time > cutoff);
}
}
markStarted() {
this.started = true;
this.startMem = process.memoryUsage().heapUsed;
this.startTime = Date.now();
this.lastUsage = this.readSystemCpu();
this.lastTime = this.startTime;
this.eventLoopMonitor = monitorEventLoopDelay({ resolution: 20 });
this.eventLoopMonitor.enable();
this.recordMetrics();
}
countRequest(name, tat) {
// we ignore name for now, but we might split the tat tracking up by name
// at some stage
this.requestCount++;
this.requestTime = this.requestTime + tat;
}
addTask(name, frequency) {
let info = {};
this.taskMap.set(name, info);
info.frequency = frequency;
info.state = "Started";
info.status = "started"
}
task(name, state) {
let info = this.taskMap.get(name);
if (info) {
info.date = Date.now();
info.state = state;
info.status = 'working';
}
}
taskDone(name, state) {
let info = this.taskMap.get(name);
if (info) {
info.date = Date.now();
info.state = state;
info.status = 'resting';
}
}
taskError(name, state) {
let info = this.taskMap.get(name);
if (info) {
info.date = Date.now();
info.state = state;
info.status = 'error';
}
}
taskDetails() {
if (this.taskMap.size == 0) {
return "";
}
let html = '<table class="grid" >';
html += "<tr><th>Background Task</th><th>Status</th><th>Frequency</th><th>Last Seen</th></tr>";
for (let m of this.taskMap.keys()) {
let mm = this.taskMap.get(m);
let color = this.getTaskColor(mm.status);
html += `<tr style="background-color: ${color}"><td>`;
html += escape(m);
html += "</td><td>";
html += escape(mm.state);
html += "</td><td>";
html += mm.frequency;
html += "</td><td>";
html += Utilities.formatDuration(mm.date, Date.now());
html += "</td></tr>";
}
html += "</table>";
return html;
}
finishStats() {
clearInterval(this.timer);
}
readSystemCpu() {
const os = require('os');
const cpus = os.cpus();
let idle = 0, total = 0;
for (const cpu of cpus) {
idle += cpu.times.idle;
total += cpu.times.user + cpu.times.nice + cpu.times.sys + cpu.times.idle + cpu.times.irq;
}
return { idle, total };
}
getTaskColor(status) {
switch (status) {
case "started": return "LightGrey";
case "working": return "LightGreen";
case "resting": return "White";
case "error": return "LightRed";
default: return "DarkBlue"; // should not happen
}
}
}
module.exports = ServerStats;