From 37d90643f242b522719ac2054a20c217075242de Mon Sep 17 00:00:00 2001 From: rozetko Date: Wed, 9 Jan 2019 01:07:00 +0300 Subject: [PATCH] Start implementing (#119) --- src/controllers/analytic_controller.ts | 43 ++++++++++++++++++++++++++ src/models/threshold.ts | 15 +++++++++ src/module.ts | 13 +++----- src/partials/tab_analytics.html | 23 ++++++++++++-- src/services/analytic_service.ts | 10 ++++++ 5 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 src/models/threshold.ts diff --git a/src/controllers/analytic_controller.ts b/src/controllers/analytic_controller.ts index 35e368a..1c05ab0 100644 --- a/src/controllers/analytic_controller.ts +++ b/src/controllers/analytic_controller.ts @@ -12,6 +12,7 @@ import { Segment, SegmentId } from '../models/segment'; import { SegmentsSet } from '../models/segment_set'; import { SegmentArray } from '../models/segment_array'; import { ServerInfo } from '../models/info'; +import { Threshold, Condition } from '../models/threshold'; import { ANALYTIC_UNIT_COLORS } from '../colors'; @@ -43,6 +44,7 @@ export class AnalyticController { private _graphLocked: boolean = false; private _statusRunners: Set = new Set(); private _serverInfo: ServerInfo; + private _thresholds: Threshold[]; constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) { if(_panelObject.analyticUnits === undefined) { @@ -51,6 +53,9 @@ export class AnalyticController { this._labelingDataAddedSegments = new SegmentArray(); this._labelingDataDeletedSegments = new SegmentArray(); this._analyticUnitsSet = new AnalyticUnitsSet(this._panelObject.analyticUnits); + this._thresholds = []; + this.updateThresholds(); + // this.analyticUnits.forEach(a => this.runEnabledWaiter(a)); } @@ -87,6 +92,9 @@ export class AnalyticController { this._newAnalyticUnit.id = await this._analyticService.postNewItem( metricExpanded, datasourceRequest, this._newAnalyticUnit, panelId ); + if(this._newAnalyticUnit.detectorType === 'threshold') { + await this.saveThreshold(this._newAnalyticUnit.id); + } this._analyticUnitsSet.addItem(this._newAnalyticUnit); this._creatingNewAnalyticType = false; this._savingNewAnalyticUnit = false; @@ -335,6 +343,41 @@ export class AnalyticController { await this._analyticService.setAnalyticUnitAlert(analyticUnit); } + async updateThresholds() { + const ids = _.map(this._panelObject.analyticUnits, (analyticUnit: any) => analyticUnit.id); + const thresholds = await this._analyticService.getThresholds(ids); + console.log(thresholds) + this._thresholds = thresholds; + } + + getThreshold(id: AnalyticUnitId) { + let threshold = _.find(this._thresholds, { id }); + if(threshold === undefined) { + threshold = { + id, + value: 0, + condition: Condition.ABOVE + }; + this._thresholds.push(threshold); + } + return threshold; + } + + async saveThreshold(id: AnalyticUnitId) { + const threshold = this.getThreshold(id); + if(threshold.value === undefined) { + throw new Error('Cannot save threshold with undefined value'); + } + if(threshold.condition === undefined) { + throw new Error('Cannot save threshold with undefined condition'); + } + return this._analyticService.updateThreshold(threshold); + } + + public get conditions() { + return _.values(Condition); + } + private async _runStatusWaiter(analyticUnit: AnalyticUnit) { if(analyticUnit === undefined || analyticUnit === null) { throw new Error('analyticUnit not defined'); diff --git a/src/models/threshold.ts b/src/models/threshold.ts new file mode 100644 index 0000000..0ce23a2 --- /dev/null +++ b/src/models/threshold.ts @@ -0,0 +1,15 @@ +import { AnalyticUnitId } from './analytic_unit'; + +export enum Condition { + ABOVE = '>', + ABOVE_OR_EQUAL = '>=', + EQUAL = '=', + LESS_OR_EQUAL = '<=', + LESS = '<' +}; + +export type Threshold = { + id: AnalyticUnitId, + value: number, + condition: Condition +}; diff --git a/src/module.ts b/src/module.ts index 3d5c072..1b9b969 100644 --- a/src/module.ts +++ b/src/module.ts @@ -17,9 +17,7 @@ import { axesEditorComponent } from './axes_editor'; import { MetricsPanelCtrl } from 'grafana/app/plugins/sdk'; import { appEvents } from 'grafana/app/core/core' import { BackendSrv } from 'grafana/app/core/services/backend_srv'; -import { AlertSrv } from 'grafana/app/core/services/alert_srv' - -import config from 'grafana/app/core/config'; +import { AlertSrv } from 'grafana/app/core/services/alert_srv'; import _ from 'lodash'; @@ -143,9 +141,9 @@ class GraphCtrl extends MetricsPanelCtrl { /** @ngInject */ constructor( - $scope, $injector, $http, + $scope, $injector, $http, private annotationsSrv, - private keybindingSrv, + private keybindingSrv, private backendSrv: BackendSrv, private popoverSrv, private contextSrv, @@ -162,8 +160,6 @@ class GraphCtrl extends MetricsPanelCtrl { this.analyticService = new AnalyticService(this.backendURL, $http, this.backendSrv, this.alertSrv); - this.updateAnalyticUnitTypes(); - this.runBackendConnectivityCheck(); this.analyticsController = new AnalyticController(this.panel, this.analyticService, this.events); @@ -215,7 +211,7 @@ class GraphCtrl extends MetricsPanelCtrl { return val; } - async updateAnalyticUnitTypes(){ + async updateAnalyticUnitTypes() { const analyticUnitTypes = await this.analyticService.getAnalyticUnitTypes(); this._analyticUnitTypes = analyticUnitTypes; } @@ -240,6 +236,7 @@ class GraphCtrl extends MetricsPanelCtrl { let connected = await this.analyticService.isBackendOk(); if(connected) { + this.updateAnalyticUnitTypes(); this.alertSrv.set( 'Connected to Hastic server', `Hastic server: "${this.backendURL}"`, diff --git a/src/partials/tab_analytics.html b/src/partials/tab_analytics.html index 4618586..93535ae 100644 --- a/src/partials/tab_analytics.html +++ b/src/partials/tab_analytics.html @@ -22,7 +22,7 @@