|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
import { sendAnalyticWebhook, sendInfoWebhook } from './notification_service'; |
|
|
|
|
import { sendAnalyticWebhook, sendInfoWebhook, InfoAlert, AnalyticAlert, WebhookType } from './notification_service'; |
|
|
|
|
|
|
|
|
|
import * as _ from 'lodash'; |
|
|
|
|
import * as AnalyticUnit from '../models/analytic_unit_model'; |
|
|
|
@ -11,9 +11,26 @@ export class Alert {
|
|
|
|
|
constructor(protected analyticUnit: AnalyticUnit.AnalyticUnit) {}; |
|
|
|
|
public receive(segment: Segment) { |
|
|
|
|
if(this.enabled) { |
|
|
|
|
sendAnalyticWebhook(this.analyticUnit.name, segment); |
|
|
|
|
const alert = this.makeAlert(segment); |
|
|
|
|
sendAnalyticWebhook(alert); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
protected makeAlert(segment): AnalyticAlert { |
|
|
|
|
const alert: AnalyticAlert = { |
|
|
|
|
type: WebhookType.DETECT, |
|
|
|
|
analyticUnitType: this.analyticUnit.type, |
|
|
|
|
analyticUnitName: this.analyticUnit.name, |
|
|
|
|
analyticUnitId: this.analyticUnit.id, |
|
|
|
|
panelUrl: this.analyticUnit.panelUrl, |
|
|
|
|
from: segment.from, |
|
|
|
|
to: segment.to
|
|
|
|
|
}; |
|
|
|
|
if(segment.params) { |
|
|
|
|
alert.params = segment.params; |
|
|
|
|
} |
|
|
|
|
return alert; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class PatternAlert extends Alert { |
|
|
|
@ -24,7 +41,7 @@ class PatternAlert extends Alert {
|
|
|
|
|
if(this.lastSentSegment === undefined || !segment.equals(this.lastSentSegment) ) { |
|
|
|
|
this.lastSentSegment = segment; |
|
|
|
|
if(this.enabled) { |
|
|
|
|
sendAnalyticWebhook(this.analyticUnit.name, segment); |
|
|
|
|
sendAnalyticWebhook(this.makeAlert(segment)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -39,14 +56,14 @@ class ThresholdAlert extends Alert {
|
|
|
|
|
if(this.lastOccurence === 0) { |
|
|
|
|
this.lastOccurence = segment.from; |
|
|
|
|
if(this.enabled) { |
|
|
|
|
sendAnalyticWebhook(this.analyticUnit.name, segment); |
|
|
|
|
sendAnalyticWebhook(this.makeAlert(segment)); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
if(segment.from - this.lastOccurence > this.EXPIRE_PERIOD_MS) { |
|
|
|
|
if(this.enabled) { |
|
|
|
|
console.log(`time between threshold occurences ${segment.from - this.lastOccurence}ms, send alert`); |
|
|
|
|
sendAnalyticWebhook(this.analyticUnit.name, segment); |
|
|
|
|
sendAnalyticWebhook(this.makeAlert(segment)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -80,18 +97,23 @@ export class AlertService {
|
|
|
|
|
this._alerts[id].receive(segment); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
public sendMsg(message: string, optionalInfo = {}) { |
|
|
|
|
let message_payload = { |
|
|
|
|
message |
|
|
|
|
}; |
|
|
|
|
sendInfoWebhook(Object.assign(message_payload, optionalInfo)); |
|
|
|
|
public sendMsg(message: string, type: WebhookType, optionalInfo = {}) { |
|
|
|
|
const now = Date.now(); |
|
|
|
|
const infoAlert: InfoAlert = { |
|
|
|
|
message, |
|
|
|
|
params: optionalInfo, |
|
|
|
|
type, |
|
|
|
|
from: now, |
|
|
|
|
to: now |
|
|
|
|
} |
|
|
|
|
sendInfoWebhook(infoAlert); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public getGrafanaAvailableReporter() { |
|
|
|
|
if(!this._grafanaAvailableReporter) { |
|
|
|
|
this._grafanaAvailableReporter = availableReporter( |
|
|
|
|
'Grafana available', |
|
|
|
|
'Grafana unavailable for pulling data', |
|
|
|
|
['Grafana available', WebhookType.RECOVERY], |
|
|
|
|
['Grafana unavailable for pulling data', WebhookType.FAILURE], |
|
|
|
|
this.sendMsg, |
|
|
|
|
this.sendMsg |
|
|
|
|
); |
|
|
|
@ -99,6 +121,15 @@ export class AlertService {
|
|
|
|
|
return this._grafanaAvailableReporter; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public getAvailableWebhook(recoveryMsg: string, failureMsg: string) { |
|
|
|
|
return availableReporter( |
|
|
|
|
[recoveryMsg, WebhookType.RECOVERY], |
|
|
|
|
[failureMsg, WebhookType.FAILURE], |
|
|
|
|
this.sendMsg, |
|
|
|
|
this.sendMsg |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public addAnalyticUnit(analyticUnit: AnalyticUnit.AnalyticUnit) { |
|
|
|
|
let detector = AnalyticUnit.getDetectorByType(analyticUnit.type); |
|
|
|
|
let alertsType = {}; |
|
|
|
|