From fbf54f00238fddcca0a4ed2268a2a74350e43dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Marcondes?= <55978943+cauemarcondes@users.noreply.github.com> Date: Thu, 16 Jul 2020 14:03:50 +0100 Subject: [PATCH] showing alerts created in the management page (#72031) --- .../services/get_observability_alerts.test.ts | 46 ++++++++++++++++++- .../services/get_observability_alerts.ts | 8 ++-- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/observability/public/services/get_observability_alerts.test.ts b/x-pack/plugins/observability/public/services/get_observability_alerts.test.ts index 36ef1241983a5..2027c9fa4a25d 100644 --- a/x-pack/plugins/observability/public/services/get_observability_alerts.test.ts +++ b/x-pack/plugins/observability/public/services/get_observability_alerts.test.ts @@ -40,7 +40,43 @@ describe('getObservabilityAlerts', () => { expect(alerts).toEqual([]); }); - it('Shows alerts from Observability', async () => { + it('Returns empty array when alerts are not allowed based on consumer type', async () => { + const core = ({ + http: { + get: async () => { + return { + data: [ + { + id: 1, + consumer: 'siem', + }, + { + id: 2, + consumer: 'kibana', + }, + { + id: 3, + consumer: 'index', + }, + { + id: 4, + consumer: 'foo', + }, + { + id: 5, + consumer: 'bar', + }, + ], + }; + }, + basePath, + }, + } as unknown) as AppMountContext['core']; + const alerts = await getObservabilityAlerts({ core }); + expect(alerts).toEqual([]); + }); + + it('Shows alerts from Observability and Alerts', async () => { const core = ({ http: { get: async () => { @@ -66,6 +102,10 @@ describe('getObservabilityAlerts', () => { id: 5, consumer: 'metrics', }, + { + id: 6, + consumer: 'alerts', + }, ], }; }, @@ -91,6 +131,10 @@ describe('getObservabilityAlerts', () => { id: 5, consumer: 'metrics', }, + { + id: 6, + consumer: 'alerts', + }, ]); }); }); diff --git a/x-pack/plugins/observability/public/services/get_observability_alerts.ts b/x-pack/plugins/observability/public/services/get_observability_alerts.ts index 58ff9c92acbff..fe5451597688a 100644 --- a/x-pack/plugins/observability/public/services/get_observability_alerts.ts +++ b/x-pack/plugins/observability/public/services/get_observability_alerts.ts @@ -7,6 +7,8 @@ import { AppMountContext } from 'kibana/public'; import { Alert } from '../../../alerts/common'; +const allowedConsumers = ['apm', 'uptime', 'logs', 'metrics', 'alerts']; + export async function getObservabilityAlerts({ core }: { core: AppMountContext['core'] }) { try { const { data = [] }: { data: Alert[] } = await core.http.get( @@ -19,11 +21,7 @@ export async function getObservabilityAlerts({ core }: { core: AppMountContext[' } ); - return data.filter(({ consumer }) => { - return ( - consumer === 'apm' || consumer === 'uptime' || consumer === 'logs' || consumer === 'metrics' - ); - }); + return data.filter(({ consumer }) => allowedConsumers.includes(consumer)); } catch (e) { // eslint-disable-next-line no-console console.error('Error while fetching alerts', e);