From 6b7c414497dabc46dbf59a0342f852cc3c9bdd13 Mon Sep 17 00:00:00 2001 From: Alexey Velikiy Date: Sat, 7 Jul 2018 10:12:54 +0300 Subject: [PATCH] AnalyticUnitKey -> AnalyticUnitId --- src/controllers/analytic_controller.ts | 15 +++++++++------ src/models/analytic_unit.ts | 12 ++++++------ src/module.ts | 8 ++++---- src/services/analytic_service.ts | 12 ++++++------ 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/controllers/analytic_controller.ts b/src/controllers/analytic_controller.ts index 45c6899..b3149f3 100644 --- a/src/controllers/analytic_controller.ts +++ b/src/controllers/analytic_controller.ts @@ -1,7 +1,9 @@ +// Corresponds to https://github.com/hastic/hastic-server/blob/master/server/src/models/analytic_unit.ts + import { AnalyticService } from '../services/analytic_service' import { - AnalyticUnitKey, AnalyticUnit, + AnalyticUnitId, AnalyticUnit, AnalyticUnitsSet, AnalyticSegment, AnalyticSegmentsSearcher, AnalyticSegmentPair } from '../models/analytic_unit'; import { MetricExpanded } from '../models/metric'; @@ -9,6 +11,7 @@ import { DatasourceRequest } from '../models/datasource'; import { Segment, SegmentKey } from '../models/segment'; import { SegmentsSet } from '../models/segment_set'; import { SegmentArray } from '../models/segment_array'; + import { Emitter } from 'grafana/app/core/utils/emitter' import _ from 'lodash'; @@ -23,7 +26,7 @@ export const REGION_DELETE_COLOR_DARK = 'white'; export class AnalyticController { private _analyticUnitsSet: AnalyticUnitsSet; - private _selectedAnalyticUnitKey: AnalyticUnitKey = null; + private _selectedAnalyticUnitKey: AnalyticUnitId = null; private _labelingDataAddedSegments: SegmentsSet; private _labelingDataDeletedSegments: SegmentsSet; @@ -33,7 +36,7 @@ export class AnalyticController { private _tempIdCounted = -1; private _graphLocked = false; - private _statusRunners: Set = new Set(); + private _statusRunners: Set = new Set(); constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) { @@ -91,7 +94,7 @@ export class AnalyticController { return this._analyticUnitsSet.byKey(this._selectedAnalyticUnitKey); } - async toggleAnomalyTypeLabelingMode(key: AnalyticUnitKey) { + async toggleAnomalyTypeLabelingMode(key: AnalyticUnitId) { if(this.labelingAnomaly && this.labelingAnomaly.saving) { throw new Error('Can`t toggel during saving'); } @@ -158,7 +161,7 @@ export class AnalyticController { return this._analyticUnitsSet.items; } - onAnomalyColorChange(key: AnalyticUnitKey, value) { + onAnomalyColorChange(key: AnalyticUnitId, value) { this._analyticUnitsSet.byKey(key).color = value; } @@ -332,7 +335,7 @@ export class AnalyticController { return this._tempIdCounted; } - public toggleVisibility(key: AnalyticUnitKey, value?: boolean) { + public toggleVisibility(key: AnalyticUnitId, value?: boolean) { var anomaly = this._analyticUnitsSet.byKey(key); if(value !== undefined) { anomaly.visible = value; diff --git a/src/models/analytic_unit.ts b/src/models/analytic_unit.ts index 7e61b63..c0e525e 100644 --- a/src/models/analytic_unit.ts +++ b/src/models/analytic_unit.ts @@ -8,7 +8,7 @@ import _ from 'lodash'; export type AnalyticSegmentPair = { anomalyType: AnalyticUnit, segment: AnalyticSegment }; export type AnalyticSegmentsSearcher = (point: number, rangeDist: number) => AnalyticSegmentPair[]; -export type AnalyticUnitKey = string; +export type AnalyticUnitId = string; export class AnalyticSegment extends Segment { constructor(public labeled: boolean, key: SegmentKey, from: number, to: number) { @@ -42,7 +42,7 @@ export class AnalyticUnit { //this._metric = new Metric(_panelObject.metric); } - get key(): AnalyticUnitKey { return this.name; } + get key(): AnalyticUnitId { return this.name; } set name(value: string) { this._panelObject.name = value; } get name(): string { return this._panelObject.name; } @@ -118,14 +118,14 @@ export class AnalyticUnit { export class AnalyticUnitsSet { - private _mapKeyIndex: Map; + private _mapKeyIndex: Map; private _items: AnalyticUnit[]; constructor(private _panelObject: any[]) { if(_panelObject === undefined) { throw new Error('panel object can`t be undefined'); } - this._mapKeyIndex = new Map(); + this._mapKeyIndex = new Map(); this._items = _panelObject.map(p => new AnalyticUnit(p)); this._rebuildIndex(); } @@ -138,7 +138,7 @@ export class AnalyticUnitsSet { this._items.push(anomalyType); } - removeAnomalyType(key: AnalyticUnitKey) { + removeAnomalyType(key: AnalyticUnitId) { var index = this._mapKeyIndex[key]; this._panelObject.splice(index, 1); this._items.splice(index, 1); @@ -151,7 +151,7 @@ export class AnalyticUnitsSet { }); } - byKey(key: AnalyticUnitKey): AnalyticUnit { + byKey(key: AnalyticUnitId): AnalyticUnit { return this._items[this._mapKeyIndex[key]]; } diff --git a/src/module.ts b/src/module.ts index ee1a642..d9e6ae9 100644 --- a/src/module.ts +++ b/src/module.ts @@ -8,7 +8,7 @@ import { GraphLegend } from './graph_legend'; import { DataProcessor } from './data_processor'; import { MetricExpanded } from './models/metric'; import { DatasourceRequest } from './models/datasource'; -import { AnalyticUnitKey, AnalyticUnit } from './models/analytic_unit'; +import { AnalyticUnitId, AnalyticUnit } from './models/analytic_unit'; import { AnalyticService } from './services/analytic_service'; import { AnalyticController } from './controllers/analytic_controller'; @@ -525,7 +525,7 @@ class GraphCtrl extends MetricsPanelCtrl { this.render(this.seriesList); } - onColorChange(key: AnalyticUnitKey, value) { + onColorChange(key: AnalyticUnitId, value) { this.analyticsController.onAnomalyColorChange(key, value); this.render(); } @@ -553,7 +553,7 @@ class GraphCtrl extends MetricsPanelCtrl { } async onToggleLabelingMode(key) { - await this.analyticsController.toggleAnomalyTypeLabelingMode(key as AnalyticUnitKey); + await this.analyticsController.toggleAnomalyTypeLabelingMode(key as AnalyticUnitId); this.$scope.$digest(); this.render(); } @@ -569,7 +569,7 @@ class GraphCtrl extends MetricsPanelCtrl { this.analyticsController.toggleAnomalyTypeAlertEnabled(anomalyType); } - onToggleVisibility(key: AnalyticUnitKey) { + onToggleVisibility(key: AnalyticUnitId) { this.analyticsController.toggleVisibility(key); this.render(); } diff --git a/src/services/analytic_service.ts b/src/services/analytic_service.ts index 621b3c4..02b0db7 100644 --- a/src/services/analytic_service.ts +++ b/src/services/analytic_service.ts @@ -2,7 +2,7 @@ import { Segment, SegmentKey } from '../models/segment'; import { MetricExpanded } from '../models/metric'; import { DatasourceRequest } from '../models/datasource'; import { SegmentsSet } from '../models/segment_set'; -import { AnalyticUnitKey, AnalyticUnit, AnalyticSegment } from '../models/analytic_unit'; +import { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analytic_unit'; import { BackendSrv } from 'grafana/app/core/services/backend_srv'; @@ -35,7 +35,7 @@ export class AnalyticService { } async updateSegments( - key: AnalyticUnitKey, addedSegments: SegmentsSet, removedSegments: SegmentsSet + key: AnalyticUnitId, addedSegments: SegmentsSet, removedSegments: SegmentsSet ): Promise { const getJSONs = (segs: SegmentsSet) => segs.getSegments().map(segment => ({ @@ -57,7 +57,7 @@ export class AnalyticService { return data.added_ids as SegmentKey[]; } - async getSegments(key: AnalyticUnitKey, from?: number, to?: number): Promise { + async getSegments(key: AnalyticUnitId, from?: number, to?: number): Promise { var payload: any = { predictor_id: key }; if(from !== undefined) { payload['from'] = from; @@ -76,7 +76,7 @@ export class AnalyticService { return segments.map(s => new AnalyticSegment(s.labeled, s.id, s.start, s.finish)); } - async * getAnomalyTypeStatusGenerator(key: AnalyticUnitKey, duration: number) { + async * getAnomalyTypeStatusGenerator(key: AnalyticUnitId, duration: number) { let statusCheck = async () => { var data = await this._backendSrv.get( this._backendURL + '/anomalies/status', { name: key } @@ -95,7 +95,7 @@ export class AnalyticService { } - async getAlertEnabled(key: AnalyticUnitKey): Promise { + async getAlertEnabled(key: AnalyticUnitId): Promise { var data = await this._backendSrv.get( this._backendURL + '/alerts', { predictor_id: key } ); @@ -103,7 +103,7 @@ export class AnalyticService { } - async setAlertEnabled(key: AnalyticUnitKey, value: boolean): Promise { + async setAlertEnabled(key: AnalyticUnitId, value: boolean): Promise { return this._backendSrv.post( this._backendURL + '/alerts', { predictor_id: key, enable: value } );