mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 21:52:43 +08:00
Expressions: Add support for variables (#41778)
This commit is contained in:
@ -1,8 +1,16 @@
|
||||
import { DataSourceInstanceSettings, DataSourcePluginMeta, PluginType } from '@grafana/data';
|
||||
import {
|
||||
DataQueryRequest,
|
||||
DataQueryResponse,
|
||||
DataSourceInstanceSettings,
|
||||
DataSourcePluginMeta,
|
||||
PluginType,
|
||||
ScopedVars,
|
||||
} from '@grafana/data';
|
||||
import { ExpressionQuery, ExpressionQueryType } from './types';
|
||||
import { ExpressionQueryEditor } from './ExpressionQueryEditor';
|
||||
import { DataSourceWithBackend } from '@grafana/runtime';
|
||||
import { DataSourceWithBackend, getDataSourceSrv, getTemplateSrv } from '@grafana/runtime';
|
||||
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
||||
import { Observable, from, mergeMap } from 'rxjs';
|
||||
|
||||
/**
|
||||
* This is a singleton instance that just pretends to be a DataSource
|
||||
@ -12,10 +20,34 @@ export class ExpressionDatasourceApi extends DataSourceWithBackend<ExpressionQue
|
||||
super(instanceSettings);
|
||||
}
|
||||
|
||||
applyTemplateVariables(query: ExpressionQuery, scopedVars: ScopedVars): Record<string, any> {
|
||||
const templateSrv = getTemplateSrv();
|
||||
return {
|
||||
...query,
|
||||
expression: templateSrv.replace(query.expression, scopedVars),
|
||||
window: templateSrv.replace(query.window, scopedVars),
|
||||
};
|
||||
}
|
||||
|
||||
getCollapsedText(query: ExpressionQuery) {
|
||||
return `Expression: ${query.type}`;
|
||||
}
|
||||
|
||||
query(request: DataQueryRequest<ExpressionQuery>): Observable<DataQueryResponse> {
|
||||
let targets = request.targets.map(async (query: ExpressionQuery): Promise<ExpressionQuery> => {
|
||||
const ds = await getDataSourceSrv().get(query.datasource);
|
||||
|
||||
if (!ds.interpolateVariablesInQueries) {
|
||||
return query;
|
||||
}
|
||||
|
||||
return ds?.interpolateVariablesInQueries([query], {})[0] as ExpressionQuery;
|
||||
});
|
||||
|
||||
let sub = from(Promise.all(targets));
|
||||
return sub.pipe(mergeMap((t) => super.query({ ...request, targets: t })));
|
||||
}
|
||||
|
||||
newQuery(query?: Partial<ExpressionQuery>): ExpressionQuery {
|
||||
return {
|
||||
refId: '--', // Replaced with query
|
||||
|
Reference in New Issue
Block a user