Commit a65f7d66 by Ryan McKinley Committed by GitHub

DataSourceWithBackend: use datasource id when datasource property is not set (#25338)

* use the instance datasource id if not specified

* better tests
parent 00a9af00
import { BackendSrv } from 'src/services';
import { DataSourceWithBackend } from './DataSourceWithBackend';
import { DataSourceJsonData, DataQuery, DataSourceInstanceSettings, DataQueryRequest } from '@grafana/data';
class MyDataSource extends DataSourceWithBackend<DataQuery, DataSourceJsonData> {
constructor(instanceSettings: DataSourceInstanceSettings<DataSourceJsonData>) {
super(instanceSettings);
}
}
const mockDatasourceRequest = jest.fn();
const backendSrv = ({
datasourceRequest: mockDatasourceRequest,
} as unknown) as BackendSrv;
jest.mock('../services', () => ({
getBackendSrv: () => backendSrv,
}));
jest.mock('..', () => ({
config: {
bootData: {
user: {
orgId: 77,
},
},
datasources: {
sample: {
id: 8674,
},
},
},
}));
describe('DataSourceWithBackend', () => {
test('check the executed queries', () => {
const settings = {
name: 'test',
id: 1234,
jsonData: {},
} as DataSourceInstanceSettings<DataSourceJsonData>;
mockDatasourceRequest.mockReset();
mockDatasourceRequest.mockReturnValue(Promise.resolve({}));
const ds = new MyDataSource(settings);
ds.query({
maxDataPoints: 10,
intervalMs: 5000,
targets: [{ refId: 'A' }, { refId: 'B', datasource: 'sample' }],
} as DataQueryRequest);
const mock = mockDatasourceRequest.mock;
expect(mock.calls.length).toBe(1);
const args = mock.calls[0][0];
expect(args).toMatchInlineSnapshot(`
Object {
"data": Object {
"queries": Array [
Object {
"datasourceId": 1234,
"intervalMs": 5000,
"maxDataPoints": 10,
"orgId": 77,
"refId": "A",
},
Object {
"datasource": "sample",
"datasourceId": 8674,
"intervalMs": 5000,
"maxDataPoints": 10,
"orgId": 77,
"refId": "B",
},
],
},
"method": "POST",
"requestId": undefined,
"url": "/api/ds/query",
}
`);
});
});
......@@ -62,21 +62,25 @@ export class DataSourceWithBackend<
targets = targets.filter(q => this.filterQuery!(q));
}
const queries = targets.map(q => {
let datasourceId = this.id;
if (q.datasource === ExpressionDatasourceID) {
return {
...q,
datasourceId: this.id,
datasourceId,
orgId,
};
}
const dsName = q.datasource && q.datasource !== 'default' ? q.datasource : config.defaultDatasource;
const ds = config.datasources[dsName];
if (!ds) {
throw new Error('Unknown Datasource: ' + q.datasource);
if (q.datasource) {
const dsName = q.datasource === 'default' ? config.defaultDatasource : q.datasource;
const ds = config.datasources[dsName];
if (!ds) {
throw new Error('Unknown Datasource: ' + q.datasource);
}
datasourceId = ds.id;
}
return {
...this.applyTemplateVariables(q, request.scopedVars),
datasourceId: ds.id,
datasourceId,
intervalMs,
maxDataPoints,
orgId,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment