From b1bdf34c7dabf2d75587fe8be20ee5e8c2165c88 Mon Sep 17 00:00:00 2001 From: rozetko Date: Thu, 21 Mar 2019 19:54:27 +0300 Subject: [PATCH] Validate connection to hastic-server #145 (#222) --- src/datasource/datasource.ts | 3 +- src/datasource/hastic_api.ts | 15 ++++++- .../graph_panel/services/analytic_service.ts | 45 ++++++++++++++++--- src/utlis.ts | 19 ++++++++ 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/datasource/datasource.ts b/src/datasource/datasource.ts index 9d5d6a0..4d95310 100644 --- a/src/datasource/datasource.ts +++ b/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 }; } } diff --git a/src/datasource/hastic_api.ts b/src/datasource/hastic_api.ts index 7c281b5..7ec45cf 100644 --- a/src/datasource/hastic_api.ts +++ b/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; } } diff --git a/src/panel/graph_panel/services/analytic_service.ts b/src/panel/graph_panel/services/analytic_service.ts index 4eaf684..7fbfa61 100644 --- a/src/panel/graph_panel/services/analytic_service.ts +++ b/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; } diff --git a/src/utlis.ts b/src/utlis.ts index 9be795d..b9d8093 100644 --- a/src/utlis.ts +++ b/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; +}