api/ds/query: simplify data sources lookup for queries and expressions (#41172)
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
DataQueryRequest,
|
||||
DataQueryResponseData,
|
||||
MutableDataFrame,
|
||||
DataSourceRef,
|
||||
} from '@grafana/data';
|
||||
import { of } from 'rxjs';
|
||||
|
||||
@@ -29,7 +30,7 @@ jest.mock('../services', () => ({
|
||||
getBackendSrv: () => backendSrv,
|
||||
getDataSourceSrv: () => {
|
||||
return {
|
||||
getInstanceSettings: () => ({ id: 8674 }),
|
||||
getInstanceSettings: (ref?: DataSourceRef) => ({ type: ref?.type ?? '?', uid: ref?.uid ?? '?' }),
|
||||
};
|
||||
},
|
||||
}));
|
||||
@@ -39,6 +40,8 @@ describe('DataSourceWithBackend', () => {
|
||||
const settings = {
|
||||
name: 'test',
|
||||
id: 1234,
|
||||
uid: 'abc',
|
||||
type: 'dummy',
|
||||
jsonData: {},
|
||||
} as DataSourceInstanceSettings<DataSourceJsonData>;
|
||||
|
||||
@@ -49,7 +52,7 @@ describe('DataSourceWithBackend', () => {
|
||||
ds.query({
|
||||
maxDataPoints: 10,
|
||||
intervalMs: 5000,
|
||||
targets: [{ refId: 'A' }, { refId: 'B', datasource: 'sample' }],
|
||||
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
|
||||
} as DataQueryRequest);
|
||||
|
||||
const mock = mockDatasourceRequest.mock;
|
||||
@@ -61,14 +64,19 @@ describe('DataSourceWithBackend', () => {
|
||||
"data": Object {
|
||||
"queries": Array [
|
||||
Object {
|
||||
"datasourceId": 1234,
|
||||
"datasource": Object {
|
||||
"type": "dummy",
|
||||
"uid": "abc",
|
||||
},
|
||||
"intervalMs": 5000,
|
||||
"maxDataPoints": 10,
|
||||
"refId": "A",
|
||||
},
|
||||
Object {
|
||||
"datasource": "sample",
|
||||
"datasourceId": 8674,
|
||||
"datasource": Object {
|
||||
"type": "sample",
|
||||
"uid": "?",
|
||||
},
|
||||
"intervalMs": 5000,
|
||||
"maxDataPoints": 10,
|
||||
"refId": "B",
|
||||
|
||||
@@ -11,13 +11,32 @@ import {
|
||||
parseLiveChannelAddress,
|
||||
StreamingFrameOptions,
|
||||
StreamingFrameAction,
|
||||
getDataSourceRef,
|
||||
DataSourceRef,
|
||||
} from '@grafana/data';
|
||||
import { merge, Observable, of } from 'rxjs';
|
||||
import { catchError, switchMap } from 'rxjs/operators';
|
||||
import { getBackendSrv, getDataSourceSrv, getGrafanaLiveSrv } from '../services';
|
||||
import { BackendDataSourceResponse, toDataQueryResponse } from './queryResponse';
|
||||
|
||||
const ExpressionDatasourceID = '__expr__';
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export const ExpressionDatasourceRef = Object.freeze({
|
||||
type: '__expr__',
|
||||
uid: '__expr__',
|
||||
});
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export function isExpressionReference(ref?: DataSourceRef | string | null): boolean {
|
||||
if (!ref) {
|
||||
return false;
|
||||
}
|
||||
const v = (ref as any).type ?? ref;
|
||||
return v === ExpressionDatasourceRef.type || v === '-100'; // -100 was a legacy accident that should be removed
|
||||
}
|
||||
|
||||
class HealthCheckError extends Error {
|
||||
details: HealthCheckResultDetails;
|
||||
@@ -89,12 +108,12 @@ class DataSourceWithBackend<
|
||||
}
|
||||
|
||||
const queries = targets.map((q) => {
|
||||
let datasourceId = this.id;
|
||||
let datasource = this.getRef();
|
||||
|
||||
if (q.datasource === ExpressionDatasourceID) {
|
||||
if (isExpressionReference(q.datasource)) {
|
||||
return {
|
||||
...q,
|
||||
datasourceId,
|
||||
datasource: ExpressionDatasourceRef,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -105,12 +124,12 @@ class DataSourceWithBackend<
|
||||
throw new Error(`Unknown Datasource: ${JSON.stringify(q.datasource)}`);
|
||||
}
|
||||
|
||||
datasourceId = ds.id;
|
||||
datasource = getDataSourceRef(ds);
|
||||
}
|
||||
|
||||
return {
|
||||
...this.applyTemplateVariables(q, request.scopedVars),
|
||||
datasourceId,
|
||||
datasource,
|
||||
intervalMs,
|
||||
maxDataPoints,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user