-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (46 loc) · 1.37 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
'use strict';
const oohttp = require('oohttp');
class Prometheus {
constructor({ url }) {
this.url = url;
this.http = new oohttp.Base(url);
this.http.rejectUnauthorized = false;
}
async getActiveAlertManagers() {
const json = await this.http.request('GET', '/prometheus/api/v1/alertmanagers').toJson();
return json.data.activeAlertmanagers.map(activeAlertManager =>
new AlertManager({ url: activeAlertManager.url.replace('/alertmanager/api/v1/alerts', '')})
);
}
async query(query) {
const json = await this.http.request('GET', `/prometheus/api/v1/query?query=${encodeURIComponent(query)}`).toJson();
return json.data.result.map(metric => new Metric(metric));
}
}
class AlertManager {
constructor({ url }) {
this.http = new oohttp.Base(url);
this.http.rejectUnauthorized = false;
}
async getAlerts() {
const json = await this.http.request('GET', '/alertmanager/api/v1/alerts').toJson();
return json.data.map(alert => new Alert(alert));
}
}
class Alert {
constructor(obj) {
Object.assign(this, obj);
if (typeof this.startsAt === 'string') {
this.startsAt = new Date(this.startsAt);
}
if (typeof this.endsAt === 'string') {
this.endsAt = new Date(this.endsAt);
}
}
}
class Metric {
constructor(obj) {
Object.assign(this, obj);
}
}
module.exports = { Prometheus, AlertManager };