mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
@grafana/runtime: Avoid calling applyTemplateVariables for the wrong datasource (#57921)
This commit is contained in:
parent
f8656d269d
commit
448358ac66
@ -42,7 +42,8 @@ jest.mock('../services', () => ({
|
||||
|
||||
describe('DataSourceWithBackend', () => {
|
||||
test('check the executed queries', () => {
|
||||
const mock = runQueryAndReturnFetchMock({
|
||||
const { mock, ds } = createMockDatasource();
|
||||
ds.query({
|
||||
maxDataPoints: 10,
|
||||
intervalMs: 5000,
|
||||
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
|
||||
@ -93,8 +94,22 @@ describe('DataSourceWithBackend', () => {
|
||||
`);
|
||||
});
|
||||
|
||||
test('should apply template variables only for the current data source', () => {
|
||||
const { mock, ds } = createMockDatasource();
|
||||
ds.applyTemplateVariables = jest.fn();
|
||||
ds.query({
|
||||
maxDataPoints: 10,
|
||||
intervalMs: 5000,
|
||||
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
|
||||
} as DataQueryRequest);
|
||||
|
||||
expect(mock.calls.length).toBe(1);
|
||||
expect(ds.applyTemplateVariables).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('check that the executed queries is hidden from inspector', () => {
|
||||
const mock = runQueryAndReturnFetchMock({
|
||||
const { mock, ds } = createMockDatasource();
|
||||
ds.query({
|
||||
maxDataPoints: 10,
|
||||
intervalMs: 5000,
|
||||
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
|
||||
@ -169,9 +184,7 @@ describe('DataSourceWithBackend', () => {
|
||||
});
|
||||
});
|
||||
|
||||
function runQueryAndReturnFetchMock(
|
||||
request: DataQueryRequest
|
||||
): jest.MockContext<Promise<FetchResponse>, BackendSrvRequest[]> {
|
||||
function createMockDatasource() {
|
||||
const settings = {
|
||||
name: 'test',
|
||||
id: 1234,
|
||||
@ -184,7 +197,5 @@ function runQueryAndReturnFetchMock(
|
||||
mockDatasourceRequest.mockReturnValue(Promise.resolve({} as FetchResponse));
|
||||
|
||||
const ds = new MyDataSource(settings);
|
||||
ds.query(request);
|
||||
|
||||
return mockDatasourceRequest.mock;
|
||||
return { ds, mock: mockDatasourceRequest.mock };
|
||||
}
|
||||
|
@ -133,6 +133,7 @@ class DataSourceWithBackend<
|
||||
const queries = targets.map((q) => {
|
||||
let datasource = this.getRef();
|
||||
let datasourceId = this.id;
|
||||
let shouldApplyTemplateVariables = true;
|
||||
|
||||
if (isExpressionReference(q.datasource)) {
|
||||
hasExpr = true;
|
||||
@ -149,8 +150,15 @@ class DataSourceWithBackend<
|
||||
throw new Error(`Unknown Datasource: ${JSON.stringify(q.datasource)}`);
|
||||
}
|
||||
|
||||
datasource = ds.rawRef ?? getDataSourceRef(ds);
|
||||
datasourceId = ds.id;
|
||||
const dsRef = ds.rawRef ?? getDataSourceRef(ds);
|
||||
const dsId = ds.id;
|
||||
if (dsRef.uid !== datasource.uid || datasourceId !== dsId) {
|
||||
datasource = dsRef;
|
||||
datasourceId = dsId;
|
||||
// If the query is using a different datasource, we would need to retrieve the datasource
|
||||
// instance (async) and apply the template variables but it seems it's not necessary for now.
|
||||
shouldApplyTemplateVariables = false;
|
||||
}
|
||||
}
|
||||
if (datasource.type?.length) {
|
||||
pluginIDs.add(datasource.type);
|
||||
@ -159,7 +167,7 @@ class DataSourceWithBackend<
|
||||
dsUIDs.add(datasource.uid);
|
||||
}
|
||||
return {
|
||||
...this.applyTemplateVariables(q, request.scopedVars),
|
||||
...(shouldApplyTemplateVariables ? this.applyTemplateVariables(q, request.scopedVars) : q),
|
||||
datasource,
|
||||
datasourceId, // deprecated!
|
||||
intervalMs,
|
||||
|
Loading…
Reference in New Issue
Block a user