diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/index.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/index.ts index ca197a80950d7..a1bdce8a7ccb2 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/index.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/index.ts @@ -20,6 +20,6 @@ export { convertToLastValueColumn } from './last_value'; export { convertToStaticValueColumn } from './static_value'; export { convertToFiltersColumn } from './filters'; export { convertToDateHistogramColumn } from './date_histogram'; -export { converToTermsColumns } from './terms'; +export { converToTermsColumn } from './terms'; export * from './types'; diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/terms.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/terms.ts index 4db8211bba35d..91a115c167ba4 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/terms.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/convert/terms.ts @@ -23,7 +23,7 @@ const getOrderByWithAgg = (series: Series, columns: Column[]): OrderByWithAgg | return { orderBy: { type: 'alphabetical' } }; } - if (series.terms_order_by === '_count') { + if (series.terms_order_by === '_count' || !series.terms_order_by) { const columnId = uuid(); return { orderBy: { type: 'column', columnId }, @@ -56,7 +56,11 @@ const getOrderByWithAgg = (series: Series, columns: Column[]): OrderByWithAgg | }; }; -export const convertToTermsParams = (series: Series, columns: Column[]): TermsParams | null => { +export const convertToTermsParams = ( + series: Series, + columns: Column[], + secondaryFields: string[] +): TermsParams | null => { const orderDirection: TermsParams['orderDirection'] = series.terms_direction === 'asc' ? 'asc' : 'desc'; @@ -75,23 +79,25 @@ export const convertToTermsParams = (series: Series, columns: Column[]): TermsPa orderDirection, parentFormat: { id: 'terms' }, ...orderByWithAgg, + secondaryFields, }; }; export const converToTermsColumn = ( - termField: string, + termFields: string[], series: Series, columns: Column[], dataView: DataView, isSplit: boolean = false ): TermsColumn | null => { - const field = dataView.getFieldByName(termField); + const [baseField, ...secondaryFields] = termFields; + const field = dataView.getFieldByName(baseField); if (!field) { return null; } - const params = convertToTermsParams(series, columns); + const params = convertToTermsParams(series, columns, secondaryFields); if (!params) { return params; } @@ -106,13 +112,3 @@ export const converToTermsColumn = ( params, }; }; - -export const converToTermsColumns = ( - termFields: string[], - series: Series, - columns: Column[], - dataView: DataView, - isSplit: boolean = false -): Array => { - return termFields.map((field) => converToTermsColumn(field, series, columns, dataView, isSplit)); -}; diff --git a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/buckets_columns.ts b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/buckets_columns.ts index aa7a58f1fd9e1..ce33e78a26094 100644 --- a/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/buckets_columns.ts +++ b/src/plugins/vis_types/timeseries/public/convert_to_lens/lib/series/buckets_columns.ts @@ -13,7 +13,7 @@ import { Column, convertToFiltersColumn, convertToDateHistogramColumn, - converToTermsColumns, + converToTermsColumn, } from '../convert'; import { getValidColumns } from './columns'; @@ -49,10 +49,7 @@ export const getBucketsColumns = ( ) => { if (series.split_mode === 'filters' || series.split_mode === 'filter') { const filterColumn = convertToFiltersColumn(series, true); - if (!filterColumn) { - return null; - } - return [filterColumn]; + return getValidColumns([filterColumn]); } if (series.split_mode === 'terms') { const splitFields = getFieldsForTerms(series.terms_field); @@ -68,14 +65,11 @@ export const getBucketsColumns = ( splitFields[0], true ); - if (!dateHistogramColumn) { - return null; - } - return [dateHistogramColumn]; + return getValidColumns([dateHistogramColumn]); } - const termsColumns = converToTermsColumns(splitFields, series, columns, dataView, isSplit); - return getValidColumns(termsColumns); + const termsColumn = converToTermsColumn(splitFields, series, columns, dataView, isSplit); + return getValidColumns([termsColumn]); } return []; }; diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/indexpattern_suggestions.ts b/x-pack/plugins/lens/public/indexpattern_datasource/indexpattern_suggestions.ts index efc63fa6a63de..123dcce6f3664 100644 --- a/x-pack/plugins/lens/public/indexpattern_datasource/indexpattern_suggestions.ts +++ b/x-pack/plugins/lens/public/indexpattern_datasource/indexpattern_suggestions.ts @@ -12,6 +12,7 @@ import type { Column, AnyColumnWithReferences, AnyColumnWithSourceField, + TermsColumn, } from '@kbn/visualizations-plugin/common/convert_to_lens'; import { generateId } from '../id_generator'; import type { @@ -206,6 +207,10 @@ function isFieldBasedColumn(column: Column): column is AnyColumnWithSourceField return 'sourceField' in column; } +function isTermsColumn(column: Column): column is TermsColumn { + return column.operationType === 'terms'; +} + function getSourceField(column: Column, indexPattern: IndexPattern) { return isFieldBasedColumn(column) ? column.sourceField === 'document' @@ -256,6 +261,21 @@ function convertToColumnChange(columns: Layer['columns'], indexPattern: IndexPat }, ]; } + if ( + isTermsColumn(column) && + column.params.orderAgg && + !columns.some((c) => c.columnId === column.params.orderAgg?.columnId) + ) { + const orderAggColumn: ColumnChange = { + op: column.params.orderAgg.operationType, + columnId: column.params.orderAgg.columnId, + field: getSourceField(column.params.orderAgg, indexPattern), + indexPattern, + visualizationGroups: [], + columnParams: getParams(column.params.orderAgg), + }; + acc.push(orderAggColumn); + } acc.push(newColumn); } diff --git a/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/index.tsx b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/index.tsx index 3959aa9778e0b..eec39bd530944 100644 --- a/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/index.tsx +++ b/x-pack/plugins/lens/public/indexpattern_datasource/operations/definitions/terms/index.tsx @@ -226,18 +226,20 @@ export const termsOperation: OperationDefinition< sourceField: field.name, isBucketed: true, params: { - size: columnParams?.size ?? previousBucketsLength === 0 ? 5 : DEFAULT_SIZE, + size: columnParams?.size ?? (previousBucketsLength === 0 ? 5 : DEFAULT_SIZE), orderBy: - columnParams?.orderBy ?? existingMetricColumn + columnParams?.orderBy ?? + (existingMetricColumn ? { type: 'column', columnId: existingMetricColumn, } - : { type: 'alphabetical', fallback: true }, - orderDirection: columnParams?.orderDirection ?? existingMetricColumn ? 'desc' : 'asc', + : { type: 'alphabetical', fallback: true }), + orderDirection: columnParams?.orderDirection ?? (existingMetricColumn ? 'desc' : 'asc'), otherBucket: (columnParams?.otherBucket ?? true) && !indexPattern.hasRestrictions, missingBucket: columnParams?.missingBucket ?? false, parentFormat: columnParams?.parentFormat ?? { id: 'terms' }, + secondaryFields: columnParams?.secondaryFields, }, }; },