Browse Source

Start implementing (#119)

master
rozetko 5 years ago committed by Alexey Velikiy
parent
commit
37d90643f2
  1. 43
      src/controllers/analytic_controller.ts
  2. 15
      src/models/threshold.ts
  3. 13
      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 { SegmentArray } from '../models/segment_array';
import { ServerInfo } from '../models/info';
import { Threshold, Condition } from '../models/threshold';
import { ANALYTIC_UNIT_COLORS } from '../colors';
@ -43,6 +44,7 @@ export class AnalyticController {
private _graphLocked: boolean = false;
private _statusRunners: Set<AnalyticUnitId> = new Set<AnalyticUnitId>();
private _serverInfo: ServerInfo;
private _thresholds: Threshold[];
constructor(private _panelObject: any, private _analyticService: AnalyticService, private _emitter: Emitter) {
if(_panelObject.analyticUnits === undefined) {
@ -51,6 +53,9 @@ export class AnalyticController {
this._labelingDataAddedSegments = new SegmentArray<AnalyticSegment>();
this._labelingDataDeletedSegments = new SegmentArray<AnalyticSegment>();
this._analyticUnitsSet = new AnalyticUnitsSet(this._panelObject.analyticUnits);
this._thresholds = [];
this.updateThresholds();
// this.analyticUnits.forEach(a => this.runEnabledWaiter(a));
}
@ -87,6 +92,9 @@ export class AnalyticController {
this._newAnalyticUnit.id = await this._analyticService.postNewItem(
metricExpanded, datasourceRequest, this._newAnalyticUnit, panelId
);
if(this._newAnalyticUnit.detectorType === 'threshold') {
await this.saveThreshold(this._newAnalyticUnit.id);
}
this._analyticUnitsSet.addItem(this._newAnalyticUnit);
this._creatingNewAnalyticType = false;
this._savingNewAnalyticUnit = false;
@ -335,6 +343,41 @@ export class AnalyticController {
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) {
if(analyticUnit === undefined || analyticUnit === null) {
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
};

13
src/module.ts

@ -17,9 +17,7 @@ import { axesEditorComponent } from './axes_editor';
import { MetricsPanelCtrl } from 'grafana/app/plugins/sdk';
import { appEvents } from 'grafana/app/core/core'
import { BackendSrv } from 'grafana/app/core/services/backend_srv';
import { AlertSrv } from 'grafana/app/core/services/alert_srv'
import config from 'grafana/app/core/config';
import { AlertSrv } from 'grafana/app/core/services/alert_srv';
import _ from 'lodash';
@ -143,9 +141,9 @@ class GraphCtrl extends MetricsPanelCtrl {
/** @ngInject */
constructor(
$scope, $injector, $http,
$scope, $injector, $http,
private annotationsSrv,
private keybindingSrv,
private keybindingSrv,
private backendSrv: BackendSrv,
private popoverSrv,
private contextSrv,
@ -162,8 +160,6 @@ class GraphCtrl extends MetricsPanelCtrl {
this.analyticService = new AnalyticService(this.backendURL, $http, this.backendSrv, this.alertSrv);
this.updateAnalyticUnitTypes();
this.runBackendConnectivityCheck();
this.analyticsController = new AnalyticController(this.panel, this.analyticService, this.events);
@ -215,7 +211,7 @@ class GraphCtrl extends MetricsPanelCtrl {
return val;
}
async updateAnalyticUnitTypes(){
async updateAnalyticUnitTypes() {
const analyticUnitTypes = await this.analyticService.getAnalyticUnitTypes();
this._analyticUnitTypes = analyticUnitTypes;
}
@ -240,6 +236,7 @@ class GraphCtrl extends MetricsPanelCtrl {
let connected = await this.analyticService.isBackendOk();
if(connected) {
this.updateAnalyticUnitTypes();
this.alertSrv.set(
'Connected to Hastic server',
`Hastic server: "${this.backendURL}"`,

23
src/partials/tab_analytics.html

@ -22,7 +22,7 @@
<div class="gf-form-select-wrapper">
<select class="gf-form-input width-12"
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"
/>
</div>
@ -44,7 +44,11 @@
/>
</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"
ng-click="ctrl.onToggleLabelingMode(analyticUnit.id)"
ng-disabled="analyticUnit.status === 'LEARNING'"
@ -57,6 +61,21 @@
</a>
</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">
<a
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 { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analytic_unit';
import { ServerInfo } from '../models/info';
import { Threshold } from '../models/threshold';
import { BackendSrv } from 'grafana/app/core/services/backend_srv';
import { AlertSrv } from 'grafana/app/core/services/alert_srv';
@ -24,6 +25,15 @@ export class AnalyticService {
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(
metric: MetricExpanded, datasourceRequest: DatasourceRequest,
newItem: AnalyticUnit, panelId: number

Loading…
Cancel
Save