From 64772a3a8231a8ec0d2cf75666a8993ca365bdbd Mon Sep 17 00:00:00 2001 From: rozetko Date: Fri, 12 Apr 2019 16:34:17 +0300 Subject: [PATCH] Hotfix for "Hastic instance name #558": - serialize alerts in sendWebhook() --- server/src/services/notification_service.ts | 27 ++++++++++----------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/server/src/services/notification_service.ts b/server/src/services/notification_service.ts index f01d84c..c58b73a 100644 --- a/server/src/services/notification_service.ts +++ b/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 {