Browse Source

Validate connection to hastic-server #145 (#222)

master
rozetko 6 years ago committed by GitHub
parent
commit
b1bdf34c7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/datasource/datasource.ts
  2. 15
      src/datasource/hastic_api.ts
  3. 45
      src/panel/graph_panel/services/analytic_service.ts
  4. 19
      src/utlis.ts

3
src/datasource/datasource.ts

@ -17,7 +17,6 @@ export class HasticDatasource {
async testDatasource() {
try {
await this.hastic.get('/');
// TODO: check if it is hastic
return {
status: 'success', title: 'Success',
message: 'Datasource is working'
@ -26,7 +25,7 @@ export class HasticDatasource {
console.error(err);
return {
status: 'error', title: 'Error',
message: 'Hastic connection error'
message: err.message
};
}
}

15
src/datasource/hastic_api.ts

@ -1,3 +1,5 @@
import { isHasticServerResponse, isSupportedServerVersion, SUPPORTED_SERVER_VERSION } from '../utlis';
import { BackendSrv } from 'grafana/app/core/services/backend_srv';
@ -8,7 +10,7 @@ export default class HasticAPI {
this.url = instanceSettings.url;
}
get(url: string, params?: any) {
async get(url: string, params?: any) {
return this._query('GET', url, params);
}
@ -25,6 +27,15 @@ export default class HasticAPI {
}
const response = await this.backendSrv.datasourceRequest(options);
return response.data;
const responseData = response.data;
if(!isHasticServerResponse(responseData)) {
throw new Error(`Something is working at "${url}" but it's not Hastic Server`);
}
if(!isSupportedServerVersion(responseData)) {
throw new Error(`Hastic Server at "${url}" has unsupported version (got ${responseData.packageVersion}, should be ${SUPPORTED_SERVER_VERSION})`);
}
return responseData;
}
}

45
src/panel/graph_panel/services/analytic_service.ts

@ -6,6 +6,8 @@ import { AnalyticUnitId, AnalyticUnit, AnalyticSegment } from '../models/analyti
import { HasticServerInfo, HasticServerInfoUnknown } from '../models/hastic_server_info';
import { Threshold } from '../models/threshold';
import { isHasticServerResponse, isSupportedServerVersion, SUPPORTED_SERVER_VERSION } from '../../../utlis';
import { appEvents } from 'grafana/app/core/core';
@ -23,7 +25,11 @@ export class AnalyticService {
}
async getAnalyticUnitTypes() {
return this.get('/analyticUnits/types');
const resp = await this.get('/analyticUnits/types');
if(resp === undefined) {
return {};
}
return resp;
}
async getAnalyticUnits(panelId: string) {
@ -86,7 +92,15 @@ export class AnalyticService {
this._isUp = false;
return false;
}
await this.get('/');
const response = await this.get('/');
if(!isHasticServerResponse(response)) {
this.displayWrongUrlAlert();
this._isUp = false;
}
if(!isSupportedServerVersion(response)) {
this.displayUnsupportedVersionAlert(response.packageVersion);
this._isUp = false;
}
return this._isUp;
}
@ -203,17 +217,16 @@ export class AnalyticService {
} else {
requestObject.data = data;
}
let response = await this.$http(requestObject);
const response = await this.$http(requestObject);
this._isUp = true;
return response.data;
} catch(error) {
if(error.xhrStatus === 'error') {
if(error.xhrstatus === 'error' || error.status !== 200) {
this.displayConnectionErrorAlert();
this._isUp = false;
} else {
this._isUp = true;
}
// throw error;
}
}
@ -255,12 +268,32 @@ export class AnalyticService {
appEvents.emit(
'alert-error',
[
'No connection to Hastic Datasource',
'No connection to Hastic Server',
`Hastic Datasource URL: "${this._hasticDatasourceURL}"`,
]
);
}
private displayWrongUrlAlert() {
appEvents.emit(
'alert-error',
[
'Please check Hastic Server URL',
`Something is working at "${this._hasticDatasourceURL}" but it's not Hastic Server`,
]
);
}
private displayUnsupportedVersionAlert(actual: string) {
appEvents.emit(
'alert-error',
[
'Unsupported Hastic Server version',
`Hastic Server at "${this._hasticDatasourceURL}" has unsupported version (got ${actual}, should be ${SUPPORTED_SERVER_VERSION})`,
]
);
}
public get isUp(): boolean {
return this._isUp;
}

19
src/utlis.ts

@ -1,5 +1,7 @@
import url from 'url-parse';
import * as _ from 'lodash';
export const SUPPORTED_SERVER_VERSION = '0.3.1-beta';
export function normalizeUrl(inputUrl: string) {
if(!inputUrl) {
@ -28,3 +30,20 @@ export function normalizeUrl(inputUrl: string) {
}
return finalUrl;
}
export function isHasticServerResponse(response: any) {
if(response === undefined) {
return false;
}
if(!_.has(response, 'server')) {
return false;
}
return true;
}
export function isSupportedServerVersion(response: any) {
if(response.packageVersion !== SUPPORTED_SERVER_VERSION) {
return false;
}
return true;
}

Loading…
Cancel
Save