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

22
src/controllers/analytic_controller.ts

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

19
src/models/analytic_unit.ts

@ -28,7 +28,7 @@ export class AnalyticUnit {
private _status: string;
private _error: string;
private _metric: Metric;
private _alertEnabled?: boolean;
constructor(private _panelObject?: any) {
@ -36,19 +36,20 @@ export class AnalyticUnit {
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);
}
get id(): AnalyticUnitId { return this._panelObject.id; }
set id(value: AnalyticUnitId) { this._panelObject.id = value; }
set name(value: string) { this._panelObject.name = value; }
get name(): string { return this._panelObject.name; }
set pattern(value: string) { this._panelObject.pattern = value; }
get pattern(): string { return this._panelObject.pattern.split(' ')[0]; }
set type(value: string) { this._panelObject.type = value; }
get type(): string { return this._panelObject.type; }
set confidence(value: number) { this._panelObject.confidence = value; }
get confidence(): number { return this._panelObject.confidence; }
@ -65,7 +66,7 @@ export class AnalyticUnit {
get saving(): boolean { return this._saving; }
set saving(value: boolean) { this._saving = value; }
get visible(): boolean {
get visible(): boolean {
return (this._panelObject.visible === undefined) ? true : this._panelObject.visible
}
set visible(value: boolean) {
@ -88,13 +89,13 @@ export class AnalyticUnit {
set segments(value: SegmentsSet<AnalyticSegment>) {
this._segmentSet.setSegments(value.getSegments());
}
get status() { return this._status; }
set status(value) {
if(
value !== 'ready' &&
value !== 'learning' &&
value !== 'pending' &&
value !== 'ready' &&
value !== 'learning' &&
value !== 'pending' &&
value !== 'failed'
) {
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 {
static template = template;
ANALYTIC_TYPES: Array<String> = ['General', 'Drops', 'Peaks'];
hiddenSeries: any = {};
seriesList: any = [];
@ -43,7 +44,7 @@ class GraphCtrl extends MetricsPanelCtrl {
processor: DataProcessor;
datasourceRequest: DatasourceRequest;
patterns: Array<String> = ['General', 'Drops', 'Peaks', 'Jumps'];
anomalyTypes = []; // TODO: remove it later. Only for alert tab
analyticsController: AnalyticController;
@ -566,7 +567,7 @@ class GraphCtrl extends MetricsPanelCtrl {
}
onAnomalyAlertChange(anomalyType: AnalyticUnit) {
this.analyticsController.toggleAnomalyTypeAlertEnabled(anomalyType);
this.analyticsController.toggleAlertEnabled(anomalyType);
}
onToggleVisibility(key: AnalyticUnitId) {

8
src/partials/tab_analytics.html

@ -12,8 +12,8 @@
<label class="gf-form-label width-8"> Type </label>
<div class="gf-form-select-wrapper">
<select class="gf-form-input width-12"
ng-model="analyticUnit.pattern"
ng-options="pattern as pattern for pattern in ctrl.patterns"
ng-model="analyticUnit.type"
ng-options="type as type for type in ctrl.ANALYTIC_TYPES"
ng-disabled="true"
/>
</div>
@ -126,8 +126,8 @@
<label class="gf-form-label width-8"> Type </label>
<div class="gf-form-select-wrapper">
<select class="gf-form-input width-12"
ng-model="ctrl.analyticsController.newAnalyticUnit.pattern"
ng-options="pattern as pattern disable when (pattern !== 'General' && pattern !== 'Drops' && pattern !== 'Peaks') for pattern in ctrl.patterns"
ng-model="ctrl.analyticsController.newAnalyticUnit.type"
ng-options="type as type for type in ctrl.ANALYTIC_TYPES"
/>
</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';
export class AnalyticService {
constructor(private _backendURL: string, private _backendSrv: BackendSrv) {
}
@ -15,22 +14,23 @@ export class AnalyticService {
async postNewItem(
metric: MetricExpanded, datasourceRequest: DatasourceRequest,
newItem: AnalyticUnit, panelId: number
) {
): Promise<AnalyticUnitId> {
return this._backendSrv.post(
this._backendURL + '/analyticUnits',
{
name: newItem.id,
name: newItem.name,
metric: metric.toJSON(),
panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`,
datasource: datasourceRequest,
pattern: newItem.pattern
type: newItem.type
}
)
).then(res => res.id as AnalyticUnitId);
};
async isBackendOk(): Promise<boolean> {
try {
var data = await this._backendSrv.get(this._backendURL);
// TODO: check version
return true;
} catch(e) {
return false;
@ -38,7 +38,7 @@ export class AnalyticService {
}
async updateSegments(
key: AnalyticUnitId, addedSegments: SegmentsSet<Segment>, removedSegments: SegmentsSet<Segment>
id: AnalyticUnitId, addedSegments: SegmentsSet<Segment>, removedSegments: SegmentsSet<Segment>
): Promise<SegmentId[]> {
const getJSONs = (segs: SegmentsSet<Segment>) => segs.getSegments().map(segment => ({
@ -47,20 +47,23 @@ export class AnalyticService {
}));
var payload = {
name: key,
id,
addedSegments: getJSONs(addedSegments),
removedSegments: removedSegments.getSegments().map(s => s.id)
}
var data = await this._backendSrv.patch(this._backendURL + '/segments', payload);
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[];
}
async getSegments(id: AnalyticUnitId, from?: number, to?: number): Promise<AnalyticSegment[]> {
if(id === undefined) {
throw new Error('id is undefined');
}
var payload: any = { id };
if(from !== undefined) {
payload['from'] = from;
@ -68,10 +71,7 @@ export class AnalyticService {
if(to !== undefined) {
payload['to'] = to;
}
var data = await this._backendSrv.get(
this._backendURL + '/segments',
payload
);
var data = await this._backendSrv.get(this._backendURL + '/segments', payload);
if(data.segments === undefined) {
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));
}
async * getAnomalyTypeStatusGenerator(key: AnalyticUnitId, duration: number) {
async * getAnomalyTypeStatusGenerator(id: AnalyticUnitId, duration: number) {
if(id === undefined) {
throw new Error('id is undefined');
}
let statusCheck = async () => {
var data = await this._backendSrv.get(
this._backendURL + '/analyticUnits/status', { name: key }
this._backendURL + '/analyticUnits/status', { id }
);
return data;
}
@ -99,6 +102,9 @@ export class AnalyticService {
}
async getAlertEnabled(id: AnalyticUnitId): Promise<boolean> {
if(id === undefined) {
throw new Error('id is undefined');
}
var data = await this._backendSrv.get(
this._backendURL + '/alerts', { id }
);
@ -107,6 +113,9 @@ export class AnalyticService {
}
async setAlertEnabled(id: AnalyticUnitId, enabled: boolean): Promise<void> {
if(id === undefined) {
throw new Error('id is undefined');
}
return this._backendSrv.post(
this._backendURL + '/alerts', { id, enabled }
);

Loading…
Cancel
Save