DataSourceSrv: Filter out non queryable data sources by default (#31144)

This commit is contained in:
Torkel Ödegaard 2021-02-15 11:27:16 +01:00 committed by GitHub
parent 0c3c17592e
commit 50faeb3078
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 0 deletions

View File

@ -28,12 +28,31 @@ export interface DataSourceSrv {
/** @public */
export interface GetDataSourceListFilters {
/** Include mixed deta source by setting this to true */
mixed?: boolean;
/** Only return data sources that support metrics response */
metrics?: boolean;
/** Only return data sources that support tracing response */
tracing?: boolean;
/** Only return data sources that support annotations */
annotations?: boolean;
/**
* By default only data sources that can be queried will be returned. Meaning they have tracing,
* metrics, logs or annotations flag set in plugin.json file
* */
all?: boolean;
/** Set to true to return dashboard data source */
dashboard?: boolean;
/** Set to true to return data source variables */
variables?: boolean;
/** filter list by plugin */
pluginId?: string;
}

View File

@ -157,6 +157,15 @@ export class DatasourceSrv implements DataSourceService {
if (filters.pluginId && x.meta.id !== filters.pluginId) {
return false;
}
if (
!filters.all &&
x.meta.metrics !== true &&
x.meta.annotations !== true &&
x.meta.tracing !== true &&
x.meta.logs !== true
) {
return false;
}
return true;
});

View File

@ -75,6 +75,12 @@ describe('datasource_srv', () => {
uid: 'uid-code-Jaeger',
meta: { tracing: true, id: 'jaeger' },
},
CannotBeQueried: {
type: 'no-query',
name: 'no-query',
uid: 'no-query',
meta: { id: 'no-query' },
},
};
describe('Given a list of data sources', () => {
@ -120,6 +126,13 @@ describe('datasource_srv', () => {
});
});
it('Should by default filter out data sources that cannot be queried', () => {
const list = dataSourceSrv.getList({});
expect(list.find((x) => x.name === 'no-query')).toBeUndefined();
const all = dataSourceSrv.getList({ all: true });
expect(all.find((x) => x.name === 'no-query')).toBeDefined();
});
it('Can get list of data sources with variables: true', () => {
const list = dataSourceSrv.getList({ metrics: true, variables: true });
expect(list[0].name).toBe('${datasource}');