2023-07-18 09:15:43 -05:00
|
|
|
import { CoreApp, DataSourceApi, hasQueryExportSupport, hasQueryImportSupport } from '@grafana/data';
|
|
|
|
import { getTemplateSrv } from '@grafana/runtime';
|
2021-11-15 11:15:13 -06:00
|
|
|
import { isExpressionReference } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
2023-07-18 09:15:43 -05:00
|
|
|
import { DataQuery } from '@grafana/schema';
|
|
|
|
import { getNextRefIdChar } from 'app/core/utils/query';
|
2021-11-15 11:15:13 -06:00
|
|
|
|
2022-04-12 06:52:55 -05:00
|
|
|
export async function updateQueries(
|
|
|
|
nextDS: DataSourceApi,
|
2022-05-24 06:14:56 -05:00
|
|
|
nextDSUidOrVariableExpression: string,
|
2021-11-15 11:15:13 -06:00
|
|
|
queries: DataQuery[],
|
2022-04-12 06:52:55 -05:00
|
|
|
currentDS?: DataSourceApi
|
|
|
|
): Promise<DataQuery[]> {
|
|
|
|
let nextQueries = queries;
|
2022-05-24 06:14:56 -05:00
|
|
|
const datasource = { type: nextDS.type, uid: nextDSUidOrVariableExpression };
|
2022-12-05 08:44:04 -06:00
|
|
|
const DEFAULT_QUERY = { ...nextDS?.getDefaultQuery?.(CoreApp.PanelEditor), datasource, refId: 'A' };
|
2021-11-15 11:15:13 -06:00
|
|
|
|
|
|
|
// we are changing data source type
|
2022-04-12 06:52:55 -05:00
|
|
|
if (currentDS?.meta.id !== nextDS.meta.id) {
|
2021-11-15 11:15:13 -06:00
|
|
|
// If changing to mixed do nothing
|
2022-04-12 06:52:55 -05:00
|
|
|
if (nextDS.meta.mixed) {
|
2021-11-15 11:15:13 -06:00
|
|
|
return queries;
|
2022-04-12 06:52:55 -05:00
|
|
|
}
|
|
|
|
// when both data sources support abstract queries
|
|
|
|
else if (hasQueryExportSupport(currentDS) && hasQueryImportSupport(nextDS)) {
|
|
|
|
const abstractQueries = await currentDS.exportToAbstractQueries(queries);
|
|
|
|
nextQueries = await nextDS.importFromAbstractQueries(abstractQueries);
|
|
|
|
}
|
|
|
|
// when datasource supports query import
|
|
|
|
else if (currentDS && nextDS.importQueries) {
|
|
|
|
nextQueries = await nextDS.importQueries(queries, currentDS);
|
|
|
|
}
|
2023-07-18 09:15:43 -05:00
|
|
|
// Otherwise clear queries that do not match the next datasource UID
|
2022-04-12 06:52:55 -05:00
|
|
|
else {
|
2023-07-18 09:15:43 -05:00
|
|
|
if (currentDS) {
|
|
|
|
const templateSrv = getTemplateSrv();
|
|
|
|
const reducedQueries: DataQuery[] = [];
|
|
|
|
let nextUid = nextDS.uid;
|
|
|
|
const nextIsTemplate = templateSrv.containsTemplate(nextDSUidOrVariableExpression);
|
|
|
|
if (nextIsTemplate) {
|
|
|
|
nextUid = templateSrv.replace(nextDS.uid);
|
|
|
|
}
|
|
|
|
// Queries will only be preserved if the datasource UID of the query matches the UID
|
|
|
|
// of the next chosen datasource
|
|
|
|
const nextDsQueries = queries.reduce((reduced, currentQuery) => {
|
|
|
|
if (currentQuery.datasource) {
|
|
|
|
let currUid = currentQuery.datasource.uid;
|
|
|
|
const currIsTemplate = templateSrv.containsTemplate(currUid);
|
|
|
|
if (currIsTemplate) {
|
|
|
|
currUid = templateSrv.replace(currentQuery.datasource.uid);
|
|
|
|
}
|
|
|
|
if (currUid === nextUid && currIsTemplate === nextIsTemplate) {
|
|
|
|
currentQuery.refId = getNextRefIdChar(reduced);
|
|
|
|
return reduced.concat([currentQuery]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reduced;
|
|
|
|
}, reducedQueries);
|
|
|
|
|
|
|
|
if (nextDsQueries.length > 0) {
|
|
|
|
return nextDsQueries;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 08:44:04 -06:00
|
|
|
return [DEFAULT_QUERY];
|
2021-11-15 11:15:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-12 06:52:55 -05:00
|
|
|
if (nextQueries.length === 0) {
|
2022-12-05 08:44:04 -06:00
|
|
|
return [DEFAULT_QUERY];
|
2022-04-12 06:52:55 -05:00
|
|
|
}
|
|
|
|
|
2021-11-15 11:15:13 -06:00
|
|
|
// Set data source on all queries except expression queries
|
2022-04-12 06:52:55 -05:00
|
|
|
return nextQueries.map((query) => {
|
|
|
|
if (!isExpressionReference(query.datasource) && !nextDS.meta.mixed) {
|
2021-11-15 11:15:13 -06:00
|
|
|
query.datasource = datasource;
|
|
|
|
}
|
|
|
|
return query;
|
|
|
|
});
|
|
|
|
}
|