Browse Source

some commit

master
Alexey Velikiy 6 years ago
parent
commit
0de869640d
  1. 14754
      dist/module.js
  2. 8
      dist/partials/tab_analytics.html
  3. 22
      src/controllers/analytic_controller.ts
  4. 19
      src/models/analytic_unit.ts
  5. 5
      src/module.ts
  6. 8
      src/partials/tab_analytics.html
  7. 37
      src/services/analytic_service.ts

14754
dist/module.js vendored

File diff suppressed because one or more lines are too long

8
dist/partials/tab_analytics.html vendored

@ -12,8 +12,8 @@
<label class="gf-form-label width-8"> Type </label> <label class="gf-form-label width-8"> Type </label>
<div class="gf-form-select-wrapper"> <div class="gf-form-select-wrapper">
<select class="gf-form-input width-12" <select class="gf-form-input width-12"
ng-model="analyticUnit.pattern" ng-model="analyticUnit.type"
ng-options="pattern as pattern for pattern in ctrl.patterns" ng-options="type as type for type in ctrl.ANALYTIC_TYPES"
ng-disabled="true" ng-disabled="true"
/> />
</div> </div>
@ -126,8 +126,8 @@
<label class="gf-form-label width-8"> Type </label> <label class="gf-form-label width-8"> Type </label>
<div class="gf-form-select-wrapper"> <div class="gf-form-select-wrapper">
<select class="gf-form-input width-12" <select class="gf-form-input width-12"
ng-model="ctrl.analyticsController.newAnalyticUnit.pattern" ng-model="ctrl.analyticsController.newAnalyticUnit.type"
ng-options="pattern as pattern disable when (pattern !== 'General' && pattern !== 'Drops' && pattern !== 'Peaks') for pattern in ctrl.patterns" ng-options="type as type for type in ctrl.ANALYTIC_TYPES"
/> />
</div> </div>

22
src/controllers/analytic_controller.ts

