You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
956 B

import { findById, AnalyticUnitId } from '../models/analytic_unit_model';
6 years ago
import axios from 'axios';
6 years ago
6 years ago
6 years ago
// TODO: send notification with payload without dep to AnalyticUnit
export async function sendNotification(id: AnalyticUnitId, active: boolean) {
let anomalyName = (await findById(id)).name
6 years ago
console.log('Notification ' + anomalyName);
let notification = {
anomaly: anomalyName,
status: ''
};
if(active) {
notification.status = 'alert';
6 years ago
} else {
notification.status = 'OK';
6 years ago
}
6 years ago
// TODO: more to config
let endpoint = process.env.HASTIC_ALERT_ENDPOINT;
if(endpoint === undefined) {
console.error(`Can't send alert, env HASTIC_ALERT_ENDPOINT is undefined`);
return;
}
try {
var data = await axios.post(endpoint, {
method: 'POST',
body: JSON.stringify(notification)
})
console.log(data);
} catch(err) {
6 years ago
console.error(`Can't send alert to ${endpoint}. Error: ${err}`);
6 years ago
}
6 years ago
}