|
|
@ -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 } |
|
|
|
); |
|
|
|
); |
|
|
|