-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthMonitor.js
55 lines (49 loc) · 1.65 KB
/
HealthMonitor.js
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
var fs = require('fs');
var utils = require('./utilities');
var MAX_HEALTH_POINTS = 100;
var SCRAPE_TIMEOUT = utils.minutes(1);
class HealthMonitor {
/**
* @param {!DownloadManager} downloadManager
* @param {!PythonOnline} pythonOnline
*/
constructor(pythonOnline) {
this._pythonOnline = pythonOnline;
this._healthPoints = [];
this._startTime = Date.now();
this._getHealthPoint();
}
async _getHealthPoint() {
var compilations = this._pythonOnline.compilations();
var compilationsCount = compilations.length;
var inflightCompilationsCount = compilations.filter(compilation => !compilation.finished).length;
var [resultsFolderSize, tmpFolderSize] = await Promise.all([
utils.dirSize(this._pythonOnline.resultsFolderPath()),
utils.dirSize(this._pythonOnline.tmpFolderPath()),
]);
var timestamp = Date.now();
this._healthPoints.push({
compilationsCount,
inflightCompilationsCount,
resultsFolderSize,
tmpFolderSize,
timestamp
});
if (this._healthPoints.length > 2 * MAX_HEALTH_POINTS)
this._healthPoints = this._healthPoints.slice(this._healthPoints.length - MAX_HEALTH_POINTS);
setTimeout(this._getHealthPoint.bind(this), SCRAPE_TIMEOUT);
}
/**
* @return {number}
*/
uptime() {
return Date.now() - this._startTime;
}
/**
* @return {!Array<!Object>}
*/
healthPoints() {
return this._healthPoints.slice(this._healthPoints.length - MAX_HEALTH_POINTS);
}
}
module.exports = HealthMonitor;