Skip to content

Commit 790679b

Browse files
[8.13] [Tabify] Handle doc_count with care on time shift scenarios (elastic#178394) (elastic#178726)
# Backport This will backport the following commits from `main` to `8.13`: - [[Tabify] Handle doc_count with care on time shift scenarios (elastic#178394)](elastic#178394) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Marco Liberati","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-03-14T13:17:15Z","message":"[Tabify] Handle doc_count with care on time shift scenarios (elastic#178394)\n\n## Summary\r\n\r\nFixes elastic#178073\r\n\r\nThis PR restructure a bit the `tabify` code to explain and handle the\r\ncount with time shift with extra care.\r\n\r\nThe fix itself is one line, but I thought it was worth expanding a\r\nlittle bit more this obscure part of the code AND scope it down the fix\r\nto a specific scenario. The `hasMultipleDocCountAtRootWithFilters` can\r\nbe perhaps removed once many more tests can be performed.\r\n\r\n### Checklist\r\n- [x]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas added for features that require explanation or tutorials\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios","sha":"84304bc0dc29f49301ba4443daca35238698d453","branchLabelMapping":{"^v8.14.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Visualizations","Feature:Lens","v8.14.0","v8.13.1"],"title":"[Tabify] Handle doc_count with care on time shift scenarios","number":178394,"url":"https://github.com/elastic/kibana/pull/178394","mergeCommit":{"message":"[Tabify] Handle doc_count with care on time shift scenarios (elastic#178394)\n\n## Summary\r\n\r\nFixes elastic#178073\r\n\r\nThis PR restructure a bit the `tabify` code to explain and handle the\r\ncount with time shift with extra care.\r\n\r\nThe fix itself is one line, but I thought it was worth expanding a\r\nlittle bit more this obscure part of the code AND scope it down the fix\r\nto a specific scenario. The `hasMultipleDocCountAtRootWithFilters` can\r\nbe perhaps removed once many more tests can be performed.\r\n\r\n### Checklist\r\n- [x]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas added for features that require explanation or tutorials\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios","sha":"84304bc0dc29f49301ba4443daca35238698d453"}},"sourceBranch":"main","suggestedTargetBranches":["8.13"],"targetPullRequestStates":[{"branch":"main","label":"v8.14.0","branchLabelMappingKey":"^v8.14.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/178394","number":178394,"mergeCommit":{"message":"[Tabify] Handle doc_count with care on time shift scenarios (elastic#178394)\n\n## Summary\r\n\r\nFixes elastic#178073\r\n\r\nThis PR restructure a bit the `tabify` code to explain and handle the\r\ncount with time shift with extra care.\r\n\r\nThe fix itself is one line, but I thought it was worth expanding a\r\nlittle bit more this obscure part of the code AND scope it down the fix\r\nto a specific scenario. The `hasMultipleDocCountAtRootWithFilters` can\r\nbe perhaps removed once many more tests can be performed.\r\n\r\n### Checklist\r\n- [x]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas added for features that require explanation or tutorials\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios","sha":"84304bc0dc29f49301ba4443daca35238698d453"}},{"branch":"8.13","label":"v8.13.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Marco Liberati <[email protected]>
1 parent 4c90aec commit 790679b

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

src/plugins/data/common/search/aggs/agg_configs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export class AggConfigs {
117117
isSamplingEnabled() {
118118
return (
119119
isSamplingEnabled(this.opts.probability) &&
120-
this.getRequestAggs().filter((agg) => !agg.type.hasNoDsl).length > 0
120+
this.getRequestAggs().some((agg) => !agg.type.hasNoDsl)
121121
);
122122
}
123123

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
export const timeOffsetFiltersWithZeroDocCountResponse = {
10+
hits: {
11+
total: 474,
12+
max_score: 0,
13+
hits: [],
14+
},
15+
aggregations: {
16+
doc_count: 0,
17+
doc_count_86400000: 234,
18+
},
19+
};

src/plugins/data/common/search/tabify/tabify.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ import { AggConfigs, BucketAggParam, IAggConfig, IAggConfigs } from '../aggs';
1212
import { mockAggTypesRegistry } from '../aggs/test_helpers';
1313
import { metricOnly, threeTermBuckets } from './fixtures/fake_hierarchical_data';
1414
import { isSamplingEnabled } from '../aggs/utils/sampler';
15+
import { timeOffsetFiltersWithZeroDocCountResponse } from './fixtures/fake_timeoffset_data';
1516

1617
describe('tabifyAggResponse Integration', () => {
1718
const typesRegistry = mockAggTypesRegistry();
1819

1920
for (const probability of [1, 0.5, undefined]) {
2021
function getTitlePostfix() {
21-
if (!isSamplingEnabled(probability)) {
22+
if (probability == null) {
2223
return '';
2324
}
25+
if (probability === 1) {
26+
return ` - with no sampling (probability = 1)`;
27+
}
2428
return ` - with sampling (probability = ${probability})`;
2529
}
2630

@@ -243,5 +247,20 @@ describe('tabifyAggResponse Integration', () => {
243247
});
244248
});
245249
});
250+
251+
describe(`edge cases${getTitlePostfix()}`, () => {
252+
test('it should correctly report zero doc count for unshifted bucket', () => {
253+
const aggConfigs = createAggConfigs([
254+
mockAggConfig({ type: 'count', schema: 'metric' }),
255+
mockAggConfig({ type: 'count', schema: 'metric', params: { timeShift: '1d' } }),
256+
]);
257+
258+
// no need to wrap with sampling as count is not affected by it
259+
const tabbed = tabifyAggResponse(aggConfigs, timeOffsetFiltersWithZeroDocCountResponse, {
260+
metricsAtAllLevels: false,
261+
});
262+
expect(tabbed.rows[0]).toEqual({ 'col-0-1': 0, 'col-1-2': 234 });
263+
});
264+
});
246265
}
247266
});

src/plugins/data/common/search/tabify/tabify.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,27 @@ export function tabifyAggResponse(
146146
}
147147

148148
const write = new TabbedAggResponseWriter(aggConfigs, respOpts || {});
149+
// Check whether there's a time shift for a count operation at root level
150+
const hasMultipleDocCountAtRootWithFilters = Object.keys(esResponse.aggregations ?? {}).some(
151+
(key) => /doc_count_/.test(key)
152+
);
153+
149154
const topLevelBucket: AggResponseBucket = {
150155
...(aggConfigs.isSamplingEnabled()
151156
? esResponse.aggregations?.sampling
152157
: esResponse.aggregations),
153-
doc_count: esResponse.aggregations?.doc_count || esResponse.hits?.total,
158+
doc_count: esResponse.aggregations?.doc_count,
154159
};
155160

161+
// The fix itself is one line, but it's a bit hard to clear assess the full impact of it
162+
// therefore here's a check to scope down the impact to the known scenario with the bug https://github.com/elastic/kibana/issues/178073
163+
// this can be lifted off once the full impact is assessed
164+
if (!topLevelBucket.doc_count) {
165+
if (!hasMultipleDocCountAtRootWithFilters) {
166+
topLevelBucket.doc_count = esResponse.hits?.total;
167+
}
168+
}
169+
156170
collectBucket(aggConfigs, write, topLevelBucket, '', 1);
157171

158172
return {

0 commit comments

Comments
 (0)