Skip to content

Commit

Permalink
[ResponseOps][Rules][Alerts] Link stack rule details page refresh but…
Browse files Browse the repository at this point in the history
…ton to alerts table (elastic#195736)

## Summary

Forwards the reload request from the Stack Management rule detail page
`🔄 Refresh` button to the alerts table.

## To verify

1. Create a Stack rule that fires alerts
2. Visit that rule's detail page
3. Check that the `🔄 Refresh` button correctly refreshes the alerts
table (i.e. by changing the rule params and checking that the alerts
update accordingly)

## References

Fixes elastic#188405, elastic#183155
  • Loading branch information
umbopepato authored Oct 17, 2024
1 parent f25b3be commit 6a0ad10
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 6a0ad10

Please sign in to comment.