-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (65 loc) · 2.3 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
// Require Third-Party Dependencies
import FastPriorityQueue from "fastpriorityqueue";
import Addon from "@slimio/addon";
// Require Internal Dependencies
import { buildMICRow } from "./src/utils.js";
// CONSTANTS & GLOBALS
const AGGREGATE_INTERVAL_MS = 1000;
const Cards = new Map();
const Aggregator = new Addon("aggregator").lockOn("events");
/**
* @function checkMic
* @param {object} mic
* @param {!number} micId
*/
async function checkMic(mic, micId) {
try {
const options = { withSubscriber: true };
const stats = await Aggregator.sendOne("events.get_mic_stats", [micId, options]);
// REVIEW: declare mic.sample_interval as milliseconds by default ?
const maxSampleMilliseconds = (mic.sample_interval * 1000) * 720;
const maxSampleTimestamp = Date.now() - maxSampleMilliseconds;
for (const { level, count, timestamp } of stats) {
console.log(`mic(${micId}) have ${count} metrics on level ${level}`);
if (timestamp > maxSampleTimestamp) {
continue;
}
const deleteOptions = { since: maxSampleTimestamp, level };
await Aggregator.sendOne("events.delete_mic_rows", [micId, deleteOptions]);
}
}
catch (error) {
console.error(error);
}
}
/**
* @function aggregateInterval
*/
async function aggregateInterval() {
for (const [id, mic] of Cards.entries()) {
if (mic.timer.walk() && id > 2 && id < 4) {
checkMic(mic, id);
}
}
}
Aggregator.registerInterval(aggregateInterval, AGGREGATE_INTERVAL_MS);
Aggregator.on("awake", async() => {
const mics = await Aggregator.sendOne("events.get_mic");
Aggregator.logger.writeLine(`fetch ${mics.length} metric identity cards`);
const filteredMics = mics.filter((mic) => !Cards.has(mic.id));
for (const mic of filteredMics) {
Cards.set(mic.id, buildMICRow(mic));
}
await Aggregator.ready();
});
Aggregator.of(Addon.Subjects.micCreate).filter((row) => !Cards.has(row[1])).subscribe(async(row) => {
try {
const [, id] = row;
const mic = await Aggregator.sendOne("events.get_mic", [id]);
Cards.set(id, buildMICRow(mic));
}
catch (error) {
Aggregator.logger.writeLine(error.message);
}
}, console.error);
export default Aggregator;