@ -30,7 +30,7 @@ export class AnalyticController {
private _labelingDataAddedSegments: SegmentsSet<AnalyticSegment>; private _labelingDataAddedSegments: SegmentsSet<AnalyticSegment>;
private _labelingDataDeletedSegments: SegmentsSet<AnalyticSegment>; private _labelingDataDeletedSegments: SegmentsSet<AnalyticSegment>;
private _newAnalyticUnitType: AnalyticUnit = null; private _newAnalyticUnit: AnalyticUnit = null;
private _creatingNewAnalyticType: boolean = false; private _creatingNewAnalyticType: boolean = false;
private _savingNewAnalyticUnit: boolean = false; private _savingNewAnalyticUnit: boolean = false;
private _tempIdCounted = -1; private _tempIdCounted = -1;
@ -65,24 +65,26 @@ export class AnalyticController {
} }
createNew() { createNew() {
this._newAnalyticUnitType = new AnalyticUnit(); this._newAnalyticUnit = new AnalyticUnit();
this._creatingNewAnalyticType = true; this._creatingNewAnalyticType = true;
this._savingNewAnalyticUnit = false; this._savingNewAnalyticUnit = false;
} }
async saveNew(metricExpanded: MetricExpanded, datasourceRequest: DatasourceRequest, panelId: number) { async saveNew(metricExpanded: MetricExpanded, datasourceRequest: DatasourceRequest, panelId: number) {
this._savingNewAnalyticUnit = true; this._savingNewAnalyticUnit = true;
await this._analyticService.postNewItem(metricExpanded, datasourceRequest, this._newAnalyticUnitType, panelId); this._newAnalyticUnit.id = await this._analyticService.postNewItem(
this._analyticUnitsSet.addItem(this._newAnalyticUnitType); metricExpanded, datasourceRequest, this._newAnalyticUnit, panelId
);
this._analyticUnitsSet.addItem(this._newAnalyticUnit);
this._creatingNewAnalyticType = false; this._creatingNewAnalyticType = false;
this._savingNewAnalyticUnit = false; this._savingNewAnalyticUnit = false;
this.runEnabledWaiter(this._newAnalyticUnitType); this.runEnabledWaiter(this._newAnalyticUnit);
this._runStatusWaiter(this._newAnalyticUnitType); this._runStatusWaiter(this._newAnalyticUnit);
} }
get creatingNew() { return this._creatingNewAnalyticType; } get creatingNew() { return this._creatingNewAnalyticType; }
get saving() { return this._savingNewAnalyticUnit; } get saving() { return this._savingNewAnalyticUnit; }
get newAnalyticUnit(): AnalyticUnit { return this._newAnalyticUnitType; } get newAnalyticUnit(): AnalyticUnit { return this._newAnalyticUnit; }
get graphLocked() { return this._graphLocked; } get graphLocked() { return this._graphLocked; }
set graphLocked(value) { this._graphLocked = value; } set graphLocked(value) { this._graphLocked = value; }
@ -286,6 +288,10 @@ export class AnalyticController {
throw new Error('anomalyType not defined'); throw new Error('anomalyType not defined');
} }
if(anomalyType.id === undefined) {
throw new Error('anomalyType.id is undefined');
}
if(this._statusRunners.has(anomalyType.id)) { if(this._statusRunners.has(anomalyType.id)) {
return; return;
} }
@ -322,7 +328,7 @@ export class AnalyticController {
} }
} }
async toggleAnomalyTypeAlertEnabled(anomalyType: AnalyticUnit) { async toggleAlertEnabled(anomalyType: AnalyticUnit) {
var enabled = anomalyType.alertEnabled; var enabled = anomalyType.alertEnabled;
anomalyType.alertEnabled = undefined; anomalyType.alertEnabled = undefined;
await this._analyticService.setAlertEnabled(anomalyType.id, enabled); await this._analyticService.setAlertEnabled(anomalyType.id, enabled);

19
src/models/analytic_unit.ts

@ -28,7 +28,7 @@ export class AnalyticUnit {
private _status: string; private _status: string;
private _error: string; private _error: string;
private _metric: Metric; private _metric: Metric;
private _alertEnabled?: boolean; private _alertEnabled?: boolean;
constructor(private _panelObject?: any) { constructor(private _panelObject?: any) {
@ -36,19 +36,20 @@ export class AnalyticUnit {
this._panelObject = {}; this._panelObject = {};
} }
_.defaults(this._panelObject, { _.defaults(this._panelObject, {
name: 'AnalyticUnitName', confidence: 0.2, color: 'red', pattern: 'General' name: 'AnalyticUnitName', confidence: 0.2, color: 'red', type: 'General'
}); });
//this._metric = new Metric(_panelObject.metric); //this._metric = new Metric(_panelObject.metric);
} }
get id(): AnalyticUnitId { return this._panelObject.id; } get id(): AnalyticUnitId { return this._panelObject.id; }
set id(value: AnalyticUnitId) { this._panelObject.id = value; }
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; }
set pattern(value: string) { this._panelObject.pattern = value; } set type(value: string) { this._panelObject.type = value; }
get pattern(): string { return this._panelObject.pattern.split(' ')[0]; } get type(): string { return this._panelObject.type; }
set confidence(value: number) { this._panelObject.confidence = value; } set confidence(value: number) { this._panelObject.confidence = value; }
get confidence(): number { return this._panelObject.confidence; } get confidence(): number { return this._panelObject.confidence; }
@ -65,7 +66,7 @@ export class AnalyticUnit {
get saving(): boolean { return this._saving; } get saving(): boolean { return this._saving; }
set saving(value: boolean) { this._saving = value; } set saving(value: boolean) { this._saving = value; }
get visible(): boolean { get visible(): boolean {
return (this._panelObject.visible === undefined) ? true : this._panelObject.visible return (this._panelObject.visible === undefined) ? true : this._panelObject.visible
} }
set visible(value: boolean) { set visible(value: boolean) {
@ -88,13 +89,13 @@ export class AnalyticUnit {
set segments(value: SegmentsSet<AnalyticSegment>) { set segments(value: SegmentsSet<AnalyticSegment>) {
this._segmentSet.setSegments(value.getSegments()); this._segmentSet.setSegments(value.getSegments());
} }
get status() { return this._status; } get status() { return this._status; }
set status(value) { set status(value) {
if( if(
value !== 'ready' && value !== 'ready' &&
value !== 'learning' && value !== 'learning' &&
value !== 'pending' && value !== 'pending' &&
value !== 'failed' value !== 'failed'
) { ) {
throw new Error('Unsupported status value: ' + value); throw new Error('Unsupported status value: ' + value);

5
src/module.ts

@ -26,6 +26,7 @@ const BACKEND_VARIABLE_NAME = 'HASTIC_SERVER_URL';
class GraphCtrl extends MetricsPanelCtrl { class GraphCtrl extends MetricsPanelCtrl {
static template = template; static template = template;
ANALYTIC_TYPES: Array<String> = ['General', 'Drops', 'Peaks'];
hiddenSeries: any = {}; hiddenSeries: any = {};
seriesList: any = []; seriesList: any = [];
@ -43,7 +44,7 @@ class GraphCtrl extends MetricsPanelCtrl {
processor: DataProcessor; processor: DataProcessor;
datasourceRequest: DatasourceRequest; datasourceRequest: DatasourceRequest;
patterns: Array<String> = ['General', 'Drops', 'Peaks', 'Jumps'];
anomalyTypes = []; // TODO: remove it later. Only for alert tab anomalyTypes = []; // TODO: remove it later. Only for alert tab
analyticsController: AnalyticController; analyticsController: AnalyticController;
@ -566,7 +567,7 @@ class GraphCtrl extends MetricsPanelCtrl {
} }
onAnomalyAlertChange(anomalyType: AnalyticUnit) { onAnomalyAlertChange(anomalyType: AnalyticUnit) {
this.analyticsController.toggleAnomalyTypeAlertEnabled(anomalyType); this.analyticsController.toggleAlertEnabled(anomalyType);
} }
onToggleVisibility(key: AnalyticUnitId) { onToggleVisibility(key: AnalyticUnitId) {

8
src/partials/tab_analytics.html

@ -12,8 +12,8 @@
<label class="gf-form-label width-8"> Type </label> <label class="gf-form-label width-8"> Type </label>
<div class="gf-form-select-wrapper"> <div class="gf-form-select-wrapper">
<select class="gf-form-input width-12" <select class="gf-form-input width-12"
ng-model="analyticUnit.pattern" ng-model="analyticUnit.type"
ng-options="pattern as pattern for pattern in ctrl.patterns" ng-options="type as type for type in ctrl.ANALYTIC_TYPES"
ng-disabled="true" ng-disabled="true"
/> />
</div> </div>
@ -126,8 +126,8 @@
<label class="gf-form-label width-8"> Type </label> <label class="gf-form-label width-8"> Type </label>
<div class="gf-form-select-wrapper"> <div class="gf-form-select-wrapper">
<select class="gf-form-input width-12" <select class="gf-form-input width-12"
ng-model="ctrl.analyticsController.newAnalyticUnit.pattern" ng-model="ctrl.analyticsController.newAnalyticUnit.type"
ng-options="pattern as pattern disable when (pattern !== 'General' && pattern !== 'Drops' && pattern !== 'Peaks') for pattern in ctrl.patterns" ng-options="type as type for type in ctrl.ANALYTIC_TYPES"
/> />
</div> </div>

37
src/services/analytic_service.ts

@ -7,7 +7,6 @@ import { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analyti
import { BackendSrv } from 'grafana/app/core/services/backend_srv'; import { BackendSrv } from 'grafana/app/core/services/backend_srv';
export class AnalyticService { export class AnalyticService {
constructor(private _backendURL: string, private _backendSrv: BackendSrv) { constructor(private _backendURL: string, private _backendSrv: BackendSrv) {
} }
@ -15,22 +14,23 @@ export class AnalyticService {
async postNewItem( async postNewItem(
metric: MetricExpanded, datasourceRequest: DatasourceRequest, metric: MetricExpanded, datasourceRequest: DatasourceRequest,
newItem: AnalyticUnit, panelId: number newItem: AnalyticUnit, panelId: number
) { ): Promise<AnalyticUnitId> {
return this._backendSrv.post( return this._backendSrv.post(
this._backendURL + '/analyticUnits', this._backendURL + '/analyticUnits',
{ {
name: newItem.id, name: newItem.name,
metric: metric.toJSON(), metric: metric.toJSON(),
panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`, panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`,
datasource: datasourceRequest, datasource: datasourceRequest,
pattern: newItem.pattern type: newItem.type
} }
) ).then(res => res.id as AnalyticUnitId);
}; };
async isBackendOk(): Promise<boolean> { async isBackendOk(): Promise<boolean> {
try { try {
var data = await this._backendSrv.get(this._backendURL); var data = await this._backendSrv.get(this._backendURL);
// TODO: check version
return true; return true;
} catch(e) { } catch(e) {
return false; return false;
@ -38,7 +38,7 @@ export class AnalyticService {
} }
async updateSegments( async updateSegments(
key: AnalyticUnitId, addedSegments: SegmentsSet<Segment>, removedSegments: SegmentsSet<Segment> id: AnalyticUnitId, addedSegments: SegmentsSet<Segment>, removedSegments: SegmentsSet<Segment>
): Promise<SegmentId[]> { ): Promise<SegmentId[]> {
const getJSONs = (segs: SegmentsSet<Segment>) => segs.getSegments().map(segment => ({ const getJSONs = (segs: SegmentsSet<Segment>) => segs.getSegments().map(segment => ({
@ -47,20 +47,23 @@ export class AnalyticService {
})); }));
var payload = { var payload = {
name: key, id,
addedSegments: getJSONs(addedSegments), addedSegments: getJSONs(addedSegments),
removedSegments: removedSegments.getSegments().map(s => s.id) removedSegments: removedSegments.getSegments().map(s => s.id)
} }
var data = await this._backendSrv.patch(this._backendURL + '/segments', payload); var data = await this._backendSrv.patch(this._backendURL + '/segments', payload);
if(data.addedIds === undefined) { if(data.addedIds === undefined) {
throw new Error('Server didn`t send added_ids'); throw new Error('Server didn`t send addedIds');
} }
return data.addedIds as SegmentId[]; return data.addedIds as SegmentId[];
} }
async getSegments(id: AnalyticUnitId, from?: number, to?: number): Promise<AnalyticSegment[]> { async getSegments(id: AnalyticUnitId, from?: number, to?: number): Promise<AnalyticSegment[]> {
if(id === undefined) {
throw new Error('id is undefined');
}
var payload: any = { id }; var payload: any = { id };
if(from !== undefined) { if(from !== undefined) {
payload['from'] = from; payload['from'] = from;
@ -68,10 +71,7 @@ export class AnalyticService {
if(to !== undefined) { if(to !== undefined) {
payload['to'] = to; payload['to'] = to;
} }
var data = await this._backendSrv.get( var data = await this._backendSrv.get(this._backendURL + '/segments', payload);
this._backendURL + '/segments',
payload
);
if(data.segments === undefined) { if(data.segments === undefined) {
throw new Error('Server didn`t return segments array'); throw new Error('Server didn`t return segments array');
} }
@ -79,10 +79,13 @@ 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: AnalyticUnitId, duration: number) { async * getAnomalyTypeStatusGenerator(id: AnalyticUnitId, duration: number) {
if(id === undefined) {
throw new Error('id is undefined');
}
let statusCheck = async () => { let statusCheck = async () => {
var data = await this._backendSrv.get( var data = await this._backendSrv.get(
this._backendURL + '/analyticUnits/status', { name: key } this._backendURL + '/analyticUnits/status', { id }
); );
return data; return data;
} }
@ -99,6 +102,9 @@ export class AnalyticService {
} }
async getAlertEnabled(id: AnalyticUnitId): Promise<boolean> { async getAlertEnabled(id: AnalyticUnitId): Promise<boolean> {
if(id === undefined) {
throw new Error('id is undefined');
}
var data = await this._backendSrv.get( var data = await this._backendSrv.get(
this._backendURL + '/alerts', { id } this._backendURL + '/alerts', { id }
); );
@ -107,6 +113,9 @@ export class AnalyticService {
} }
async setAlertEnabled(id: AnalyticUnitId, enabled: boolean): Promise<void> { async setAlertEnabled(id: AnalyticUnitId, enabled: boolean): Promise<void> {
if(id === undefined) {
throw new Error('id is undefined');
}
return this._backendSrv.post( return this._backendSrv.post(
this._backendURL + '/alerts', { id, enabled } this._backendURL + '/alerts', { id, enabled }
); );

Loading…
Cancel
Save