Skip to content

Commit

Permalink
[RAM] Add aggs to know how many rules are snoozed (#128212)
Browse files Browse the repository at this point in the history
* add aggs to know how many of snoozed rule exist

* simplify + update o11y

* fix tests

* fix jest

* bring back test
  • Loading branch information
XavierM authored Mar 24, 2022
1 parent f462be7 commit a743498
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 9 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/common/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export interface AlertAggregations {
alertExecutionStatus: { [status: string]: number };
ruleEnabledStatus: { enabled: number; disabled: number };
ruleMutedStatus: { muted: number; unmuted: number };
ruleSnoozedStatus: { snoozed: number };
}

export interface MappedParamsProperties {
Expand Down
9 changes: 9 additions & 0 deletions x-pack/plugins/alerting/server/routes/aggregate_rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ describe('aggregateRulesRoute', () => {
muted: 2,
unmuted: 39,
},
ruleSnoozedStatus: {
snoozed: 4,
},
};
rulesClient.aggregate.mockResolvedValueOnce(aggregateResult);

Expand Down Expand Up @@ -88,6 +91,9 @@ describe('aggregateRulesRoute', () => {
"muted": 2,
"unmuted": 39,
},
"rule_snoozed_status": Object {
"snoozed": 4,
},
},
}
`);
Expand Down Expand Up @@ -120,6 +126,9 @@ describe('aggregateRulesRoute', () => {
muted: 2,
unmuted: 39,
},
rule_snoozed_status: {
snoozed: 4,
},
},
});
});
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/alerting/server/routes/aggregate_rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ const rewriteBodyRes: RewriteResponseCase<AggregateResult> = ({
alertExecutionStatus,
ruleEnabledStatus,
ruleMutedStatus,
ruleSnoozedStatus,
...rest
}) => ({
...rest,
rule_execution_status: alertExecutionStatus,
rule_enabled_status: ruleEnabledStatus,
rule_muted_status: ruleMutedStatus,
rule_snoozed_status: ruleSnoozedStatus,
});

export const aggregateRulesRoute = (
Expand Down
22 changes: 22 additions & 0 deletions x-pack/plugins/alerting/server/rules_client/rules_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ export interface RuleAggregation {
doc_count: number;
}>;
};
snoozed: {
buckets: Array<{
key: number;
key_as_string: string;
doc_count: number;
}>;
};
}

export interface ConstructorOptions {
Expand Down Expand Up @@ -191,6 +198,7 @@ export interface AggregateResult {
alertExecutionStatus: { [status: string]: number };
ruleEnabledStatus?: { enabled: number; disabled: number };
ruleMutedStatus?: { muted: number; unmuted: number };
ruleSnoozedStatus?: { snoozed: number };
}

export interface FindResult<Params extends RuleTypeParams> {
Expand Down Expand Up @@ -859,6 +867,7 @@ export class RulesClient {
);
throw error;
}

const { filter: authorizationFilter } = authorizationTuple;
const resp = await this.unsecuredSavedObjectsClient.find<RawRule, RuleAggregation>({
...options,
Expand All @@ -879,6 +888,13 @@ export class RulesClient {
muted: {
terms: { field: 'alert.attributes.muteAll' },
},
snoozed: {
date_range: {
field: 'alert.attributes.snoozeEndTime',
format: 'strict_date_time',
ranges: [{ from: 'now' }],
},
},
},
});

Expand All @@ -894,6 +910,7 @@ export class RulesClient {
muted: 0,
unmuted: 0,
},
ruleSnoozedStatus: { snoozed: 0 },
};

for (const key of RuleExecutionStatusValues) {
Expand Down Expand Up @@ -935,6 +952,11 @@ export class RulesClient {
unmuted: mutedBuckets.find((bucket) => bucket.key === 0)?.doc_count ?? 0,
};

const snoozedBuckets = resp.aggregations.snoozed.buckets;
ret.ruleSnoozedStatus = {
snoozed: snoozedBuckets.reduce((acc, bucket) => acc + bucket.doc_count, 0),
};

return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ describe('aggregate()', () => {
{ key: 1, key_as_string: '1', doc_count: 3 },
],
},
snoozed: {
buckets: [
{
key: '2022-03-21T20:22:01.501Z-*',
format: 'strict_date_time',
from: 1.647894121501e12,
from_as_string: '2022-03-21T20:22:01.501Z',
doc_count: 2,
},
],
},
},
});

Expand Down Expand Up @@ -146,6 +157,9 @@ describe('aggregate()', () => {
"muted": 3,
"unmuted": 27,
},
"ruleSnoozedStatus": Object {
"snoozed": 2,
},
}
`);
expect(unsecuredSavedObjectsClient.find).toHaveBeenCalledTimes(1);
Expand All @@ -166,6 +180,13 @@ describe('aggregate()', () => {
muted: {
terms: { field: 'alert.attributes.muteAll' },
},
snoozed: {
date_range: {
field: 'alert.attributes.snoozeEndTime',
format: 'strict_date_time',
ranges: [{ from: 'now' }],
},
},
},
},
]);
Expand Down Expand Up @@ -193,6 +214,13 @@ describe('aggregate()', () => {
muted: {
terms: { field: 'alert.attributes.muteAll' },
},
snoozed: {
date_range: {
field: 'alert.attributes.snoozeEndTime',
format: 'strict_date_time',
ranges: [{ from: 'now' }],
},
},
},
},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface RuleStatsState {
disabled: number;
muted: number;
error: number;
snoozed: number;
}

