mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 23:33:29 +08:00

* refactor(frontend): update runtime import paths for grafana/runtime/src -> grafana/runtime * feat(runtime): introduce internal api entrypoint and exports property * refactor(frontend): update runtime imports to use internal entrypoint * chore(betterer): update results file * refactor(bookmarks): update runtime/unstable import * chore(betterer): update results file * test(frontend): fix failing tests due to mocking nested runtime imports * test(datasourcesrv): fix failing tests due to mocks * chore(alerting): clean up redundant import * fix(packages): fix default require export pointing to types declaration file * docs(packages): update readme related to exports * chore(internationalization): fix import paths * chore(betterer): update results file
78 lines
2.8 KiB
TypeScript
78 lines
2.8 KiB
TypeScript
import { CoreApp, DataSourceApi, getNextRefId, hasQueryExportSupport, hasQueryImportSupport } from '@grafana/data';
|
|
import { getTemplateSrv, isExpressionReference } from '@grafana/runtime';
|
|
import { DataQuery } from '@grafana/schema';
|
|
|
|
export async function updateQueries(
|
|
nextDS: DataSourceApi,
|
|
nextDSUidOrVariableExpression: string,
|
|
queries: DataQuery[],
|
|
currentDS?: DataSourceApi
|
|
): Promise<DataQuery[]> {
|
|
let nextQueries = queries;
|
|
const datasource = { ...nextDS.getRef(), uid: nextDSUidOrVariableExpression };
|
|
const DEFAULT_QUERY = { ...nextDS?.getDefaultQuery?.(CoreApp.PanelEditor), datasource, refId: 'A' };
|
|
|
|
// we are changing data source type
|
|
if (currentDS?.meta.id !== nextDS.meta.id) {
|
|
// If changing to mixed do nothing
|
|
if (nextDS.meta.mixed) {
|
|
return queries;
|
|
}
|
|
// 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);
|
|
}
|
|
// Otherwise clear queries that do not match the next datasource UID
|
|
else {
|
|
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 = getNextRefId(reduced);
|
|
return reduced.concat([currentQuery]);
|
|
}
|
|
}
|
|
return reduced;
|
|
}, reducedQueries);
|
|
|
|
if (nextDsQueries.length > 0) {
|
|
return nextDsQueries;
|
|
}
|
|
}
|
|
|
|
return [DEFAULT_QUERY];
|
|
}
|
|
}
|
|
|
|
if (nextQueries.length === 0) {
|
|
return [DEFAULT_QUERY];
|
|
}
|
|
|
|
// Set data source on all queries except expression queries
|
|
return nextQueries.map((query) => {
|
|
if (!isExpressionReference(query.datasource) && !nextDS.meta.mixed) {
|
|
query.datasource = datasource;
|
|
}
|
|
return query;
|
|
});
|
|
}
|