Browse Source

renaming++

master
Alexey Velikiy 6 years ago
parent
commit
fcf0b9a1b3
  1. 10
      dist/module.js
  2. 14
      src/controllers/analytic_controller.ts
  3. 8
      src/models/analytic_unit.ts
  4. 14
      src/models/segment.ts
  5. 23
      src/models/segment_array.ts
  6. 8
      src/models/segment_set.ts
  7. 37
      src/services/analytic_service.ts

10
dist/module.js vendored

File diff suppressed because one or more lines are too long

14
src/controllers/analytic_controller.ts

@ -8,7 +8,7 @@ import {
} from '../models/analytic_unit';
import { MetricExpanded } from '../models/metric';
import { DatasourceRequest } from '../models/datasource';
import { Segment, SegmentKey } from '../models/segment';
import { Segment, SegmentId } from '../models/segment';
import { SegmentsSet } from '../models/segment_set';
import { SegmentArray } from '../models/segment_array';
@ -72,7 +72,7 @@ export class AnalyticController {
async saveNew(metricExpanded: MetricExpanded, datasourceRequest: DatasourceRequest, panelId: number) {
this._savingNewAnalyticUnit = true;
await this._analyticService.postNewAnalyticUnit(metricExpanded, datasourceRequest, this._newAnalyticUnitType, panelId);
await this._analyticService.postNewItem(metricExpanded, datasourceRequest, this._newAnalyticUnitType, panelId);
this._analyticUnitsSet.addItem(this._newAnalyticUnitType);
this._creatingNewAnalyticType = false;
this._savingNewAnalyticUnit = false;
@ -114,7 +114,7 @@ export class AnalyticController {
this.labelingAnomaly.saving = true;
var newIds = await this._saveLabelingData();
this._labelingDataAddedSegments.getSegments().forEach((s, i) => {
this.labelingAnomaly.segments.updateKey(s.key, newIds[i]);
this.labelingAnomaly.segments.updateId(s.id, newIds[i]);
})
this.labelingAnomaly.saving = false;
@ -125,7 +125,7 @@ export class AnalyticController {
undoLabeling() {
this._labelingDataAddedSegments.getSegments().forEach(s => {
this.labelingAnomaly.segments.remove(s.key);
this.labelingAnomaly.segments.remove(s.id);
});
this._labelingDataDeletedSegments.getSegments().forEach(s => {
this.labelingAnomaly.segments.addSegment(s);
@ -191,12 +191,12 @@ export class AnalyticController {
var allSegmentsSet = new SegmentArray(allSegmentsList);
if(anomalyType.selected) {
this._labelingDataAddedSegments.getSegments().forEach(s => allSegmentsSet.addSegment(s));
this._labelingDataDeletedSegments.getSegments().forEach(s => allSegmentsSet.remove(s.key));
this._labelingDataDeletedSegments.getSegments().forEach(s => allSegmentsSet.remove(s.id));
}
anomalyType.segments = allSegmentsSet;
}
private async _saveLabelingData(): Promise<SegmentKey[]> {
private async _saveLabelingData(): Promise<SegmentId[]> {
var anomaly = this.labelingAnomaly;
if(anomaly === null) {
throw new Error('anomaly is not selected');
@ -260,7 +260,7 @@ export class AnalyticController {
deleteLabelingAnomalySegmentsInRange(from: number, to: number) {
var allRemovedSegs = this.labelingAnomaly.removeSegmentsInRange(from, to);
allRemovedSegs.forEach(s => {
if(!this._labelingDataAddedSegments.has(s.key)) {
if(!this._labelingDataAddedSegments.has(s.id)) {
this._labelingDataDeletedSegments.addSegment(s);
}
});

8
src/models/analytic_unit.ts

@ -1,6 +1,6 @@
import { SegmentsSet } from './segment_set';
import { SegmentArray } from './segment_array';
import { Segment, SegmentKey } from './segment';
import { Segment, SegmentId } from './segment';
import { Metric } from './metric';
import _ from 'lodash';
@ -11,7 +11,7 @@ export type AnalyticSegmentsSearcher = (point: number, rangeDist: number) => Ana
export type AnalyticUnitId = string;
export class AnalyticSegment extends Segment {
constructor(public labeled: boolean, key: SegmentKey, from: number, to: number) {
constructor(public labeled: boolean, key: SegmentId, from: number, to: number) {
super(key, from, to);
if(!_.isBoolean(labeled)) {
throw new Error('labeled value is not boolean');
@ -36,7 +36,7 @@ export class AnalyticUnit {
this._panelObject = {};
}
_.defaults(this._panelObject, {
name: 'anomaly_name', confidence: 0.2, color: 'red', pattern: 'General'
name: 'analytcUnitName', confidence: 0.2, color: 'red', pattern: 'General'
});
//this._metric = new Metric(_panelObject.metric);
@ -75,7 +75,7 @@ export class AnalyticUnit {
get metric() { return this._metric; }
addLabeledSegment(segment: Segment): AnalyticSegment {
var asegment = new AnalyticSegment(true, segment.key, segment.from, segment.to);
var asegment = new AnalyticSegment(true, segment.id, segment.from, segment.to);
this._segmentSet.addSegment(asegment);
return asegment;
}

14
src/models/segment.ts

@ -1,8 +1,8 @@
export type SegmentKey = number;
export type SegmentId = number;
export class Segment {
constructor(private _key: SegmentKey, public from: number, public to: number) {
if(isNaN(this._key)) {
constructor(private _id: SegmentId, public from: number, public to: number) {
if(isNaN(this._id)) {
throw new Error('Key can`t be NaN');
}
if(isNaN(+from)) {
@ -13,8 +13,8 @@ export class Segment {
}
}
get key(): SegmentKey { return this._key; }
set key(value) { this._key = value; }
get id(): SegmentId { return this._id; }
set id(value) { this._id = value; }
get middle() { return (this.from + this.to) / 2; }
@ -27,10 +27,10 @@ export class Segment {
var q = Math.round(this.middle + allDist * portion / 2);
p = Math.min(p, this.from);
q = Math.max(q, this.to);
return new Segment(this._key, p, q);
return new Segment(this._id, p, q);
}
equals(segment: Segment) {
return this._key === segment._key;
return this._id === segment._id;
}
}

23
src/models/segment_array.ts

@ -1,12 +1,12 @@
import { SegmentsSet } from './segment_set';
import { Segment, SegmentKey } from './segment';
import { Segment, SegmentId } from './segment';
import _ from 'lodash';
export class SegmentArray<T extends Segment> implements SegmentsSet<T> {
private _segments: T[];
private _keyToSegment: Map<SegmentKey, T> = new Map<SegmentKey, T>();
private _keyToSegment: Map<SegmentId, T> = new Map<SegmentId, T>();
constructor(private segments?: T[]) {
this.setSegments(segments);
@ -40,10 +40,10 @@ export class SegmentArray<T extends Segment> implements SegmentsSet<T> {
}
addSegment(segment: T) {
if(this.has(segment.key)) {
throw new Error(`Segment with key ${segment.key} exists in set`);
if(this.has(segment.id)) {
throw new Error(`Segment with key ${segment.id} exists in set`);
}
this._keyToSegment.set(segment.key, segment);
this._keyToSegment.set(segment.id, segment);
this._segments.push(segment);
}
@ -60,7 +60,7 @@ export class SegmentArray<T extends Segment> implements SegmentsSet<T> {
for(var i = 0; i < this._segments.length; i++) {
var s = this._segments[i];
if(from <= s.from && s.to <= to) {
this._keyToSegment.delete(s.key);
this._keyToSegment.delete(s.id);
deleted.push(s);
} else {
newSegments.push(s);
@ -79,26 +79,25 @@ export class SegmentArray<T extends Segment> implements SegmentsSet<T> {
this._keyToSegment.clear();
}
has(key: SegmentKey): boolean {
has(key: SegmentId): boolean {
return this._keyToSegment.has(key);
}
remove(key: SegmentKey): boolean {
remove(key: SegmentId): boolean {
if(!this.has(key)) {
return false;
}
var index = this._segments.findIndex(s => s.key === key);
var index = this._segments.findIndex(s => s.id === key);
this._segments.splice(index, 1);
this._keyToSegment.delete(key);
return true;
}
updateKey(fromKey: SegmentKey, toKey: SegmentKey) {
updateId(fromKey: SegmentId, toKey: SegmentId) {
var segment = this._keyToSegment.get(fromKey);
this._keyToSegment.delete(fromKey);
segment.key = toKey;
segment.id = toKey;
this._keyToSegment.set(toKey, segment);
}
}

8
src/models/segment_set.ts

@ -1,4 +1,4 @@
import { Segment, SegmentKey } from './segment'
import { Segment, SegmentId } from './segment'
export interface SegmentsSet<T extends Segment> {
getSegments(from?: number, to?: number): T[];
@ -6,9 +6,9 @@ export interface SegmentsSet<T extends Segment> {
addSegment(segment: T): void;
findSegments(point: number, rangeDist: number): T[];
removeInRange(from: number, to: number): T[];
remove(key: SegmentKey): boolean;
has(key: SegmentKey): boolean;
remove(id: SegmentId): boolean;
has(id: SegmentId): boolean;
clear(): void;
updateKey(fromKey: SegmentKey, toKey: SegmentKey): void;
updateId(fromId: SegmentId, toId: SegmentId): void;
length: number;
}

37
src/services/analytic_service.ts

@ -1,4 +1,4 @@
import { Segment, SegmentKey } from '../models/segment';
import { Segment, SegmentId } from '../models/segment';
import { MetricExpanded } from '../models/metric';
import { DatasourceRequest } from '../models/datasource';
import { SegmentsSet } from '../models/segment_set';
@ -12,15 +12,18 @@ export class AnalyticService {
constructor(private _backendURL: string, private _backendSrv: BackendSrv) {
}
async postNewAnalyticUnit(metric: MetricExpanded, datasourceRequest: DatasourceRequest, newAnomalyType: AnalyticUnit, panelId: number) {
async postNewItem(
metric: MetricExpanded, datasourceRequest: DatasourceRequest,
newItem: AnalyticUnit, panelId: number
) {
return this._backendSrv.post(
this._backendURL + '/anomalies',
this._backendURL + '/analyticUnits',
{
name: newAnomalyType.name,
name: newItem.name,
metric: metric.toJSON(),
panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`,
datasource: datasourceRequest,
pattern: newAnomalyType.pattern
pattern: newItem.pattern
}
)
};
@ -36,7 +39,7 @@ export class AnalyticService {
async updateSegments(
key: AnalyticUnitId, addedSegments: SegmentsSet<Segment>, removedSegments: SegmentsSet<Segment>
): Promise<SegmentKey[]> {
): Promise<SegmentId[]> {
const getJSONs = (segs: SegmentsSet<Segment>) => segs.getSegments().map(segment => ({
"start": segment.from,
@ -45,20 +48,20 @@ export class AnalyticService {
var payload = {
name: key,
added_segments: getJSONs(addedSegments),
removed_segments: removedSegments.getSegments().map(s => s.key)
addedSegments: getJSONs(addedSegments),
removedSegments: removedSegments.getSegments().map(s => s.id)
}
var data = await this._backendSrv.patch(this._backendURL + '/segments', payload);
if(data.added_ids === undefined) {
if(data.addedIds === undefined) {
throw new Error('Server didn`t send added_ids');
}
return data.added_ids as SegmentKey[];
return data.addedIds as SegmentId[];
}
async getSegments(key: AnalyticUnitId, from?: number, to?: number): Promise<AnalyticSegment[]> {
var payload: any = { predictor_id: key };
async getSegments(id: AnalyticUnitId, from?: number, to?: number): Promise<AnalyticSegment[]> {
var payload: any = { id };
if(from !== undefined) {
payload['from'] = from;
}
@ -79,7 +82,7 @@ export class AnalyticService {
async * getAnomalyTypeStatusGenerator(key: AnalyticUnitId, duration: number) {
let statusCheck = async () => {
var data = await this._backendSrv.get(
this._backendURL + '/anomalies/status', { name: key }
this._backendURL + '/analyticUnits/status', { name: key }
);
return data;
}
@ -95,17 +98,17 @@ export class AnalyticService {
}
async getAlertEnabled(key: AnalyticUnitId): Promise<boolean> {
async getAlertEnabled(id: AnalyticUnitId): Promise<boolean> {
var data = await this._backendSrv.get(
this._backendURL + '/alerts', { predictor_id: key }
this._backendURL + '/alerts', { id }
);
return data.enable as boolean;
}
async setAlertEnabled(key: AnalyticUnitId, value: boolean): Promise<void> {
async setAlertEnabled(id: AnalyticUnitId, enabled: boolean): Promise<void> {
return this._backendSrv.post(
this._backendURL + '/alerts', { predictor_id: key, enable: value }
this._backendURL + '/alerts', { id, enabled }
);
}

Loading…
Cancel
Save