From 3f5f8de7b42ccaa6071fd81f5b43e27f334e9cc0 Mon Sep 17 00:00:00 2001 From: rozetko Date: Thu, 6 Dec 2018 21:13:24 +0300 Subject: [PATCH] Alerting web hooks tab hastic version #101 (#104) * Add Webhooks tab * Remove old unused method * Change alert status and send it to server --- src/controllers/analytic_controller.ts | 5 +++ src/models/analytic_unit.ts | 8 ++++- src/module.ts | 50 +++++--------------------- src/partials/tab_webhooks.html | 20 +++++++++++ src/services/analytic_service.ts | 9 ++++- 5 files changed, 49 insertions(+), 43 deletions(-) create mode 100644 src/partials/tab_webhooks.html diff --git a/src/controllers/analytic_controller.ts b/src/controllers/analytic_controller.ts index f369760..9119169 100644 --- a/src/controllers/analytic_controller.ts +++ b/src/controllers/analytic_controller.ts @@ -330,6 +330,11 @@ export class AnalyticController { } } + async toggleAnalyticUnitAlert(analyticUnit: AnalyticUnit) { + analyticUnit.alert = analyticUnit.alert ? true : false; + await this._analyticService.setAnalyticUnitAlert(analyticUnit); + } + private async _runStatusWaiter(analyticUnit: AnalyticUnit) { if(analyticUnit === undefined || analyticUnit === null) { throw new Error('analyticUnit not defined'); diff --git a/src/models/analytic_unit.ts b/src/models/analytic_unit.ts index d120b9d..7bc8f6d 100644 --- a/src/models/analytic_unit.ts +++ b/src/models/analytic_unit.ts @@ -37,7 +37,10 @@ export class AnalyticUnit { this._panelObject = {}; } _.defaults(this._panelObject, { - name: 'AnalyticUnitName', confidence: 0.2, color: ANALYTIC_UNIT_COLORS[0], type: 'GENERAL' + name: 'AnalyticUnitName', + color: ANALYTIC_UNIT_COLORS[0], + type: 'GENERAL', + alert: false }); //this._metric = new Metric(_panelObject.metric); @@ -58,6 +61,9 @@ export class AnalyticUnit { set color(value: string) { this._panelObject.color = value; } get color(): string { return this._panelObject.color; } + set alert(value: boolean) { this._panelObject.alert = value; } + get alert(): boolean { return this._panelObject.alert; } + get selected(): boolean { return this._selected; } set selected(value: boolean) { this._selected = value; } diff --git a/src/module.ts b/src/module.ts index 6060880..116e63f 100644 --- a/src/module.ts +++ b/src/module.ts @@ -254,10 +254,11 @@ class GraphCtrl extends MetricsPanelCtrl { const partialPath = this.panelPath + 'partials'; this.addEditorTab('Analytics', `${partialPath}/tab_analytics.html`, 2); - this.addEditorTab('Axes', axesEditorComponent, 3); - this.addEditorTab('Legend', `${partialPath}/tab_legend.html`, 4); - this.addEditorTab('Display', `${partialPath}/tab_display.html`, 5); - this.addEditorTab('Plugin info', `${partialPath}/tab_info.html`, 6); + this.addEditorTab('Webhooks', `${partialPath}/tab_webhooks.html`, 3); + this.addEditorTab('Axes', axesEditorComponent, 4); + this.addEditorTab('Legend', `${partialPath}/tab_legend.html`, 5); + this.addEditorTab('Display', `${partialPath}/tab_display.html`, 6); + this.addEditorTab('Plugin info', `${partialPath}/tab_info.html`, 7); this.subTabIndex = 0; } @@ -303,8 +304,6 @@ class GraphCtrl extends MetricsPanelCtrl { range: this.range, }); - //this.onPredictionReceived(this.seriesList); - this.dataWarning = null; const hasSomePoint = this.seriesList.some(s => s.datapoints.length > 0); @@ -337,41 +336,6 @@ class GraphCtrl extends MetricsPanelCtrl { } - onPredictionReceived(spanList) { - var predictions = []; - for (var span of spanList) { - var predictionLow = { - target: '', - color: '', - datapoints: [] - }; - var predictionHigh = { - target: '', - color: '', - datapoints: [] - }; - - for (var datapoint of span.datapoints) { - predictionHigh.datapoints.push([datapoint[0] + 2, datapoint[1]]); - predictionLow.datapoints.push([datapoint[0] - 2, datapoint[1]]); - } - - predictionHigh.target = `${span.label} high`; - predictionLow.target = `${span.label} low`; - predictionHigh.color = span.color; - predictionLow.color = span.color; - predictions.push(predictionHigh, predictionLow); - } - var predictionSeries = this.processor.getSeriesList({ - dataList: predictions, - range: this.range - }); - for (var serie of predictionSeries) { - serie.prediction = true; - this.seriesList.push(serie); - } - } - onRender(data) { if (!this.seriesList) { return; @@ -551,6 +515,10 @@ class GraphCtrl extends MetricsPanelCtrl { this.render(this.seriesList); } + onAnalyticUnitAlertChange(analyticUnit: AnalyticUnit) { + this.analyticsController.toggleAnalyticUnitAlert(analyticUnit); + } + onColorChange(id: AnalyticUnitId, value: string) { if(id === undefined) { throw new Error('id is undefined'); diff --git a/src/partials/tab_webhooks.html b/src/partials/tab_webhooks.html new file mode 100644 index 0000000..7d8ec0b --- /dev/null +++ b/src/partials/tab_webhooks.html @@ -0,0 +1,20 @@ +
+
Hastic server at "{{ctrl.backendURL}}" is not available
+ +
+ +
+
Enable webhooks
+
+
+ + +
+
+
diff --git a/src/services/analytic_service.ts b/src/services/analytic_service.ts index 1ccb93a..6b11b27 100644 --- a/src/services/analytic_service.ts +++ b/src/services/analytic_service.ts @@ -127,7 +127,14 @@ export class AnalyticService { }; } - private async _analyticRequest(method: string, url: string, data?) { + async setAnalyticUnitAlert(analyticUnit: AnalyticUnit) { + return this.patch('/analyticUnits/alert', { + analyticUnitId: analyticUnit.id, + alert: analyticUnit.alert + }); + } + + private async _analyticRequest(method: string, url: string, data?: any) { try { method = method.toUpperCase(); url = this._backendURL + url;