api/ds/query: simplify data sources lookup for queries and expressions (#41172)

This commit is contained in:
Ryan McKinley
2021-11-05 08:12:55 -07:00
committed by GitHub
parent c8b7373016
commit 3489721ed6
16 changed files with 255 additions and 224 deletions
@@ -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,
};