From 8357ceae7b27d70ad19e9d72de9d98de0373d471 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Sun, 23 Apr 2023 14:17:59 +0100 Subject: [PATCH] [v9.5.x] Alerting: fix condition to distinguish multiple datasources type in dropdown (#67066) --- .../rules/MultipleDataSourcePicker.tsx | 18 ++++----- .../alerting/unified/utils/datasource.test.ts | 39 +++++++++++++++++++ .../alerting/unified/utils/datasource.ts | 4 ++ 3 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 public/app/features/alerting/unified/utils/datasource.test.ts diff --git a/public/app/features/alerting/unified/components/rules/MultipleDataSourcePicker.tsx b/public/app/features/alerting/unified/components/rules/MultipleDataSourcePicker.tsx index 4e6942011b0..119d0fc8ec0 100644 --- a/public/app/features/alerting/unified/components/rules/MultipleDataSourcePicker.tsx +++ b/public/app/features/alerting/unified/components/rules/MultipleDataSourcePicker.tsx @@ -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 { 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)' : ''}`, diff --git a/public/app/features/alerting/unified/utils/datasource.test.ts b/public/app/features/alerting/unified/utils/datasource.test.ts new file mode 100644 index 00000000000..3c10151be9f --- /dev/null +++ b/public/app/features/alerting/unified/utils/datasource.test.ts @@ -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); +}); diff --git a/public/app/features/alerting/unified/utils/datasource.ts b/public/app/features/alerting/unified/utils/datasource.ts index 0d309b1cca5..23b234dbecc 100644 --- a/public/app/features/alerting/unified/utils/datasource.ts +++ b/public/app/features/alerting/unified/utils/datasource.ts @@ -191,3 +191,7 @@ export function getDefaultOrFirstCompatibleDataSource(): DataSourceInstanceSetti return defaultIsCompatible ? defaultDataSource : getFirstCompatibleDataSource(); } + +export function isDataSourceManagingAlerts(ds: DataSourceInstanceSettings) { + return ds.jsonData.manageAlerts !== false; //if this prop is undefined it defaults to true +}