mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: fix condition to distinguish multiple datasources type in dropdown (#67065)
* Add function to check if a datasource is managing alerts * Use helper function to get datasources that manage alerts
This commit is contained in:
@@ -12,6 +12,8 @@ import { getDataSourceSrv, DataSourcePickerState, DataSourcePickerProps } from '
|
||||
import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend';
|
||||
import { ActionMeta, HorizontalGroup, PluginSignatureBadge, MultiSelect } from '@grafana/ui';
|
||||
|
||||
import { isDataSourceManagingAlerts } from '../../utils/datasource';
|
||||
|
||||
export interface MultipleDataSourcePickerProps extends Omit<DataSourcePickerProps, 'onChange' | 'current'> {
|
||||
onChange: (ds: DataSourceInstanceSettings, action: 'add' | 'remove') => void;
|
||||
current: string[] | undefined;
|
||||
@@ -102,9 +104,7 @@ export const MultipleDataSourcePicker = (props: MultipleDataSourcePickerProps) =
|
||||
type,
|
||||
});
|
||||
|
||||
const alertManagingDs = dataSources
|
||||
.filter((ds) => ds.jsonData.manageAlerts)
|
||||
.map((ds) => ({
|
||||
const alertManagingDs = dataSources.filter(isDataSourceManagingAlerts).map((ds) => ({
|
||||
value: ds.name,
|
||||
label: `${ds.name}${ds.isDefault ? ' (default)' : ''}`,
|
||||
imgUrl: ds.meta.info.logos.small,
|
||||
@@ -112,7 +112,7 @@ export const MultipleDataSourcePicker = (props: MultipleDataSourcePickerProps) =
|
||||
}));
|
||||
|
||||
const nonAlertManagingDs = dataSources
|
||||
.filter((ds) => !ds.jsonData.manageAlerts)
|
||||
.filter((ds) => !isDataSourceManagingAlerts(ds))
|
||||
.map((ds) => ({
|
||||
value: ds.name,
|
||||
label: `${ds.name}${ds.isDefault ? ' (default)' : ''}`,
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { mockDataSource } from '../mocks';
|
||||
|
||||
import { isDataSourceManagingAlerts } from './datasource';
|
||||
|
||||
describe('isDataSourceManagingAlerts', () => {
|
||||
it('should return true when the prop is set as true', () => {
|
||||
expect(
|
||||
isDataSourceManagingAlerts(
|
||||
mockDataSource({
|
||||
jsonData: {
|
||||
manageAlerts: true,
|
||||
},
|
||||
})
|
||||
)
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('should return true when the prop is undefined', () => {
|
||||
expect(
|
||||
isDataSourceManagingAlerts(
|
||||
mockDataSource({
|
||||
jsonData: {},
|
||||
})
|
||||
)
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return false when the prop is set as false', () => {
|
||||
expect(
|
||||
isDataSourceManagingAlerts(
|
||||
mockDataSource({
|
||||
jsonData: {
|
||||
manageAlerts: false,
|
||||
},
|
||||
})
|
||||
)
|
||||
).toBe(false);
|
||||
});
|
||||
@@ -191,3 +191,7 @@ export function getDefaultOrFirstCompatibleDataSource(): DataSourceInstanceSetti
|
||||
|
||||
return defaultIsCompatible ? defaultDataSource : getFirstCompatibleDataSource();
|
||||
}
|
||||
|
||||
export function isDataSourceManagingAlerts(ds: DataSourceInstanceSettings<DataSourceJsonData>) {
|
||||
return ds.jsonData.manageAlerts !== false; //if this prop is undefined it defaults to true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user