From ede504bec345fd7745439ff46c8ba87ab1209abf Mon Sep 17 00:00:00 2001 From: rozetko Date: Mon, 4 Mar 2019 20:56:03 +0300 Subject: [PATCH] Finalyze datasource --- src/datasource/datasource.ts | 26 ++++++++++++++++++++++---- src/datasource/hastic_api.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/datasource/hastic_api.ts diff --git a/src/datasource/datasource.ts b/src/datasource/datasource.ts index ceecb23..9d5d6a0 100644 --- a/src/datasource/datasource.ts +++ b/src/datasource/datasource.ts @@ -1,16 +1,34 @@ +import HasticAPI from './hastic_api'; + +import { BackendSrv } from 'grafana/app/core/services/backend_srv'; + export class HasticDatasource { + private hastic: HasticAPI; /** @ngInject */ - constructor(public instanceSettings: any) { - + constructor(instanceSettings: any, backendSrv: BackendSrv) { + this.hastic = new HasticAPI(instanceSettings, backendSrv); } async query(options: any) { - + console.log(options); } async testDatasource() { - + try { + await this.hastic.get('/'); + // TODO: check if it is hastic + return { + status: 'success', title: 'Success', + message: 'Datasource is working' + }; + } catch(err) { + console.error(err); + return { + status: 'error', title: 'Error', + message: 'Hastic connection error' + }; + } } metricFindQuery(options: any) { diff --git a/src/datasource/hastic_api.ts b/src/datasource/hastic_api.ts new file mode 100644 index 0000000..4c9b836 --- /dev/null +++ b/src/datasource/hastic_api.ts @@ -0,0 +1,29 @@ +import { BackendSrv } from 'grafana/app/core/services/backend_srv'; + +export default class HasticAPI { + private url: string; + + constructor(instanceSettings: any, private backendSrv: BackendSrv) { + this.url = instanceSettings.url; + } + + get(url: string, params?: any) { + return this._query('GET', url, params); + } + + private async _query(method: string, url: string, data?: any) { + method = method.toUpperCase(); + let options: any = { + method, + url: this.url + url + }; + if(method === 'GET' || method === 'DELETE') { + options.params = data; + } else { + options.data = data; + } + + const response = await this.backendSrv.datasourceRequest(options); + return response.data; + } +}