Browse Source

Hotfix for "Hastic instance name #558":

- serialize alerts in sendWebhook()
pull/1/head
rozetko 6 years ago
parent
commit
64772a3a82
  1. 27
      server/src/services/notification_service.ts

27
server/src/services/notification_service.ts

@ -1,4 +1,3 @@
import { Segment } from '../models/segment_model';
import * as AnalyticUnit from '../models/analytic_unit_model';
import { HASTIC_WEBHOOK_URL, HASTIC_WEBHOOK_TYPE, HASTIC_WEBHOOK_SECRET, HASTIC_INSTANCE_NAME } from '../config';
@ -41,41 +40,41 @@ export declare type InfoAlert = {
export async function sendAnalyticWebhook(alert: AnalyticAlert) {
const fromTime = new Date(alert.from).toLocaleTimeString();
const toTime = new Date(alert.to).toLocaleTimeString();
console.log(`Sending alert unit:${alert.analyticUnitName} from: ${fromTime} to: ${toTime}`);
console.log(`Sending alert unit: ${alert.analyticUnitName} from: ${fromTime} to: ${toTime}`);
let payload;
if(HASTIC_WEBHOOK_TYPE === ContentType.JSON) {
payload = JSON.stringify(alert);
} else if(HASTIC_WEBHOOK_TYPE === ContentType.URLENCODED) {
payload = querystring.stringify(alert);
} else {
throw new Error(`Unknown webhook type: ${HASTIC_WEBHOOK_TYPE}`);
}
sendWebhook(payload);
sendWebhook(alert);
}
export async function sendInfoWebhook(alert: InfoAlert) {
if(alert && typeof alert === 'object') {
console.log(`Sending info webhook ${JSON.stringify(alert.message)}`);
sendWebhook(alert, ContentType.JSON);
sendWebhook(alert);
} else {
console.error(`skip sending Info webhook, got corrupted message ${alert}`);
}
}
export async function sendWebhook(payload: any, contentType = HASTIC_WEBHOOK_TYPE) {
export async function sendWebhook(payload: any) {
if(HASTIC_WEBHOOK_URL === null) {
throw new Error(`Can't send alert, HASTIC_WEBHOOK_URL is undefined`);
}
payload.instanceName = HASTIC_INSTANCE_NAME;
if(HASTIC_WEBHOOK_TYPE === ContentType.JSON) {
payload = JSON.stringify(alert);
} else if(HASTIC_WEBHOOK_TYPE === ContentType.URLENCODED) {
payload = querystring.stringify(alert);
} else {
throw new Error(`Unknown webhook type: ${HASTIC_WEBHOOK_TYPE}`);
}
// TODO: use HASTIC_WEBHOOK_SECRET
const options = {
method: 'POST',
url: HASTIC_WEBHOOK_URL,
data: payload,
headers: { 'Content-Type': contentType }
headers: { 'Content-Type': HASTIC_WEBHOOK_TYPE }
};
try {

Loading…
Cancel
Save