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', () => {
|
describe('DataSourceWithBackend', () => {
|
||||||
test('check the executed queries', () => {
|
test('check the executed queries', () => {
|
||||||
const mock = runQueryAndReturnFetchMock({
|
const { mock, ds } = createMockDatasource();
|
||||||
|
ds.query({
|
||||||
maxDataPoints: 10,
|
maxDataPoints: 10,
|
||||||
intervalMs: 5000,
|
intervalMs: 5000,
|
||||||
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
|
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', () => {
|
test('check that the executed queries is hidden from inspector', () => {
|
||||||
const mock = runQueryAndReturnFetchMock({
|
const { mock, ds } = createMockDatasource();
|
||||||
|
ds.query({
|
||||||
maxDataPoints: 10,
|
maxDataPoints: 10,
|
||||||
intervalMs: 5000,
|
intervalMs: 5000,
|
||||||
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
|
targets: [{ refId: 'A' }, { refId: 'B', datasource: { type: 'sample' } }],
|
||||||
@ -169,9 +184,7 @@ describe('DataSourceWithBackend', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function runQueryAndReturnFetchMock(
|
function createMockDatasource() {
|
||||||
request: DataQueryRequest
|
|
||||||
): jest.MockContext<Promise<FetchResponse>, BackendSrvRequest[]> {
|
|
||||||
const settings = {
|
const settings = {
|
||||||
name: 'test',
|
name: 'test',
|
||||||
id: 1234,
|
id: 1234,
|
||||||
@ -184,7 +197,5 @@ function runQueryAndReturnFetchMock(
|
|||||||
mockDatasourceRequest.mockReturnValue(Promise.resolve({} as FetchResponse));
|
mockDatasourceRequest.mockReturnValue(Promise.resolve({} as FetchResponse));
|
||||||
|
|
||||||
const ds = new MyDataSource(settings);
|
const ds = new MyDataSource(settings);
|
||||||
ds.query(request);
|
return { ds, mock: mockDatasourceRequest.mock };
|
||||||
|
|
||||||
return mockDatasourceRequest.mock;
|
|
||||||
}
|
}
|
||||||
|
@ -133,6 +133,7 @@ class DataSourceWithBackend<
|
|||||||
const queries = targets.map((q) => {
|
const queries = targets.map((q) => {
|
||||||
let datasource = this.getRef();
|
let datasource = this.getRef();
|
||||||
let datasourceId = this.id;
|
let datasourceId = this.id;
|
||||||
|
let shouldApplyTemplateVariables = true;
|
||||||
|
|
||||||
if (isExpressionReference(q.datasource)) {
|
if (isExpressionReference(q.datasource)) {
|
||||||
hasExpr = true;
|
hasExpr = true;
|
||||||
@ -149,8 +150,15 @@ class DataSourceWithBackend<
|
|||||||
throw new Error(`Unknown Datasource: ${JSON.stringify(q.datasource)}`);
|
throw new Error(`Unknown Datasource: ${JSON.stringify(q.datasource)}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
datasource = ds.rawRef ?? getDataSourceRef(ds);
|
const dsRef = ds.rawRef ?? getDataSourceRef(ds);
|
||||||
datasourceId = ds.id;
|
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) {
|
if (datasource.type?.length) {
|
||||||
pluginIDs.add(datasource.type);
|
pluginIDs.add(datasource.type);
|
||||||
@ -159,7 +167,7 @@ class DataSourceWithBackend<
|
|||||||
dsUIDs.add(datasource.uid);
|
dsUIDs.add(datasource.uid);
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
...this.applyTemplateVariables(q, request.scopedVars),
|
...(shouldApplyTemplateVariables ? this.applyTemplateVariables(q, request.scopedVars) : q),
|
||||||
datasource,
|
datasource,
|
||||||
datasourceId, // deprecated!
|
datasourceId, // deprecated!
|
||||||
intervalMs,
|
intervalMs,
|
||||||
|
Loading…
Reference in New Issue
Block a user