Browse Source

AnalyticUnitKey -> AnalyticUnitId

master
Alexey Velikiy 6 years ago
parent
commit
6b7c414497
  1. 15
      src/controllers/analytic_controller.ts
  2. 12
      src/models/analytic_unit.ts
  3. 8
      src/module.ts
  4. 12
      src/services/analytic_service.ts

15
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 { AnalyticService } from '../services/analytic_service'
import { import {
AnalyticUnitKey, AnalyticUnit, AnalyticUnitId, AnalyticUnit,
AnalyticUnitsSet, AnalyticSegment, AnalyticSegmentsSearcher, AnalyticSegmentPair AnalyticUnitsSet, AnalyticSegment, AnalyticSegmentsSearcher, AnalyticSegmentPair
} from '../models/analytic_unit'; } from '../models/analytic_unit';
import { MetricExpanded } from '../models/metric'; import { MetricExpanded } from '../models/metric';
@ -9,6 +11,7 @@ import { DatasourceRequest } from '../models/datasource';
import { Segment, SegmentKey } from '../models/segment'; import { Segment, SegmentKey } from '../models/segment';
import { SegmentsSet } from '../models/segment_set'; import { SegmentsSet } from '../models/segment_set';
import { SegmentArray } from '../models/segment_array'; import { SegmentArray } from '../models/segment_array';
import { Emitter } from 'grafana/app/core/utils/emitter' import { Emitter } from 'grafana/app/core/utils/emitter'
import _ from 'lodash'; import _ from 'lodash';
@ -23,7 +26,7 @@ export const REGION_DELETE_COLOR_DARK = 'white';
export class AnalyticController { export class AnalyticController {
private _analyticUnitsSet: AnalyticUnitsSet; private _analyticUnitsSet: AnalyticUnitsSet;
private _selectedAnalyticUnitKey: AnalyticUnitKey = null; private _selectedAnalyticUnitKey: AnalyticUnitId = null;
private _labelingDataAddedSegments: SegmentsSet<AnalyticSegment>; private _labelingDataAddedSegments: SegmentsSet<AnalyticSegment>;
private _labelingDataDeletedSegments: SegmentsSet<AnalyticSegment>; private _labelingDataDeletedSegments: SegmentsSet<AnalyticSegment>;
@ -33,7 +36,7 @@ export class AnalyticController {
private _tempIdCounted = -1; private _tempIdCounted = -1;
private _graphLocked = false; private _graphLocked = false;
private _statusRunners: Set<AnalyticUnitKey> = new Set<AnalyticUnitKey>(); private _statusRunners: Set<AnalyticUnitId> = new Set<AnalyticUnitId>();
constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) { constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) {
@ -91,7 +94,7 @@ export class AnalyticController {
return this._analyticUnitsSet.byKey(this._selectedAnalyticUnitKey); return this._analyticUnitsSet.byKey(this._selectedAnalyticUnitKey);
} }
async toggleAnomalyTypeLabelingMode(key: AnalyticUnitKey) { async toggleAnomalyTypeLabelingMode(key: AnalyticUnitId) {
if(this.labelingAnomaly && this.labelingAnomaly.saving) { if(this.labelingAnomaly && this.labelingAnomaly.saving) {
throw new Error('Can`t toggel during saving'); throw new Error('Can`t toggel during saving');
} }
@ -158,7 +161,7 @@ export class AnalyticController {
return this._analyticUnitsSet.items; return this._analyticUnitsSet.items;
} }
onAnomalyColorChange(key: AnalyticUnitKey, value) { onAnomalyColorChange(key: AnalyticUnitId, value) {
this._analyticUnitsSet.byKey(key).color = value; this._analyticUnitsSet.byKey(key).color = value;
} }
@ -332,7 +335,7 @@ export class AnalyticController {
return this._tempIdCounted; return this._tempIdCounted;
} }
public toggleVisibility(key: AnalyticUnitKey, value?: boolean) { public toggleVisibility(key: AnalyticUnitId, value?: boolean) {
var anomaly = this._analyticUnitsSet.byKey(key); var anomaly = this._analyticUnitsSet.byKey(key);
if(value !== undefined) { if(value !== undefined) {
anomaly.visible = value; anomaly.visible = value;

12
src/models/analytic_unit.ts

@ -8,7 +8,7 @@ import _ from 'lodash';
export type AnalyticSegmentPair = { anomalyType: AnalyticUnit, segment: AnalyticSegment }; export type AnalyticSegmentPair = { anomalyType: AnalyticUnit, segment: AnalyticSegment };
export type AnalyticSegmentsSearcher = (point: number, rangeDist: number) => AnalyticSegmentPair[]; export type AnalyticSegmentsSearcher = (point: number, rangeDist: number) => AnalyticSegmentPair[];
export type AnalyticUnitKey = string; export type AnalyticUnitId = string;
export class AnalyticSegment extends Segment { export class AnalyticSegment extends Segment {
constructor(public labeled: boolean, key: SegmentKey, from: number, to: number) { constructor(public labeled: boolean, key: SegmentKey, from: number, to: number) {
@ -42,7 +42,7 @@ export class AnalyticUnit {
//this._metric = new Metric(_panelObject.metric); //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; } set name(value: string) { this._panelObject.name = value; }
get name(): string { return this._panelObject.name; } get name(): string { return this._panelObject.name; }
@ -118,14 +118,14 @@ export class AnalyticUnit {
export class AnalyticUnitsSet { export class AnalyticUnitsSet {
private _mapKeyIndex: Map<AnalyticUnitKey, number>; private _mapKeyIndex: Map<AnalyticUnitId, number>;
private _items: AnalyticUnit[]; private _items: AnalyticUnit[];
constructor(private _panelObject: any[]) { constructor(private _panelObject: any[]) {
if(_panelObject === undefined) { if(_panelObject === undefined) {
throw new Error('panel object can`t be undefined'); throw new Error('panel object can`t be undefined');
} }
this._mapKeyIndex = new Map<AnalyticUnitKey, number>(); this._mapKeyIndex = new Map<AnalyticUnitId, number>();
this._items = _panelObject.map(p => new AnalyticUnit(p)); this._items = _panelObject.map(p => new AnalyticUnit(p));
this._rebuildIndex(); this._rebuildIndex();
} }
@ -138,7 +138,7 @@ export class AnalyticUnitsSet {
this._items.push(anomalyType); this._items.push(anomalyType);
} }
removeAnomalyType(key: AnalyticUnitKey) { removeAnomalyType(key: AnalyticUnitId) {
var index = this._mapKeyIndex[key]; var index = this._mapKeyIndex[key];
this._panelObject.splice(index, 1); this._panelObject.splice(index, 1);
this._items.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]]; return this._items[this._mapKeyIndex[key]];
} }

8
src/module.ts

@ -8,7 +8,7 @@ import { GraphLegend } from './graph_legend';
import { DataProcessor } from './data_processor'; import { DataProcessor } from './data_processor';
import { MetricExpanded } from './models/metric'; import { MetricExpanded } from './models/metric';
import { DatasourceRequest } from './models/datasource'; 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 { AnalyticService } from './services/analytic_service';
import { AnalyticController } from './controllers/analytic_controller'; import { AnalyticController } from './controllers/analytic_controller';
@ -525,7 +525,7 @@ class GraphCtrl extends MetricsPanelCtrl {
this.render(this.seriesList); this.render(this.seriesList);
} }
onColorChange(key: AnalyticUnitKey, value) { onColorChange(key: AnalyticUnitId, value) {
this.analyticsController.onAnomalyColorChange(key, value); this.analyticsController.onAnomalyColorChange(key, value);
this.render(); this.render();
} }
@ -553,7 +553,7 @@ class GraphCtrl extends MetricsPanelCtrl {
} }
async onToggleLabelingMode(key) { async onToggleLabelingMode(key) {
await this.analyticsController.toggleAnomalyTypeLabelingMode(key as AnalyticUnitKey); await this.analyticsController.toggleAnomalyTypeLabelingMode(key as AnalyticUnitId);
this.$scope.$digest(); this.$scope.$digest();
this.render(); this.render();
} }
@ -569,7 +569,7 @@ class GraphCtrl extends MetricsPanelCtrl {
this.analyticsController.toggleAnomalyTypeAlertEnabled(anomalyType); this.analyticsController.toggleAnomalyTypeAlertEnabled(anomalyType);
} }
onToggleVisibility(key: AnalyticUnitKey) { onToggleVisibility(key: AnalyticUnitId) {
this.analyticsController.toggleVisibility(key); this.analyticsController.toggleVisibility(key);
this.render(); this.render();
} }

12
src/services/analytic_service.ts

@ -2,7 +2,7 @@ import { Segment, SegmentKey } from '../models/segment';
import { MetricExpanded } from '../models/metric'; import { MetricExpanded } from '../models/metric';
import { DatasourceRequest } from '../models/datasource'; import { DatasourceRequest } from '../models/datasource';
import { SegmentsSet } from '../models/segment_set'; 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'; import { BackendSrv } from 'grafana/app/core/services/backend_srv';
@ -35,7 +35,7 @@ export class AnalyticService {
} }
async updateSegments( async updateSegments(
key: AnalyticUnitKey, addedSegments: SegmentsSet<Segment>, removedSegments: SegmentsSet<Segment> key: AnalyticUnitId, addedSegments: SegmentsSet<Segment>, removedSegments: SegmentsSet<Segment>
): Promise<SegmentKey[]> { ): Promise<SegmentKey[]> {
const getJSONs = (segs: SegmentsSet<Segment>) => segs.getSegments().map(segment => ({ const getJSONs = (segs: SegmentsSet<Segment>) => segs.getSegments().map(segment => ({
@ -57,7 +57,7 @@ export class AnalyticService {
return data.added_ids as SegmentKey[]; return data.added_ids as SegmentKey[];
} }
async getSegments(key: AnalyticUnitKey, from?: number, to?: number): Promise<AnalyticSegment[]> { async getSegments(key: AnalyticUnitId, from?: number, to?: number): Promise<AnalyticSegment[]> {
var payload: any = { predictor_id: key }; var payload: any = { predictor_id: key };
if(from !== undefined) { if(from !== undefined) {
payload['from'] = from; payload['from'] = from;
@ -76,7 +76,7 @@ export class AnalyticService {
return segments.map(s => new AnalyticSegment(s.labeled, s.id, s.start, s.finish)); 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 () => { let statusCheck = async () => {
var data = await this._backendSrv.get( var data = await this._backendSrv.get(
this._backendURL + '/anomalies/status', { name: key } this._backendURL + '/anomalies/status', { name: key }
@ -95,7 +95,7 @@ export class AnalyticService {
} }
async getAlertEnabled(key: AnalyticUnitKey): Promise<boolean> { async getAlertEnabled(key: AnalyticUnitId): Promise<boolean> {
var data = await this._backendSrv.get( var data = await this._backendSrv.get(
this._backendURL + '/alerts', { predictor_id: key } this._backendURL + '/alerts', { predictor_id: key }
); );
@ -103,7 +103,7 @@ export class AnalyticService {
} }
async setAlertEnabled(key: AnalyticUnitKey, value: boolean): Promise<void> { async setAlertEnabled(key: AnalyticUnitId, value: boolean): Promise<void> {
return this._backendSrv.post( return this._backendSrv.post(
this._backendURL + '/alerts', { predictor_id: key, enable: value } this._backendURL + '/alerts', { predictor_id: key, enable: value }
); );

Loading…
Cancel
Save