Skip to content

Commit

Permalink
[ML] Transforms: Adds date picker to transform wizard for data view w…
Browse files Browse the repository at this point in the history
…ith time fields. (elastic#149049)

Adds a date picker to the transform wizard for data views with time
fields. The time range will be applied to previews only.
  • Loading branch information
walterra authored Feb 1, 2023
1 parent ea69956 commit 0085aae
Show file tree
Hide file tree
Showing 39 changed files with 931 additions and 329 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const getMockedTimefilter = () => {
enableAutoRefreshSelector: jest.fn(),
getRefreshInterval: jest.fn(),
setRefreshInterval: jest.fn(),
getActiveBounds: jest.fn(),
getTime: jest.fn(),
isAutoRefreshSelectorEnabled: jest.fn(),
isTimeRangeSelectorEnabled: jest.fn(),
Expand All @@ -68,7 +69,7 @@ const getMockedTimefilter = () => {
};
};

const getMockedDatePickeDependencies = () => {
const getMockedDatePickerDependencies = () => {
return {
data: {
query: {
Expand Down Expand Up @@ -138,7 +139,7 @@ describe('TimeSeriesExplorerUrlStateManager', () => {
render(
<MlContext.Provider value={kibanaContextValueMock}>
<I18nProvider>
<DatePickerContextProvider {...getMockedDatePickeDependencies()}>
<DatePickerContextProvider {...getMockedDatePickerDependencies()}>
<TimeSeriesExplorerUrlStateManager {...props} />
</DatePickerContextProvider>
</I18nProvider>
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/transform/common/types/date_picker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export interface TimeRangeMs {
from: number;
to: number;
}
39 changes: 22 additions & 17 deletions x-pack/plugins/transform/public/app/common/data_grid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,44 @@
* 2.0.
*/

import { getPreviewTransformRequestBody, SimpleQuery } from '.';
import type { DataView } from '@kbn/data-views-plugin/common';

import { getIndexDevConsoleStatement, getPivotPreviewDevConsoleStatement } from './data_grid';
import { getPreviewTransformRequestBody, SimpleQuery } from '.';
import { getIndexDevConsoleStatement, getTransformPreviewDevConsoleStatement } from './data_grid';

describe('Transform: Data Grid', () => {
test('getPivotPreviewDevConsoleStatement()', () => {
test('getTransformPreviewDevConsoleStatement()', () => {
const query: SimpleQuery = {
query_string: {
query: '*',
default_operator: 'AND',
},
};

const request = getPreviewTransformRequestBody('the-index-pattern-title', query, {
pivot: {
group_by: {
'the-group-by-agg-name': {
terms: {
field: 'the-group-by-field',
const request = getPreviewTransformRequestBody(
{ getIndexPattern: () => 'the-index-pattern-title' } as DataView,
query,
{
pivot: {
group_by: {
'the-group-by-agg-name': {
terms: {
field: 'the-group-by-field',
},
},
},
},
aggregations: {
'the-agg-agg-name': {
avg: {
field: 'the-agg-field',
aggregations: {
'the-agg-agg-name': {
avg: {
field: 'the-agg-field',
},
},
},
},
},
});
}
);

const pivotPreviewDevConsoleStatement = getPivotPreviewDevConsoleStatement(request);
const pivotPreviewDevConsoleStatement = getTransformPreviewDevConsoleStatement(request);

expect(pivotPreviewDevConsoleStatement).toBe(`POST _transform/_preview
{
Expand Down
8 changes: 5 additions & 3 deletions x-pack/plugins/transform/public/app/common/data_grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@

import type { PostTransformsPreviewRequestSchema } from '../../../common/api_schemas/transforms';

import { PivotQuery } from './request';
import { TransformConfigQuery } from './request';

export const INIT_MAX_COLUMNS = 20;

export const getPivotPreviewDevConsoleStatement = (request: PostTransformsPreviewRequestSchema) => {
export const getTransformPreviewDevConsoleStatement = (
request: PostTransformsPreviewRequestSchema
) => {
return `POST _transform/_preview\n${JSON.stringify(request, null, 2)}\n`;
};

export const getIndexDevConsoleStatement = (query: PivotQuery, dataViewTitle: string) => {
export const getIndexDevConsoleStatement = (query: TransformConfigQuery, dataViewTitle: string) => {
return `GET ${dataViewTitle}/_search\n${JSON.stringify(
{
query,
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/transform/public/app/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export { isAggName } from './aggregations';
export {
getIndexDevConsoleStatement,
getPivotPreviewDevConsoleStatement,
getTransformPreviewDevConsoleStatement,
INIT_MAX_COLUMNS,
} from './data_grid';
export type { EsDoc, EsDocSource } from './fields';
Expand Down Expand Up @@ -64,12 +64,12 @@ export {
pivotGroupByFieldSupport,
PIVOT_SUPPORTED_GROUP_BY_AGGS,
} from './pivot_group_by';
export type { PivotQuery, SimpleQuery } from './request';
export type { TransformConfigQuery, SimpleQuery } from './request';
export {
defaultQuery,
getPreviewTransformRequestBody,
getCreateTransformRequestBody,
getPivotQuery,
getTransformConfigQuery,
getRequestPayload,
isDefaultQuery,
isMatchAllQuery,
Expand Down
64 changes: 38 additions & 26 deletions x-pack/plugins/transform/public/app/common/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import type { DataView } from '@kbn/data-views-plugin/common';

import { PIVOT_SUPPORTED_AGGS } from '../../../common/types/pivot_aggs';

import { PivotGroupByConfig } from '.';
Expand All @@ -19,19 +21,19 @@ import {
getPreviewTransformRequestBody,
getCreateTransformRequestBody,
getCreateTransformSettingsRequestBody,
getPivotQuery,
getTransformConfigQuery,
getMissingBucketConfig,
getRequestPayload,
isDefaultQuery,
isMatchAllQuery,
isSimpleQuery,
matchAllQuery,
PivotQuery,
type TransformConfigQuery,
} from './request';
import { LatestFunctionConfigUI } from '../../../common/types/transform';
import type { RuntimeField } from '@kbn/data-views-plugin/common';

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

const groupByTerms: PivotGroupByConfig = {
agg: PIVOT_SUPPORTED_GROUP_BY_AGGS.TERMS,
Expand Down Expand Up @@ -62,12 +64,12 @@ describe('Transform: Common', () => {

test('isDefaultQuery()', () => {
expect(isDefaultQuery(defaultQuery)).toBe(true);
expect(isDefaultQuery(matchAllQuery)).toBe(false);
expect(isDefaultQuery(matchAllQuery)).toBe(true);
expect(isDefaultQuery(simpleQuery)).toBe(false);
});

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

expect(query).toEqual({
query_string: {
Expand All @@ -78,14 +80,18 @@ describe('Transform: Common', () => {
});

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

const request = getPreviewTransformRequestBody('the-data-view-title', query, {
pivot: {
aggregations: { 'the-agg-agg-name': { avg: { field: 'the-agg-field' } } },
group_by: { 'the-group-by-agg-name': { terms: { field: 'the-group-by-field' } } },
},
});
const request = getPreviewTransformRequestBody(
{ getIndexPattern: () => 'the-data-view-title' } as DataView,
query,
{
pivot: {
aggregations: { 'the-agg-agg-name': { avg: { field: 'the-agg-field' } } },
group_by: { 'the-group-by-agg-name': { terms: { field: 'the-group-by-field' } } },
},
}
);

expect(request).toEqual({
pivot: {
Expand All @@ -100,13 +106,17 @@ describe('Transform: Common', () => {
});

test('getPreviewTransformRequestBody() with comma-separated index pattern', () => {
const query = getPivotQuery('the-query');
const request = getPreviewTransformRequestBody('the-data-view-title,the-other-title', query, {
pivot: {
aggregations: { 'the-agg-agg-name': { avg: { field: 'the-agg-field' } } },
group_by: { 'the-group-by-agg-name': { terms: { field: 'the-group-by-field' } } },
},
});
const query = getTransformConfigQuery('the-query');
const request = getPreviewTransformRequestBody(
{ getIndexPattern: () => 'the-data-view-title,the-other-title' } as DataView,
query,
{
pivot: {
aggregations: { 'the-agg-agg-name': { avg: { field: 'the-agg-field' } } },
group_by: { 'the-group-by-agg-name': { terms: { field: 'the-group-by-field' } } },
},
}
);

expect(request).toEqual({
pivot: {
Expand Down Expand Up @@ -172,9 +182,9 @@ describe('Transform: Common', () => {
});

test('getPreviewTransformRequestBody() with missing_buckets config', () => {
const query = getPivotQuery('the-query');
const query = getTransformConfigQuery('the-query');
const request = getPreviewTransformRequestBody(
'the-data-view-title',
{ getIndexPattern: () => 'the-data-view-title' } as DataView,
query,
getRequestPayload([aggsAvg], [{ ...groupByTerms, ...{ missing_bucket: true } }])
);
Expand All @@ -194,11 +204,12 @@ describe('Transform: Common', () => {
});

test('getCreateTransformRequestBody() skips default values', () => {
const pivotState: StepDefineExposedState = {
const transformConfigState: StepDefineExposedState = {
aggList: { 'the-agg-name': aggsAvg },
groupByList: { 'the-group-by-name': groupByTerms },
isAdvancedPivotEditorEnabled: false,
isAdvancedSourceEditorEnabled: false,
isDatePickerApplyEnabled: false,
sourceConfigUpdated: false,
searchLanguage: 'kuery',
searchString: 'the-query',
Expand Down Expand Up @@ -239,8 +250,8 @@ describe('Transform: Common', () => {
};

const request = getCreateTransformRequestBody(
'the-data-view-title',
pivotState,
{ getIndexPattern: () => 'the-data-view-title' } as DataView,
transformConfigState,
transformDetailsState
);

Expand Down Expand Up @@ -278,6 +289,7 @@ describe('Transform: Common', () => {
groupByList: { 'the-group-by-name': groupByTerms },
isAdvancedPivotEditorEnabled: false,
isAdvancedSourceEditorEnabled: false,
isDatePickerApplyEnabled: false,
sourceConfigUpdated: false,
searchLanguage: 'kuery',
searchString: 'the-query',
Expand Down Expand Up @@ -319,7 +331,7 @@ describe('Transform: Common', () => {
};

const request = getCreateTransformRequestBody(
'the-data-view-title',
{ getIndexPattern: () => 'the-data-view-title' } as DataView,
pivotState,
transformDetailsState
);
Expand Down
Loading

0 comments on commit 0085aae

Please sign in to comment.