From 165bf83ebfb0eebc2ab5b0f8ab18c611c63c663e Mon Sep 17 00:00:00 2001 From: neptunian Date: Tue, 3 Aug 2021 15:59:05 -0400 Subject: [PATCH] fix parsing of cluster alerts --- .../plugins/monitoring/common/types/alerts.ts | 3 +++ .../server/lib/alerts/fetch_status.ts | 9 +------ .../lib/cluster/get_clusters_from_request.ts | 27 ++++++++----------- x-pack/plugins/monitoring/server/types.ts | 10 ++++++- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/x-pack/plugins/monitoring/common/types/alerts.ts b/x-pack/plugins/monitoring/common/types/alerts.ts index e13d791ca162b..39323e0d7b27c 100644 --- a/x-pack/plugins/monitoring/common/types/alerts.ts +++ b/x-pack/plugins/monitoring/common/types/alerts.ts @@ -15,6 +15,9 @@ import { export type CommonAlert = Alert | SanitizedAlert; +export interface RulesByType { + [type: string]: CommonAlertStatus[]; +} export interface CommonAlertStatus { states: CommonAlertState[]; rawAlert: Alert | SanitizedAlert; diff --git a/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts b/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts index 6e8b82b5907d6..6355cb3eb26b9 100644 --- a/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts +++ b/x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts @@ -8,16 +8,9 @@ import { AlertInstanceState } from '../../../common/types/alerts'; import { RulesClient } from '../../../../alerting/server'; import { AlertsFactory } from '../../alerts'; -import { - CommonAlertStatus, - CommonAlertState, - CommonAlertFilter, -} from '../../../common/types/alerts'; +import { CommonAlertState, CommonAlertFilter, RulesByType } from '../../../common/types/alerts'; import { ALERTS } from '../../../common/constants'; -interface RulesByType { - [type: string]: CommonAlertStatus[]; -} export async function fetchStatus( rulesClient: RulesClient, alertTypes: string[] | undefined, diff --git a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.ts b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.ts index ab421312244f7..a2b3434e6b3f7 100644 --- a/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.ts +++ b/x-pack/plugins/monitoring/server/lib/cluster/get_clusters_from_request.ts @@ -35,6 +35,7 @@ import { getStandaloneClusterDefinition, hasStandaloneClusters } from '../standa import { getLogTypes } from '../logs'; import { isInCodePath } from './is_in_code_path'; import { LegacyRequest, Cluster } from '../../types'; +import { RulesByType } from '../../../common/types/alerts'; /** * Get all clusters or the cluster associated with {@code clusterUuid} when it is defined. @@ -142,21 +143,16 @@ export async function getClustersFromRequest( } else { try { cluster.alerts = { - list: Object.keys(alertStatus).reduce((accum, alertName) => { - const value = alertStatus[alertName]; - if (value.states && value.states.length) { - Reflect.set(accum, alertName, { - ...value, - states: value.states.filter( - (state) => - state.state.cluster.clusterUuid === - get(cluster, 'elasticsearch.cluster.id', cluster.cluster_uuid) - ), - }); - } else { - Reflect.set(accum, alertName, value); - } - return accum; + list: Object.keys(alertStatus).reduce((acc, ruleTypeName) => { + acc[ruleTypeName] = alertStatus[ruleTypeName].map((rule) => ({ + ...rule, + states: rule.states.filter( + (state) => + state.state.cluster.clusterUuid === + get(cluster, 'elasticsearch.cluster.id', cluster.cluster_uuid) + ), + })); + return acc; }, {}), alertsMeta: { enabled: true, @@ -177,7 +173,6 @@ export async function getClustersFromRequest( } } } - // add kibana data const kibanas = isInCodePath(codePaths, [CODE_PATH_KIBANA]) && !isStandaloneCluster diff --git a/x-pack/plugins/monitoring/server/types.ts b/x-pack/plugins/monitoring/server/types.ts index a057b72d89b29..8120840aba03e 100644 --- a/x-pack/plugins/monitoring/server/types.ts +++ b/x-pack/plugins/monitoring/server/types.ts @@ -31,6 +31,7 @@ import { PluginSetupContract as FeaturesPluginSetupContract } from '../../featur import { EncryptedSavedObjectsPluginSetup } from '../../encrypted_saved_objects/server'; import { CloudSetup } from '../../cloud/server'; import { ElasticsearchModifiedSource } from '../common/types/es'; +import { RulesByType } from '../common/types/alerts'; export interface MonitoringLicenseService { refresh: () => Promise; @@ -151,9 +152,16 @@ export interface LegacyServer { export type Cluster = ElasticsearchModifiedSource & { ml?: { jobs: any }; logs?: any; - alerts?: any; + alerts?: AlertsOnCluster; }; +export interface AlertsOnCluster { + list: RulesByType; + alertsMeta: { + enabled: boolean; + }; +} + export interface Bucket { key: string; uuids: {