mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Expressions: More robust expression check (#65006)
More robust expression check
This commit is contained in:
@@ -7,15 +7,15 @@ import {
|
||||
ScopedVars,
|
||||
} from '@grafana/data';
|
||||
import {
|
||||
GetDataSourceListFilters,
|
||||
DataSourceSrv as DataSourceService,
|
||||
getDataSourceSrv as getDataSourceService,
|
||||
TemplateSrv,
|
||||
getTemplateSrv,
|
||||
getLegacyAngularInjector,
|
||||
getBackendSrv,
|
||||
GetDataSourceListFilters,
|
||||
getDataSourceSrv as getDataSourceService,
|
||||
getLegacyAngularInjector,
|
||||
getTemplateSrv,
|
||||
TemplateSrv,
|
||||
} from '@grafana/runtime';
|
||||
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
||||
import { ExpressionDatasourceRef, isExpressionReference } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
||||
import appEvents from 'app/core/app_events';
|
||||
import config from 'app/core/config';
|
||||
import {
|
||||
@@ -80,6 +80,13 @@ export class DatasourceSrv implements DataSourceService {
|
||||
return this.settingsMapByUid[this.defaultName] ?? this.settingsMapByName[this.defaultName];
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// Complex logic to support template variable data source names
|
||||
// For this we just pick the current or first data source in the variable
|
||||
if (nameOrUid[0] === '$') {
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
DataSourcePluginMeta,
|
||||
ScopedVar,
|
||||
} from '@grafana/data';
|
||||
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
||||
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
|
||||
|
||||
// Datasource variable $datasource with current value 'BBB'
|
||||
@@ -199,6 +200,25 @@ describe('datasource_srv', () => {
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('should return expression settings with either expression UIDs', () => {
|
||||
const exprWithOldUID = dataSourceSrv.getInstanceSettings('-100');
|
||||
expect(exprWithOldUID?.name).toBe('Expression');
|
||||
expect(exprWithOldUID?.uid).toBe(ExpressionDatasourceRef.uid);
|
||||
expect(exprWithOldUID?.type).toBe(ExpressionDatasourceRef.type);
|
||||
|
||||
const exprWithNewUID = dataSourceSrv.getInstanceSettings('__expr__');
|
||||
expect(exprWithNewUID?.name).toBe('Expression');
|
||||
expect(exprWithNewUID?.uid).toBe(ExpressionDatasourceRef.uid);
|
||||
expect(exprWithNewUID?.type).toBe(ExpressionDatasourceRef.type);
|
||||
});
|
||||
|
||||
it('should return expression settings with expression name', () => {
|
||||
const exprWithName = dataSourceSrv.getInstanceSettings('Expression');
|
||||
expect(exprWithName?.name).toBe(ExpressionDatasourceRef.name);
|
||||
expect(exprWithName?.uid).toBe(ExpressionDatasourceRef.uid);
|
||||
expect(exprWithName?.type).toBe(ExpressionDatasourceRef.type);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when getting external metric sources', () => {
|
||||
|
||||
Reference in New Issue
Block a user