From c40ce7c025efdf208c1e9577b26c4937d53585d3 Mon Sep 17 00:00:00 2001 From: rozetko Date: Thu, 17 Jan 2019 15:29:55 +0300 Subject: [PATCH] Send metric configuration after labeling #121 (#123) * More proper datasource request getting * Minor fixes * Move metric sending to labels saving && rm old pieces of code --- src/controllers/analytic_controller.ts | 21 ++++++---- src/models/analytic_unit.ts | 6 --- src/models/metric.ts | 19 --------- src/module.ts | 54 ++++++++++++++++++-------- src/services/analytic_service.ts | 22 +++++------ 5 files changed, 62 insertions(+), 60 deletions(-) diff --git a/src/controllers/analytic_controller.ts b/src/controllers/analytic_controller.ts index 1c05ab0..8828b7e 100644 --- a/src/controllers/analytic_controller.ts +++ b/src/controllers/analytic_controller.ts @@ -44,6 +44,8 @@ export class AnalyticController { private _graphLocked: boolean = false; private _statusRunners: Set = new Set(); private _serverInfo: ServerInfo; + private _currentMetric: MetricExpanded; + private _currentDatasource: DatasourceRequest; private _thresholds: Threshold[]; constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) { @@ -87,10 +89,10 @@ export class AnalyticController { } } - async saveNew(metricExpanded: MetricExpanded, datasourceRequest: DatasourceRequest, panelId: number) { + async saveNew(panelUrl: string) { this._savingNewAnalyticUnit = true; this._newAnalyticUnit.id = await this._analyticService.postNewItem( - metricExpanded, datasourceRequest, this._newAnalyticUnit, panelId + this._newAnalyticUnit, panelUrl ); if(this._newAnalyticUnit.detectorType === 'threshold') { await this.saveThreshold(this._newAnalyticUnit.id); @@ -116,7 +118,10 @@ export class AnalyticController { return this._analyticUnitsSet.byId(this._selectedAnalyticUnitId); } - async toggleUnitTypeLabelingMode(id: AnalyticUnitId) { + async toggleUnitTypeLabelingMode(id: AnalyticUnitId, metric: MetricExpanded, datasource: DatasourceRequest) { + this._currentMetric = metric; + this._currentDatasource = datasource; + if(this.labelingUnit && this.labelingUnit.saving) { throw new Error('Can`t toggle during saving'); } @@ -134,16 +139,16 @@ export class AnalyticController { return; } this.labelingUnit.saving = true; - var newIds = await this._saveLabelingData(); + const newIds = await this._saveLabelingData(); this._labelingDataAddedSegments.getSegments().forEach((s, i) => { this.labelingUnit.segments.updateId(s.id, newIds[i]); }); this.labelingUnit.saving = false; - var anomaly = this.labelingUnit; + let unit = this.labelingUnit; this.dropLabeling(); - this._runStatusWaiter(anomaly); + this._runStatusWaiter(unit); } undoLabeling() { @@ -223,7 +228,7 @@ export class AnalyticController { } private async _saveLabelingData(): Promise { - var unit = this.labelingUnit; + let unit = this.labelingUnit; if(unit === null) { throw new Error('analytic unit is not selected'); } @@ -235,6 +240,8 @@ export class AnalyticController { return []; } + + await this._analyticService.updateMetric(unit.id, this._currentMetric, this._currentDatasource); return this._analyticService.updateSegments( unit.id, this._labelingDataAddedSegments, this._labelingDataDeletedSegments ); diff --git a/src/models/analytic_unit.ts b/src/models/analytic_unit.ts index 0c07ae2..1925345 100644 --- a/src/models/analytic_unit.ts +++ b/src/models/analytic_unit.ts @@ -1,7 +1,6 @@ import { SegmentsSet } from './segment_set'; import { SegmentArray } from './segment_array'; import { Segment, SegmentId } from './segment'; -import { Metric } from './metric'; import { ANALYTIC_UNIT_COLORS } from '../colors'; @@ -30,7 +29,6 @@ export class AnalyticUnit { private _segmentSet = new SegmentArray(); private _status: string; private _error: string; - private _metric: Metric; constructor(private _panelObject?: any) { if(_panelObject === undefined) { @@ -43,8 +41,6 @@ export class AnalyticUnit { type: 'GENERAL', alert: false }); - - //this._metric = new Metric(_panelObject.metric); } get id(): AnalyticUnitId { return this._panelObject.id; } @@ -84,8 +80,6 @@ export class AnalyticUnit { this._panelObject.visible = value; } - get metric() { return this._metric; } - addLabeledSegment(segment: Segment): AnalyticSegment { var asegment = new AnalyticSegment(true, segment.id, segment.from, segment.to); this._segmentSet.addSegment(asegment); diff --git a/src/models/metric.ts b/src/models/metric.ts index da597b7..d0d6470 100644 --- a/src/models/metric.ts +++ b/src/models/metric.ts @@ -24,16 +24,6 @@ export class Target { } } -export class Metric { - constructor(private _panelObj: any) { - if(_panelObj === undefined) { - throw new Error('_panelObj is undefined'); - } - } - get datasource(): string { return this._panelObj.datasource; } - get targetHashs(): TargetHash[] { return this._panelObj.targetHashs; } -} - export class MetricExpanded { private _targets: Target[]; constructor(public datasource: string, targets: any[]) { @@ -51,12 +41,3 @@ export class MetricExpanded { } } } - -export class MetricMap { - private _cache: Map = new Map(); - constructor(datasource: string, targets: Target[]) { - targets.forEach(t => { - this._cache.set(t.getHash(), t); - }); - } -} diff --git a/src/module.ts b/src/module.ts index 1b9b969..7f654df 100644 --- a/src/module.ts +++ b/src/module.ts @@ -33,9 +33,11 @@ class GraphCtrl extends MetricsPanelCtrl { dataList: any = []; // annotations: any = []; + private _datasourceRequest: DatasourceRequest; + private _datasources: any; - _panelPath: any; - _renderError: boolean = false; + private _panelPath: any; + private _renderError: boolean = false; // annotationsPromise: any; dataWarning: any; @@ -43,8 +45,6 @@ class GraphCtrl extends MetricsPanelCtrl { subTabIndex: number; processor: DataProcessor; - datasourceRequest: DatasourceRequest; - analyticsController: AnalyticController; _graphRenderer: GraphRenderer; @@ -186,10 +186,12 @@ class GraphCtrl extends MetricsPanelCtrl { this.$scope.$digest(); }); + this._datasources = {}; + appEvents.on('ds-request-response', data => { let requestConfig = data.config; - this.datasourceRequest = { + this._datasourceRequest = { url: requestConfig.url, method: requestConfig.method, data: requestConfig.data, @@ -220,7 +222,7 @@ class GraphCtrl extends MetricsPanelCtrl { return this._analyticUnitTypes; } - get analyticUnitDetectorTypes () { + get analyticUnitDetectorTypes() { return _.keys(this._analyticUnitTypes); } @@ -503,13 +505,11 @@ class GraphCtrl extends MetricsPanelCtrl { } async saveNew() { - this.refresh(); try { - await this.analyticsController.saveNew( - new MetricExpanded(this.panel.datasource, this.panel.targets), - this.datasourceRequest, - this.panel.id - ); + const panelId = this.panel.id; + const panelUrl = window.location.origin + window.location.pathname + `?panelId=${panelId}`; + + await this.analyticsController.saveNew(panelUrl); } catch(e) { this.alertSrv.set( 'Error while saving analytic unit', @@ -541,7 +541,7 @@ class GraphCtrl extends MetricsPanelCtrl { this.render(); } - onCancelLabeling(key) { + onCancelLabeling(id: AnalyticUnitId) { this.$scope.$root.appEvent('confirm-modal', { title: 'Clear anomaly labeling', text2: 'Your changes will be lost.', @@ -549,7 +549,7 @@ class GraphCtrl extends MetricsPanelCtrl { icon: 'fa-warning', altActionText: 'Save', onAltAction: () => { - this.onToggleLabelingMode(key); + this.onToggleLabelingMode(id); }, onConfirm: () => { this.analyticsController.undoLabeling(); @@ -558,8 +558,11 @@ class GraphCtrl extends MetricsPanelCtrl { }); } - async onToggleLabelingMode(key) { - await this.analyticsController.toggleUnitTypeLabelingMode(key as AnalyticUnitId); + async onToggleLabelingMode(id: AnalyticUnitId) { + this.refresh(); + const datasource = await this._getDatasourceRequest(); + const metric = new MetricExpanded(this.panel.datasource, this.panel.targets); + await this.analyticsController.toggleUnitTypeLabelingMode(id, metric, datasource); this.$scope.$digest(); this.render(); } @@ -578,13 +581,30 @@ class GraphCtrl extends MetricsPanelCtrl { } private async _updatePanelInfo() { - const datasource = await this.backendSrv.get(`/api/datasources/name/${this.panel.datasource}`); + const datasource = await this._getDatasourceByName(this.panel.datasource); this._panelInfo = { grafanaVersion: this.contextSrv.version, grafanaUrl: window.location.host, datasourceType: datasource.type, hasticServerUrl: this.backendURL + }; + } + + private async _getDatasourceRequest() { + if(this._datasourceRequest.type === undefined) { + const datasource = await this._getDatasourceByName(this.panel.datasource); + this._datasourceRequest.type = datasource.type; + } + return this._datasourceRequest; + } + + private async _getDatasourceByName(name: string) { + if(this._datasources[name] === undefined) { + const datasource = await this.backendSrv.get(`/api/datasources/name/${name}`); + return datasource; + } else { + return this._datasources[name]; } } diff --git a/src/services/analytic_service.ts b/src/services/analytic_service.ts index b7726dd..da3a111 100644 --- a/src/services/analytic_service.ts +++ b/src/services/analytic_service.ts @@ -34,24 +34,24 @@ export class AnalyticService { return await this.patch('/threshold', threshold); } - async postNewItem( - metric: MetricExpanded, datasourceRequest: DatasourceRequest, - newItem: AnalyticUnit, panelId: number - ): Promise { - let datasource = await this._backendSrv.get(`/api/datasources/name/${metric.datasource}`); - datasourceRequest.type = datasource.type; - + async postNewItem(newItem: AnalyticUnit, panelUrl: string): Promise { const response = await this.post('/analyticUnits', { - panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`, + panelUrl, type: newItem.type, - name: newItem.name, - metric: metric.toJSON(), - datasource: datasourceRequest + name: newItem.name }); return response.id as AnalyticUnitId; } + async updateMetric(analyticUnitId: AnalyticUnitId, metric: MetricExpanded, datasource: DatasourceRequest) { + await this.patch('/analyticUnits/metric', { + analyticUnitId, + metric: metric.toJSON(), + datasource + }); + } + async removeAnalyticUnit(id: AnalyticUnitId) { return this.delete('/analyticUnits', { id }); }