Skip to content

Commit

Permalink
Use rxjs
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod committed Sep 24, 2021
1 parent b24adea commit d99f9ba
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 61 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"dependencies": {
"lodash": "^4.17.21",
"react-virtualized-auto-sizer": "^1.0.5"
"react-virtualized-auto-sizer": "^1.0.5",
"rxjs": "^7.3.0"
},
"homepage": "https://github.com/simPod/grafana-json-datasource#readme"
}
50 changes: 29 additions & 21 deletions spec/DataSource.jest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { isDataFrame } from '@grafana/data';
import { DateTime } from '@grafana/data/datetime/moment_wrapper';
import { BackendSrv, getBackendSrv, getTemplateSrv, setBackendSrv, setTemplateSrv } from '@grafana/runtime';
import {
BackendSrv,
FetchResponse,
getBackendSrv,
getTemplateSrv,
setBackendSrv,
setTemplateSrv,
} from '@grafana/runtime';
import { of } from 'rxjs';
import { DataSource } from '../src/DataSource';
import { QueryRequest } from '../src/types';
import TemplateSrvStub from './lib/TemplateSrvStub';
Expand Down Expand Up @@ -52,8 +60,8 @@ describe('GenericDatasource', () => {

it('should return the server results when a target is set', (done) => {
setBackendSrv({
datasourceRequest: (request) =>
Promise.resolve({
fetch: (request) =>
of({
data: [
{
target: 'X',
Expand Down Expand Up @@ -82,14 +90,14 @@ describe('GenericDatasource', () => {
});

it('should return the metric target results when a target is set', (done) => {
getBackendSrv().datasourceRequest = (request) => {
getBackendSrv().fetch = (request) => {
const target = request.data.target;
const result = [target + '_0', target + '_1', target + '_2'];

return Promise.resolve({
return of({
_request: request,
data: result,
});
} as unknown as FetchResponse);
};

const templateSrvStub = new TemplateSrvStub();
Expand All @@ -109,11 +117,11 @@ describe('GenericDatasource', () => {
});

it('should return the metric results when the target is an empty string', (done) => {
getBackendSrv().datasourceRequest = (request) =>
Promise.resolve({
getBackendSrv().fetch = (request) =>
of({
_request: request,
data: ['metric_0', 'metric_1', 'metric_2'],
});
} as unknown as FetchResponse);

const templateSrvStub = new TemplateSrvStub();
templateSrvStub.replace = (data) => data ?? '';
Expand All @@ -132,11 +140,11 @@ describe('GenericDatasource', () => {
});

it('should return the metric results when the args are an empty object', (done) => {
getBackendSrv().datasourceRequest = (request) =>
Promise.resolve({
getBackendSrv().fetch = (request) =>
of({
_request: request,
data: ['metric_0', 'metric_1', 'metric_2'],
});
} as unknown as FetchResponse);

const templateSrvStub = new TemplateSrvStub();
templateSrvStub.replace = (data) => data ?? '';
Expand All @@ -155,14 +163,14 @@ describe('GenericDatasource', () => {
});

it('should return the metric target results when the args are a string', (done) => {
getBackendSrv().datasourceRequest = (request) => {
getBackendSrv().fetch = (request) => {
const target = request.data.target;
const result = [target + '_0', target + '_1', target + '_2'];

return Promise.resolve({
return of({
_request: request,
data: result,
});
} as unknown as FetchResponse);
};

const templateSrvStub = new TemplateSrvStub();
Expand Down Expand Up @@ -265,11 +273,11 @@ describe('GenericDatasource', () => {
{ type: 'string', text: 'two', key: 'Two' },
];

getBackendSrv().datasourceRequest = (request) =>
Promise.resolve({
getBackendSrv().fetch = (request) =>
of({
data,
_request: request,
});
} as unknown as FetchResponse);

ds.getTagKeys().then((result) => {
expect(result).toHaveLength(2);
Expand All @@ -290,11 +298,11 @@ describe('GenericDatasource', () => {
{ key: 'drei', text: 'Drei!' },
];

getBackendSrv().datasourceRequest = (request) =>
Promise.resolve({
getBackendSrv().fetch = (request) =>
of({
data,
_request: request,
});
} as unknown as FetchResponse);

ds.getTagValues(null).then((result) => {
expect(result).toHaveLength(3);
Expand Down
121 changes: 82 additions & 39 deletions src/DataSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AnnotationEvent,
DataFrame,
DataQueryResponse,
DataSourceApi,
DataSourceInstanceSettings,
Expand All @@ -8,7 +9,10 @@ import {
} from '@grafana/data';
import { AnnotationQueryRequest } from '@grafana/data/types/datasource';
import { getBackendSrv, getTemplateSrv } from '@grafana/runtime';
import { BackendSrvRequest } from '@grafana/runtime/services/backendSrv';
import { isEqual, isObject } from 'lodash';
import { lastValueFrom, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import {
GenericOptions,
GrafanaQuery,
Expand Down Expand Up @@ -51,15 +55,24 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {

options.scopedVars = { ...this.getVariables(), ...options.scopedVars };

return this.doRequest({
url: `${this.url}/query`,
data: request,
method: 'POST',
}).then((entry) => {
entry.data = entry.data.map(toDataFrame);

return entry;
});
return lastValueFrom(
this.doFetch<any[]>({
url: `${this.url}/query`,
data: request,
method: 'POST',
}).pipe(
map((response) => {
response.data = response.data.map(toDataFrame) as DataFrame[];

return response;
}),
catchError((err) => {
console.error(err);

return of({ data: [] });
})
)
);
}

annotations = {};
Expand All @@ -68,10 +81,12 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
const errorMessageBase = 'Data source is not working';

try {
const response = await this.doRequest({
url: this.url,
method: 'GET',
});
const response = await lastValueFrom(
this.doFetch({
url: this.url,
method: 'GET',
}).pipe(map((response) => response))
);

if (response.status === 200) {
return { status: 'success', message: 'Data source is working', title: 'Success' };
Expand Down Expand Up @@ -108,35 +123,54 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
target: getTemplateSrv().replace(variableQuery.query, undefined, 'regex'),
};

return this.doRequest({
url: `${this.url}/search`,
data: interpolated,
method: 'POST',
}).then(this.mapToTextValue);
return lastValueFrom(
this.doFetch({
url: `${this.url}/search`,
data: interpolated,
method: 'POST',
}).pipe(
map((response) => this.mapToTextValue(response)),
catchError((err) => {
console.error(err);

return of([]);
})
)
);
}

getTagKeys(options?: any): Promise<MetricFindTagKeys[]> {
return new Promise((resolve) => {
this.doRequest({
return lastValueFrom(
this.doFetch({
url: `${this.url}/tag-keys`,
method: 'POST',
data: options,
}).then((result: any) => {
return resolve(result.data);
});
});
}).pipe(
map((result: any) => result.data),
catchError((err) => {
console.error(err);

return of([]);
})
)
);
}

getTagValues(options: any): Promise<MetricFindTagValues[]> {
return new Promise((resolve) => {
this.doRequest({
return lastValueFrom(
this.doFetch({
url: `${this.url}/tag-values`,
method: 'POST',
data: options,
}).then((result: any) => {
return resolve(result.data);
});
});
}).pipe(
map((result: any) => result.data),
catchError((err) => {
console.error(err);

return of([]);
})
)
);
}

annotationQuery(
Expand All @@ -157,13 +191,22 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
variables: this.getVariables(),
};

return this.doRequest({
url: `${this.url}/annotations`,
method: 'POST',
data: annotationQuery,
}).then((result: any) => {
return result.data;
});
return lastValueFrom(
this.doFetch({
url: `${this.url}/annotations`,
method: 'POST',
data: annotationQuery,
}).pipe(
map((result: any) => {
return result.data;
}),
catchError((err) => {
console.error(err);

return of([]);
})
)
);
}

mapToTextValue(result: any) {
Expand All @@ -179,11 +222,11 @@ export class DataSource extends DataSourceApi<GrafanaQuery, GenericOptions> {
});
}

doRequest(options: any) {
doFetch<T>(options: BackendSrvRequest) {
options.withCredentials = this.withCredentials;
options.headers = this.headers;

return getBackendSrv().datasourceRequest(options);
return getBackendSrv().fetch<T>(options);
}

processTargets(options: QueryRequest) {
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10599,6 +10599,13 @@ rxjs@^6.3.3, rxjs@^6.4.0:
dependencies:
tslib "^1.9.0"

rxjs@^7.3.0:
version "7.3.0"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.3.0.tgz#39fe4f3461dc1e50be1475b2b85a0a88c1e938c6"
integrity sha512-p2yuGIg9S1epc3vrjKf6iVb3RCaAYjYskkO+jHIaV0IjOPlJop4UnodOoFb2xeNwlguqLYvGw1b1McillYb5Gw==
dependencies:
tslib "~2.1.0"

safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@^5.2.0, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
Expand Down Expand Up @@ -11703,6 +11710,11 @@ tslib@^2.0.3:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01"
integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw==

tslib@~2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a"
integrity sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A==

tsutils@^3.21.0:
version "3.21.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"
Expand Down

0 comments on commit d99f9ba

Please sign in to comment.