This repository has been archived by the owner on Aug 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
report-stopped-devices.ts
138 lines (126 loc) · 3.72 KB
/
report-stopped-devices.ts
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
import config from "./config";
const winston = require("winston");
import eventUtil, { PowerEvents } from "./api/V1/eventUtil";
import moment, { Moment } from "moment";
import { sendEmail } from "./emailUtil";
import models from "./models";
const ADMIN_EMAILS = ["[email protected]"];
async function getUserEvents(powerEvents: PowerEvents[]) {
const groupAdmins = {};
const userEvents = {};
for (const event of powerEvents) {
if (!groupAdmins.hasOwnProperty(event.Device.GroupId)) {
const adminUsers = await event.Device.Group.getUsers({
through: { where: { admin: true } }
});
groupAdmins[event.Device.GroupId] = adminUsers;
}
for (const user of groupAdmins[event.Device.GroupId]) {
if (userEvents.hasOwnProperty(user.id)) {
userEvents[user.id].powerEvents.push(event);
} else {
userEvents[user.id] = { user: user, powerEvents: [event] };
}
}
}
return userEvents;
}
async function main() {
if (!config.smtpDetails) {
throw "No SMTP details found in config/app.js";
}
const powerEvents = (
await eventUtil.powerEventsPerDevice({ query: {} }, true)
).filter(
(device: PowerEvents) =>
device.hasStopped == true && device.hasAlerted == false
);
if (powerEvents.length == 0) {
log.info("No new stopped devices");
return;
}
const userEvents = await getUserEvents(powerEvents);
let success = false;
for (const userID in userEvents) {
const userInfo = userEvents[userID];
const html = generateHtml(userInfo.powerEvents);
const text = generateText(userInfo.powerEvents);
success =
success ||
(await sendEmail(html, text, userInfo.user.email, "Stopped Devices"));
}
for (const email of ADMIN_EMAILS) {
const html = generateHtml(powerEvents);
const text = generateText(powerEvents);
success =
success || (await sendEmail(html, text, email, "Stopped Devices"));
}
if (success) {
const detail = await models.DetailSnapshot.getOrCreateMatching(
"stop-reported",
{}
);
const detailsId = detail.id;
const eventList = [];
const time = new Date();
for (const powerEvent of powerEvents) {
eventList.push({
DeviceId: powerEvent.Device.id,
EventDetailId: detailsId,
dateTime: time
});
}
try {
await models.Event.bulkCreate(eventList);
} catch (exception) {
log.error("Failed to record stop-reported events.", exception.message);
}
}
}
function generateText(stoppedDevices: PowerEvents[]): string {
let textBody = `Stopped Devices ${moment().format("MMM ddd Do ha")}\r\n`;
for (const event of stoppedDevices) {
let deviceText = `${event.Device.Group.groupname}- ${
event.Device.devicename
} id: ${
event.Device.id
} has stopped, last powered on ${event.lastStarted.format(
"MMM ddd Do ha"
)}\r\n`;
textBody += deviceText;
}
textBody += "Thanks, Cacophony Team";
return textBody;
}
function generateHtml(stoppedDevices: PowerEvents[]): string {
let html = `<b>Stopped Devices ${moment().format("MMM ddd Do ha")} </b>`;
html += "<ul>";
for (const event of stoppedDevices) {
let deviceText = `<li>${event.Device.Group.groupname}-${
event.Device.devicename
} id: ${
event.Device.id
} has stopped, last powered on ${event.lastStarted.format(
"MMM ddd Do ha"
)}</li>`;
html += deviceText;
}
html += "</ul>";
html += "<br><p>Thanks,<br> Cacophony Team</p>";
return html;
}
const log = new winston.Logger({
transports: [
new winston.transports.Console({
timestamp: function () {
return moment().format();
},
colorize: true
})
]
});
main()
.catch(log.error)
.then(() => {
process.exit(0);
});