|
|
@ -5,46 +5,41 @@ import { SegmentsSet } from '../models/segment_set'; |
|
|
|
import { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analytic_unit'; |
|
|
|
import { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analytic_unit'; |
|
|
|
import { ServerInfo } from '../models/info'; |
|
|
|
import { ServerInfo } from '../models/info'; |
|
|
|
|
|
|
|
|
|
|
|
import { BackendSrv } from 'grafana/app/core/services/backend_srv'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class AnalyticService { |
|
|
|
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( |
|
|
|
async postNewItem( |
|
|
|
metric: MetricExpanded, datasourceRequest: DatasourceRequest,
|
|
|
|
metric: MetricExpanded, datasourceRequest: DatasourceRequest,
|
|
|
|
newItem: AnalyticUnit, panelId: number |
|
|
|
newItem: AnalyticUnit, panelId: number |
|
|
|
): Promise<AnalyticUnitId> { |
|
|
|
): 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; |
|
|
|
datasourceRequest.type = datasource.type; |
|
|
|
|
|
|
|
|
|
|
|
return this._backendSrv.post( |
|
|
|
const response = await this.post(this._backendURL + '/analyticUnits', { |
|
|
|
this._backendURL + '/analyticUnits',
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`, |
|
|
|
panelUrl: window.location.origin + window.location.pathname + `?panelId=${panelId}&fullscreen`, |
|
|
|
type: newItem.type, |
|
|
|
type: newItem.type, |
|
|
|
name: newItem.name, |
|
|
|
name: newItem.name, |
|
|
|
metric: metric.toJSON(), |
|
|
|
metric: metric.toJSON(), |
|
|
|
datasource: datasourceRequest |
|
|
|
datasource: datasourceRequest |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return response.id as AnalyticUnitId; |
|
|
|
} |
|
|
|
} |
|
|
|
).then(res => res.id as AnalyticUnitId); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async isBackendOk(): Promise<boolean> { |
|
|
|
async isBackendOk(): Promise<boolean> { |
|
|
|
try { |
|
|
|
await this.get(this._backendURL); |
|
|
|
var data = await this._backendSrv.get(this._backendURL); |
|
|
|
|
|
|
|
// TODO: check version
|
|
|
|
return this._isUp; |
|
|
|
return true; |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async updateSegments( |
|
|
|
async updateSegments( |
|
|
|
id: 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 => ({ |
|
|
|
from: segment.from, |
|
|
|
from: segment.from, |
|
|
|
to: segment.to |
|
|
|
to: segment.to |
|
|
@ -56,11 +51,10 @@ export class AnalyticService { |
|
|
|
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.patch(this._backendURL + '/segments', payload); |
|
|
|
if(data.addedIds === undefined) { |
|
|
|
if(data.addedIds === undefined) { |
|
|
|
throw new Error('Server didn`t send addedIds'); |
|
|
|
throw new Error('Server didn`t send addedIds'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return data.addedIds as SegmentId[]; |
|
|
|
return data.addedIds as SegmentId[]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -75,7 +69,7 @@ export class AnalyticService { |
|
|
|
if(to !== undefined) { |
|
|
|
if(to !== undefined) { |
|
|
|
payload['to'] = to; |
|
|
|
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) { |
|
|
|
if(data.segments === undefined) { |
|
|
|
throw new Error('Server didn`t return segments array'); |
|
|
|
throw new Error('Server didn`t return segments array'); |
|
|
|
} |
|
|
|
} |
|
|
@ -88,9 +82,7 @@ export class AnalyticService { |
|
|
|
throw new Error('id is undefined'); |
|
|
|
throw new Error('id is undefined'); |
|
|
|
} |
|
|
|
} |
|
|
|
let statusCheck = async () => { |
|
|
|
let statusCheck = async () => { |
|
|
|
var data = await this._backendSrv.get( |
|
|
|
var data = await this.get(this._backendURL + '/analyticUnits/status', { id }); |
|
|
|
this._backendURL + '/analyticUnits/status', { id } |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
return data; |
|
|
|
return data; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -102,34 +94,29 @@ export class AnalyticService { |
|
|
|
yield await statusCheck(); |
|
|
|
yield await statusCheck(); |
|
|
|
await timeout(); |
|
|
|
await timeout(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getAlertEnabled(id: AnalyticUnitId): Promise<boolean> { |
|
|
|
async getAlertEnabled(id: AnalyticUnitId): Promise<boolean> { |
|
|
|
if(id === undefined) { |
|
|
|
if(id === undefined) { |
|
|
|
throw new Error('id is undefined'); |
|
|
|
throw new Error('id is undefined'); |
|
|
|
} |
|
|
|
} |
|
|
|
var data = await this._backendSrv.get( |
|
|
|
var data = await this.get(this._backendURL + '/alerts', { id }); |
|
|
|
this._backendURL + '/alerts', { id } |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
if(data.enabled === undefined) { |
|
|
|
if(data.enabled === undefined) { |
|
|
|
throw new Error('Server didn`t return "enabled"'); |
|
|
|
throw new Error('Server didn`t return "enabled"'); |
|
|
|
} |
|
|
|
} |
|
|
|
return data.enabled as boolean; |
|
|
|
return data.enabled as boolean; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async setAlertEnabled(id: AnalyticUnitId, enabled: boolean): Promise<void> { |
|
|
|
async setAlertEnabled(id: AnalyticUnitId, enabled: boolean): Promise<void> { |
|
|
|
if(id === undefined) { |
|
|
|
if(id === undefined) { |
|
|
|
throw new Error('id is undefined'); |
|
|
|
throw new Error('id is undefined'); |
|
|
|
} |
|
|
|
} |
|
|
|
return this._backendSrv.post( |
|
|
|
return await this.post(this._backendURL + '/alerts', { id, enabled }); |
|
|
|
this._backendURL + '/alerts', { id, enabled } |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getServerInfo(): Promise<ServerInfo> { |
|
|
|
async getServerInfo(): Promise<ServerInfo> { |
|
|
|
let data = await this._backendSrv.get(this._backendURL); |
|
|
|
let data = await this.get(this._backendURL); |
|
|
|
|
|
|
|
console.log(data); |
|
|
|
return { |
|
|
|
return { |
|
|
|
nodeVersion: data.nodeVersion, |
|
|
|
nodeVersion: data.nodeVersion, |
|
|
|
packageVersion: data.packageVersion, |
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|