export interface TopAlert {
Expand Down Expand Up @@ -90,6 +91,7 @@ function AlertsPage() {
disabled: 0,
muted: 0,
error: 0,
snoozed: 0,
});

useEffect(() => {
Expand All @@ -111,18 +113,21 @@ function AlertsPage() {
const response = await loadRuleAggregations({
http,
});
const { ruleExecutionStatus, ruleMutedStatus, ruleEnabledStatus } = response;
if (ruleExecutionStatus && ruleMutedStatus && ruleEnabledStatus) {
const { ruleExecutionStatus, ruleMutedStatus, ruleEnabledStatus, ruleSnoozedStatus } =
response;
if (ruleExecutionStatus && ruleMutedStatus && ruleEnabledStatus && ruleSnoozedStatus) {
const total = Object.values(ruleExecutionStatus).reduce((acc, value) => acc + value, 0);
const { disabled } = ruleEnabledStatus;
const { muted } = ruleMutedStatus;
const { error } = ruleExecutionStatus;
const { snoozed } = ruleSnoozedStatus;
setRuleStats({
...ruleStats,
total,
disabled,
muted,
error,
snoozed,
});
}
setRuleStatsLoading(false);
Expand Down Expand Up @@ -263,9 +268,9 @@ function AlertsPage() {
data-test-subj="statDisabled"
/>,
<EuiStat
title={ruleStats.muted}
title={ruleStats.muted + ruleStats.snoozed}
description={i18n.translate('xpack.observability.alerts.ruleStats.muted', {
defaultMessage: 'Muted',
defaultMessage: 'Snoozed',
})}
color="primary"
titleSize="xs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export default function createAggregateTests({ getService }: FtrProviderContext)
const response = await supertest.get(
`${getUrlPrefix(Spaces.space1.id)}/internal/alerting/rules/_aggregate`
);

expect(response.status).to.eql(200);
expect(response.body).to.eql({
rule_enabled_status: {
Expand All @@ -42,6 +41,9 @@ export default function createAggregateTests({ getService }: FtrProviderContext)
muted: 0,
unmuted: 0,
},
rule_snoozed_status: {
snoozed: 0,
},
});
});

Expand Down Expand Up @@ -96,12 +98,11 @@ export default function createAggregateTests({ getService }: FtrProviderContext)
// calls are successful, the call to aggregate may return stale totals if called
// too early.
await delay(1000);
const reponse = await supertest.get(
const response = await supertest.get(
`${getUrlPrefix(Spaces.space1.id)}/internal/alerting/rules/_aggregate`
);

expect(reponse.status).to.eql(200);
expect(reponse.body).to.eql({
expect(response.status).to.eql(200);
expect(response.body).to.eql({
rule_enabled_status: {
disabled: 0,
enabled: 7,
Expand All @@ -118,6 +119,9 @@ export default function createAggregateTests({ getService }: FtrProviderContext)
muted: 0,
unmuted: 7,
},
rule_snoozed_status: {
snoozed: 0,
},
});
});

Expand Down Expand Up @@ -195,6 +199,9 @@ export default function createAggregateTests({ getService }: FtrProviderContext)
muted: 0,
unmuted: 7,
},
ruleSnoozedStatus: {
snoozed: 0,
},
});
});
});
Expand Down

0 comments on commit a743498

Please sign in to comment.