Skip to content

Commit 578137f

Browse files
[Maps] top term percentage field property (#59386)
* [Maps] top term percentage property * populate percentage in feature properties * TS work * clean up TS * fix all type errors * unit test for esAggFieldsFactory * clean up * i18n cleanup * do not show decimal place for perentage * fix jest expects * fix eslint errors * tslint errors * handle empty top bucket aggregation Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 5ff13ad commit 578137f

23 files changed

+439
-212
lines changed

x-pack/legacy/plugins/maps/common/constants.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ export const ES_SEARCH = 'ES_SEARCH';
5555
export const ES_PEW_PEW = 'ES_PEW_PEW';
5656
export const EMS_XYZ = 'EMS_XYZ'; // identifies a custom TMS source. Name is a little unfortunate.
5757

58-
export const FIELD_ORIGIN = {
59-
SOURCE: 'source',
60-
JOIN: 'join',
61-
};
58+
export enum FIELD_ORIGIN {
59+
SOURCE = 'source',
60+
JOIN = 'join',
61+
}
6262

6363
export const SOURCE_DATA_ID_ORIGIN = 'source';
6464
export const META_ID_ORIGIN_SUFFIX = 'meta';
@@ -139,6 +139,8 @@ export enum GRID_RESOLUTION {
139139
MOST_FINE = 'MOST_FINE',
140140
}
141141

142+
export const TOP_TERM_PERCENTAGE_SUFFIX = '__percentage';
143+
142144
export const COUNT_PROP_LABEL = i18n.translate('xpack.maps.aggs.defaultCountLabel', {
143145
defaultMessage: 'count',
144146
});

x-pack/legacy/plugins/maps/common/descriptor_types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export type AbstractESAggDescriptor = AbstractESSourceDescriptor & {
4040
};
4141

4242
export type ESGeoGridSourceDescriptor = AbstractESAggDescriptor & {
43-
requestType: RENDER_AS;
44-
resolution: GRID_RESOLUTION;
43+
requestType?: RENDER_AS;
44+
resolution?: GRID_RESOLUTION;
4545
};
4646

4747
export type ESSearchSourceDescriptor = AbstractESSourceDescriptor & {

x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.js

Lines changed: 0 additions & 96 deletions
This file was deleted.

x-pack/legacy/plugins/maps/public/layers/fields/es_agg_field.test.js

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { ESAggField, esAggFieldsFactory } from './es_agg_field';
8+
import { AGG_TYPE, FIELD_ORIGIN } from '../../../common/constants';
9+
import { IESAggSource } from '../sources/es_agg_source';
10+
import { IIndexPattern } from 'src/plugins/data/public';
11+
12+
const mockIndexPattern = {
13+
title: 'wildIndex',
14+
fields: [
15+
{
16+
name: 'foo*',
17+
},
18+
],
19+
} as IIndexPattern;
20+
21+
const mockEsAggSource = {
22+
getAggKey: (aggType: AGG_TYPE, fieldName: string) => {
23+
return 'agg_key';
24+
},
25+
getAggLabel: (aggType: AGG_TYPE, fieldName: string) => {
26+
return 'agg_label';
27+
},
28+
getIndexPattern: async () => {
29+
return mockIndexPattern;
30+
},
31+
} as IESAggSource;
32+
33+
const defaultParams = {
34+
label: 'my agg field',
35+
source: mockEsAggSource,
36+
aggType: AGG_TYPE.COUNT,
37+
origin: FIELD_ORIGIN.SOURCE,
38+
};
39+
40+
describe('supportsFieldMeta', () => {
41+
test('Non-counting aggregations should support field meta', () => {
42+
const avgMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.AVG });
43+
expect(avgMetric.supportsFieldMeta()).toBe(true);
44+
const maxMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.MAX });
45+
expect(maxMetric.supportsFieldMeta()).toBe(true);
46+
const minMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.MIN });
47+
expect(minMetric.supportsFieldMeta()).toBe(true);
48+
const termsMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.TERMS });
49+
expect(termsMetric.supportsFieldMeta()).toBe(true);
50+
});
51+
52+
test('Counting aggregations should not support field meta', () => {
53+
const countMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.COUNT });
54+
expect(countMetric.supportsFieldMeta()).toBe(false);
55+
const sumMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.SUM });
56+
expect(sumMetric.supportsFieldMeta()).toBe(false);
57+
const uniqueCountMetric = new ESAggField({ ...defaultParams, aggType: AGG_TYPE.UNIQUE_COUNT });
58+
expect(uniqueCountMetric.supportsFieldMeta()).toBe(false);
59+
});
60+
});
61+
62+
describe('esAggFieldsFactory', () => {
63+
test('Should only create top terms field when term field is not provided', () => {
64+
const fields = esAggFieldsFactory(
65+
{ type: AGG_TYPE.TERMS },
66+
mockEsAggSource,
67+
FIELD_ORIGIN.SOURCE
68+
);
69+
expect(fields.length).toBe(1);
70+
});
71+
72+
test('Should create top terms and top terms percentage fields', () => {
73+
const fields = esAggFieldsFactory(
74+
{ type: AGG_TYPE.TERMS, field: 'myField' },
75+
mockEsAggSource,
76+
FIELD_ORIGIN.SOURCE
77+
);
78+
expect(fields.length).toBe(2);
79+
});
80+
});

0 commit comments

Comments
 (0)