2023-01-31 18:50:10 +01:00
|
|
|
import { from, mergeMap, Observable } from 'rxjs';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2022-02-17 10:50:22 -05:00
|
|
|
import {
|
|
|
|
|
DataQueryRequest,
|
|
|
|
|
DataQueryResponse,
|
|
|
|
|
DataSourceInstanceSettings,
|
|
|
|
|
DataSourcePluginMeta,
|
|
|
|
|
PluginType,
|
|
|
|
|
ScopedVars,
|
|
|
|
|
} from '@grafana/data';
|
|
|
|
|
import { DataSourceWithBackend, getDataSourceSrv, getTemplateSrv } from '@grafana/runtime';
|
2021-11-05 08:12:55 -07:00
|
|
|
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
|
|
|
|
import { ExpressionQueryEditor } from './ExpressionQueryEditor';
|
2023-03-13 07:53:19 -05:00
|
|
|
import { ExpressionDatasourceUID, ExpressionQuery, ExpressionQueryType } from './types';
|
2019-10-30 11:38:28 -07:00
|
|
|
|
|
|
|
|
/**
|
2019-10-31 16:22:00 -07:00
|
|
|
* This is a singleton instance that just pretends to be a DataSource
|
2019-10-30 11:38:28 -07:00
|
|
|
*/
|
2020-01-14 03:59:48 -08:00
|
|
|
export class ExpressionDatasourceApi extends DataSourceWithBackend<ExpressionQuery> {
|
2021-10-29 10:57:24 -07:00
|
|
|
constructor(public instanceSettings: DataSourceInstanceSettings) {
|
2019-10-30 11:38:28 -07:00
|
|
|
super(instanceSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 10:50:22 -05:00
|
|
|
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),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-30 11:38:28 -07:00
|
|
|
getCollapsedText(query: ExpressionQuery) {
|
|
|
|
|
return `Expression: ${query.type}`;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 10:50:22 -05:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 17:03:55 -04:00
|
|
|
return ds?.interpolateVariablesInQueries([query], request.scopedVars)[0] as ExpressionQuery;
|
2022-02-17 10:50:22 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let sub = from(Promise.all(targets));
|
|
|
|
|
return sub.pipe(mergeMap((t) => super.query({ ...request, targets: t })));
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-21 13:57:17 +02:00
|
|
|
newQuery(query?: Partial<ExpressionQuery>): ExpressionQuery {
|
2019-10-30 11:38:28 -07:00
|
|
|
return {
|
|
|
|
|
refId: '--', // Replaced with query
|
2021-10-29 10:57:24 -07:00
|
|
|
datasource: ExpressionDatasourceRef,
|
2022-07-12 11:50:58 +02:00
|
|
|
type: query?.type ?? ExpressionQueryType.math,
|
|
|
|
|
...query,
|
2019-10-30 11:38:28 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 15:10:14 +02:00
|
|
|
export const instanceSettings: DataSourceInstanceSettings = {
|
2019-10-30 11:38:28 -07:00
|
|
|
id: -100,
|
2021-04-29 15:10:14 +02:00
|
|
|
uid: ExpressionDatasourceUID,
|
2022-05-02 11:42:20 +02:00
|
|
|
name: ExpressionDatasourceRef.name,
|
2021-12-21 07:11:11 -07:00
|
|
|
type: ExpressionDatasourceRef.type,
|
2021-08-06 03:24:35 -04:00
|
|
|
access: 'proxy',
|
2021-04-29 15:10:14 +02:00
|
|
|
meta: {
|
|
|
|
|
baseUrl: '',
|
|
|
|
|
module: '',
|
|
|
|
|
type: PluginType.datasource,
|
2021-11-05 08:12:55 -07:00
|
|
|
name: ExpressionDatasourceRef.type,
|
|
|
|
|
id: ExpressionDatasourceRef.type,
|
2021-04-29 15:10:14 +02:00
|
|
|
info: {
|
|
|
|
|
author: {
|
|
|
|
|
name: 'Grafana Labs',
|
|
|
|
|
},
|
|
|
|
|
logos: {
|
|
|
|
|
small: 'public/img/icn-datasource.svg',
|
|
|
|
|
large: 'public/img/icn-datasource.svg',
|
|
|
|
|
},
|
|
|
|
|
description: 'Adds expression support to Grafana',
|
|
|
|
|
screenshots: [],
|
|
|
|
|
links: [],
|
|
|
|
|
updated: '',
|
|
|
|
|
version: '',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
jsonData: {},
|
2022-08-26 11:27:28 +01:00
|
|
|
readOnly: true,
|
2021-04-29 15:10:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const dataSource = new ExpressionDatasourceApi(instanceSettings);
|
|
|
|
|
dataSource.meta = {
|
2021-11-05 08:12:55 -07:00
|
|
|
id: ExpressionDatasourceRef.type,
|
2019-11-06 22:13:20 -08:00
|
|
|
info: {
|
|
|
|
|
logos: {
|
|
|
|
|
small: 'public/img/icn-datasource.svg',
|
|
|
|
|
large: 'public/img/icn-datasource.svg',
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-10-30 11:38:28 -07:00
|
|
|
} as DataSourcePluginMeta;
|
2021-04-29 15:10:14 +02:00
|
|
|
dataSource.components = {
|
2019-10-30 11:38:28 -07:00
|
|
|
QueryEditor: ExpressionQueryEditor,
|
|
|
|
|
};
|