Browse Source

Hotfix for "Hastic instance name #558":

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

25
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 * as AnalyticUnit from '../models/analytic_unit_model';
import { HASTIC_WEBHOOK_URL, HASTIC_WEBHOOK_TYPE, HASTIC_WEBHOOK_SECRET, HASTIC_INSTANCE_NAME } from '../config'; import { HASTIC_WEBHOOK_URL, HASTIC_WEBHOOK_TYPE, HASTIC_WEBHOOK_SECRET, HASTIC_INSTANCE_NAME } from '../config';
@ -43,39 +42,39 @@ export async function sendAnalyticWebhook(alert: AnalyticAlert) {
const toTime = new Date(alert.to).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; sendWebhook(alert);
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);
} }
export async function sendInfoWebhook(alert: InfoAlert) { export async function sendInfoWebhook(alert: InfoAlert) {
if(alert && typeof alert === 'object') { if(alert && typeof alert === 'object') {
console.log(`Sending info webhook ${JSON.stringify(alert.message)}`); console.log(`Sending info webhook ${JSON.stringify(alert.message)}`);
sendWebhook(alert, ContentType.JSON); sendWebhook(alert);
} else { } else {
console.error(`skip sending Info webhook, got corrupted message ${alert}`); 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) { if(HASTIC_WEBHOOK_URL === null) {
throw new Error(`Can't send alert, HASTIC_WEBHOOK_URL is undefined`); throw new Error(`Can't send alert, HASTIC_WEBHOOK_URL is undefined`);
} }
payload.instanceName = HASTIC_INSTANCE_NAME; 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 // TODO: use HASTIC_WEBHOOK_SECRET
const options = { const options = {
method: 'POST', method: 'POST',
url: HASTIC_WEBHOOK_URL, url: HASTIC_WEBHOOK_URL,
data: payload, data: payload,
headers: { 'Content-Type': contentType } headers: { 'Content-Type': HASTIC_WEBHOOK_TYPE }
}; };
try { try {

Loading…
Cancel
Save