|
|
|
@ -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'; |
|
|
|
|
|
|
|
|
@ -43,39 +42,39 @@ export async function sendAnalyticWebhook(alert: AnalyticAlert) {
|
|
|
|
|
const toTime = new Date(alert.to).toLocaleTimeString(); |
|
|
|
|
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 { |
|
|
|
|