|
|
@ -1,10 +1,11 @@ |
|
|
|
import { Segment, SegmentId } from '../models/segment'; |
|
|
|
import { SegmentId } from '../models/segment'; |
|
|
|
import { MetricExpanded } from '../models/metric'; |
|
|
|
import { MetricExpanded } from '../models/metric'; |
|
|
|
import { DatasourceRequest } from '../models/datasource'; |
|
|
|
import { DatasourceRequest } from '../models/datasource'; |
|
|
|
import { SegmentsSet } from '../models/segment_set'; |
|
|
|
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 { Threshold } from '../models/threshold'; |
|
|
|
import { Threshold } from '../models/threshold'; |
|
|
|
|
|
|
|
|
|
|
|
import { appEvents } from 'grafana/app/core/core'; |
|
|
|
import { appEvents } from 'grafana/app/core/core'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -12,12 +13,12 @@ export class AnalyticService { |
|
|
|
private _isUp = false; |
|
|
|
private _isUp = false; |
|
|
|
|
|
|
|
|
|
|
|
constructor( |
|
|
|
constructor( |
|
|
|
private _backendURL: string, |
|
|
|
private _hasticDatasourceURL: string, |
|
|
|
private $http |
|
|
|
private $http |
|
|
|
) { } |
|
|
|
) { } |
|
|
|
|
|
|
|
|
|
|
|
async getAnalyticUnitTypes() { |
|
|
|
async getAnalyticUnitTypes() { |
|
|
|
return await this.get('/analyticUnits/types'); |
|
|
|
return this.get('/analyticUnits/types'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async getThresholds(ids: AnalyticUnitId[]) { |
|
|
|
async getThresholds(ids: AnalyticUnitId[]) { |
|
|
@ -28,8 +29,8 @@ export class AnalyticService { |
|
|
|
return resp.thresholds.filter(t => t !== null); |
|
|
|
return resp.thresholds.filter(t => t !== null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async updateThreshold(threshold: Threshold) { |
|
|
|
async updateThreshold(threshold: Threshold): Promise<void> { |
|
|
|
return await this.patch('/threshold', threshold); |
|
|
|
return this.patch('/threshold', threshold); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async postNewItem( |
|
|
|
async postNewItem( |
|
|
@ -49,7 +50,11 @@ export class AnalyticService { |
|
|
|
return response.id as AnalyticUnitId; |
|
|
|
return response.id as AnalyticUnitId; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async updateMetric(analyticUnitId: AnalyticUnitId, metric: MetricExpanded, datasource: DatasourceRequest) { |
|
|
|
async updateMetric( |
|
|
|
|
|
|
|
analyticUnitId: AnalyticUnitId, |
|
|
|
|
|
|
|
metric: MetricExpanded, |
|
|
|
|
|
|
|
datasource: DatasourceRequest |
|
|
|
|
|
|
|
) { |
|
|
|
await this.patch('/analyticUnits/metric', { |
|
|
|
await this.patch('/analyticUnits/metric', { |
|
|
|
analyticUnitId, |
|
|
|
analyticUnitId, |
|
|
|
metric: metric.toJSON(), |
|
|
|
metric: metric.toJSON(), |
|
|
@ -61,8 +66,8 @@ export class AnalyticService { |
|
|
|
return this.delete('/analyticUnits', { id }); |
|
|
|
return this.delete('/analyticUnits', { id }); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async isBackendOk(): Promise<boolean> { |
|
|
|
async isDatasourceOk(): Promise<boolean> { |
|
|
|
if(!this._checkBackendUrl()) { |
|
|
|
if(!this._checkDatasourceConfig()) { |
|
|
|
this._isUp = false; |
|
|
|
this._isUp = false; |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
@ -71,7 +76,9 @@ export class AnalyticService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async updateSegments( |
|
|
|
async updateSegments( |
|
|
|
id: AnalyticUnitId, addedSegments: SegmentsSet<AnalyticSegment>, removedSegments: SegmentsSet<AnalyticSegment> |
|
|
|
id: AnalyticUnitId, |
|
|
|
|
|
|
|
addedSegments: SegmentsSet<AnalyticSegment>, |
|
|
|
|
|
|
|
removedSegments: SegmentsSet<AnalyticSegment> |
|
|
|
): Promise<SegmentId[]> { |
|
|
|
): Promise<SegmentId[]> { |
|
|
|
const getJSONs = (segs: SegmentsSet<AnalyticSegment>) => segs.getSegments().map(segment => ({ |
|
|
|
const getJSONs = (segs: SegmentsSet<AnalyticSegment>) => segs.getSegments().map(segment => ({ |
|
|
|
from: segment.from, |
|
|
|
from: segment.from, |
|
|
@ -184,7 +191,7 @@ export class AnalyticService { |
|
|
|
private async _analyticRequest(method: string, url: string, data?: any) { |
|
|
|
private async _analyticRequest(method: string, url: string, data?: any) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
method = method.toUpperCase(); |
|
|
|
method = method.toUpperCase(); |
|
|
|
url = this._backendURL + url; |
|
|
|
url = this._hasticDatasourceURL + url; |
|
|
|
let requestObject: any = { method, url }; |
|
|
|
let requestObject: any = { method, url }; |
|
|
|
if(method === 'GET' || method === 'DELETE') { |
|
|
|
if(method === 'GET' || method === 'DELETE') { |
|
|
|
requestObject.params = data; |
|
|
|
requestObject.params = data; |
|
|
@ -196,7 +203,7 @@ export class AnalyticService { |
|
|
|
return response.data; |
|
|
|
return response.data; |
|
|
|
} catch(error) { |
|
|
|
} catch(error) { |
|
|
|
if(error.xhrStatus === 'error') { |
|
|
|
if(error.xhrStatus === 'error') { |
|
|
|
this.displayConnectionAlert(); |
|
|
|
this.displayConnectionErrorAlert(); |
|
|
|
this._isUp = false; |
|
|
|
this._isUp = false; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
this._isUp = true; |
|
|
|
this._isUp = true; |
|
|
@ -205,14 +212,17 @@ export class AnalyticService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get hasticDatasourceURL(): string { |
|
|
|
private _checkBackendUrl(): boolean { |
|
|
|
return this._hasticDatasourceURL; |
|
|
|
if(this._backendURL === null || this._backendURL === undefined || this._backendURL === '') { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private _checkDatasourceConfig(): boolean { |
|
|
|
|
|
|
|
if(this._hasticDatasourceURL === null || this._hasticDatasourceURL === undefined || this._hasticDatasourceURL === '') { |
|
|
|
appEvents.emit( |
|
|
|
appEvents.emit( |
|
|
|
'alert-warning', |
|
|
|
'alert-warning', |
|
|
|
[ |
|
|
|
[ |
|
|
|
`Datasource (or URL in datasource) is missing`, |
|
|
|
`Hastic Datasource is missing`, |
|
|
|
`Please set it in datasource config. More info: https://github.com/hastic/hastic-grafana-app/wiki/Getting-started` |
|
|
|
`Please setup Hastic Datasource. More info: https://github.com/hastic/hastic-grafana-app/wiki/Getting-started` |
|
|
|
] |
|
|
|
] |
|
|
|
); |
|
|
|
); |
|
|
|
return false; |
|
|
|
return false; |
|
|
@ -236,12 +246,12 @@ export class AnalyticService { |
|
|
|
return this._analyticRequest('DELETE', url, data); |
|
|
|
return this._analyticRequest('DELETE', url, data); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private displayConnectionAlert() { |
|
|
|
private displayConnectionErrorAlert() { |
|
|
|
appEvents.emit( |
|
|
|
appEvents.emit( |
|
|
|
'alert-error', |
|
|
|
'alert-error', |
|
|
|
[ |
|
|
|
[ |
|
|
|
'No connection to Hastic server', |
|
|
|
'No connection to Hastic Datasource', |
|
|
|
`Hastic server: "${this._backendURL}"`, |
|
|
|
`Hastic Datasource URL: "${this._hasticDatasourceURL}"`, |
|
|
|
] |
|
|
|
] |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|