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

[SOM] use aggregation for type counts #168918

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
@@ -0,0 +1,45 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { estypes } from '@elastic/elasticsearch';
import type { SavedObjectsFindOptions } from '@kbn/core-saved-objects-api-server';
import type { SavedObjectsClientContract } from '@kbn/core/server';

export const getSavedObjectCounts = async ({
types,
options,
client,
}: {
types: string[];
options: SavedObjectsFindOptions;
client: SavedObjectsClientContract;
}): Promise<Record<string, number>> => {
const body = await client.find<void, { types: estypes.AggregationsStringTermsAggregate }>({
...options,
type: types,
perPage: 0,
aggs: {
types: {
terms: {
field: 'type',
size: types.length,
},
},
},
});

const buckets =
(body.aggregations?.types?.buckets as estypes.AggregationsStringTermsBucketKeys[]) || [];

const counts = buckets.reduce((memo, bucket) => {
memo[bucket.key] = bucket.doc_count;
return memo;
}, {} as Record<string, number>);
Comment on lines +39 to +42
Copy link
Contributor Author

@pgayvallet pgayvallet Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have API integration FTR tests for this endpoint:

describe('scroll_count', () => {
describe('with less than 10k objects', () => {


return counts;
};
1 change: 1 addition & 0 deletions src/plugins/saved_objects_management/server/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
export { toSavedObjectWithMeta } from './to_saved_object_with_meta';
export { injectMetaAttributes } from './inject_meta_attributes';
export { findRelationships } from './find_relationships';
export { getSavedObjectCounts } from './get_saved_objects_counts';
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { schema } from '@kbn/config-schema';
import type { IRouter, SavedObjectsCreatePointInTimeFinderOptions } from '@kbn/core/server';
import { chain } from 'lodash';
import type { v1 } from '../../common';
import { getSavedObjectCounts } from '../lib';

export const registerScrollForCountRoute = (router: IRouter) => {
router.post(
Expand Down Expand Up @@ -44,27 +45,19 @@ export const registerScrollForCountRoute = (router: IRouter) => {
const client = getClient({ includedHiddenTypes });
const findOptions: SavedObjectsCreatePointInTimeFinderOptions = {
type: typesToInclude,
perPage: 500,
...(searchString ? { search: `${searchString}*`, searchFields: ['title'] } : {}),
...(references ? { hasReference: references, hasReferenceOperator: 'OR' } : {}),
};
if (searchString) {
findOptions.search = `${searchString}*`;
findOptions.searchFields = ['title'];
}
if (references) {
findOptions.hasReference = references;
findOptions.hasReferenceOperator = 'OR';
}

const rawCounts = await getSavedObjectCounts({
types: typesToInclude,
client,
options: findOptions,
});

const counts: Record<string, number> = {};
for (const type of typesToInclude) {
counts[type] = 0;
}

const finder = client.createPointInTimeFinder(findOptions);
for await (const { saved_objects: savedObjects } of finder.find()) {
for (const { type } of savedObjects) {
counts[type]++;
}
counts[type] = rawCounts[type] ?? 0;
}

const body: v1.ScrollCountResponseHTTP = counts;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/saved_objects_management/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@kbn/shared-ux-router",
"@kbn/core-ui-settings-browser-mocks",
"@kbn/core-ui-settings-browser",
"@kbn/core-saved-objects-api-server",
],
"exclude": [
"target/**/*",
Expand Down
Loading