Browse Source

Start implementing (#119)

master
rozetko 6 years ago committed by Alexey Velikiy
parent
commit
37d90643f2
  1. 43
      src/controllers/analytic_controller.ts
  2. 15
      src/models/threshold.ts
  3. 7
      src/module.ts
  4. 23
      src/partials/tab_analytics.html
  5. 10
      src/services/analytic_service.ts

43
src/controllers/analytic_controller.ts

@ -12,6 +12,7 @@ import { Segment, SegmentId } 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 { ServerInfo } from '../models/info'; import { ServerInfo } from '../models/info';
import { Threshold, Condition } from '../models/threshold';
import { ANALYTIC_UNIT_COLORS } from '../colors'; import { ANALYTIC_UNIT_COLORS } from '../colors';
@ -43,6 +44,7 @@ export class AnalyticController {
private _graphLocked: boolean = false; private _graphLocked: boolean = false;
private _statusRunners: Set<AnalyticUnitId> = new Set<AnalyticUnitId>(); private _statusRunners: Set<AnalyticUnitId> = new Set<AnalyticUnitId>();
private _serverInfo: ServerInfo; private _serverInfo: ServerInfo;
private _thresholds: Threshold[];
constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) { constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) {
if(_panelObject.analyticUnits === undefined) { if(_panelObject.analyticUnits === undefined) {
@ -51,6 +53,9 @@ export class AnalyticController {
this._labelingDataAddedSegments = new SegmentArray<AnalyticSegment>(); this._labelingDataAddedSegments = new SegmentArray<AnalyticSegment>();
this._labelingDataDeletedSegments = new SegmentArray<AnalyticSegment>(); this._labelingDataDeletedSegments = new SegmentArray<AnalyticSegment>();
this._analyticUnitsSet = new AnalyticUnitsSet(this._panelObject.analyticUnits); this._analyticUnitsSet = new AnalyticUnitsSet(this._panelObject.analyticUnits);
this._thresholds = [];
this.updateThresholds();
// this.analyticUnits.forEach(a => this.runEnabledWaiter(a)); // this.analyticUnits.forEach(a => this.runEnabledWaiter(a));
} }
@ -87,6 +92,9 @@ export class AnalyticController {
this._newAnalyticUnit.id = await this._analyticService.postNewItem( this._newAnalyticUnit.id = await this._analyticService.postNewItem(
metricExpanded, datasourceRequest, this._newAnalyticUnit, panelId metricExpanded, datasourceRequest, this._newAnalyticUnit, panelId
); );
if(this._newAnalyticUnit.detectorType === 'threshold') {
await this.saveThreshold(this._newAnalyticUnit.id);
}
this._analyticUnitsSet.addItem(this._newAnalyticUnit); this._analyticUnitsSet.addItem(this._newAnalyticUnit);
this._creatingNewAnalyticType = false; this._creatingNewAnalyticType = false;
this._savingNewAnalyticUnit = false; this._savingNewAnalyticUnit = false;
@ -335,6 +343,41 @@ export class AnalyticController {
await this._analyticService.setAnalyticUnitAlert(analyticUnit); await this._analyticService.setAnalyticUnitAlert(analyticUnit);
} }
async updateThresholds() {
const ids = _.map(this._panelObject.analyticUnits, (analyticUnit: any) => analyticUnit.id);
const thresholds = await this._analyticService.getThresholds(ids);
console.log(thresholds)
this._thresholds = thresholds;
}
getThreshold(id: AnalyticUnitId) {
let threshold = _.find(this._thresholds, { id });
if(threshold === undefined) {
threshold = {
id,
value: 0,
condition: Condition.ABOVE
};
this._thresholds.push(threshold);
}
return threshold;
}
async saveThreshold(id: AnalyticUnitId) {
const threshold = this.getThreshold(id);
if(threshold.value === undefined) {
throw new Error('Cannot save threshold with undefined value');
}
if(threshold.condition === undefined) {
throw new Error('Cannot save threshold with undefined condition');
}
return this._analyticService.updateThreshold(threshold);
}
public get conditions() {
return _.values(Condition);
}
private async _runStatusWaiter(analyticUnit: AnalyticUnit) { private async _runStatusWaiter(analyticUnit: AnalyticUnit) {
if(analyticUnit === undefined || analyticUnit === null) { if(analyticUnit === undefined || analyticUnit === null) {
throw new Error('analyticUnit not defined'); throw new Error('analyticUnit not defined');

15
src/models/threshold.ts

@ -0,0 +1,15 @@
import { AnalyticUnitId } from './analytic_unit';
export enum Condition {
ABOVE = '>',
ABOVE_OR_EQUAL = '>=',
EQUAL = '=',
LESS_OR_EQUAL = '<=',
LESS = '<'
};
export type Threshold = {
id: AnalyticUnitId,
value: number,
condition: Condition
};

7
src/module.ts

@ -17,9 +17,7 @@ import { axesEditorComponent } from './axes_editor';
import { MetricsPanelCtrl } from 'grafana/app/plugins/sdk'; import { MetricsPanelCtrl } from 'grafana/app/plugins/sdk';
import { appEvents } from 'grafana/app/core/core' import { appEvents } from 'grafana/app/core/core'
import { BackendSrv } from 'grafana/app/core/services/backend_srv'; import { BackendSrv } from 'grafana/app/core/services/backend_srv';
import { AlertSrv } from 'grafana/app/core/services/alert_srv' import { AlertSrv } from 'grafana/app/core/services/alert_srv';
import config from 'grafana/app/core/config';
import _ from 'lodash'; import _ from 'lodash';
@ -162,8 +160,6 @@ class GraphCtrl extends MetricsPanelCtrl {
this.analyticService = new AnalyticService(this.backendURL, $http, this.backendSrv, this.alertSrv); this.analyticService = new AnalyticService(this.backendURL, $http, this.backendSrv, this.alertSrv);
this.updateAnalyticUnitTypes();
this.runBackendConnectivityCheck(); this.runBackendConnectivityCheck();
this.analyticsController = new AnalyticController(this.panel, this.analyticService, this.events); this.analyticsController = new AnalyticController(this.panel, this.analyticService, this.events);
@ -240,6 +236,7 @@ class GraphCtrl extends MetricsPanelCtrl {
let connected = await this.analyticService.isBackendOk(); let connected = await this.analyticService.isBackendOk();
if(connected) { if(connected) {
this.updateAnalyticUnitTypes();
this.alertSrv.set( this.alertSrv.set(
'Connected to Hastic server', 'Connected to Hastic server',
`Hastic server: "${this.backendURL}"`, `Hastic server: "${this.backendURL}"`,

23
src/partials/tab_analytics.html

@ -22,7 +22,7 @@
<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.type" ng-model="analyticUnit.type"
ng-options="type.value as type.name for type in ctrl.analyticUnitTypes.pattern" ng-options="type.value as type.name for type in ctrl.analyticUnitTypes[analyticUnit.detectorType]"
ng-disabled="true" ng-disabled="true"
/> />
</div> </div>
@ -44,7 +44,11 @@
/> />
</span> </span>
<label class="gf-form-label" ng-style="analyticUnit.status === 'LEARNING' && { 'cursor': 'not-allowed' }"> <label
class="gf-form-label"
ng-if="analyticUnit.detectorType === 'pattern'"
ng-style="analyticUnit.status === 'LEARNING' && { 'cursor': 'not-allowed' }"
>
<a class="pointer" tabindex="1" <a class="pointer" tabindex="1"
ng-click="ctrl.onToggleLabelingMode(analyticUnit.id)" ng-click="ctrl.onToggleLabelingMode(analyticUnit.id)"
ng-disabled="analyticUnit.status === 'LEARNING'" ng-disabled="analyticUnit.status === 'LEARNING'"
@ -57,6 +61,21 @@
</a> </a>
</label> </label>
<select class="gf-form-input width-5"
ng-model="ctrl.analyticsController.getThreshold(analyticUnit.id).condition"
ng-options="type for type in ctrl.analyticsController.conditions"
ng-if="analyticUnit.detectorType === 'threshold'"
ng-blur="ctrl.analyticsController.saveThreshold(analyticUnit.id)"
/>
<input
class="gf-form-input width-5"
type="number"
ng-model="ctrl.analyticsController.getThreshold(analyticUnit.id).value"
ng-if="analyticUnit.detectorType === 'threshold'"
ng-blur="ctrl.analyticsController.saveThreshold(analyticUnit.id)"
/>
<label class="gf-form-label"> <label class="gf-form-label">
<a <a
ng-if="analyticUnit.visible" ng-if="analyticUnit.visible"

10
src/services/analytic_service.ts

@ -4,6 +4,7 @@ import { DatasourceRequest } from '../models/datasource';
import { SegmentsSet } from '../models/segment_set'; import { SegmentsSet } from '../models/segment_set';
import { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analytic_unit'; import { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analytic_unit';
import { ServerInfo } from '../models/info'; import { ServerInfo } from '../models/info';
import { Threshold } from '../models/threshold';
import { BackendSrv } from 'grafana/app/core/services/backend_srv'; import { BackendSrv } from 'grafana/app/core/services/backend_srv';
import { AlertSrv } from 'grafana/app/core/services/alert_srv'; import { AlertSrv } from 'grafana/app/core/services/alert_srv';
@ -24,6 +25,15 @@ export class AnalyticService {
return await this.get('/analyticUnits/types'); return await this.get('/analyticUnits/types');
} }
async getThresholds(ids: AnalyticUnitId[]) {
const resp = await this.get('/threshold', { ids: ids.join(',') });
return resp.thresholds.filter(t => t !== null);
}
async updateThreshold(threshold: Threshold) {
return await this.patch('/threshold', threshold);
}
async postNewItem( async postNewItem(
metric: MetricExpanded, datasourceRequest: DatasourceRequest, metric: MetricExpanded, datasourceRequest: DatasourceRequest,
newItem: AnalyticUnit, panelId: number newItem: AnalyticUnit, panelId: number

Loading…
Cancel
Save