Skip to content

Commit 3485e52

Browse files
[Search][Query Rules] Rule sets filtering search (#217477)
## Summary Search filtering over the query rulesets table: ![CleanShot 2025-04-09 at 10 25 00@2x](https://github.com/user-attachments/assets/78be7842-f892-454d-a01e-50dee27bdf18) [Jira ticket](https://elasticco.atlassian.net/browse/SEARCH-928) ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [x] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [ ] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) ### Identify risks Does this PR introduce any risks? For example, consider risks like hard to test bugs, performance regression, potential of data loss. Describe the risk, its severity, and mitigation for each identified risk. Invite stakeholders and evaluate how to proceed before merging. - [ ] [See some risk examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx) - [ ] ... --------- Co-authored-by: Elastic Machine <[email protected]>
1 parent 51074fc commit 3485e52

File tree

5 files changed

+248
-18
lines changed

5 files changed

+248
-18
lines changed

x-pack/solutions/search/plugins/search_query_rules/public/components/query_rules_sets/query_rules_sets.tsx

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,37 @@
88
import React, { useState } from 'react';
99

1010
import { QueryRulesListRulesetsQueryRulesetListItem } from '@elastic/elasticsearch/lib/api/types';
11-
import { EuiBasicTable, EuiBasicTableColumn, EuiLink } from '@elastic/eui';
11+
import {
12+
EuiBasicTable,
13+
EuiBasicTableColumn,
14+
EuiFlexGroup,
15+
EuiFlexItem,
16+
EuiLink,
17+
} from '@elastic/eui';
1218
import { i18n } from '@kbn/i18n';
1319
import { DEFAULT_PAGE_VALUE, paginationToPage } from '../../../common/pagination';
1420
import { useFetchQueryRulesSets } from '../../hooks/use_fetch_query_rules_sets';
21+
import { useQueryRulesSetsTableData } from '../../hooks/use_query_rules_sets_table_data';
22+
import { QueryRulesSetsSearch } from './query_rules_sets_search';
1523

1624
export const QueryRulesSets = () => {
1725
const [pageIndex, setPageIndex] = useState(0);
1826
const [pageSize, setPageSize] = useState(DEFAULT_PAGE_VALUE.size);
27+
const [searchKey, setSearchKey] = useState('');
1928
const { from } = paginationToPage({ pageIndex, pageSize, totalItemCount: 0 });
2029
const { data: queryRulesData } = useFetchQueryRulesSets({ from, size: pageSize });
2130

31+
const { queryRulesSetsFilteredData, pagination } = useQueryRulesSetsTableData(
32+
queryRulesData?.data,
33+
searchKey,
34+
pageIndex,
35+
pageSize
36+
);
37+
2238
if (!queryRulesData) {
2339
return null;
2440
}
2541

26-
const pagination = {
27-
initialPageSize: 25,
28-
pageSizeOptions: [10, 25, 50],
29-
...queryRulesData._meta,
30-
pageSize,
31-
pageIndex,
32-
};
3342
const columns: Array<EuiBasicTableColumn<QueryRulesListRulesetsQueryRulesetListItem>> = [
3443
{
3544
field: 'ruleset_id',
@@ -59,15 +68,22 @@ export const QueryRulesSets = () => {
5968
];
6069

6170
return (
62-
<EuiBasicTable
63-
data-test-subj="queryRulesSetTable"
64-
items={queryRulesData.data}
65-
columns={columns}
66-
pagination={pagination}
67-
onChange={({ page: changedPage }) => {
68-
setPageIndex(changedPage.index);
69-
setPageSize(changedPage.size);
70-
}}
71-
/>
71+
<EuiFlexGroup direction="column">
72+
<EuiFlexItem>
73+
<QueryRulesSetsSearch searchKey={searchKey} setSearchKey={setSearchKey} />
74+
</EuiFlexItem>
75+
<EuiFlexItem>
76+
<EuiBasicTable
77+
data-test-subj="queryRulesSetTable"
78+
items={queryRulesSetsFilteredData} // Use filtered data from hook
79+
columns={columns}
80+
pagination={pagination}
81+
onChange={({ page: changedPage }) => {
82+
setPageIndex(changedPage.index);
83+
setPageSize(changedPage.size);
84+
}}
85+
/>
86+
</EuiFlexItem>
87+
</EuiFlexGroup>
7288
);
7389
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { render, screen, fireEvent } from '@testing-library/react';
9+
import { QueryRulesSetsSearch } from './query_rules_sets_search';
10+
import React from 'react';
11+
12+
describe('QueryRulesSetsSearch', () => {
13+
const mockSetSearchKey = jest.fn();
14+
15+
it('renders correctly', () => {
16+
render(<QueryRulesSetsSearch searchKey="" setSearchKey={mockSetSearchKey} />);
17+
expect(screen.getByRole('searchbox')).toBeInTheDocument();
18+
});
19+
20+
it('input value matches searchKey prop', () => {
21+
render(<QueryRulesSetsSearch searchKey="test" setSearchKey={mockSetSearchKey} />);
22+
expect(screen.getByRole('searchbox')).toHaveValue('test');
23+
});
24+
25+
it('calls setSearchKey on input change', () => {
26+
render(<QueryRulesSetsSearch searchKey="" setSearchKey={mockSetSearchKey} />);
27+
fireEvent.change(screen.getByRole('searchbox'), { target: { value: 'new search' } });
28+
expect(mockSetSearchKey).toHaveBeenCalledWith('new search');
29+
});
30+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { EuiFieldSearch } from '@elastic/eui';
9+
import React, { useCallback } from 'react';
10+
11+
interface QueryRulesSetsSearchProps {
12+
searchKey: string;
13+
setSearchKey: React.Dispatch<React.SetStateAction<string>>;
14+
}
15+
16+
export const QueryRulesSetsSearch: React.FC<QueryRulesSetsSearchProps> = ({
17+
searchKey,
18+
setSearchKey,
19+
}) => {
20+
const onSearch = useCallback(
21+
(newSearch: string) => {
22+
const trimSearch = newSearch.trim();
23+
setSearchKey(trimSearch);
24+
},
25+
[setSearchKey]
26+
);
27+
28+
return (
29+
<EuiFieldSearch
30+
aria-label="Search query rules sets"
31+
placeholder="Search"
32+
onChange={(e) => setSearchKey(e.target.value)}
33+
onSearch={onSearch}
34+
value={searchKey}
35+
data-test-subj="searchFieldQueryRulesSets"
36+
/>
37+
);
38+
};
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { renderHook } from '@testing-library/react';
9+
import { useQueryRulesSetsTableData } from './use_query_rules_sets_table_data';
10+
11+
const queryRulesSets = [
12+
{
13+
ruleset_id: 'ruleset-01',
14+
rule_total_count: 1,
15+
rule_criteria_types_counts: { exact: 1, fuzzy: 0 },
16+
rule_type_counts: { pinned: 2, excluded: 1 },
17+
},
18+
{
19+
ruleset_id: 'ruleset-02',
20+
rule_total_count: 2,
21+
rule_criteria_types_counts: { exact: 1, fuzzy: 1 },
22+
rule_type_counts: { pinned: 2, excluded: 1 },
23+
},
24+
{
25+
ruleset_id: 'ruleset-03',
26+
rule_total_count: 3,
27+
rule_criteria_types_counts: { exact: 1, fuzzy: 2 },
28+
rule_type_counts: { pinned: 2, excluded: 1 },
29+
},
30+
];
31+
32+
describe('useQueryRulesSetsTableData', () => {
33+
it('should return correct pagination', () => {
34+
// Given a specific pageIndex and pageSize
35+
const pageIndex = 1;
36+
const pageSize = 3;
37+
38+
// When the hook is called
39+
const { result } = renderHook(() =>
40+
useQueryRulesSetsTableData(queryRulesSets, '', pageIndex, pageSize)
41+
);
42+
43+
// Then the pagination object should reflect the input params and data length
44+
expect(result.current.pagination).toEqual({
45+
pageIndex,
46+
pageSize,
47+
totalItemCount: queryRulesSets.length,
48+
pageSizeOptions: [10, 25, 50],
49+
});
50+
});
51+
52+
it('should filter data based on searchKey', () => {
53+
// Given a search term that matches one ruleset
54+
const searchKey = 'ruleset-02';
55+
56+
// When the hook is called with that search term
57+
const { result } = renderHook(() =>
58+
useQueryRulesSetsTableData(queryRulesSets, searchKey, 0, 10)
59+
);
60+
61+
// Then only the matching ruleset should be returned
62+
expect(result.current.queryRulesSetsFilteredData).toHaveLength(1);
63+
expect(result.current.queryRulesSetsFilteredData[0].ruleset_id).toBe('ruleset-02');
64+
65+
// And the pagination should reflect the filtered count
66+
expect(result.current.pagination.totalItemCount).toBe(1);
67+
});
68+
69+
it('should return all data when searchKey is empty', () => {
70+
// Given an empty search term
71+
const searchKey = '';
72+
73+
// When the hook is called
74+
const { result } = renderHook(() =>
75+
useQueryRulesSetsTableData(queryRulesSets, searchKey, 0, 10)
76+
);
77+
78+
// Then all rulesets should be returned
79+
expect(result.current.queryRulesSetsFilteredData).toEqual(queryRulesSets);
80+
expect(result.current.queryRulesSetsFilteredData).toHaveLength(queryRulesSets.length);
81+
82+
// And the pagination should reflect the total count
83+
expect(result.current.pagination.totalItemCount).toBe(queryRulesSets.length);
84+
});
85+
86+
it('should handle pagination correctly', () => {
87+
// Given specific pagination parameters
88+
const pageIndex = 2;
89+
const pageSize = 5;
90+
91+
// When the hook is called
92+
const { result } = renderHook(() =>
93+
useQueryRulesSetsTableData(queryRulesSets, '', pageIndex, pageSize)
94+
);
95+
96+
// Then the filtered data should contain all items
97+
expect(result.current.queryRulesSetsFilteredData).toEqual(queryRulesSets);
98+
99+
// And the pagination should correctly reflect the input parameters
100+
expect(result.current.pagination).toEqual({
101+
pageIndex,
102+
pageSize,
103+
totalItemCount: queryRulesSets.length,
104+
pageSizeOptions: [10, 25, 50],
105+
});
106+
});
107+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { useMemo } from 'react';
9+
import { Pagination } from '@elastic/eui';
10+
import { QueryRulesListRulesetsQueryRulesetListItem } from '@elastic/elasticsearch/lib/api/types';
11+
12+
interface UseQueryRulesSetsTableDataProps {
13+
queryRulesSetsFilteredData: QueryRulesListRulesetsQueryRulesetListItem[];
14+
pagination: Pagination;
15+
}
16+
17+
export const useQueryRulesSetsTableData = (
18+
data: QueryRulesListRulesetsQueryRulesetListItem[] | undefined,
19+
searchKey: string,
20+
pageIndex: number,
21+
pageSize: number
22+
): UseQueryRulesSetsTableDataProps => {
23+
const queryRulesSetsFilteredData = useMemo(() => {
24+
if (!data) return [];
25+
return data.filter((item) => item.ruleset_id.toLowerCase().includes(searchKey.toLowerCase()));
26+
}, [data, searchKey]);
27+
28+
const pagination: Pagination = useMemo(
29+
() => ({
30+
pageIndex,
31+
pageSize,
32+
totalItemCount: queryRulesSetsFilteredData.length,
33+
pageSizeOptions: [10, 25, 50],
34+
}),
35+
[queryRulesSetsFilteredData.length, pageIndex, pageSize]
36+
);
37+
38+
return { queryRulesSetsFilteredData, pagination };
39+
};

0 commit comments

Comments
 (0)