Browse Source

Finalyze datasource

master
rozetko 6 years ago
parent
commit
ede504bec3
  1. 26
      src/datasource/datasource.ts
  2. 29
      src/datasource/hastic_api.ts

26
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) {

29
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;
}
}
Loading…
Cancel
Save