[Search][Query Rules] Ruleset table (#217170)

## Summary

Listing the rulsets created, otherwise the Empty prompt will be
rendered.

- [x] Create paginated query ruleset table as with design.
- [x] This is the table only, no edit/create/update actions available.

![CleanShot 2025-04-04 at 14 53
17@2x](https://github.com/user-attachments/assets/2a59848a-8ebc-42e8-8f19-b94d5e9f4e10)

Jira ticket: https://elasticco.atlassian.net/browse/SEARCH-927

### 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 <elasticmachine@users.noreply.github.com>
This commit is contained in:
José Luis González 2025-04-08 10:13:32 +02:00 committed by GitHub
parent 624410524f
commit ba91787976
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 131 additions and 1 deletions

View file

@ -25,6 +25,7 @@ import { EmptyPrompt } from '../empty_prompt/empty_prompt';
import { ErrorPrompt } from '../error_prompt/error_prompt';
import { isPermissionError } from '../../utils/query_rules_utils';
import queryRulesBackground from '../../assets/query-rule-background.svg';
import { QueryRulesSets } from '../query_rules_sets/query_rules_sets';
export const QueryRulesOverview = () => {
const {
@ -103,7 +104,6 @@ export const QueryRulesOverview = () => {
)}
<KibanaPageTemplate.Section
restrictWidth
alignment="center"
contentProps={{
css:
!isInitialLoading && !isError && queryRulesData?._meta.totalItemCount !== 0
@ -115,6 +115,9 @@ export const QueryRulesOverview = () => {
{isError && (
<ErrorPrompt errorType={isPermissionError(error) ? 'missingPermissions' : 'generic'} />
)}
{!isInitialLoading && queryRulesData && queryRulesData._meta.totalItemCount > 0 && (
<QueryRulesSets />
)}
{!isInitialLoading && queryRulesData && queryRulesData._meta.totalItemCount === 0 && (
<EuiFlexGroup justifyContent="center" alignItems="center" direction="column">
<EuiFlexItem>

View file

@ -0,0 +1,54 @@
/*
* 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 from 'react';
import { render, screen } from '@testing-library/react';
import { QueryRulesSets } from './query_rules_sets';
jest.mock('../../hooks/use_fetch_query_rules_sets', () => ({
useFetchQueryRulesSets: () => ({
data: {
data: [
{
ruleset_id: 'Query Rule Set 1',
rule_total_count: 2,
},
{
ruleset_id: 'Query Rule Set 2',
rule_total_count: 3,
},
],
_meta: { pageIndex: 0, pageSize: 10, totalItemCount: 2 },
},
isLoading: false,
isError: false,
}),
}));
describe('Search Query Rules Sets list', () => {
it('should render the list with query rule sets', () => {
render(<QueryRulesSets />);
const queryRulesSetTable = screen.getByTestId('queryRulesSetTable');
expect(queryRulesSetTable).toBeInTheDocument();
const queryRulesSetItemNames = screen.getAllByTestId('queryRuleSetName');
expect(queryRulesSetItemNames).toHaveLength(2);
expect(queryRulesSetItemNames[0].textContent).toBe('Query Rule Set 1');
expect(queryRulesSetItemNames[1].textContent).toBe('Query Rule Set 2');
const queryRulesSetItemRuleCounts = screen.getAllByTestId('queryRuleSetItemRuleCount');
expect(queryRulesSetItemRuleCounts).toHaveLength(2);
expect(queryRulesSetItemRuleCounts[0].textContent).toBe('2');
expect(queryRulesSetItemRuleCounts[1].textContent).toBe('3');
const queryRulesSetItemPageSize = screen.getByTestId('tablePaginationPopoverButton');
const queryRulesSetPageButton = screen.getByTestId('pagination-button-0');
expect(queryRulesSetItemPageSize).toBeInTheDocument();
expect(queryRulesSetPageButton).toBeInTheDocument();
});
});

View file

@ -0,0 +1,73 @@
/*
* 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 { QueryRulesListRulesetsQueryRulesetListItem } from '@elastic/elasticsearch/lib/api/types';
import { EuiBasicTable, EuiBasicTableColumn, EuiLink } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { DEFAULT_PAGE_VALUE, paginationToPage } from '../../../common/pagination';
import { useFetchQueryRulesSets } from '../../hooks/use_fetch_query_rules_sets';
export const QueryRulesSets = () => {
const [pageIndex, setPageIndex] = useState(0);
const [pageSize, setPageSize] = useState(DEFAULT_PAGE_VALUE.size);
const { from } = paginationToPage({ pageIndex, pageSize, totalItemCount: 0 });
const { data: queryRulesData } = useFetchQueryRulesSets({ from, size: pageSize });
if (!queryRulesData) {
return null;
}
const pagination = {
initialPageSize: 25,
pageSizeOptions: [10, 25, 50],
...queryRulesData._meta,
pageSize,
pageIndex,
};
const columns: Array<EuiBasicTableColumn<QueryRulesListRulesetsQueryRulesetListItem>> = [
{
field: 'ruleset_id',
name: i18n.translate('xpack.queryRules.queryRulesSetTable.nameColumn', {
defaultMessage: 'Query Rule Set',
}),
render: (name: string) => (
<EuiLink
data-test-subj="queryRuleSetName"
onClick={() => {
// Navigate to the ruleset details page
}}
>
{name}
</EuiLink>
),
},
{
field: 'rule_total_count',
name: i18n.translate('xpack.queryRules.queryRulesSetTable.ruleCount', {
defaultMessage: 'Rule Count',
}),
render: (ruleCount: number) => (
<div data-test-subj="queryRuleSetItemRuleCount">{ruleCount}</div>
),
},
];
return (
<EuiBasicTable
data-test-subj="queryRulesSetTable"
items={queryRulesData.data}
columns={columns}
pagination={pagination}
onChange={({ page: changedPage }) => {
setPageIndex(changedPage.index);
setPageSize(changedPage.size);
}}
/>
);
};