-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Infra UI] Convert terms aggregation to composite for field selection…
… filtering (#47500) (#48675) * Convert from large terms agg to multiple smaller composite * Fixing bug in snapshot * Reducing duplicate code for composite agg requests * Adding filter for last 24 hours * Creating a helper for the afterKey handlers
- Loading branch information
1 parent
cfab2a9
commit 172e68d
Showing
6 changed files
with
172 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
x-pack/legacy/plugins/infra/server/utils/create_afterkey_handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { set } from 'lodash'; | ||
import { InfraDatabaseSearchResponse } from '../lib/adapters/framework'; | ||
|
||
export const createAfterKeyHandler = ( | ||
optionsAfterKeyPath: string | string[], | ||
afterKeySelector: (input: InfraDatabaseSearchResponse<any, any>) => any | ||
) => <Options>(options: Options, response: InfraDatabaseSearchResponse<any, any>): Options => { | ||
if (!response.aggregations) { | ||
return options; | ||
} | ||
const newOptions = { ...options }; | ||
const afterKey = afterKeySelector(response); | ||
set(newOptions, optionsAfterKeyPath, afterKey); | ||
return newOptions; | ||
}; |
54 changes: 54 additions & 0 deletions
54
x-pack/legacy/plugins/infra/server/utils/get_all_composite_data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { | ||
InfraBackendFrameworkAdapter, | ||
InfraFrameworkRequest, | ||
InfraDatabaseSearchResponse, | ||
} from '../lib/adapters/framework'; | ||
|
||
export const getAllCompositeData = async < | ||
Aggregation = undefined, | ||
Bucket = {}, | ||
Options extends object = {} | ||
>( | ||
framework: InfraBackendFrameworkAdapter, | ||
request: InfraFrameworkRequest, | ||
options: Options, | ||
bucketSelector: (response: InfraDatabaseSearchResponse<{}, Aggregation>) => Bucket[], | ||
onAfterKey: (options: Options, response: InfraDatabaseSearchResponse<{}, Aggregation>) => Options, | ||
previousBuckets: Bucket[] = [] | ||
): Promise<Bucket[]> => { | ||
const response = await framework.callWithRequest<{}, Aggregation>(request, 'search', options); | ||
|
||
// Nothing available, return the previous buckets. | ||
if (response.hits.total.value === 0) { | ||
return previousBuckets; | ||
} | ||
|
||
// if ES doesn't return an aggregations key, something went seriously wrong. | ||
if (!response.aggregations) { | ||
throw new Error('Whoops!, `aggregations` key must always be returned.'); | ||
} | ||
|
||
const currentBuckets = bucketSelector(response); | ||
|
||
// if there are no currentBuckets then we are finished paginating through the results | ||
if (currentBuckets.length === 0) { | ||
return previousBuckets; | ||
} | ||
|
||
// There is possibly more data, concat previous and current buckets and call ourselves recursively. | ||
const newOptions = onAfterKey(options, response); | ||
return getAllCompositeData( | ||
framework, | ||
request, | ||
newOptions, | ||
bucketSelector, | ||
onAfterKey, | ||
previousBuckets.concat(currentBuckets) | ||
); | ||
}; |