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 5 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 _statusRunners: Set<AnalyticUnitId> = new Set<AnalyticUnitId>();
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<SegmentId[]> {
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
);

6
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<AnalyticSegment>();
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);

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 {
private _targets: Target[];
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 = [];
// 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];
}
}

22
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<AnalyticUnitId> {
let datasource = await this._backendSrv.get(`/api/datasources/name/${metric.datasource}`);
datasourceRequest.type = datasource.type;
async postNewItem(newItem: AnalyticUnit, panelUrl: string): Promise<AnalyticUnitId> {
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 });
}

Loading…
Cancel
Save