Skip to content

Commit

Permalink
fix parsing of cluster alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
neptunian committed Aug 3, 2021
1 parent d9b279a commit 165bf83
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/monitoring/common/types/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {

export type CommonAlert = Alert<AlertTypeParams> | SanitizedAlert<AlertTypeParams>;

export interface RulesByType {
[type: string]: CommonAlertStatus[];
}
export interface CommonAlertStatus {
states: CommonAlertState[];
rawAlert: Alert<AlertTypeParams> | SanitizedAlert<AlertTypeParams>;
Expand Down
9 changes: 1 addition & 8 deletions x-pack/plugins/monitoring/server/lib/alerts/fetch_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<RulesByType>((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,
Expand All @@ -177,7 +173,6 @@ export async function getClustersFromRequest(
}
}
}

// add kibana data
const kibanas =
isInCodePath(codePaths, [CODE_PATH_KIBANA]) && !isStandaloneCluster
Expand Down
10 changes: 9 additions & 1 deletion x-pack/plugins/monitoring/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit 165bf83

Please sign in to comment.