Skip to content

Commit

Permalink
[SOM] use aggregation for type counts (#168918)
Browse files Browse the repository at this point in the history
## Summary

Use an aggregation for type counts in SO management instead of scrolling
through all the documents.

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
pgayvallet and kibanamachine authored Oct 17, 2023
1 parent 4113d25 commit 0c1f828
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
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>);

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';
27 changes: 10 additions & 17 deletions src/plugins/saved_objects_management/server/routes/scroll_count.ts
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

0 comments on commit 0c1f828

Please sign in to comment.