[v9.3.x] @grafana/runtime: Avoid calling applyTemplateVariables for the wrong datasource (#59029)

@grafana/runtime: Avoid calling applyTemplateVariables for the wrong datasource (#57921)

(cherry picked from commit 448358ac66)

Co-authored-by: Andres Martinez Gotor <andres.martinez@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2022-11-22 06:50:47 -05:00
committed by GitHub
co-authored by Andres Martinez Gotor
parent 90a904fbd5
commit 1bcdaeb910
2 changed files with 30 additions and 11 deletions
@@ -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,