-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSnapshot.js
69 lines (69 loc) · 2.68 KB
/
Snapshot.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Snapshot = void 0;
const Fs = require("fs");
const os_1 = require("os");
const planck_http_fetch_1 = require("planck-http-fetch");
const Log_1 = require("./Log");
const INACTIVE_AFTER_M = 5;
class Snapshot {
constructor(_config) {
this._config = _config;
this._data = {
host: (0, os_1.hostname)(),
app: {}
};
if (!this._config.snapshot)
this._config.snapshot = {};
this._data.token = this._config.snapshot.token;
if (this._config.snapshot.inactiveAfterM == null)
this._config.snapshot.inactiveAfterM = INACTIVE_AFTER_M;
}
push(appId, app, key, history, v) {
if (!this._data.app[appId])
this._data.app[appId] = { name: app, metric: {}, inactive: false };
this._data.app[appId].timeStamp = new Date().getTime();
this._data.app[appId].metric[key] = { history, v };
}
last(appId, key) {
if (!this._data.app[appId] || !this._data.app[appId].metric[key])
return undefined;
return this._data.app[appId].metric[key].v.v;
}
dump() {
this._data.timeStamp = new Date().getTime();
Fs.writeFile(`History_${new Date().toISOString()}.json`, JSON.stringify(this._data), (ex) => {
if (ex)
(0, Log_1.error)(`can't dump history -> ${ex.message || ex}`);
});
}
async send() {
if (!this._config.snapshot.url || !this._config.snapshot.token || this._config.snapshot.disabled === true)
return;
try {
this._data.timeStamp = new Date().getTime();
const fetch = new planck_http_fetch_1.Fetch(this._config.snapshot.url);
if (this._config.snapshot.auth && this._config.snapshot.auth.user) // auth
fetch.basicAuth(this._config.snapshot.auth.user, this._config.snapshot.auth.password);
await fetch.fetch(JSON.stringify(this._data));
}
catch (ex) {
(0, Log_1.error)(`snapshot push failed -> ${ex.message || ex}`);
}
}
/**
* detects if application is inactive based on last received probe time
*/
inactivate() {
const t = new Date().getTime();
for (const id of Object.keys(this._data.app)) {
const app = this._data.app[id], dt = (t - app.timeStamp) / 60000;
const inactive = dt > this._config.snapshot.inactiveAfterM;
if (app.inactive !== inactive)
(0, Log_1.info)(`app [${app.name}] inactive = ${inactive}`);
app.inactive = inactive;
}
}
}
exports.Snapshot = Snapshot;
//# sourceMappingURL=Snapshot.js.map