Browse Source

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
master
rozetko 6 years ago committed by Alexey Velikiy
parent
commit
c40ce7c025
  1. 21
      src/controllers/analytic_controller.ts
  2. 6
      src/models/analytic_unit.ts
  3. 19
      src/models/metric.ts
  4. 54
      src/module.ts
  5. 22
      src/services/analytic_service.ts

21
src/controllers/analytic_controller.ts

@ -44,6 +44,8 @@ export class AnalyticController {
private _graphLocked: boolean = false; private _graphLocked: boolean = false;
private _statusRunners: Set<AnalyticUnitId> = new Set<AnalyticUnitId>(); private _statusRunners: Set<AnalyticUnitId> = new Set<AnalyticUnitId>();
private _serverInfo: ServerInfo; private _serverInfo: ServerInfo;
private _currentMetric: MetricExpanded;
private _currentDatasource: DatasourceRequest;
private _thresholds: Threshold[]; private _thresholds: Threshold[];
constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) { 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._savingNewAnalyticUnit = true;
this._newAnalyticUnit.id = await this._analyticService.postNewItem( this._newAnalyticUnit.id = await this._analyticService.postNewItem(
metricExpanded, datasourceRequest, this._newAnalyticUnit, panelId this._newAnalyticUnit, panelUrl
); );
if(this._newAnalyticUnit.detectorType === 'threshold') { if(this._newAnalyticUnit.detectorType === 'threshold') {
await this.saveThreshold(this._newAnalyticUnit.id); await this.saveThreshold(this._newAnalyticUnit.id);
@ -116,7 +118,10 @@ export class AnalyticController {
return this._analyticUnitsSet.byId(this._selectedAnalyticUnitId); 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) { if(this.labelingUnit && this.labelingUnit.saving) {
throw new Error('Can`t toggle during saving'); throw new Error('Can`t toggle during saving');
} }
@ -134,16 +139,16 @@ export class AnalyticController {
return; return;
} }
this.labelingUnit.saving = true; this.labelingUnit.saving = true;
var newIds = await this._saveLabelingData(); const newIds = await this._saveLabelingData();
this._labelingDataAddedSegments.getSegments().forEach((s, i) => { this._labelingDataAddedSegments.getSegments().forEach((s, i) => {
this.labelingUnit.segments.updateId(s.id, newIds[i]); this.labelingUnit.segments.updateId(s.id, newIds[i]);
}); });
this.labelingUnit.saving = false; this.labelingUnit.saving = false;
var anomaly = this.labelingUnit; let unit = this.labelingUnit;
this.dropLabeling(); this.dropLabeling();
this._runStatusWaiter(anomaly); this._runStatusWaiter(unit);
} }
undoLabeling() { undoLabeling() {
@ -223,7 +228,7 @@ export class AnalyticController {
} }
private async _saveLabelingData(): Promise<SegmentId[]> { private async _saveLabelingData(): Promise<SegmentId[]> {
var unit = this.labelingUnit; let unit = this.labelingUnit;
if(unit === null) { if(unit === null) {
throw new Error('analytic unit is not selected'); throw new Error('analytic unit is not selected');
} }
@ -235,6 +240,8 @@ export class AnalyticController {
return []; return [];
} }
await this._analyticService.updateMetric(unit.id, this._currentMetric, this._currentDatasource);
return this._analyticService.updateSegments( return this._analyticService.updateSegments(
unit.id, this._labelingDataAddedSegments, this._labelingDataDeletedSegments unit.id, this._labelingDataAddedSegments, this._labelingDataDeletedSegments
); );

6
src/models/analytic_unit.ts

@ -1,7 +1,6 @@
import { SegmentsSet } from './segment_set'; import { SegmentsSet } from './segment_set';
import { SegmentArray } from './segment_array'; import { SegmentArray } from './segment_array';
import { Segment, SegmentId } from './segment'; import { Segment, SegmentId } from './segment';
import { Metric } from './metric';
import { ANALYTIC_UNIT_COLORS } from '../colors'; import { ANALYTIC_UNIT_COLORS } from '../colors';
@ -30,7 +29,6 @@ export class AnalyticUnit {
private _segmentSet = new SegmentArray<AnalyticSegment>(); private _segmentSet = new SegmentArray<AnalyticSegment>();
private _status: string; private _status: string;
private _error: string; private _error: string;
private _metric: Metric;
constructor(private _panelObject?: any) { constructor(private _panelObject?: any) {
if(_panelObject === undefined) { if(_panelObject === undefined) {
@ -43,8 +41,6 @@ export class AnalyticUnit {
type: 'GENERAL', type: 'GENERAL',
alert: false alert: false
}); });
//this._metric = new Metric(_panelObject.metric);
} }
get id(): AnalyticUnitId { return this._panelObject.id; } get id(): AnalyticUnitId { return this._panelObject.id; }
@ -84,8 +80,6 @@ export class AnalyticUnit {
this._panelObject.visible = value; this._panelObject.visible = value;
} }
get metric() { return this._metric; }
addLabeledSegment(segment: Segment): AnalyticSegment { addLabeledSegment(segment: Segment): AnalyticSegment {
var asegment = new AnalyticSegment(true, segment.id, segment.from, segment.to); var asegment = new AnalyticSegment(true, segment.id, segment.from, segment.to);
this._segmentSet.addSegment(asegment); this._segmentSet.addSegment(asegment);

19
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 { export class MetricExpanded {
private _targets: Target[]; private _targets: Target[];
constructor(public datasource: string, targets: any[]) { constructor(public datasource: string, targets: any[]) {
@ -51,12 +41,3 @@ export class MetricExpanded {
} }
} }
} }
export class MetricMap {
private _cache: Map<TargetHash, Target> = new Map<TargetHash, Target>();
constructor(datasource: string, targets: Target[]) {
targets.forEach(t => {
this._cache.set(t.getHash(), t);
});
}
}

54
src/module.ts

@ -33,9 +33,11 @@ class GraphCtrl extends MetricsPanelCtrl {
dataList: any = []; dataList: any = [];
// annotations: any = []; // annotations: any = [];
private _datasourceRequest: DatasourceRequest;
private _datasources: any;
_panelPath: any; private _panelPath: any;
_renderError: boolean = false; private _renderError: boolean = false;
// annotationsPromise: any; // annotationsPromise: any;
dataWarning: any; dataWarning: any;
@ -43,8 +45,6 @@ class GraphCtrl extends MetricsPanelCtrl {
subTabIndex: number; subTabIndex: number;
processor: DataProcessor; processor: DataProcessor;
datasourceRequest: DatasourceRequest;
analyticsController: AnalyticController; analyticsController: AnalyticController;
_graphRenderer: GraphRenderer; _graphRenderer: GraphRenderer;
@ -186,10 +186,12 @@ class GraphCtrl extends MetricsPanelCtrl {
this.$scope.$digest(); this.$scope.$digest();
}); });
this._datasources = {};
appEvents.on('ds-request-response', data => { appEvents.on('ds-request-response', data => {
let requestConfig = data.config; let requestConfig = data.config;
this.datasourceRequest = { this._datasourceRequest = {
url: requestConfig.url, url: requestConfig.url,
method: requestConfig.method, method: requestConfig.method,
data: requestConfig.data, data: requestConfig.data,
@ -220,7 +222,7 @@ class GraphCtrl extends MetricsPanelCtrl {
return this._analyticUnitTypes; return this._analyticUnitTypes;
} }
get analyticUnitDetectorTypes () { get analyticUnitDetectorTypes() {
return _.keys(this._analyticUnitTypes); return _.keys(this._analyticUnitTypes);
} }
@ -503,13 +505,11 @@ class GraphCtrl extends MetricsPanelCtrl {
} }
async saveNew() { async saveNew() {
this.refresh();
try { try {
await this.analyticsController.saveNew( const panelId = this.panel.id;
new MetricExpanded(this.panel.datasource, this.panel.targets), const panelUrl = window.location.origin + window.location.pathname + `?panelId=${panelId}`;
this.datasourceRequest,
this.panel.id await this.analyticsController.saveNew(panelUrl);
);
} catch(e) { } catch(e) {
this.alertSrv.set( this.alertSrv.set(
'Error while saving analytic unit', 'Error while saving analytic unit',
@ -541,7 +541,7 @@ class GraphCtrl extends MetricsPanelCtrl {
this.render(); this.render();
} }
onCancelLabeling(key) { onCancelLabeling(id: AnalyticUnitId) {
this.$scope.$root.appEvent('confirm-modal', { this.$scope.$root.appEvent('confirm-modal', {
title: 'Clear anomaly labeling', title: 'Clear anomaly labeling',
text2: 'Your changes will be lost.', text2: 'Your changes will be lost.',
@ -549,7 +549,7 @@ class GraphCtrl extends MetricsPanelCtrl {
icon: 'fa-warning', icon: 'fa-warning',
altActionText: 'Save', altActionText: 'Save',
onAltAction: () => { onAltAction: () => {
this.onToggleLabelingMode(key); this.onToggleLabelingMode(id);
}, },
onConfirm: () => { onConfirm: () => {
this.analyticsController.undoLabeling(); this.analyticsController.undoLabeling();
@ -558,8 +558,11 @@ class GraphCtrl extends MetricsPanelCtrl {
}); });
} }
async onToggleLabelingMode(key) { async onToggleLabelingMode(id: AnalyticUnitId) {
await this.analyticsController.toggleUnitTypeLabelingMode(key as 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.$scope.$digest();
this.render(); this.render();
} }
@ -578,13 +581,30 @@ class GraphCtrl extends MetricsPanelCtrl {
} }
private async _updatePanelInfo() { 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 = { this._panelInfo = {
grafanaVersion: this.contextSrv.version, grafanaVersion: this.contextSrv.version,
grafanaUrl: window.location.host, grafanaUrl: window.location.host,
datasourceType: datasource.type, datasourceType: datasource.type,
hasticServerUrl: this.backendURL 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];
} }
} }

22
src/services/analytic_service.ts

@ -34,24 +34,24 @@ export class AnalyticService {
return await this.patch('/threshold', threshold); return await this.patch('/threshold', threshold);
} }
async postNewItem( async postNewItem(newItem: AnalyticUnit, panelUrl: string): Promise<AnalyticUnitId> {
metric: MetricExpanded, datasourceRequest: DatasourceRequest,
newItem: AnalyticUnit, panelId: number
): Promise<AnalyticUnitId> {
let datasource = await this._backendSrv.get(`/api/datasources/name/${metric.datasource}`);
datasourceRequest.type = datasource.type;
const response = await this.post('/analyticUnits', { const response = await this.post('/analyticUnits', {
panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`, panelUrl,
type: newItem.type, type: newItem.type,
name: newItem.name, name: newItem.name
metric: metric.toJSON(),
datasource: datasourceRequest
}); });
return response.id as AnalyticUnitId; 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) { async removeAnalyticUnit(id: AnalyticUnitId) {
return this.delete('/analyticUnits', { id }); return this.delete('/analyticUnits', { id });
} }

Loading…
Cancel
Save