Send status e-mails

This commit is contained in:
Romein van Buren 2023-02-24 16:24:25 +01:00
parent 2119e4d668
commit 9f28a8f30e
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49

View File

@ -22,7 +22,7 @@ const icons = {
const servicesNotifiedAboutOutage = new Set(); const servicesNotifiedAboutOutage = new Set();
let renderedDashboard = null; let renderedDashboard = null;
async function processOutage({ outage, server, settings, onDateUpdated }) { async function processOutage({ outage, server, onDateUpdated }) {
if (typeof onDateUpdated !== 'function') { if (typeof onDateUpdated !== 'function') {
onDateUpdated = () => null; onDateUpdated = () => null;
} }
@ -34,12 +34,6 @@ async function processOutage({ outage, server, settings, onDateUpdated }) {
{ $set: { lastChecked: new Date() } } { $set: { lastChecked: new Date() } }
).then(() => onDateUpdated(id)); ).then(() => onDateUpdated(id));
// Get service entry
const service = await server
.storage
.store('smartyellow/webservice')
.findOne({ id });
// Get last heartbeat // Get last heartbeat
const heartbeat = await server const heartbeat = await server
.storage .storage
@ -187,18 +181,18 @@ module.exports = {
description: 'Tags that can be assigned to outage messages to categorise them.', description: 'Tags that can be assigned to outage messages to categorise them.',
default: {}, default: {},
}, },
// emailSender: { emailSender: {
// type: 'string', type: 'string',
// label: 'notification sender', label: 'notification sender',
// description: 'Sender of notifications about service statuses. Format: Name <email@example.com>', description: 'Sender of notifications about service statuses. Format: Name <email@example.com>',
// default: '', default: '',
// }, },
// emailRecipient: { emailRecipients: {
// type: 'array', type: 'array',
// label: 'notification recipients', label: 'e-mail recipients',
// description: 'Recipients of notifications about service statuses. Format: Name <email@example.com>', description: 'Recipients of e-mail notifications about service statuses. Format: Name <email@example.com>',
// default: [], default: [],
// }, },
smsRecipients: { smsRecipients: {
type: 'array', type: 'array',
label: 'SMS recipients', label: 'SMS recipients',
@ -412,6 +406,62 @@ module.exports = {
}, },
}, },
{ id: 'sendEmailOnOutage',
order: 10,
event: 'populateDashboardTiles',
purpose: 'Sends an e-mail when a tile with priority of 1 or higher is rendered',
handler: async ({ tiles }) => {
if ((typeof server.sendEmail !== 'function') || (typeof settings.emailSender !== 'string') || !settings.emailRecipients?.length) {
// Bad configuration or no email extension.
return;
}
const tilesToNotifyAbout = new Set();
for (const tile of tiles) {
if (tile.prio < 1) {
// Tile is not of priority 1 or higher. Remove its ID from servicesNotifiedAboutOutage.
servicesNotifiedAboutOutage.delete(tile.serviceId);
}
else {
// Tile is of sufficient priority.
if (servicesNotifiedAboutOutage.has(tile.serviceId)) {
// Already notified about: do nothing.
}
else {
// Not yet notified about: send email.
servicesNotifiedAboutOutage.add(tile.serviceId);
tilesToNotifyAbout.add(tile);
}
}
}
if (tilesToNotifyAbout.size > 0) {
server.debug('Sending status e-mails for the following services: ', [ ...tilesToNotifyAbout ].map(t => t.serviceId).join(', '));
const message = '<p>The following tiles are updated to have priority 1 or higher:</p><ul>'
+ [ ...tilesToNotifyAbout ].map(tile => {
let text = `<li><p>${tile.service.name?.en || tile.serviceId}: ${tile.statusText}`;
if (tile.badges?.length > 0) {
text += ' (' + tile.badges.map(String).join(', ') + ')';
}
text += '.</p></li>';
return text;
}).join('')
+ '</ul>';
settings.emailRecipients.forEach(address => server.sendEmail({
subject: `[alert] ${tilesToNotifyAbout.size} new tile${tilesToNotifyAbout.size === 1 ? '' : 's'}!`,
sender: settings.emailSender,
to: address,
body: message,
}).catch(err => {
server.error('status: failed to send e-mail');
server.error(err);
}));
}
},
},
{ id: 'sendSmsOnOutage', { id: 'sendSmsOnOutage',
order: 10, order: 10,
event: 'populateDashboardTiles', event: 'populateDashboardTiles',
@ -450,7 +500,7 @@ module.exports = {
to: phoneNumber, to: phoneNumber,
msg: 'The following service/s is/are experiencing outage: ' + string, msg: 'The following service/s is/are experiencing outage: ' + string,
}).catch(err => { }).catch(err => {
server.error('Failed to send status SMS'); server.error('status: failed to send SMS');
server.error(err); server.error(err);
})); }));
} }