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