Skip to content

Commit 3350bff

Browse files
author
Tim Roes
authored
Allow histogram fields in average and sum aggregations (#66891)
* Allow histogram fields in average and sum aggs * Fix PR review
1 parent c579b67 commit 3350bff

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

src/plugins/data/public/search/aggs/metrics/avg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const getAvgMetricAgg = ({ getInternalStartServices }: AvgMetricAggDepend
5151
{
5252
name: 'field',
5353
type: 'field',
54-
filterFieldTypes: KBN_FIELD_TYPES.NUMBER,
54+
filterFieldTypes: [KBN_FIELD_TYPES.NUMBER, KBN_FIELD_TYPES.HISTOGRAM],
5555
},
5656
],
5757
},

src/plugins/data/public/search/aggs/metrics/sum.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const getSumMetricAgg = ({ getInternalStartServices }: SumMetricAggDepend
5454
{
5555
name: 'field',
5656
type: 'field',
57-
filterFieldTypes: KBN_FIELD_TYPES.NUMBER,
57+
filterFieldTypes: [KBN_FIELD_TYPES.NUMBER, KBN_FIELD_TYPES.HISTOGRAM],
5858
},
5959
],
6060
},

test/functional/page_objects/visualize_chart_page.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ export function VisualizeChartPageProvider({ getService, getPageObjects }: FtrPr
312312

313313
/**
314314
* If you are writing new tests, you should rather look into getTableVisContent method instead.
315+
* @deprecated Use getTableVisContent instead.
315316
*/
316317
public async getTableVisData() {
317318
return await testSubjects.getVisibleText('paginated-table-body');

x-pack/test/functional/apps/visualize/precalculated_histogram.ts

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,62 @@ export default function({ getService, getPageObjects }: FtrProviderContext) {
2424
return esArchiver.unload('pre_calculated_histogram');
2525
});
2626

27-
const initHistogramBarChart = async () => {
28-
await PageObjects.visualize.navigateToNewVisualization();
29-
await PageObjects.visualize.clickVerticalBarChart();
30-
await PageObjects.visualize.clickNewSearch('histogram-test');
31-
await PageObjects.visChart.waitForVisualization();
32-
};
33-
34-
const getFieldOptionsForAggregation = async (aggregation: string): Promise<string[]> => {
35-
await PageObjects.visEditor.clickBucket('Y-axis', 'metrics');
36-
await PageObjects.visEditor.selectAggregation(aggregation, 'metrics');
37-
const fieldValues = await PageObjects.visEditor.getField();
38-
return fieldValues;
39-
};
40-
4127
it('appears correctly in discover', async function() {
4228
await PageObjects.common.navigateToApp('discover');
4329
const rowData = await PageObjects.discover.getDocTableIndex(1);
4430
expect(rowData.includes('"values": [ 0.3, 1, 3, 4.2, 4.8 ]')).to.be.ok();
4531
});
4632

47-
it('appears in the field options of a Percentiles aggregation', async function() {
48-
await initHistogramBarChart();
49-
const fieldValues: string[] = await getFieldOptionsForAggregation('Percentiles');
50-
log.debug('Percentiles Fields = ' + fieldValues);
51-
expect(fieldValues[0]).to.be('histogram-content');
52-
});
33+
describe('works in visualizations', () => {
34+
before(async () => {
35+
await PageObjects.visualize.navigateToNewVisualization();
36+
await PageObjects.visualize.clickDataTable();
37+
await PageObjects.visualize.clickNewSearch('histogram-test');
38+
await PageObjects.visChart.waitForVisualization();
39+
await PageObjects.visEditor.clickMetricEditor();
40+
});
41+
42+
const renderTableForAggregation = async (aggregation: string) => {
43+
await PageObjects.visEditor.selectAggregation(aggregation, 'metrics');
44+
await PageObjects.visEditor.selectField('histogram-content', 'metrics');
45+
await PageObjects.visEditor.clickGo();
46+
47+
return await PageObjects.visChart.getTableVisContent();
48+
};
49+
50+
it('with percentiles aggregation', async () => {
51+
const data = (await renderTableForAggregation('Percentiles')) as string[][];
52+
expect(data[0]).to.have.property('length', 7);
53+
// Percentile values are not deterministic, so we can't check for the exact values here,
54+
// but just check they are all within the given range
55+
// see https://github.com/elastic/elasticsearch/issues/49225
56+
expect(data[0].every((p: string) => Number(p) >= 0.3 && Number(p) <= 5)).to.be(true);
57+
});
58+
59+
it('with percentile ranks aggregation', async () => {
60+
const data = await renderTableForAggregation('Percentile Ranks');
61+
expect(data).to.eql([['0%']]);
62+
});
63+
64+
it('with average aggregation', async () => {
65+
const data = await renderTableForAggregation('Average');
66+
expect(data).to.eql([['2.8510720308359434']]);
67+
});
68+
69+
it('with median aggregation', async () => {
70+
// Percentile values (which are used by median behind the scenes) are not deterministic,
71+
// so we can't check for the exact values here, but just check they are all within the given range
72+
// see https://github.com/elastic/elasticsearch/issues/49225
73+
const data = await renderTableForAggregation('Median');
74+
const value = Number(data[0][0]);
75+
expect(value).to.be.above(3.0);
76+
expect(value).to.be.below(3.3);
77+
});
5378

54-
it('appears in the field options of a Percentile Ranks aggregation', async function() {
55-
const fieldValues: string[] = await getFieldOptionsForAggregation('Percentile Ranks');
56-
log.debug('Percentile Ranks Fields = ' + fieldValues);
57-
expect(fieldValues[0]).to.be('histogram-content');
79+
it('with sum aggregation', async () => {
80+
const data = await renderTableForAggregation('Sum');
81+
expect(data).to.eql([['11834.800000000001']]);
82+
});
5883
});
5984
});
6085
}

0 commit comments

Comments
 (0)