Skip to content

Commit

Permalink
[8.15] [ResponseOps][Rules][Alerts] Link stack rule details page refr…
Browse files Browse the repository at this point in the history
…esh button to alerts table (#195736) (#196652)

# Backport

This will backport the following commits from `main` to `8.15`:
- [[ResponseOps][Rules][Alerts] Link stack rule details page refresh
button to alerts table
(#195736)](#195736)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Umberto
Pepato","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-17T09:46:29Z","message":"[ResponseOps][Rules][Alerts]
Link stack rule details page refresh button to alerts table
(#195736)\n\n## Summary\r\n\r\nForwards the reload request from the
Stack Management rule detail page\r\n`🔄 Refresh` button to the alerts
table.\r\n\r\n## To verify\r\n\r\n1. Create a Stack rule that fires
alerts\r\n2. Visit that rule's detail page\r\n3. Check that the `🔄
Refresh` button correctly refreshes the alerts\r\ntable (i.e. by
changing the rule params and checking that the alerts\r\nupdate
accordingly)\r\n\r\n## References\r\n\r\nFixes #188405,
#183155","sha":"6a0ad109087fd8b9efe9198a6df23f4658929c05","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Team:ResponseOps","v9.0.0","backport:prev-major","v8.16.0","v8.15.3"],"title":"[ResponseOps][Rules][Alerts]
Link stack rule details page refresh button to alerts
table","number":195736,"url":"https://github.com/elastic/kibana/pull/195736","mergeCommit":{"message":"[ResponseOps][Rules][Alerts]
Link stack rule details page refresh button to alerts table
(#195736)\n\n## Summary\r\n\r\nForwards the reload request from the
Stack Management rule detail page\r\n`🔄 Refresh` button to the alerts
table.\r\n\r\n## To verify\r\n\r\n1. Create a Stack rule that fires
alerts\r\n2. Visit that rule's detail page\r\n3. Check that the `🔄
Refresh` button correctly refreshes the alerts\r\ntable (i.e. by
changing the rule params and checking that the alerts\r\nupdate
accordingly)\r\n\r\n## References\r\n\r\nFixes #188405,
#183155","sha":"6a0ad109087fd8b9efe9198a6df23f4658929c05"}},"sourceBranch":"main","suggestedTargetBranches":["8.16","8.15"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/195736","number":195736,"mergeCommit":{"message":"[ResponseOps][Rules][Alerts]
Link stack rule details page refresh button to alerts table
(#195736)\n\n## Summary\r\n\r\nForwards the reload request from the
Stack Management rule detail page\r\n`🔄 Refresh` button to the alerts
table.\r\n\r\n## To verify\r\n\r\n1. Create a Stack rule that fires
alerts\r\n2. Visit that rule's detail page\r\n3. Check that the `🔄
Refresh` button correctly refreshes the alerts\r\ntable (i.e. by
changing the rule params and checking that the alerts\r\nupdate
accordingly)\r\n\r\n## References\r\n\r\nFixes #188405,
#183155","sha":"6a0ad109087fd8b9efe9198a6df23f4658929c05"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.15","label":"v8.15.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Umberto Pepato <[email protected]>
  • Loading branch information
kibanamachine and umbopepato authored Oct 17, 2024
1 parent 78986c3 commit 75f97f9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

import * as React from 'react';
import { Suspense } from 'react';
import { shallow } from 'enzyme';
import { waitFor } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { mountWithIntl, nextTick } from '@kbn/test-jest-helpers';
import { act } from 'react-dom/test-utils';
Expand All @@ -21,6 +23,10 @@ import { ruleTypeRegistryMock } from '../../../rule_type_registry.mock';
import { useKibana } from '../../../../common/lib/kibana';
import { useBulkGetMaintenanceWindows } from '../../alerts_table/hooks/use_bulk_get_maintenance_windows';
import { getMaintenanceWindowMockMap } from '../../alerts_table/maintenance_windows/index.mock';
import { loadRuleTypes } from '../../../lib/rule_api/rule_types';

jest.mock('../../../lib/rule_api/rule_types');
jest.mocked(loadRuleTypes).mockResolvedValue([]);

const mockUseKibanaReturnValue = createStartServicesMock();
jest.mock('../../../../common/lib/kibana', () => ({
Expand All @@ -37,6 +43,15 @@ jest.mock('../../../lib/rule_api/load_execution_log_aggregations', () => ({
loadExecutionLogAggregations: jest.fn(),
}));

const mockAlertsTable = jest.fn(() => {
return <div data-test-subj="alertsTable" />;
});
jest.mock('../../alerts_table/alerts_table_state', () => ({
__esModule: true,
AlertsTableState: mockAlertsTable,
default: mockAlertsTable,
}));

const { loadExecutionLogAggregations } = jest.requireMock(
'../../../lib/rule_api/load_execution_log_aggregations'
);
Expand All @@ -60,7 +75,6 @@ const useBulkGetMaintenanceWindowsMock = useBulkGetMaintenanceWindows as jest.Mo
const ruleTypeRegistry = ruleTypeRegistryMock.create();

import { getIsExperimentalFeatureEnabled } from '../../../../common/get_experimental_features';
import { waitFor } from '@testing-library/react';
import { createStartServicesMock } from '../../../../common/lib/kibana/kibana_react.mock';

const fakeNow = new Date('2020-02-09T23:15:41.941Z');
Expand Down Expand Up @@ -118,9 +132,11 @@ const queryClient = new QueryClient({

const RuleComponentWithProvider = (props: RuleComponentProps) => {
return (
<QueryClientProvider client={queryClient}>
<RuleComponent {...props} />
</QueryClientProvider>
<Suspense fallback={null}>
<QueryClientProvider client={queryClient}>
<RuleComponent {...props} />
</QueryClientProvider>
</Suspense>
);
};

Expand Down Expand Up @@ -277,6 +293,48 @@ describe('rules', () => {
alertToListItem(fakeNow.getTime(), 'us-east', ruleUsEast),
]);
});

it('requests a table refresh when the refresh token changes', async () => {
jest.useFakeTimers();
const rule = mockRule({
enabled: false,
});
const ruleType = mockRuleType({
hasFieldsForAAD: true,
});
const ruleSummary = mockRuleSummary();
jest.setSystemTime(fake2MinutesAgo);

const wrapper = mountWithIntl(
<RuleComponentWithProvider
{...mockAPIs}
rule={rule}
ruleType={ruleType}
ruleSummary={ruleSummary}
readOnly={false}
/>
);

await waitFor(() => wrapper.find('[data-test-subj="alertsTable"]'));

jest.setSystemTime(fakeNow);

wrapper.setProps({
refreshToken: {
resolve: () => undefined,
reject: () => undefined,
},
});

expect(mockAlertsTable).toHaveBeenCalledWith(
expect.objectContaining({
lastReloadRequestTime: fakeNow.getTime(),
}),
expect.anything()
);

jest.useRealTimers();
});
});

describe('alertToListItem', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { lazy, useCallback } from 'react';
import React, { lazy, useCallback, useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiSpacer, EuiFlexGroup, EuiFlexItem, EuiTabbedContent } from '@elastic/eui';
import { AlertStatusValues, ALERTING_FEATURE_ID } from '@kbn/alerting-plugin/common';
Expand Down Expand Up @@ -71,6 +71,9 @@ export function RuleComponent({
}: RuleComponentProps) {
const { ruleTypeRegistry, actionTypeRegistry, alertsTableConfigurationRegistry } =
useKibana().services;
// The lastReloadRequestTime should be updated when the refreshToken changes
// eslint-disable-next-line react-hooks/exhaustive-deps
const lastReloadRequestTime = useMemo(() => new Date().getTime(), [refreshToken]);

const alerts = Object.entries(ruleSummary.alerts)
.map(([alertId, alert]) => alertToListItem(durationEpoch, alertId, alert))
Expand Down Expand Up @@ -110,6 +113,7 @@ export function RuleComponent({
}
query={{ bool: { filter: { term: { [ALERT_RULE_UUID]: rule.id } } } }}
showAlertStatusWithFlapping
lastReloadRequestTime={lastReloadRequestTime}
/>
);
}
Expand All @@ -124,6 +128,7 @@ export function RuleComponent({
}, [
alerts,
alertsTableConfigurationRegistry,
lastReloadRequestTime,
onMuteAction,
readOnly,
rule.consumer,
Expand Down

0 comments on commit 75f97f9

Please sign in to comment.