Expressions: Fix expression load with legacy UID -100 (#65950)

* Fix expressions instance settings loading

* Introduce a new method to get name or uid

* Update public/app/features/plugins/datasource_srv.ts

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>

* Move getNameOrUid method outside the class

---------

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
ismail simsek
2023-04-12 16:33:31 +02:00
committed by GitHub
parent 3224b4c8f0
commit d4bd024951
4 changed files with 86 additions and 34 deletions

View File

@@ -65,26 +65,17 @@ export class DatasourceSrv implements DataSourceService {
ref: string | null | undefined | DataSourceRef,
scopedVars?: ScopedVars
): DataSourceInstanceSettings | undefined {
const isstring = typeof ref === 'string';
let nameOrUid = isstring ? (ref as string) : ((ref as any)?.uid as string | undefined);
if (nameOrUid === 'default' || nameOrUid === null || nameOrUid === undefined) {
if (!isstring && ref) {
const type = (ref as any)?.type as string;
if (type === ExpressionDatasourceRef.type) {
return expressionDatasource.instanceSettings;
} else if (type) {
console.log('FIND Default instance for datasource type?', ref);
}
}
return this.settingsMapByUid[this.defaultName] ?? this.settingsMapByName[this.defaultName];
}
let nameOrUid = getNameOrUid(ref);
// Expressions has a new UID as __expr__ See: https://github.com/grafana/grafana/pull/62510/
// But we still have dashboards/panels with old expression UID (-100)
// To support both UIDs until we migrate them all to new one, this check is necessary
if (isExpressionReference(nameOrUid)) {
return expressionDatasource.instanceSettings;
return expressionInstanceSettings;
}
if (nameOrUid === 'default' || nameOrUid == null) {
return this.settingsMapByUid[this.defaultName] ?? this.settingsMapByName[this.defaultName];
}
// Complex logic to support template variable data source names
@@ -114,15 +105,19 @@ export class DatasourceSrv implements DataSourceService {
};
}
return this.settingsMapByUid[nameOrUid] ?? this.settingsMapByName[nameOrUid];
return this.settingsMapByUid[nameOrUid] ?? this.settingsMapByName[nameOrUid] ?? this.settingsMapById[nameOrUid];
}
get(ref?: string | DataSourceRef | null, scopedVars?: ScopedVars): Promise<DataSourceApi> {
let nameOrUid = typeof ref === 'string' ? (ref as string) : ((ref as any)?.uid as string | undefined);
let nameOrUid = getNameOrUid(ref);
if (!nameOrUid) {
return this.get(this.defaultName);
}
if (isExpressionReference(ref)) {
return Promise.resolve(this.datasources[ExpressionDatasourceUID]);
}
// Check if nameOrUid matches a uid and then get the name
const byName = this.settingsMapByName[nameOrUid];
if (byName) {
@@ -154,7 +149,7 @@ export class DatasourceSrv implements DataSourceService {
}
// find the metadata
const instanceSettings = this.settingsMapByUid[key] ?? this.settingsMapByName[key] ?? this.settingsMapById[key];
const instanceSettings = this.getInstanceSettings(key);
if (!instanceSettings) {
return Promise.reject({ message: `Datasource ${key} was not found` });
}
@@ -349,6 +344,15 @@ export class DatasourceSrv implements DataSourceService {
}
}
export function getNameOrUid(ref?: string | DataSourceRef | null): string | undefined {
if (isExpressionReference(ref)) {
return ExpressionDatasourceRef.uid;
}
const isString = typeof ref === 'string';
return isString ? (ref as string) : ((ref as any)?.uid as string | undefined);
}
export function variableInterpolation(value: any[]) {
if (Array.isArray(value)) {
return value[0];