-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Cloud Posture] Support pagination in benchmarks page #128486
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * 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 { type TypeOf, schema } from '@kbn/config-schema'; | ||
|
|
||
| export const DEFAULT_BENCHMARKS_PER_PAGE = 20; | ||
| export const BENCHMARK_PACKAGE_POLICY_PREFIX = 'package_policy.'; | ||
| export const benchmarksInputSchema = schema.object({ | ||
| /** | ||
| * The page of objects to return | ||
| */ | ||
| page: schema.number({ defaultValue: 1, min: 1 }), | ||
| /** | ||
| * The number of objects to include in each page | ||
| */ | ||
| per_page: schema.number({ defaultValue: DEFAULT_BENCHMARKS_PER_PAGE, min: 0 }), | ||
| /** | ||
| * Once of PackagePolicy fields for sorting the found objects. | ||
| * Sortable fields: | ||
| * - package_policy.id | ||
| * - package_policy.name | ||
| * - package_policy.policy_id | ||
| * - package_policy.namespace | ||
| * - package_policy.updated_at | ||
| * - package_policy.updated_by | ||
| * - package_policy.created_at | ||
| * - package_policy.created_by, | ||
| * - package_policy.package.name | ||
| * - package_policy.package.title | ||
| * - package_policy.package.version | ||
| */ | ||
| sort_field: schema.oneOf( | ||
| [ | ||
| schema.literal('package_policy.id'), | ||
| schema.literal('package_policy.name'), | ||
| schema.literal('package_policy.policy_id'), | ||
| schema.literal('package_policy.namespace'), | ||
| schema.literal('package_policy.updated_at'), | ||
| schema.literal('package_policy.updated_by'), | ||
| schema.literal('package_policy.created_at'), | ||
| schema.literal('package_policy.created_by'), | ||
| schema.literal('package_policy.package.name'), | ||
| schema.literal('package_policy.package.title'), | ||
| ], | ||
| { defaultValue: 'package_policy.name' } | ||
| ), | ||
| /** | ||
| * The order to sort by | ||
| */ | ||
| sort_order: schema.oneOf([schema.literal('asc'), schema.literal('desc')], { | ||
| defaultValue: 'asc', | ||
| }), | ||
| /** | ||
| * Benchmark filter | ||
| */ | ||
| benchmark_name: schema.maybe(schema.string()), | ||
| }); | ||
|
|
||
| export type BenchmarksQuerySchema = TypeOf<typeof benchmarksInputSchema>; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,10 @@ import { useCISIntegrationLink } from '../../common/navigation/use_navigate_to_c | |
| import { CspPageTemplate } from '../../components/page_template'; | ||
| import { BenchmarksTable } from './benchmarks_table'; | ||
| import { ADD_A_CIS_INTEGRATION, BENCHMARK_INTEGRATIONS } from './translations'; | ||
| import { useCspBenchmarkIntegrations } from './use_csp_benchmark_integrations'; | ||
| import { | ||
| useCspBenchmarkIntegrations, | ||
| UseCspBenchmarkIntegrationsProps, | ||
| } from './use_csp_benchmark_integrations'; | ||
| import { extractErrorMessage } from '../../../common/utils/helpers'; | ||
| import { SEARCH_PLACEHOLDER } from './translations'; | ||
|
|
||
|
|
@@ -118,7 +121,13 @@ const PAGE_HEADER: EuiPageHeaderProps = { | |
| }; | ||
|
|
||
| export const Benchmarks = () => { | ||
| const [query, setQuery] = useState({ name: '', page: 1, perPage: 5 }); | ||
| const [query, setQuery] = useState<UseCspBenchmarkIntegrationsProps>({ | ||
| name: '', | ||
| page: 1, | ||
| perPage: 5, | ||
| sortField: 'package_policy.name', | ||
| sortOrder: 'asc', | ||
| }); | ||
|
|
||
| const queryResult = useCspBenchmarkIntegrations(query); | ||
|
|
||
|
|
@@ -129,7 +138,7 @@ export const Benchmarks = () => { | |
| return ( | ||
| <CspPageTemplate pageHeader={PAGE_HEADER}> | ||
| <BenchmarkSearchField | ||
| isLoading={queryResult.isLoading} | ||
| isLoading={queryResult.isFetching} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this preferred over
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When sorting the data, |
||
| onSearch={(name) => setQuery((current) => ({ ...current, name }))} | ||
| /> | ||
| <EuiSpacer /> | ||
|
|
@@ -142,13 +151,25 @@ export const Benchmarks = () => { | |
| benchmarks={queryResult.data?.items || []} | ||
| data-test-subj={BENCHMARKS_TABLE_DATA_TEST_SUBJ} | ||
| error={queryResult.error ? extractErrorMessage(queryResult.error) : undefined} | ||
| loading={queryResult.isLoading} | ||
| loading={queryResult.isFetching} | ||
| pageIndex={query.page} | ||
| pageSize={query.perPage} | ||
| sorting={{ | ||
| // @ts-expect-error - EUI types currently do not support sorting by nested fields | ||
| sort: { field: query.sortField, direction: query.sortOrder }, | ||
| allowNeutralSort: false, | ||
| }} | ||
| totalItemCount={totalItemCount} | ||
| setQuery={({ page }) => | ||
| setQuery((current) => ({ ...current, page: page.index, perPage: page.size })) | ||
| } | ||
| setQuery={({ page, sort }) => { | ||
| setQuery((current) => ({ | ||
| ...current, | ||
| page: page.index, | ||
| perPage: page.size, | ||
| sortField: | ||
| (sort?.field as UseCspBenchmarkIntegrationsProps['sortField']) || current.sortField, | ||
| sortOrder: sort?.direction || current.sortOrder, | ||
| })); | ||
| }} | ||
| noItemsMessage={ | ||
| queryResult.isSuccess && !queryResult.data.total ? ( | ||
| <BenchmarkEmptyState name={query.name} /> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think we support every field in
PackagePolicy(as we don't own it anyway)and maybe we should just take what's currently applicable by the UI. as in - just the column headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we technically support every field in that object type, but better be explicit in the docs and schema. Regarding using only what's in the UI - IMO its better to leave the API more flexible for the future