-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
834 lines (724 loc) · 26.2 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// Require Node.js Dependencies
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { performance } from "perf_hooks";
import os from "os";
import { promises as fs } from "fs";
const { readFile, mkdir } = fs;
// Require Third-Party Dependencies
import * as sqlite from "sqlite";
import sqlite3 from "sqlite3";
import uuid from "@lukeed/uuid";
import Addon from "@slimio/addon";
import is from "@slimio/is";
import Queue from "@slimio/queue";
import Utils from "@slimio/utils";
import TransactManager from "@slimio/sqlite-transaction";
const { assertEntity, assertMIC, assertAlarm, assertCorrelateID, taggedString } = Utils;
// Require Internal Dependencies
import { toUnixEpoch } from "./src/utils.js";
import SharedDB from "./src/sharedDb.js";
// Node.js CONSTANTS
const __dirname = dirname(fileURLToPath(import.meta.url));
// CONSTANTS
const DB_DIR = join(__dirname, "db");
const METRICS_DIR = join(DB_DIR, "metrics");
const POPULATE_INTERVAL_MS = 5000;
const SANITY_INTERVAL_MS = 60000;
const ENTITY_IDENTIFIER = new Set(["name", "id", "parent"]);
const { Insert, Update, Delete } = TransactManager.Actions;
const createMetricDB = taggedString`CREATE TABLE IF NOT EXISTS "${"name"}"
("value" INTEGER NOT NULL, "harvestedAt" DATE NOT NULL, "level" TINYINT DEFAULT 0 NOT NULL);`;
// GLOBALS
let db = null;
const openShareDB = new SharedDB();
/**
* @type {TransactManager}
*/
let transact = null;
// QUEUES & MAPS
const Q_METRICS = new Queue();
/** @type {Map<string, number>} */
const AVAILABLE_TYPES = new Map([
["Addon", 1],
["Alarm", 2],
["Metric", 3]
]);
/** @type {Map<string, Set<string>>} */
const SUBSCRIBERS = new Map();
// Create EVENTS Addon!
const Events = new Addon("events");
/**
* @function dbShouldBeOpen
* @description The database should be open
* @returns {void}
*/
function dbShouldBeOpen() {
if (db === null) {
throw new Error("Events database not open!");
}
}
/**
* @async
* @function getSubscriber
* @param {string} source source
* @param {string} target target
* @param {string} [kind="stats"] kind
* @returns {Promise<string>}
*/
async function getSubscriber(source, target, kind = "stats") {
const subscriber = await db.get(
"SELECT last FROM subscribers WHERE source=? AND target=? AND kind=?", source, target, kind);
if (typeof subscriber !== "undefined") {
return toUnixEpoch(new Date(subscriber.last).getTime());
}
await db.run("INSERT INTO subscribers (source, target, kind) VALUES (?, ?, ?)", source, target, kind);
return toUnixEpoch(new Date().getTime());
}
/**
* @async
* @function declareEntityDescriptor
* @description Declare one descriptor for a given entity!
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!number} entityId entityId
* @param {!Array} KeyValue descriptor key
* @returns {Promise<void>}
*/
async function declareEntityDescriptor(header, entityId, [key, value]) {
dbShouldBeOpen();
const row = await db.get(
"SELECT value FROM entity_descriptor WHERE entity_id=? AND key=?", entityId, key);
if (typeof row === "undefined") {
transact.open(Insert, "descriptor", [entityId, key, value]);
}
else if (value !== row.value) {
transact.open(Update, "descriptor", [row.value, entityId, key]);
}
}
/**
* @async
* @function getDescriptors
* @description Get one or all descriptors of a given entity
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!number} entityId entityId
* @param {string} [key] descriptor key
* @returns {Promise<void>}
*/
async function getDescriptors(header, entityId, key) {
dbShouldBeOpen();
if (typeof entityId !== "number") {
throw new TypeError("entityId should be typeof number");
}
// If key is a string, then only return one descriptor
if (typeof key === "string") {
return await db.get(
"SELECT * FROM entity_descriptor WHERE entity_id=? AND key=?", entityId, key);
}
return await db.all("SELECT * FROM entity_descriptor WHERE entity_id=?", entityId);
}
/**
* @async
* @function declareEntity
* @description Declare a new entity
* @param {!Addon.CallbackHeader} header Callback Header
* @param {*} entity entity
* @returns {Promise<number>}
*/
async function declareEntity(header, entity) {
dbShouldBeOpen();
assertEntity(entity);
const { name, parent = 1, description = null, descriptors = {} } = entity;
/** @type {{id: number, description: string}} */
let row;
if (parent === null) {
row = await db.get("SELECT id, description FROM entity WHERE name=? AND parent IS NULL", name);
}
else {
row = await db.get("SELECT id, description FROM entity WHERE name=? AND parent=?", name, parent);
}
if (typeof row !== "undefined") {
if (description !== row.description) {
transact.open(Update, "entity", [description, row.id]);
}
setImmediate(() => {
for (const [key, value] of Object.entries(descriptors)) {
declareEntityDescriptor(void 0, row.id, [key, value]);
}
});
return row.id;
}
// Else, create a new row for the entity!
const { lastID } = await db.run(
"INSERT INTO entity (uuid, name, parent, description) VALUES(?, ?, ?, ?)",
uuid(), name, parent, description
);
if (typeof lastID !== "number") {
throw new Error("Failed to insert new entity!");
}
setImmediate(() => {
for (const [key, value] of Object.entries(descriptors)) {
declareEntityDescriptor(void 0, lastID, [key, value]);
}
});
return lastID;
}
/**
* @async
* @function searchEntities
* @description Search one or many entities by matching search options
* @param {!Addon.CallbackHeader} header Callback Header
* @param {object} searchOptions search Options
* @returns {Promise<number>}
*/
async function searchEntities(header, searchOptions = Object.create(null)) {
dbShouldBeOpen();
if (!is.plainObject(searchOptions)) {
throw new TypeError("searchOptions should be a plainObject!");
}
const { name = null, pattern = null, patternIdentifier, fields = "*", createdAt = Date.now() } = searchOptions;
if (typeof fields !== "string") {
throw new TypeError("fields must be a string");
}
if (name !== null) {
return await db.get("SELECT * FROM entity WHERE name=?", name);
}
const rawResult = await db.all(`SELECT ${fields} FROM entity WHERE createdAt > ?`, createdAt);
if (typeof pattern === "string") {
const regex = new RegExp(pattern, "g");
const identifier = ENTITY_IDENTIFIER.has(patternIdentifier) ? patternIdentifier : "name";
return rawResult.filter((row) => regex.test(String(row[identifier])));
}
return rawResult;
}
/**
* @async
* @function getEntityByID
* @description Get a given entity by his ID.
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!number} entityId entity id
* @returns {Promise<any>}
*/
async function getEntityByID(header, entityId) {
dbShouldBeOpen();
const entityIdType = typeof entityId;
if (entityIdType !== "undefined" && entityIdType !== "number") {
throw new TypeError("entityId must be typeof number or undefined");
}
if (entityIdType === "undefined") {
return await db.all("SELECT * FROM entity");
}
return await db.get("SELECT * FROM entity WHERE id=?", entityId);
}
/**
* @async
* @function removeEntity
* @description Remove an entity by his id!
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!number} entityId entityId
* @returns {Promise<void>}
*/
async function removeEntity(header, entityId) {
dbShouldBeOpen();
if (typeof entityId !== "number") {
throw new TypeError("entityId must be typeof number");
}
transact.open(Delete, "entity", [entityId]);
}
/**
* @async
* @function declareMetricIdentity
* @description Remove an entity by his id!
* @param {!Addon.CallbackHeader} header Callback Header
* @param {*} mic MetricIdentityCard
* @returns {Promise<number>}
*/
async function declareMetricIdentity(header, mic) {
dbShouldBeOpen();
assertMIC(mic);
const { name, description: desc = "", unit, interval = 5, max = null, entityId } = mic;
const query = "SELECT id, description, sample_interval as interval FROM metric_identity_card WHERE name=? AND entity_id=?";
const row = await db.get(query, name, entityId);
if (typeof row !== "undefined") {
if (row.description !== desc || row.interval !== interval) {
transact.open(Update, "mic", [desc, interval, row.id]);
}
return row.id;
}
const { lastID } = await db.run( // eslint-disable-next-line
"INSERT INTO metric_identity_card (name, description, sample_unit, sample_interval, sample_max_value, db_name, entity_id) VALUES (?, ?, ?, ?, ?, ?, @entityId)",
name, desc, unit, interval, max, header.from, entityId);
// Create .db and table (if not exists).
const mDB = await sqlite.open({
filename: join(METRICS_DIR, `${header.from}.db`),
driver: sqlite3.Database
});
try {
await mDB.exec(createMetricDB({ name: `${lastID}_${name}` }));
}
finally {
mDB.close();
}
Events.executeCallback("publish", void 0, ["Metric", "create", [header.from, lastID]]);
return lastID;
}
/**
* @async
* @function publishMetric
* @description Publish a new metric (to be queue for population).
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!number} micId MetricIdentityCard ID
* @param {!Array} metricValue metric Array value
* @returns {Promise<void>}
*
* @throws {TypeError}
* @throws {Error}
*/
async function publishMetric(header, micId, [value, harvestedAt = Date.now(), level = 0]) {
dbShouldBeOpen();
if (typeof micId !== "number") {
throw new TypeError("metric micId should be typeof number!");
}
if (typeof value !== "number") {
throw new TypeError("metric value should be typeof number!");
}
if (level > 0 && header.from !== "aggregator") {
throw new Error("Only aggregate is allowed to publish metric with level higher than zero.");
}
const row = await db.get("SELECT name FROM metric_identity_card WHERE id=?", micId);
if (typeof row === "undefined") {
throw new Error(`Unable to found metric card with id ${micId}`);
}
Q_METRICS.enqueue(header.from, [`${micId}_${row.name}`, value, harvestedAt, level]);
}
/**
* @async
* @function getMIC
* @description Get a metric identity card from DB.
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!number} micId MetricIdentityCard ID
* @returns {Promise<object>}
*/
async function getMIC(header, micId) {
dbShouldBeOpen();
const micType = typeof micId;
if (micType !== "undefined" && micType !== "number") {
throw new TypeError("micId should be undefined or a number");
}
if (micType === "undefined") {
return db.all("SELECT * FROM metric_identity_card");
}
return db.get("SELECT * FROM metric_identity_card WHERE id=?", micId);
}
/**
* @async
* @function pullMIC
* @description Pull MIC from a given mic DB
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!number} micId MetricIdentityCard ID
* @param {object} [options]
* @param {number} [options.level=0] level to pull
* @param {!boolean} [options.withSubscriber=true] force execution as non-subscriber
* @returns {Promise<any>}
*/
async function pullMIC(header, micId, options = {}) {
const { withSubscriber = true, level = 0 } = options;
const mic = await getMIC(header, micId);
// TODO: we may want to check the level arg depending the aggregation mode
// The goal is to disallow invalid level (avoid creating useless subscriber)
if (typeof level !== "number") {
throw new TypeError("level must be a number");
}
const ts = withSubscriber ? await getSubscriber(header.from, micId, `pull_${level}`) : 0;
const now = toUnixEpoch(new Date().getTime());
const metricDB = await openShareDB.open(mic.db_name);
try {
const result = await metricDB.get(
`SELECT * FROM "${micId}" WHERE harvestedAt < ? AND harvestedAt > ? AND level=?`, now, ts, level);
if (withSubscriber) {
await db.run(
"UPDATE subscribers SET last=? WHERE source=? AND target=? AND kind=?", now, header.from, micId, `pull_${level}`);
}
return result;
}
catch (err) {
return null;
}
finally {
openShareDB.close(mic.db_name);
}
}
/**
* @async
* @function getMICStats
* @description Get a stats for a given MIC
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!number} micId MetricIdentityCard ID
* @param {object} [options]
* @param {!boolean} [options.walkTimestamp=false] update the subscriber timestamp
* @param {!boolean} [options.withSubscriber=true] force execution as non-subscriber
* @returns {Promise<object[]>}
*/
async function getMICStats(header, micId, options = {}) {
const { walkTimestamp = false, withSubscriber = true } = options;
const mic = await getMIC(header, micId);
const ts = withSubscriber ? await getSubscriber(header.from, micId) : 0;
const tableName = `${micId}_${mic.name}`;
const result = [];
const metricDB = await openShareDB.open(mic.db_name);
try {
// eslint-disable-next-line max-len
const countQuery = `SELECT level, count(level) AS count FROM "${tableName}" WHERE harvestedAt > ? GROUP BY level ORDER BY level`;
// eslint-disable-next-line max-len
const tsQuery = `SELECT harvestedAt, level FROM "${tableName}" GROUP BY level HAVING MIN(ROWID) ORDER BY ROWID`;
const [countRes, tsRes] = await Promise.all([
metricDB.all(countQuery, ts),
metricDB.all(tsQuery)
]);
const tsMap = new Map(tsRes.map((row) => [row.level, new Date(row.harvestedAt).getTime()]));
for (const { level, count = 0 } of countRes) {
const timestamp = tsMap.has(level) ? tsMap.get(level) : Date.now();
result.push({ level, count, timestamp });
}
}
finally {
openShareDB.close(mic.db_name);
}
if (walkTimestamp && withSubscriber) {
const now = toUnixEpoch(new Date().getTime());
await db.run(
"UPDATE subscribers SET last=? WHERE source=? AND target=? AND kind=?", now, header.from, micId, "stats");
}
return result;
}
/**
* @async
* @function deleteMICRows
* @description Delete all rows
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!number} micId MetricIdentityCard ID
* @param {object} [options]
* @param {number} [options.since]
* @param {number} [options.level=0]
* @returns {Promise<void>}
*/
async function deleteMICRows(header, micId, options = {}) {
const { since, level = 0 } = options;
if (typeof since !== "number") {
throw new TypeError("since must be typeof number");
}
const mic = await getMIC(header, micId);
const tableName = `${micId}_${mic.name}`;
const metricDB = await openShareDB.open(mic.db_name);
try {
const query = `DELETE FROM "${tableName}" WHERE harvestedAt < ? AND level = ?`;
await metricDB.run(query, toUnixEpoch(since), level);
}
finally {
openShareDB.close(mic.db_name);
}
}
/**
* @async
* @function createAlarm
* @description Create a new Alarm
* @param {!Addon.CallbackHeader} header Callback Header
* @param {*} alarm Alarm Object
* @returns {Promise<boolean>}
*/
async function createAlarm(header, alarm) {
dbShouldBeOpen();
assertAlarm(alarm);
const { message, severity, correlateKey, entityId } = alarm;
const row = await db.get(
"SELECT * FROM alarms WHERE correlate_key=? AND entity_id=?", correlateKey, entityId);
if (typeof row === "undefined") {
await db.run(
"INSERT INTO alarms (uuid, message, severity, correlate_key, entity_id) VALUES(?, ?, ?, ?, ?)",
uuid(), message, severity, correlateKey, entityId
);
Events.executeCallback("publish", void 0, ["Alarm", "open", `${entityId}#${correlateKey}`]);
return false;
}
const occur = row.occurence + 1;
await db.run(
"UPDATE alarms SET message=?, severity=?, occurence=?, updatedAt=DATETIME('now') WHERE id=?",
message, severity, occur, row.id
);
Events.executeCallback("publish", void 0, ["Alarm", "update", [row.correlate_key, occur]]);
return true;
}
/**
* @async
* @function getAlarms
* @description Get all or one alarms
* @param {!Addon.CallbackHeader} header Callback Header
* @param {string} [cid] Alarm Correlate ID
* @returns {Promise<void>}
*/
async function getAlarms(header, cid) {
dbShouldBeOpen();
if (typeof cid === "string") {
assertCorrelateID(cid);
const [entityId, correlateKey] = cid.split("#");
const alarm = await db.get("SELECT * FROM alarms WHERE correlate_key=? AND entity_id=?", correlateKey, entityId);
if (typeof alarm === "undefined") {
throw new Error(`Unable to found any alarm with CID ${cid}`);
}
return alarm;
}
return await db.all("SELECT * FROM alarms");
}
/**
* @async
* @function getAlarmsOccurence
* @description Get all alarms occurence between 2 times
* @param {!Addon.CallbackHeader} header Callback Header
* @param {string} [cid] Alarm Correlate ID
* @param {object} [options]
* @param {number} [options.time] Occurence time in minute
* @param {number} [options.severity] Lower severity to check
* @returns {Promise<void>}
*/
async function getAlarmsOccurence(header, cid, { time, severity = 0 } = {}) {
dbShouldBeOpen();
assertCorrelateID(cid);
if (!is.number(time)) {
throw new TypeError("time property should be type of number");
}
const dateNow = Date.now() / 1000;
const startDate = (dateNow - time) * 60;
const alarms = await db.get( // eslint-disable-next-line
"SELECT COUNT(*) AS result FROM events WHERE type_id=3 AND name=\"update\" AND data=? AND createdAt BETWEEN datetime(?, 'unixepoch') AND datetime(?, 'unixepoch')",
cid, startDate, dateNow
);
return alarms.result;
}
/**
* @async
* @function removeAlarm
* @description Remove one alarm
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!string} cid Alarm Correlate ID
* @returns {Promise<void>}
*/
async function removeAlarm(header, cid) {
dbShouldBeOpen();
assertCorrelateID(cid);
const [entityId, correlateKey] = cid.split("#");
transact.open(Delete, "alarms", [correlateKey, entityId]);
}
/**
* @function registerEventType
* @description Register event type
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!string} name event name
* @returns {Promise<number>}
*/
async function registerEventType(header, name) {
dbShouldBeOpen();
if (typeof name !== "string") {
throw new TypeError("name should be a string");
}
const type = await db.get("SELECT id,name FROM events_type WHERE name=?", name);
if (typeof type === "undefined") {
const ret = await db.run("INSERT INTO events_type (name) VALUES(?)", name);
if (!AVAILABLE_TYPES.has(name)) {
AVAILABLE_TYPES.set(name, ret.lastID);
}
return ret.lastID;
}
return type.id;
}
/**
* @function publish
* @description Publish a new event
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!Array} event event
* @returns {Promise<void>}
*/
async function publish(header, [type, name, data = ""]) {
if (!AVAILABLE_TYPES.has(type)) {
throw new Error(`Unknown event with typeName ${type}`);
}
if (typeof name !== "string") {
throw new TypeError("name should be typeof string");
}
// const id = AVAILABLE_TYPES.get(type);
// if (Events.isAwake) {
// transact.open(Insert, "events", [id, name, data.toString()]);
// }
// else {
// setTimeout(async() => {
// await db.run("INSERT INTO events (type_id, name, data) VALUES(?, ?, ?)", id, name, data.toString());
// }, 100);
// }
// Send data to subscribers!
const subject = `${type}.${name}`;
if (SUBSCRIBERS.has(subject)) {
const addons = [...SUBSCRIBERS.get(subject)];
Promise.all(addons.map(function sendMessage(addonName) {
return Events.sendMessage(`${addonName}.event`, {
args: [subject, data],
noReturn: true
});
}));
}
}
/**
* @function summaryStats
* @description Get the global (local) stats for events (alarms, metrics, entities..).
* @param {!Addon.CallbackHeader} header Callback Header
* @returns {Promise<void>}
*/
async function summaryStats(header) {
const stats = await Promise.all([
db.get("SELECT count(*) AS entity_count FROM entity"),
db.get("SELECT count(*) AS alarms_count FROM alarms"),
db.get("SELECT count(*) AS mic_count FROM metric_identity_card")
]);
return Object.assign({}, ...stats);
}
/**
* @function subscribe
* @description Subscribe to event
* @param {!Addon.CallbackHeader} header Callback Header
* @param {!string} subjectName Subject name
* @returns {Promise<void>}
*/
async function subscribe(header, subjectName) {
console.log(`[EVENT] subscribe : ${subjectName}`);
if (SUBSCRIBERS.has(subjectName)) {
SUBSCRIBERS.get(subjectName).add(header.from);
}
else {
SUBSCRIBERS.set(subjectName, new Set([header.from]));
}
}
/**
* @function populateMetricsInterval
* @description Metrics populate interval
* @returns {Promise<void>}
*/
async function populateMetricsInterval() {
// Handle Metrics DBs transactions
for (const id of Q_METRICS.ids()) {
const metrics = [...Q_METRICS.dequeueAll(id)];
if (metrics.length <= 0) {
continue;
}
const startTime = performance.now();
const mDB = await openShareDB.open(id);
try {
await mDB.run("BEGIN EXCLUSIVE TRANSACTION;");
await Promise.all(
metrics.map((metric) => {
const [tableName, value, harvestedAt, level = 0] = metric;
const epoch = toUnixEpoch(new Date(harvestedAt).getTime());
const query = `INSERT INTO "${tableName}" (value, harvestedAt, level) VALUES(?, ?, ?)`;
return mDB.run(query, value, epoch, level);
})
);
await mDB.run("COMMIT TRANSACTION;");
}
finally {
openShareDB.close(id);
}
const executeTime = (performance.now() - startTime).toFixed(2);
Events.logger.writeLine(`Transaction to db '${id}' executed in ${executeTime}ms`);
}
}
// Addon "Start" event listener
Events.on("start", async() => {
// Create DB Dir
await mkdir(METRICS_DIR, { recursive: true });
// Open SQLite DB
db = await sqlite.open({
filename: join(DB_DIR, "events.db"),
driver: sqlite3.Database
});
await db.exec(await readFile(join(__dirname, "sql", "events.sql"), "utf-8"));
await Promise.all([
registerEventType(void 0, "Addon"),
registerEventType(void 0, "Metric"),
registerEventType(void 0, "Alarm")
]);
// Hydrate events type (Memory Map).
const types = await db.all("SELECT id, name from events_type");
for (const type of types) {
AVAILABLE_TYPES.set(type.name, type.id);
}
// Create The transaction Manager
transact = new TransactManager(db, {
verbose: false,
interval: POPULATE_INTERVAL_MS
});
await transact.loadSubjectsFromFile(join(__dirname, "src", "sqlquery.json"));
transact.on("alarms.insert", (ts, data) => {
Events.executeCallback("publish", void 0, ["Alarm", "open", `${data[4]}#${data[3]}`]);
});
transact.on("alarms.update", (ts, data, attach) => {
Events.executeCallback("publish", void 0, ["Alarm", "update", [attach.correlateKey, data[2]]]);
});
// Declare root Entity!
declareEntity(void 0, {
name: os.hostname(),
parent: null,
description: "",
descriptors: {
arch: os.arch(),
platform: os.platform(),
release: os.release(),
type: os.type()
}
});
// Force Addon isReady by himself
await publish(void 0, ["Addon", "ready", "events"]);
Events.isReady = true;
});
// Addon "Stop" event listener
Events.on("stop", () => {
Events.isReady = false;
if (transact !== null) {
transact.exit();
transact = null;
}
db.close();
});
Events.registerInterval(populateMetricsInterval, POPULATE_INTERVAL_MS);
Events.registerInterval(async() => {
const evtTypes = await db.all("SELECT name FROM events_type");
const typesName = evtTypes.map((row) => row.name);
for (const key of SUBSCRIBERS.keys()) {
for (const name of typesName) {
if (!key.includes(name)) {
SUBSCRIBERS.delete(key);
}
}
}
}, SANITY_INTERVAL_MS);
// Register others callback(s)
Events.registerCallback("register_event_type", registerEventType);
Events.registerCallback("publish", publish);
Events.registerCallback("subscribe", subscribe);
Events.registerCallback("summary_stats", summaryStats);
// Register entity callback(s)
Events.registerCallback("declare_entity", declareEntity);
Events.registerCallback("declare_entity_descriptor", declareEntityDescriptor);
Events.registerCallback("get_descriptors", getDescriptors);
Events.registerCallback("search_entities", searchEntities);
Events.registerCallback("get_entity_by_id", getEntityByID);
Events.registerCallback("remove_entity", removeEntity);
// Register mic callback(s)
Events.registerCallback("declare_mic", declareMetricIdentity);
Events.registerCallback("publish_metric", publishMetric);
Events.registerCallback("get_mic_stats", getMICStats);
Events.registerCallback("pull_mic", pullMIC);
Events.registerCallback("delete_mic_rows", deleteMICRows);
Events.registerCallback("get_mic", getMIC);
// Register alarms callback(s)
Events.registerCallback("create_alarm", createAlarm);
Events.registerCallback("get_alarms", getAlarms);
Events.registerCallback("get_alarms_occurence", getAlarmsOccurence);
Events.registerCallback("remove_alarm", removeAlarm);
// Export "Events" addon for Core
export default Events;