|
|
|
@ -5,46 +5,41 @@ import { SegmentsSet } from '../models/segment_set';
|
|
|
|
|
import { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analytic_unit'; |
|
|
|
|
import { ServerInfo } from '../models/info'; |
|
|
|
|
|
|
|
|
|
import { BackendSrv } from 'grafana/app/core/services/backend_srv'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class AnalyticService { |
|
|
|
|
constructor(private _backendURL: string, private _backendSrv: BackendSrv) { |
|
|
|
|
private _isUp = false; |
|
|
|
|
|
|
|
|
|
constructor(private _backendURL: string, private $http, private alertSrv) { |
|
|
|
|
this.isBackendOk(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async postNewItem( |
|
|
|
|
metric: MetricExpanded, datasourceRequest: DatasourceRequest,
|
|
|
|
|
newItem: AnalyticUnit, panelId: number |
|
|
|
|
): Promise<AnalyticUnitId> { |
|
|
|
|
let datasource = await this._backendSrv.get(`/api/datasources/name/${metric.datasource}`); |
|
|
|
|
let datasource = await this.get(`/api/datasources/name/${metric.datasource}`); |
|
|
|
|
datasourceRequest.type = datasource.type; |
|
|
|
|
|
|
|
|
|
return this._backendSrv.post( |
|
|
|
|
this._backendURL + '/analyticUnits',
|
|
|
|
|
{ |
|
|
|
|
const response = await this.post(this._backendURL + '/analyticUnits', { |
|
|
|
|
panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`, |
|
|
|
|
type: newItem.type, |
|
|
|
|
name: newItem.name, |
|
|
|
|
metric: metric.toJSON(), |
|
|
|
|
datasource: datasourceRequest |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return response.id as AnalyticUnitId; |
|
|
|
|
} |
|
|
|
|
).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; |
|
|
|
|
} |
|
|
|
|
await this.get(this._backendURL); |
|
|
|
|
|
|
|
|
|
return this._isUp; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async updateSegments( |
|
|
|
|
id: AnalyticUnitId, addedSegments: SegmentsSet<Segment>, removedSegments: SegmentsSet<Segment> |
|
|
|
|
): Promise<SegmentId[]> { |
|
|
|
|
|
|
|
|
|
const getJSONs = (segs: SegmentsSet<Segment>) => segs.getSegments().map(segment => ({ |
|
|
|
|
from: segment.from, |
|
|
|
|
to: segment.to |
|
|
|
@ -56,11 +51,10 @@ export class AnalyticService {
|
|
|
|
|
removedSegments: removedSegments.getSegments().map(s => s.id) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var data = await this._backendSrv.patch(this._backendURL + '/segments', payload); |
|
|
|
|
var data = await this.patch(this._backendURL + '/segments', payload); |
|
|
|
|
if(data.addedIds === undefined) { |
|
|
|
|
throw new Error('Server didn`t send addedIds'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return data.addedIds as SegmentId[]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -75,7 +69,7 @@ export class AnalyticService {
|
|
|
|
|
if(to !== undefined) { |
|
|
|
|
payload['to'] = to; |
|
|
|
|
} |
|
|
|
|
var data = await this._backendSrv.get(this._backendURL + '/segments', payload); |
|
|
|
|
var data = await this.get(this._backendURL + '/segments', payload); |
|
|
|
|
if(data.segments === undefined) { |
|
|
|
|
throw new Error('Server didn`t return segments array'); |
|
|
|
|
} |
|
|
|
@ -88,9 +82,7 @@ export class AnalyticService {
|
|
|
|
|
throw new Error('id is undefined'); |
|
|
|
|
} |
|
|
|
|
let statusCheck = async () => { |
|
|
|
|
var data = await this._backendSrv.get( |
|
|
|
|
this._backendURL + '/analyticUnits/status', { id } |
|
|
|
|
); |
|
|
|
|
var data = await this.get(this._backendURL + '/analyticUnits/status', { id }); |
|
|
|
|
return data; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -102,34 +94,29 @@ export class AnalyticService {
|
|
|
|
|
yield await statusCheck(); |
|
|
|
|
await timeout(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 } |
|
|
|
|
); |
|
|
|
|
var data = await this.get(this._backendURL + '/alerts', { id }); |
|
|
|
|
if(data.enabled === undefined) { |
|
|
|
|
throw new Error('Server didn`t return "enabled"'); |
|
|
|
|
} |
|
|
|
|
return data.enabled as boolean; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 } |
|
|
|
|
); |
|
|
|
|
return await this.post(this._backendURL + '/alerts', { id, enabled }); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async getServerInfo(): Promise<ServerInfo> { |
|
|
|
|
let data = await this._backendSrv.get(this._backendURL); |
|
|
|
|
let data = await this.get(this._backendURL); |
|
|
|
|
console.log(data); |
|
|
|
|
return { |
|
|
|
|
nodeVersion: data.nodeVersion, |
|
|
|
|
packageVersion: data.packageVersion, |
|
|
|
@ -142,4 +129,51 @@ export class AnalyticService {
|
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async get(url, params?) { |
|
|
|
|
try { |
|
|
|
|
let response = await this.$http({ method: 'GET', url, params }); |
|
|
|
|
this._isUp = true; |
|
|
|
|
return response.data; |
|
|
|
|
} catch(error) { |
|
|
|
|
this.displayConnectionAlert(); |
|
|
|
|
console.error(error); |
|
|
|
|
this._isUp = false; |
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async post(url, data) { |
|
|
|
|
try { |
|
|
|
|
let response = await this.$http({ method: 'POST', url, data }); |
|
|
|
|
this._isUp = true; |
|
|
|
|
return response.data; |
|
|
|
|
} catch(error) { |
|
|
|
|
this.displayConnectionAlert(); |
|
|
|
|
console.error(error); |
|
|
|
|
this._isUp = false; |
|
|
|
|
}
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async patch(url, data) { |
|
|
|
|
try { |
|
|
|
|
let response = await this.$http({ method: 'PATCH', url, data }); |
|
|
|
|
this._isUp = true; |
|
|
|
|
return response.data; |
|
|
|
|
} catch(error) { |
|
|
|
|
this.displayConnectionAlert(); |
|
|
|
|
console.error(error); |
|
|
|
|
this._isUp = false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private displayConnectionAlert() { |
|
|
|
|
this.alertSrv.set( |
|
|
|
|
'No connection to Hastic server', |
|
|
|
|
`Hastic server: "${this._backendURL}"`, |
|
|
|
|
'warning', 4000 |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public get isUp() { |
|
|
|
|
return this._isUp; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|