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:
Virginia Cepeda
2023-04-21 14:27:39 -03:00
committed by GitHub
parent a29cfe5d46
commit 329d940448
3 changed files with 52 additions and 9 deletions

View File

@@ -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,17 +104,15 @@ export const MultipleDataSourcePicker = (props: MultipleDataSourcePickerProps) =
type,
});
const alertManagingDs = dataSources
.filter((ds) => ds.jsonData.manageAlerts)
.map((ds) => ({
value: ds.name,
label: `${ds.name}${ds.isDefault ? ' (default)' : ''}`,
imgUrl: ds.meta.info.logos.small,
meta: ds.meta,
}));
const alertManagingDs = dataSources.filter(isDataSourceManagingAlerts).map((ds) => ({
value: ds.name,
label: `${ds.name}${ds.isDefault ? ' (default)' : ''}`,
imgUrl: ds.meta.info.logos.small,
meta: ds.meta,
}));
const nonAlertManagingDs = dataSources
.filter((ds) => !ds.jsonData.manageAlerts)
.filter((ds) => !isDataSourceManagingAlerts(ds))
.map((ds) => ({
value: ds.name,
label: `${ds.name}${ds.isDefault ? ' (default)' : ''}`,

View File

@@ -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);
});

View File

@@ -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
}