[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
@@ -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,