Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rules migration] Add pagination functionality to rules migration table (#11313) #202494

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const SIEM_RULE_MIGRATION_PATH = `${SIEM_RULE_MIGRATIONS_PATH}/{migration
export const SIEM_RULE_MIGRATION_START_PATH = `${SIEM_RULE_MIGRATION_PATH}/start` as const;
export const SIEM_RULE_MIGRATION_RETRY_PATH = `${SIEM_RULE_MIGRATION_PATH}/retry` as const;
export const SIEM_RULE_MIGRATION_STATS_PATH = `${SIEM_RULE_MIGRATION_PATH}/stats` as const;
export const SIEM_RULE_MIGRATION_TRANSLATION_STATS_PATH =
`${SIEM_RULE_MIGRATION_PATH}/translation_stats` as const;
export const SIEM_RULE_MIGRATION_STOP_PATH = `${SIEM_RULE_MIGRATION_PATH}/stop` as const;
export const SIEM_RULE_MIGRATION_INSTALL_PATH = `${SIEM_RULE_MIGRATION_PATH}/install` as const;
export const SIEM_RULE_MIGRATION_INSTALL_TRANSLATED_PATH =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
RuleMigrationComments,
RuleMigrationTaskStats,
RuleMigration,
RuleMigrationTranslationStats,
RuleMigrationResourceData,
RuleMigrationResourceType,
RuleMigrationResource,
Expand All @@ -44,6 +45,13 @@ export const CreateRuleMigrationResponse = z.object({

export type GetAllStatsRuleMigrationResponse = z.infer<typeof GetAllStatsRuleMigrationResponse>;
export const GetAllStatsRuleMigrationResponse = z.array(RuleMigrationTaskStats);
export type GetRuleMigrationRequestQuery = z.infer<typeof GetRuleMigrationRequestQuery>;
export const GetRuleMigrationRequestQuery = z.object({
page: z.coerce.number().optional(),
per_page: z.coerce.number().optional(),
search_term: z.string().optional(),
});
export type GetRuleMigrationRequestQueryInput = z.input<typeof GetRuleMigrationRequestQuery>;

export type GetRuleMigrationRequestParams = z.infer<typeof GetRuleMigrationRequestParams>;
export const GetRuleMigrationRequestParams = z.object({
Expand All @@ -52,7 +60,13 @@ export const GetRuleMigrationRequestParams = z.object({
export type GetRuleMigrationRequestParamsInput = z.input<typeof GetRuleMigrationRequestParams>;

export type GetRuleMigrationResponse = z.infer<typeof GetRuleMigrationResponse>;
export const GetRuleMigrationResponse = z.array(RuleMigration);
export const GetRuleMigrationResponse = z.object({
/**
* The total number of rules in migration.
*/
total: z.number(),
data: z.array(RuleMigration),
});
export type GetRuleMigrationResourcesRequestQuery = z.infer<
typeof GetRuleMigrationResourcesRequestQuery
>;
Expand Down Expand Up @@ -88,6 +102,21 @@ export type GetRuleMigrationStatsRequestParamsInput = z.input<
export type GetRuleMigrationStatsResponse = z.infer<typeof GetRuleMigrationStatsResponse>;
export const GetRuleMigrationStatsResponse = RuleMigrationTaskStats;

export type GetRuleMigrationTranslationStatsRequestParams = z.infer<
typeof GetRuleMigrationTranslationStatsRequestParams
>;
export const GetRuleMigrationTranslationStatsRequestParams = z.object({
migration_id: NonEmptyString,
});
export type GetRuleMigrationTranslationStatsRequestParamsInput = z.input<
typeof GetRuleMigrationTranslationStatsRequestParams
>;

export type GetRuleMigrationTranslationStatsResponse = z.infer<
typeof GetRuleMigrationTranslationStatsResponse
>;
export const GetRuleMigrationTranslationStatsResponse = RuleMigrationTranslationStats;

export type InstallMigrationRulesRequestParams = z.infer<typeof InstallMigrationRulesRequestParams>;
export const InstallMigrationRulesRequestParams = z.object({
migration_id: NonEmptyString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,40 @@ paths:
schema:
description: The migration id to start
$ref: '../../common.schema.yaml#/components/schemas/NonEmptyString'
- name: page
in: query
required: false
schema:
type: number
- name: per_page
in: query
required: false
schema:
type: number
- name: search_term
in: query
required: false
schema:
type: string

responses:
200:
description: Indicates rule migration have been retrieved correctly.
content:
application/json:
schema:
type: array
items:
$ref: '../../rule_migration.schema.yaml#/components/schemas/RuleMigration'
type: object
required:
- total
- data
properties:
total:
type: number
description: The total number of rules in migration.
data:
type: array
items:
$ref: '../../rule_migration.schema.yaml#/components/schemas/RuleMigration'
204:
description: Indicates the migration id was not found.

Expand Down Expand Up @@ -256,7 +281,7 @@ paths:
in: path
required: true
schema:
description: The migration id to start
description: The migration id to fetch stats for
$ref: '../../common.schema.yaml#/components/schemas/NonEmptyString'
responses:
200:
Expand All @@ -268,6 +293,31 @@ paths:
204:
description: Indicates the migration id was not found.

/internal/siem_migrations/rules/{migration_id}/translation_stats:
get:
summary: Gets a rule migration translation stats
operationId: GetRuleMigrationTranslationStats
x-codegen-enabled: true
description: Retrieves the translation stats of a SIEM rules migration using the migration id provided
tags:
- SIEM Rule Migrations
parameters:
- name: migration_id
in: path
required: true
schema:
description: The migration id to fetch translation stats for
$ref: '../../common.schema.yaml#/components/schemas/NonEmptyString'
responses:
200:
description: Indicates the migration stats has been retrieved correctly.
content:
application/json:
schema:
$ref: '../../rule_migration.schema.yaml#/components/schemas/RuleMigrationTranslationStats'
204:
description: Indicates the migration id was not found.

/internal/siem_migrations/rules/{migration_id}/stop:
put:
summary: Stops an existing rule migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,38 @@ export const RuleMigrationTaskStats = z.object({
last_updated_at: z.string(),
});

/**
* The rule migration translation stats object.
*/
export type RuleMigrationTranslationStats = z.infer<typeof RuleMigrationTranslationStats>;
export const RuleMigrationTranslationStats = z.object({
/**
* The migration id
*/
id: NonEmptyString,
/**
* The rules migration translation stats.
*/
rules: z.object({
/**
* The total number of rules to migrate.
*/
total: z.number().int(),
/**
* The number of rules that matched Elastic prebuilt rules.
*/
prebuilt: z.number().int(),
/**
* The number of rules that did not match Elastic prebuilt rules and will be installed as custom rules.
*/
custom: z.number().int(),
/**
* The number of rules that can be installed.
*/
installable: z.number().int(),
}),
});

/**
* The type of the rule migration resource.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,39 @@ components:
- running
- stopped
- finished


RuleMigrationTranslationStats:
type: object
description: The rule migration translation stats object.
required:
- id
- rules
properties:
id:
description: The migration id
$ref: './common.schema.yaml#/components/schemas/NonEmptyString'
rules:
type: object
description: The rules migration translation stats.
required:
- total
- prebuilt
- custom
- installable
properties:
total:
type: integer
description: The total number of rules to migrate.
prebuilt:
type: integer
description: The number of rules that matched Elastic prebuilt rules.
custom:
type: integer
description: The number of rules that did not match Elastic prebuilt rules and will be installed as custom rules.
installable:
type: integer
description: The number of rules that can be installed.

RuleMigrationTranslationResult:
type: string
description: The rule translation result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import {
SIEM_RULE_MIGRATION_INSTALL_PATH,
SIEM_RULE_MIGRATION_PATH,
SIEM_RULE_MIGRATION_START_PATH,
SIEM_RULE_MIGRATION_TRANSLATION_STATS_PATH,
} from '../../../../common/siem_migrations/constants';
import type {
GetAllStatsRuleMigrationResponse,
GetRuleMigrationResponse,
GetRuleMigrationTranslationStatsResponse,
InstallTranslatedMigrationRulesResponse,
InstallMigrationRulesResponse,
StartRuleMigrationRequestBody,
Expand Down Expand Up @@ -67,6 +69,31 @@ export const startRuleMigration = async ({
);
};

/**
* Retrieves the translation stats for the migraion.
*
* @param migrationId `id` of the migration to retrieve translation stats for
* @param signal AbortSignal for cancelling request
*
* @throws An error if response is not OK
*/
export const getRuleMigrationTranslationStats = async ({
migrationId,
signal,
}: {
migrationId: string;
signal: AbortSignal | undefined;
}): Promise<GetRuleMigrationTranslationStatsResponse> => {
return KibanaServices.get().http.fetch<GetRuleMigrationTranslationStatsResponse>(
replaceParams(SIEM_RULE_MIGRATION_TRANSLATION_STATS_PATH, { migration_id: migrationId }),
{
method: 'GET',
version: '1',
signal,
}
);
};

/**
* Retrieves all the migration rule documents of a specific migration.
*
Expand All @@ -77,14 +104,29 @@ export const startRuleMigration = async ({
*/
export const getRuleMigrations = async ({
migrationId,
page,
perPage,
searchTerm,
signal,
}: {
migrationId: string;
page?: number;
perPage?: number;
searchTerm?: string;
signal: AbortSignal | undefined;
}): Promise<GetRuleMigrationResponse> => {
return KibanaServices.get().http.fetch<GetRuleMigrationResponse>(
replaceParams(SIEM_RULE_MIGRATION_PATH, { migration_id: migrationId }),
{ method: 'GET', version: '1', signal }
{
method: 'GET',
version: '1',
query: {
page,
per_page: perPage,
search_term: searchTerm,
},
signal,
}
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,46 @@ import type { UseQueryOptions } from '@tanstack/react-query';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { replaceParams } from '@kbn/openapi-common/shared';
import { useCallback } from 'react';
import type { RuleMigration } from '../../../../../common/siem_migrations/model/rule_migration.gen';
import { DEFAULT_QUERY_OPTIONS } from './constants';
import { getRuleMigrations } from '../api';
import type { GetRuleMigrationResponse } from '../../../../../common/siem_migrations/model/api/rules/rule_migration.gen';
import { SIEM_RULE_MIGRATION_PATH } from '../../../../../common/siem_migrations/constants';

interface UseGetMigrationRulesQueryProps {
migrationId: string;
page?: number;
perPage?: number;
searchTerm?: string;
}

export interface MigrationRulesQueryResponse {
ruleMigrations: RuleMigration[];
total: number;
}

export const useGetMigrationRulesQuery = (
migrationId: string,
options?: UseQueryOptions<GetRuleMigrationResponse>
queryArgs: UseGetMigrationRulesQueryProps,
queryOptions?: UseQueryOptions<
MigrationRulesQueryResponse,
Error,
MigrationRulesQueryResponse,
[...string[], UseGetMigrationRulesQueryProps]
>
) => {
const { migrationId } = queryArgs;
const SPECIFIC_MIGRATION_PATH = replaceParams(SIEM_RULE_MIGRATION_PATH, {
migration_id: migrationId,
});
return useQuery<GetRuleMigrationResponse>(
['GET', SPECIFIC_MIGRATION_PATH],
return useQuery(
['GET', SPECIFIC_MIGRATION_PATH, queryArgs],
async ({ signal }) => {
return getRuleMigrations({ migrationId, signal });
const response = await getRuleMigrations({ signal, ...queryArgs });

return { ruleMigrations: response.data, total: response.total };
},
{
...DEFAULT_QUERY_OPTIONS,
...options,
...queryOptions,
}
);
};
Expand All @@ -47,6 +67,10 @@ export const useInvalidateGetMigrationRulesQuery = (migrationId: string) => {
});

return useCallback(() => {
/**
* Invalidate all queries that start with SPECIFIC_MIGRATION_PATH. This
* includes the in-memory query cache and paged query cache.
*/
queryClient.invalidateQueries(['GET', SPECIFIC_MIGRATION_PATH], {
refetchType: 'active',
});
Expand Down
Loading