Skip to content

Commit

Permalink
Merge branch 'navigate-to-lens-context-converting-imporvement' of git…
Browse files Browse the repository at this point in the history
…hub.com:Kunzetsov/kibana into navigate-to-lens-context-converting-imporvement
  • Loading branch information
Kuznietsov committed Aug 23, 2022
2 parents b26ca08 + 798998b commit 28e20f7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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';

Expand All @@ -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;
}
Expand All @@ -106,13 +112,3 @@ export const converToTermsColumn = (
params,
};
};

export const converToTermsColumns = (
termFields: string[],
series: Series,
columns: Column[],
dataView: DataView,
isSplit: boolean = false
): Array<TermsColumn | null> => {
return termFields.map((field) => converToTermsColumn(field, series, columns, dataView, isSplit));
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
Column,
convertToFiltersColumn,
convertToDateHistogramColumn,
converToTermsColumns,
converToTermsColumn,
} from '../convert';
import { getValidColumns } from './columns';

Expand Down Expand Up @@ -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);
Expand All @@ -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 [];
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
},
Expand Down

0 comments on commit 28e20f7

Please sign in to comment.