[UnifiedFieldList] Fix % for field stats calculations (edge cases) (#144962)

Closes https://github.com/elastic/kibana/issues/144625

## Summary

This PR addresses the issue with calculating % for top values for apm
indices.
This commit is contained in:
Julia Rechkunova 2022-11-10 20:51:19 +01:00 committed by GitHub
parent fc3c3a95e9
commit 1607c15a32
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -193,9 +193,10 @@ export async function getNumberHistogram(
? minMaxResult.aggregations!.sample.top_values
: {
buckets: [] as Array<{ doc_count: number; key: string | number }>,
sum_other_doc_count: 0,
};
const topValuesBuckets = {
const topValues = {
buckets: terms.buckets.map((bucket) => ({
count: bucket.doc_count,
key: bucket.key,
@ -211,9 +212,11 @@ export async function getNumberHistogram(
if (histogramInterval === 0) {
return {
totalDocuments: getHitsTotal(minMaxResult),
sampledValues: minMaxResult.aggregations!.sample.sample_count.value!,
sampledValues:
sumSampledValues(topValues, terms.sum_other_doc_count) ||
minMaxResult.aggregations!.sample.sample_count.value!,
sampledDocuments: minMaxResult.aggregations!.sample.doc_count,
topValues: topValuesBuckets,
topValues,
histogram: useTopHits
? { buckets: [] }
: {
@ -244,14 +247,16 @@ export async function getNumberHistogram(
return {
totalDocuments: getHitsTotal(minMaxResult),
sampledDocuments: minMaxResult.aggregations!.sample.doc_count,
sampledValues: minMaxResult.aggregations!.sample.sample_count.value!,
sampledValues:
sumSampledValues(topValues, terms.sum_other_doc_count) ||
minMaxResult.aggregations!.sample.sample_count.value!,
histogram: {
buckets: histogramResult.aggregations!.sample.histo.buckets.map((bucket) => ({
count: bucket.doc_count,
key: bucket.key,
})),
},
topValues: topValuesBuckets,
topValues,
};
}
@ -284,16 +289,22 @@ export async function getStringSamples(
{ body: { aggs: typeof topValuesBody } }
>;
const topValues = {
buckets: topValuesResult.aggregations!.sample.top_values.buckets.map((bucket) => ({
count: bucket.doc_count,
key: bucket.key,
})),
};
return {
totalDocuments: getHitsTotal(topValuesResult),
sampledDocuments: topValuesResult.aggregations!.sample.doc_count,
sampledValues: topValuesResult.aggregations!.sample.sample_count.value!,
topValues: {
buckets: topValuesResult.aggregations!.sample.top_values.buckets.map((bucket) => ({
count: bucket.doc_count,
key: bucket.key,
})),
},
sampledValues:
sumSampledValues(
topValues,
topValuesResult.aggregations!.sample.top_values.sum_other_doc_count
) || topValuesResult.aggregations!.sample.sample_count.value!,
topValues,
};
}
@ -392,3 +403,14 @@ function getFieldRef(field: DataViewField) {
const getHitsTotal = (body: estypes.SearchResponse): number => {
return (body.hits.total as estypes.SearchTotalHits).value ?? body.hits.total ?? 0;
};
// We could use `aggregations.sample.sample_count.value` instead, but it does not always give a correct sum
// See Github issue #144625
export function sumSampledValues(
topValues: FieldStatsResponse<string | number>['topValues'],
sumOtherDocCount: number
): number {
const valuesInTopBuckets =
topValues?.buckets?.reduce((prev, bucket) => bucket.count + prev, 0) || 0;
return valuesInTopBuckets + (sumOtherDocCount || 0);
}