Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RAM] Add shareable rule tag filter #130710

Merged
merged 20 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/plugins/alerting/common/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface RuleAggregations {
ruleEnabledStatus: { enabled: number; disabled: number };
ruleMutedStatus: { muted: number; unmuted: number };
ruleSnoozedStatus: { snoozed: number };
ruleTags: string[];
}

export interface MappedParamsProperties {
Expand Down
7 changes: 7 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 @@ -60,6 +60,7 @@ describe('aggregateRulesRoute', () => {
ruleSnoozedStatus: {
snoozed: 4,
},
ruleTags: ['a', 'b', 'c'],
};
rulesClient.aggregate.mockResolvedValueOnce(aggregateResult);

Expand Down Expand Up @@ -94,6 +95,11 @@ describe('aggregateRulesRoute', () => {
"rule_snoozed_status": Object {
"snoozed": 4,
},
"rule_tags": Array [
"a",
"b",
"c",
],
},
}
`);
Expand Down Expand Up @@ -129,6 +135,7 @@ describe('aggregateRulesRoute', () => {
rule_snoozed_status: {
snoozed: 4,
},
rule_tags: ['a', 'b', 'c'],
},
});
});
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 @@ -50,13 +50,15 @@ const rewriteBodyRes: RewriteResponseCase<AggregateResult> = ({
ruleEnabledStatus,
ruleMutedStatus,
ruleSnoozedStatus,
ruleTags,
...rest
}) => ({
...rest,
rule_execution_status: alertExecutionStatus,
rule_enabled_status: ruleEnabledStatus,
rule_muted_status: ruleMutedStatus,
rule_snoozed_status: ruleSnoozedStatus,
rule_tags: ruleTags,
});

export const aggregateRulesRoute = (
Expand Down
13 changes: 13 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 @@ -133,6 +133,12 @@ export interface RuleAggregation {
doc_count: number;
}>;
};
tags: {
buckets: Array<{
key: string;
doc_count: number;
}>;
};
}

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

export interface FindResult<Params extends RuleTypeParams> {
Expand Down Expand Up @@ -921,6 +928,9 @@ export class RulesClient {
muted: {
terms: { field: 'alert.attributes.muteAll' },
},
tags: {
terms: { field: 'alert.attributes.tags', order: { _key: 'asc' } },
},
snoozed: {
date_range: {
field: 'alert.attributes.snoozeEndTime',
Expand Down Expand Up @@ -990,6 +1000,9 @@ export class RulesClient {
snoozed: snoozedBuckets.reduce((acc, bucket) => acc + bucket.doc_count, 0),
};

const tagsBuckets = resp.aggregations.tags?.buckets || [];
ret.ruleTags = tagsBuckets.map((bucket) => bucket.key);

return ret;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ describe('aggregate()', () => {
},
],
},
tags: {
buckets: [
{
key: 'a',
doc_count: 10,
},
{
key: 'b',
doc_count: 20,
},
{
key: 'c',
doc_count: 30,
},
],
},
},
});

Expand Down Expand Up @@ -160,6 +176,11 @@ describe('aggregate()', () => {
"ruleSnoozedStatus": Object {
"snoozed": 2,
},
"ruleTags": Array [
"a",
"b",
"c",
],
}
`);
expect(unsecuredSavedObjectsClient.find).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -187,6 +208,9 @@ describe('aggregate()', () => {
ranges: [{ from: 'now' }],
},
},
tags: {
terms: { field: 'alert.attributes.tags', order: { _key: 'asc' } },
},
},
},
]);
Expand Down Expand Up @@ -221,6 +245,9 @@ describe('aggregate()', () => {
ranges: [{ from: 'now' }],
},
},
tags: {
terms: { field: 'alert.attributes.tags', order: { _key: 'asc' } },
},
},
},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const allowedExperimentalValues = Object.freeze({
rulesListDatagrid: true,
internalAlertsTable: false,
internalShareableComponentsSandbox: false,
rulesListTagFilter: false,
rulesDetailLogs: true,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { useState } from 'react';
import { EuiSpacer } from '@elastic/eui';
import { getRuleTagFilterLazy } from '../../../common/get_rule_tag_filter';

export const RuleTagFilterSandbox = () => {
const [selectedTags, setSelectedTags] = useState<string[]>([]);

return (
<div style={{ flex: 1 }}>
{getRuleTagFilterLazy({
tags: ['tag1', 'tag2', 'tag3', 'tag4'],
selectedTags,
onChange: setSelectedTags,
})}
<EuiSpacer />
<div>selected tags: {JSON.stringify(selectedTags)}</div>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import React from 'react';
import { RuleStatusDropdownSandbox } from './rule_status_dropdown_sandbox';
import { RuleTagFilterSandbox } from './rule_tag_filter_sandbox';
import { RuleTagBadgeSandbox } from './rule_tag_badge_sandbox';

export const InternalShareableComponentsSandbox: React.FC<{}> = () => {
return (
<>
<RuleStatusDropdownSandbox />
<RuleTagFilterSandbox />
<RuleTagBadgeSandbox />
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,47 @@ describe('loadRuleAggregations', () => {
]
`);
});

test('should call aggregate API with tagsFilter', async () => {
const resolvedValue = {
rule_execution_status: {
ok: 4,
active: 2,
error: 1,
pending: 1,
unknown: 0,
},
};
http.get.mockResolvedValueOnce(resolvedValue);

const result = await loadRuleAggregations({
http,
searchText: 'baz',
tagsFilter: ['a', 'b', 'c'],
});

expect(result).toEqual({
ruleExecutionStatus: {
ok: 4,
active: 2,
error: 1,
pending: 1,
unknown: 0,
},
});

expect(http.get.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": "alert.attributes.tags:(a or b or c)",
"search": "baz",
"search_fields": "[\\"name\\",\\"tags\\"]",
},
},
]
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ const rewriteBodyRes: RewriteRequestCase<RuleAggregations> = ({
rule_enabled_status: ruleEnabledStatus,
rule_muted_status: ruleMutedStatus,
rule_snoozed_status: ruleSnoozedStatus,
rule_tags: ruleTags,
...rest
}: any) => ({
...rest,
ruleExecutionStatus,
ruleEnabledStatus,
ruleMutedStatus,
ruleSnoozedStatus,
ruleTags,
});

export async function loadRuleAggregations({
Expand All @@ -30,14 +32,21 @@ export async function loadRuleAggregations({
typesFilter,
actionTypesFilter,
ruleStatusesFilter,
tagsFilter,
}: {
http: HttpSetup;
searchText?: string;
typesFilter?: string[];
actionTypesFilter?: string[];
ruleStatusesFilter?: string[];
tagsFilter?: string[];
}): Promise<RuleAggregations> {
const filters = mapFiltersToKql({ typesFilter, actionTypesFilter, ruleStatusesFilter });
const filters = mapFiltersToKql({
typesFilter,
actionTypesFilter,
ruleStatusesFilter,
tagsFilter,
});
const res = await http.get<AsApiContract<RuleAggregations>>(
`${INTERNAL_BASE_ALERTING_API_PATH}/rules/_aggregate`,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ describe('mapFiltersToKql', () => {
).toEqual(['alert.attributes.executionStatus.status:(alert or statuses or filter)']);
});

test('should handle tagsFilter', () => {
expect(
mapFiltersToKql({
tagsFilter: ['a', 'b', 'c'],
})
).toEqual(['alert.attributes.tags:(a or b or c)']);
});

test('should handle typesFilter and actionTypesFilter', () => {
expect(
mapFiltersToKql({
Expand All @@ -52,17 +60,19 @@ describe('mapFiltersToKql', () => {
]);
});

test('should handle typesFilter, actionTypesFilter and ruleStatusesFilter', () => {
test('should handle typesFilter, actionTypesFilter, ruleStatusesFilter and tagsFilter', () => {
expect(
mapFiltersToKql({
typesFilter: ['type', 'filter'],
actionTypesFilter: ['action', 'types', 'filter'],
ruleStatusesFilter: ['alert', 'statuses', 'filter'],
tagsFilter: ['a', 'b', 'c'],
})
).toEqual([
'alert.attributes.alertTypeId:(type or filter)',
'(alert.attributes.actions:{ actionTypeId:action } OR alert.attributes.actions:{ actionTypeId:types } OR alert.attributes.actions:{ actionTypeId:filter })',
'alert.attributes.executionStatus.status:(alert or statuses or filter)',
'alert.attributes.tags:(a or b or c)',
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ export const mapFiltersToKql = ({
typesFilter,
actionTypesFilter,
ruleStatusesFilter,
tagsFilter,
}: {
typesFilter?: string[];
actionTypesFilter?: string[];
ruleStatusesFilter?: string[];
tagsFilter?: string[];
}): string[] => {
const filters = [];
if (typesFilter && typesFilter.length) {
Expand All @@ -32,5 +34,8 @@ export const mapFiltersToKql = ({
if (ruleStatusesFilter && ruleStatusesFilter.length) {
filters.push(`alert.attributes.executionStatus.status:(${ruleStatusesFilter.join(' or ')})`);
}
if (tagsFilter && tagsFilter.length) {
filters.push(`alert.attributes.tags:(${tagsFilter.join(' or ')})`);
}
return filters;
};
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,42 @@ describe('loadRules', () => {
]
`);
});

test('should call find API with tagsFilter', async () => {
const resolvedValue = {
page: 1,
per_page: 10,
total: 0,
data: [],
};
http.get.mockResolvedValueOnce(resolvedValue);
const result = await loadRules({
http,
tagsFilter: ['a', 'b', 'c'],
page: { index: 0, size: 10 },
});
expect(result).toEqual({
page: 1,
perPage: 10,
total: 0,
data: [],
});
expect(http.get.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_find",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": "alert.attributes.tags:(a or b or c)",
"page": 1,
"per_page": 10,
"search": undefined,
"search_fields": undefined,
"sort_field": "name",
"sort_order": "asc",
},
},
]
`);
});
});
Loading