Skip to content

Commit 96a8c27

Browse files
committed
add rule changes history fetching logic
1 parent a0faaba commit 96a8c27

4 files changed

Lines changed: 93 additions & 2 deletions

File tree

x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/api/api.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ import type {
5454
GetRuleManagementFiltersResponse,
5555
ImportRulesResponse,
5656
BulkManualRuleFillGaps,
57+
RuleChangesHistoryResponse,
5758
} from '../../../../common/api/detection_engine/rule_management';
5859
import {
5960
BulkActionTypeEnum,
61+
RULE_HISTORY_URL,
6062
RULE_MANAGEMENT_COVERAGE_OVERVIEW_URL,
6163
RULE_MANAGEMENT_FILTERS_URL,
6264
RULE_MANAGEMENT_RULES_URL_SEARCH,
@@ -81,6 +83,7 @@ import type {
8183
CreateRulesProps,
8284
ExportDocumentsProps,
8385
FetchCoverageOverviewProps,
86+
FetchRuleHistoryProps,
8487
FetchRuleProps,
8588
FetchRuleSnoozingProps,
8689
FetchSearchRulesProps,
@@ -316,6 +319,32 @@ export const fetchRuleById = async ({ id, signal }: FetchRuleProps): Promise<Rul
316319
signal,
317320
});
318321

322+
/**
323+
* Fetch the change history for a Rule.
324+
*
325+
* @param ruleId Rule SO id (not `rule_id`)
326+
* @param page 1-based page number
327+
* @param perPage items per page
328+
* @param signal to cancel request
329+
*
330+
* @returns Promise<RuleChangesHistoryResponse>
331+
*/
332+
export const fetchRuleChangeHistoryById = async ({
333+
ruleId,
334+
page,
335+
perPage,
336+
signal,
337+
}: FetchRuleHistoryProps): Promise<RuleChangesHistoryResponse> =>
338+
KibanaServices.get().http.fetch<RuleChangesHistoryResponse>(
339+
RULE_HISTORY_URL.replace('{ruleId}', encodeURIComponent(ruleId)),
340+
{
341+
method: 'GET',
342+
version: '1',
343+
query: { page, per_page: perPage },
344+
signal,
345+
}
346+
);
347+
319348
/**
320349
* Fetch rule snooze settings for each provided ruleId
321350
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 type { UseQueryOptions } from '@kbn/react-query';
9+
import { useQuery } from '@kbn/react-query';
10+
import type { RuleChangesHistoryResponse } from '../../../../../common/api/detection_engine/rule_management';
11+
import { RULE_HISTORY_URL } from '../../../../../common/api/detection_engine/rule_management';
12+
import { useAppToasts } from '../../../../common/hooks/use_app_toasts';
13+
import { fetchRuleChangeHistoryById } from '../api';
14+
import { DEFAULT_QUERY_OPTIONS } from './constants';
15+
import * as i18n from './translations';
16+
17+
const RULE_CHANGE_HISTORY_QUERY_KEY = ['GET', RULE_HISTORY_URL];
18+
19+
export interface UseChangeHistoryArgs {
20+
ruleId: string;
21+
page: number;
22+
perPage: number;
23+
}
24+
25+
/**
26+
* A wrapper around useQuery that fetches the change history for a rule.
27+
*
28+
* @param queryArgs - rule id and pagination
29+
* @param queryOptions - react-query options
30+
* @returns useQuery result
31+
*/
32+
export const useChangeHistory = (
33+
queryArgs: UseChangeHistoryArgs,
34+
queryOptions?: UseQueryOptions<
35+
RuleChangesHistoryResponse,
36+
Error,
37+
RuleChangesHistoryResponse,
38+
[...string[], UseChangeHistoryArgs]
39+
>
40+
) => {
41+
const { addError } = useAppToasts();
42+
43+
return useQuery(
44+
[...RULE_CHANGE_HISTORY_QUERY_KEY, queryArgs],
45+
({ signal }) => fetchRuleChangeHistoryById({ signal, ...queryArgs }),
46+
{
47+
...DEFAULT_QUERY_OPTIONS,
48+
staleTime: 0,
49+
...queryOptions,
50+
onError: (error) => {
51+
addError(error, { title: i18n.HISTORY_FETCH_ERROR });
52+
},
53+
}
54+
);
55+
};

x-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/logic/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ export interface FetchRuleProps {
143143
signal?: AbortSignal;
144144
}
145145

146+
export interface FetchRuleHistoryProps {
147+
ruleId: string;
148+
page: number;
149+
perPage: number;
150+
signal?: AbortSignal;
151+
}
152+
146153
export interface FetchRuleSnoozingProps {
147154
ids: string[];
148155
signal?: AbortSignal;

x-pack/solutions/security/plugins/security_solution/public/rules/routes.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ const getRulesSubRoutes = (
114114
? [
115115
{
116116
path: endpointExceptionsTabEnabled
117-
? `/rules/id/:detailName/:tabName(${RuleDetailTabs.overview}|${RuleDetailTabs.alerts}|${RuleDetailTabs.exceptions}|${RuleDetailTabs.endpointExceptions}|${RuleDetailTabs.executionResults})`
118-
: `/rules/id/:detailName/:tabName(${RuleDetailTabs.overview}|${RuleDetailTabs.alerts}|${RuleDetailTabs.exceptions}|${RuleDetailTabs.executionResults})`,
117+
? `/rules/id/:detailName/:tabName(${RuleDetailTabs.overview}|${RuleDetailTabs.alerts}|${RuleDetailTabs.exceptions}|${RuleDetailTabs.endpointExceptions}|${RuleDetailTabs.executionResults}|${RuleDetailTabs.history})`
118+
: `/rules/id/:detailName/:tabName(${RuleDetailTabs.overview}|${RuleDetailTabs.alerts}|${RuleDetailTabs.exceptions}|${RuleDetailTabs.executionResults}|${RuleDetailTabs.history})`,
119119
main: RuleDetailsTabGuard,
120120
exact: true,
121121
},

0 commit comments

Comments
 (0)