Skip to content

Commit

Permalink
[ML] Transforms: Support for missing_bucket in transform advanced piv…
Browse files Browse the repository at this point in the history
…ot editor (elastic#85758)

Adds support for missing_bucket with group-by configurations in the advanced editor for pivot configurations.
Previously, the editor would allow you to add the setting, but it would be stripped from the config once the transform gets created.
  • Loading branch information
walterra committed Dec 14, 2020
1 parent 68d885f commit 524c6bd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
3 changes: 3 additions & 0 deletions x-pack/plugins/transform/common/types/pivot_group_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ export type GenericAgg = object;
export interface TermsAgg {
terms: {
field: EsFieldName;
missing_bucket?: boolean;
};
}

export interface HistogramAgg {
histogram: {
field: EsFieldName;
interval: string;
missing_bucket?: boolean;
};
}

export interface DateHistogramAgg {
date_histogram: {
field: EsFieldName;
calendar_interval: string;
missing_bucket?: boolean;
};
}

Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/transform/public/app/common/pivot_group_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,20 @@ interface GroupByDateHistogram extends GroupByConfigBase {
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.DATE_HISTOGRAM;
field: EsFieldName;
calendar_interval: string;
missing_bucket?: boolean;
}

interface GroupByHistogram extends GroupByConfigBase {
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.HISTOGRAM;
field: EsFieldName;
interval: string;
missing_bucket?: boolean;
}

interface GroupByTerms extends GroupByConfigBase {
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS;
field: EsFieldName;
missing_bucket?: boolean;
}

export type GroupByConfigWithInterval = GroupByDateHistogram | GroupByHistogram;
Expand Down
97 changes: 51 additions & 46 deletions x-pack/plugins/transform/public/app/common/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getPreviewTransformRequestBody,
getCreateTransformRequestBody,
getCreateTransformSettingsRequestBody,
getMissingBucketConfig,
getPivotQuery,
isDefaultQuery,
isMatchAllQuery,
Expand All @@ -28,6 +29,20 @@ import {

const simpleQuery: PivotQuery = { query_string: { query: 'airline:AAL' } };

const groupByTerms: PivotGroupByConfig = {
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS,
field: 'the-group-by-field',
aggName: 'the-group-by-agg-name',
dropDownName: 'the-group-by-drop-down-name',
};

const aggsAvg: PivotAggsConfig = {
agg: PIVOT_SUPPORTED_AGGS.AVG,
field: 'the-agg-field',
aggName: 'the-agg-agg-name',
dropDownName: 'the-agg-drop-down-name',
};

describe('Transform: Common', () => {
test('isMatchAllQuery()', () => {
expect(isMatchAllQuery(defaultQuery)).toBe(false);
Expand All @@ -47,6 +62,16 @@ describe('Transform: Common', () => {
expect(isDefaultQuery(simpleQuery)).toBe(false);
});

test('getMissingBucketConfig()', () => {
expect(getMissingBucketConfig(groupByTerms)).toEqual({});
expect(getMissingBucketConfig({ ...groupByTerms, ...{ missing_bucket: true } })).toEqual({
missing_bucket: true,
});
expect(getMissingBucketConfig({ ...groupByTerms, ...{ missing_bucket: false } })).toEqual({
missing_bucket: false,
});
});

test('getPivotQuery()', () => {
const query = getPivotQuery('the-query');

Expand All @@ -60,22 +85,8 @@ describe('Transform: Common', () => {

test('getPreviewTransformRequestBody()', () => {
const query = getPivotQuery('the-query');
const groupBy: PivotGroupByConfig[] = [
{
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS,
field: 'the-group-by-field',
aggName: 'the-group-by-agg-name',
dropDownName: 'the-group-by-drop-down-name',
},
];
const aggs: PivotAggsConfig[] = [
{
agg: PIVOT_SUPPORTED_AGGS.AVG,
field: 'the-agg-field',
aggName: 'the-agg-agg-name',
dropDownName: 'the-agg-drop-down-name',
},
];
const groupBy: PivotGroupByConfig[] = [groupByTerms];
const aggs: PivotAggsConfig[] = [aggsAvg];
const request = getPreviewTransformRequestBody('the-index-pattern-title', query, groupBy, aggs);

expect(request).toEqual({
Expand All @@ -92,22 +103,8 @@ describe('Transform: Common', () => {

test('getPreviewTransformRequestBody() with comma-separated index pattern', () => {
const query = getPivotQuery('the-query');
const groupBy: PivotGroupByConfig[] = [
{
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS,
field: 'the-group-by-field',
aggName: 'the-group-by-agg-name',
dropDownName: 'the-group-by-drop-down-name',
},
];
const aggs: PivotAggsConfig[] = [
{
agg: PIVOT_SUPPORTED_AGGS.AVG,
field: 'the-agg-field',
aggName: 'the-agg-agg-name',
dropDownName: 'the-agg-drop-down-name',
},
];
const groupBy: PivotGroupByConfig[] = [groupByTerms];
const aggs: PivotAggsConfig[] = [aggsAvg];
const request = getPreviewTransformRequestBody(
'the-index-pattern-title,the-other-title',
query,
Expand All @@ -127,22 +124,30 @@ describe('Transform: Common', () => {
});
});

test('getPreviewTransformRequestBody() with missing_buckets config', () => {
const query = getPivotQuery('the-query');
const groupBy: PivotGroupByConfig[] = [{ ...groupByTerms, ...{ missing_bucket: true } }];
const aggs: PivotAggsConfig[] = [aggsAvg];
const request = getPreviewTransformRequestBody('the-index-pattern-title', query, groupBy, aggs);

expect(request).toEqual({
pivot: {
aggregations: { 'the-agg-agg-name': { avg: { field: 'the-agg-field' } } },
group_by: {
'the-group-by-agg-name': { terms: { field: 'the-group-by-field', missing_bucket: true } },
},
},
source: {
index: ['the-index-pattern-title'],
query: { query_string: { default_operator: 'AND', query: 'the-query' } },
},
});
});

test('getCreateTransformRequestBody()', () => {
const groupBy: PivotGroupByConfig = {
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS,
field: 'the-group-by-field',
aggName: 'the-group-by-agg-name',
dropDownName: 'the-group-by-drop-down-name',
};
const agg: PivotAggsConfig = {
agg: PIVOT_SUPPORTED_AGGS.AVG,
field: 'the-agg-field',
aggName: 'the-agg-agg-name',
dropDownName: 'the-agg-drop-down-name',
};
const pivotState: StepDefineExposedState = {
aggList: { 'the-agg-name': agg },
groupByList: { 'the-group-by-name': groupBy },
aggList: { 'the-agg-name': aggsAvg },
groupByList: { 'the-group-by-name': groupByTerms },
isAdvancedPivotEditorEnabled: false,
isAdvancedSourceEditorEnabled: false,
sourceConfigUpdated: false,
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/transform/public/app/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
isGroupByDateHistogram,
isGroupByHistogram,
isGroupByTerms,
GroupByConfigWithUiSupport,
PivotGroupByConfig,
} from '../common';

Expand Down Expand Up @@ -71,6 +72,12 @@ export function isDefaultQuery(query: PivotQuery): boolean {
return isSimpleQuery(query) && query.query_string.query === '*';
}

export const getMissingBucketConfig = (
g: GroupByConfigWithUiSupport
): { missing_bucket?: boolean } => {
return g.missing_bucket !== undefined ? { missing_bucket: g.missing_bucket } : {};
};

export function getPreviewTransformRequestBody(
indexPatternTitle: IndexPattern['title'],
query: PivotQuery,
Expand All @@ -95,6 +102,7 @@ export function getPreviewTransformRequestBody(
const termsAgg: TermsAgg = {
terms: {
field: g.field,
...getMissingBucketConfig(g),
},
};
request.pivot.group_by[g.aggName] = termsAgg;
Expand All @@ -103,6 +111,7 @@ export function getPreviewTransformRequestBody(
histogram: {
field: g.field,
interval: g.interval,
...getMissingBucketConfig(g),
},
};
request.pivot.group_by[g.aggName] = histogramAgg;
Expand All @@ -111,6 +120,7 @@ export function getPreviewTransformRequestBody(
date_histogram: {
field: g.field,
calendar_interval: g.calendar_interval,
...getMissingBucketConfig(g),
},
};
request.pivot.group_by[g.aggName] = dateHistogramAgg;
Expand Down

0 comments on commit 524c6bd

Please sign in to comment.