Skip to content

Commit

Permalink
[Security Solution][Detections] Update rules count indicator in the R…
Browse files Browse the repository at this point in the history
…ules table to show pagination info (elastic#138902)

**Fixes:** elastic#121754

## Summary

Changes rules count pagination count to format:

**"Showing 1-10 of 596 rules"** -> when page 1, rules per page is 10 and total rules is 596
**"Showing 6-6 of 6 rules"** -> when page 2, rules per page is 5 and total rules is 6
See other cases in tests.

**Before changes**
![image](https://user-images.githubusercontent.com/5354282/184883106-118f0041-5567-4cf8-bfd5-8383e8146018.png)
![image](https://user-images.githubusercontent.com/5354282/184884145-22d30482-cc2d-4796-8f84-d809a8ca6d8a.png)

**After changes**
![image](https://user-images.githubusercontent.com/5354282/184882149-d6c55dff-39a0-4a72-94a9-217b2e5ca7ac.png)
![image](https://user-images.githubusercontent.com/5354282/184886334-a107a2c5-31dc-4a7b-9e70-54c87c1630e0.png)

## Codebase changes

- Refactors `AllRulesUtilityBar` component to separate usage of the utility bar between the all-rules table use-case and the Exceptions table use-case, by creating a new `ExceptionsTableUtilityBar` component.

### Checklist

Delete any items that are not applicable to this PR.

- [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/packages/kbn-i18n/README.md)
- [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
- [x] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))

### For maintainers

- [ ] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

(cherry picked from commit 147e414)
  • Loading branch information
jpdjere committed Aug 23, 2022
1 parent 7ffc375 commit a270c7c
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
RULES_TABLE,
RULE_SWITCH,
SEVERITY,
SHOWING_RULES_TEXT,
} from '../../screens/alerts_detection_rules';
import {
ABOUT_CONTINUE_BTN,
Expand Down Expand Up @@ -218,18 +217,21 @@ describe('Custom query rules', () => {
const initialNumberOfRules = rules.length;
const expectedNumberOfRulesAfterDeletion = initialNumberOfRules - 1;

cy.get(SHOWING_RULES_TEXT).should('have.text', `Showing ${initialNumberOfRules} rules`);
cy.request({ url: '/api/detection_engine/rules/_find' }).then(({ body }) => {
const numberOfRules = body.data.length;
expect(numberOfRules).to.eql(initialNumberOfRules);
});

deleteFirstRule();
waitForRulesTableToBeRefreshed();

cy.get(RULES_TABLE)
.find(RULES_ROW)
.should('have.length', expectedNumberOfRulesAfterDeletion);
cy.get(SHOWING_RULES_TEXT).should(
'have.text',
`Showing ${expectedNumberOfRulesAfterDeletion} rules`
);
cy.request({ url: '/api/detection_engine/rules/_find' }).then(({ body }) => {
const numberOfRules = body.data.length;
expect(numberOfRules).to.eql(expectedNumberOfRulesAfterDeletion);
});
cy.get(CUSTOM_RULES_BTN).should(
'have.text',
`Custom rules (${expectedNumberOfRulesAfterDeletion})`
Expand All @@ -253,10 +255,10 @@ describe('Custom query rules', () => {
cy.get(RULES_TABLE)
.find(RULES_ROW)
.should('have.length', expectedNumberOfRulesAfterDeletion);
cy.get(SHOWING_RULES_TEXT).should(
'have.text',
`Showing ${expectedNumberOfRulesAfterDeletion} rule`
);
cy.request({ url: '/api/detection_engine/rules/_find' }).then(({ body }) => {
const numberOfRules = body.data.length;
expect(numberOfRules).to.eql(expectedNumberOfRulesAfterDeletion);
});
cy.get(CUSTOM_RULES_BTN).should(
'have.text',
`Custom rules (${expectedNumberOfRulesAfterDeletion})`
Expand All @@ -281,10 +283,10 @@ describe('Custom query rules', () => {
cy.get(RULES_TABLE)
.find(RULES_ROW)
.should('have.length', expectedNumberOfRulesAfterDeletion);
cy.get(SHOWING_RULES_TEXT).should(
'have.text',
`Showing ${expectedNumberOfRulesAfterDeletion} rules`
);
cy.request({ url: '/api/detection_engine/rules/_find' }).then(({ body }) => {
const numberOfRules = body.data.length;
expect(numberOfRules).to.eql(expectedNumberOfRulesAfterDeletion);
});
cy.get(CUSTOM_RULES_BTN).should(
'have.text',
`Custom rules (${expectedNumberOfRulesAfterDeletion})`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
* 2.0.
*/

import { rawRules } from '../../../server/lib/detection_engine/rules/prepackaged_rules';
import {
COLLAPSED_ACTION_BTN,
ELASTIC_RULES_BTN,
pageSelector,
RELOAD_PREBUILT_RULES_BTN,
RULES_EMPTY_PROMPT,
RULE_SWITCH,
SHOWING_RULES_TEXT,
RULES_MONITORING_TABLE,
SELECT_ALL_RULES_ON_PAGE_CHECKBOX,
RULE_NAME,
} from '../../screens/alerts_detection_rules';

import {
deleteFirstRule,
deleteSelectedRules,
Expand Down Expand Up @@ -59,7 +59,16 @@ describe('Prebuilt rules', () => {

changeRowsPerPageTo(rowsPerPage);

cy.get(SHOWING_RULES_TEXT).should('have.text', `Showing ${expectedNumberOfRules} rules`);
cy.request({ url: '/api/detection_engine/rules/_find' }).then(({ body }) => {
// Assert the total number of loaded rules equals the expected number of in-memory rules
expect(body.total).to.equal(rawRules.length);
// Assert the table was refreshed with the rules returned by the API request
const ruleNames = rawRules.map((rule) => rule.name);
cy.get(RULE_NAME).each(($item) => {
expect($item.text()).to.be.oneOf(ruleNames);
});
});

cy.get(pageSelector(expectedNumberOfPages)).should('exist');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { useFormatUrl } from '../../../../../../common/components/link_to';
import { Loader } from '../../../../../../common/components/loader';

import * as i18n from './translations';
import { AllRulesUtilityBar } from '../utility_bar';
import { ExceptionsTableUtilityBar } from './exceptions_table_utility_bar';
import type { AllExceptionListsColumns } from './columns';
import { getAllExceptionListsColumns } from './columns';
import { useAllExceptionLists } from './use_all_exception_lists';
Expand Down Expand Up @@ -378,11 +378,8 @@ export const ExceptionListsTable = React.memo(() => {
<EuiLoadingContent data-test-subj="initialLoadingPanelAllRulesTable" lines={10} />
) : (
<>
<AllRulesUtilityBar
hasBulkActions={false}
canBulkEdit={hasPermissions}
paginationTotal={exceptionListsWithRuleRefs.length ?? 0}
numberSelectedItems={0}
<ExceptionsTableUtilityBar
totalExceptionLists={exceptionListsWithRuleRefs.length}
onRefresh={handleRefresh}
/>
<EuiBasicTable<ExceptionsTableItem>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 { TestProviders } from '../../../../../../common/mock';
import { render, screen, within } from '@testing-library/react';
import { ExceptionsTableUtilityBar } from './exceptions_table_utility_bar';

describe('ExceptionsTableUtilityBar', () => {
it('displays correct exception lists label and refresh rules action button', () => {
const EXCEPTION_LISTS_NUMBER = 25;
render(
<TestProviders>
<ExceptionsTableUtilityBar
onRefresh={jest.fn()}
totalExceptionLists={EXCEPTION_LISTS_NUMBER}
/>
</TestProviders>
);

expect(screen.getByTestId('showingExceptionLists')).toBeInTheDocument();
expect(screen.getByTestId('refreshRulesAction')).toBeInTheDocument();
expect(screen.getByText(`Showing ${EXCEPTION_LISTS_NUMBER} lists`)).toBeInTheDocument();
});

it('invokes refresh on refresh action click', () => {
const mockRefresh = jest.fn();
render(
<TestProviders>
<ExceptionsTableUtilityBar onRefresh={mockRefresh} totalExceptionLists={1} />
</TestProviders>
);

const buttonWrapper = screen.getByTestId('refreshRulesAction');
within(buttonWrapper).getByRole('button').click();

expect(mockRefresh).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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 {
UtilityBar,
UtilityBarAction,
UtilityBarGroup,
UtilityBarSection,
UtilityBarText,
} from '../../../../../../common/components/utility_bar';
import * as i18n from './translations';

interface ExceptionsTableUtilityBarProps {
onRefresh?: () => void;
totalExceptionLists: number;
}

export const ExceptionsTableUtilityBar: React.FC<ExceptionsTableUtilityBarProps> = ({
onRefresh,
totalExceptionLists,
}) => {
return (
<UtilityBar border>
<UtilityBarSection>
<UtilityBarGroup>
<UtilityBarText dataTestSubj="showingExceptionLists">
{i18n.SHOWING_EXCEPTION_LISTS(totalExceptionLists)}
</UtilityBarText>
</UtilityBarGroup>
<UtilityBarGroup>
<UtilityBarAction
dataTestSubj="refreshRulesAction"
iconSide="left"
iconType="refresh"
onClick={onRefresh}
>
{i18n.REFRESH_EXCEPTIONS_TABLE}
</UtilityBarAction>
</UtilityBarGroup>
</UtilityBarSection>
</UtilityBar>
);
};

ExceptionsTableUtilityBar.displayName = 'ExceptionsTableUtilityBar';
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ export const EXCEPTION_LIST_ACTIONS = i18n.translate(
}
);

export const SHOWING_EXCEPTION_LISTS = (totalLists: number) =>
i18n.translate(
'xpack.securitySolution.detectionEngine.rules.all.exceptions.showingExceptionLists',
{
values: { totalLists },
defaultMessage: 'Showing {totalLists} {totalLists, plural, =1 {list} other {lists}}',
}
);

export const RULES_ASSIGNED_TO_TITLE = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.all.exceptions.rulesAssignedTitle',
{
Expand Down Expand Up @@ -151,3 +160,10 @@ export const EXCEPTION_LIST_SEARCH_PLACEHOLDER = i18n.translate(
defaultMessage: 'e.g. Example List Name',
}
);

export const REFRESH_EXCEPTIONS_TABLE = i18n.translate(
'xpack.securitySolution.detectionEngine.rules.all.exceptions.refresh',
{
defaultMessage: 'Refresh',
}
);
Loading

0 comments on commit a270c7c

Please sign in to comment.