Skip to content

Commit

Permalink
[ML] Add support for missing_buckets attribute in transform advanced …
Browse files Browse the repository at this point in the history
…pivot editor.
  • Loading branch information
walterra committed Dec 14, 2020
1 parent acdca75 commit 1de3ff4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 49 deletions.
5 changes: 2 additions & 3 deletions x-pack/plugins/transform/public/app/common/pivot_group_by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ interface GroupByConfigBase {
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS;
aggName: AggName;
dropDownName: string;
field: EsFieldName;
missing_bucket?: boolean;
}

// Don't allow an interval of '0', but allow a float interval of '0.1' with a leading zero.
Expand All @@ -50,19 +52,16 @@ export const dateHistogramIntervalFormatRegex = /^[1-9][0-9]*(ms|s|m|h|d|w|M|q|y

interface GroupByDateHistogram extends GroupByConfigBase {
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.DATE_HISTOGRAM;
field: EsFieldName;
calendar_interval: string;
}

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

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

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
7 changes: 7 additions & 0 deletions x-pack/plugins/transform/public/app/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export function isDefaultQuery(query: PivotQuery): boolean {
return isSimpleQuery(query) && query.query_string.query === '*';
}

export const getMissingBucketConfig = (g: PivotGroupByConfig): { 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 +99,7 @@ export function getPreviewTransformRequestBody(
const termsAgg: TermsAgg = {
terms: {
field: g.field,
...getMissingBucketConfig(g),
},
};
request.pivot.group_by[g.aggName] = termsAgg;
Expand All @@ -103,6 +108,7 @@ export function getPreviewTransformRequestBody(
histogram: {
field: g.field,
interval: g.interval,
...getMissingBucketConfig(g),
},
};
request.pivot.group_by[g.aggName] = histogramAgg;
Expand All @@ -111,6 +117,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 1de3ff4

Please sign in to comment.