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