This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (57 loc) · 1.63 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
const Cachet = require('cachet-node').Cachet;
const request = require('request');
const cachet = new Cachet({
domain: process.env.CACHET_API_URL,
token: {
value: process.env.CACHET_API_KEY,
headerOrQueryName: 'X-Cachet-Token'
}
});
const fetch = (component) => new Promise((resolve, reject) => {
request.get(component.link, (err, res, body) => {
let result;
if (err) {
console.dir(err);
/*
result = cachet.createIncident({
body: {
component_id: component.id,
name: 'Connection issue',
message: 'Connection failed.',
status: 1,
visible: 1
}
});
*/
} else if (res.statusCode >= 300) {
result = cachet.createIncident({
body: {
component_id: component.id,
name: 'Unexpected response.',
message: `statusCode: ${res.statusCode}`,
status: 1,
visible: 1
}
});
} else {
result = Promise.all([
cachet.getIncidents({
coponentId: component.id
}).then(x => x.body.data.map(x => cachet.updateIncidentById({
incident: x.data.id,
body: {
status: 4,
visible: 1
}
}))),
cachet.updateComponentById({
component: component.id,
body: {
status: 1
}
})]);
};
resolve(result);
});
}).catch(x => console.dir(x.body.errors));
cachet.getComponents().then(x => x.body.data.filter(y => y.link.startsWith('http'))).then(x => x.map(fetch)).catch(console.dir);