Skip to content

Commit

Permalink
🐛 Fix partial rows edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed May 25, 2022
1 parent 7a0de80 commit 0cc33ae
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,23 @@ export const getFormattedRow = (
columns: Datatable['columns'],
columnsFormatters: Record<string, IFieldFormat>,
xAccessor: string | undefined,
splitAccessor: string | undefined,
xScaleType: XScaleType
): { row: Datatable['rows'][number]; formattedColumns: Record<string, true> } =>
columns.reduce(
(formattedInfo, { id }) => {
const record = formattedInfo.row[id];
if (
record != null &&
// pre-format values for ordinal x axes because there can only be a single x axis formatter on chart level
(!isPrimitive(record) || (id === xAccessor && xScaleType === 'ordinal'))
!isPrimitive(record) ||
(id === xAccessor && xScaleType === 'ordinal')
) {
const overrideEmptyValue = record == null && splitAccessor;
return {
row: { ...formattedInfo.row, [id]: columnsFormatters[id]!.convert(record) },
row: {
...formattedInfo.row,
[id]: columnsFormatters[id]!.convert(overrideEmptyValue ? '' : record),
},
formattedColumns: { ...formattedInfo.formattedColumns, [id]: true },
};
}
Expand All @@ -129,6 +134,7 @@ export const getFormattedTable = (
table: Datatable,
formatFactory: FormatFactory,
xAccessor: string | ExpressionValueVisDimension | undefined,
splitAccessor: string | ExpressionValueVisDimension | undefined,
accessors: Array<string | ExpressionValueVisDimension>,
xScaleType: XScaleType
): { table: Datatable; formattedColumns: Record<string, true> } => {
Expand All @@ -146,28 +152,28 @@ export const getFormattedTable = (
{}
);

const formattedTableInfo = table.rows.reduce<{
const formattedTableInfo: {
rows: Datatable['rows'];
formattedColumns: Record<string, true>;
}>(
({ rows: formattedRows, formattedColumns }, row) => {
const formattedRowInfo = getFormattedRow(
row,
table.columns,
columnsFormatters,
xAccessor ? getAccessorByDimension(xAccessor, table.columns) : undefined,
xScaleType
);
return {
rows: [...formattedRows, formattedRowInfo.row],
formattedColumns: { ...formattedColumns, ...formattedRowInfo.formattedColumns },
};
},
{
rows: [],
formattedColumns: {},
}
);
} = {
rows: [],
formattedColumns: {},
};
for (const row of table.rows) {
const formattedRowInfo = getFormattedRow(
row,
table.columns,
columnsFormatters,
xAccessor ? getAccessorByDimension(xAccessor, table.columns) : undefined,
splitAccessor ? getAccessorByDimension(splitAccessor, table.columns) : undefined,
xScaleType
);
formattedTableInfo.rows.push(formattedRowInfo.row);
formattedTableInfo.formattedColumns = {
...formattedTableInfo.formattedColumns,
...formattedRowInfo.formattedColumns,
};
}

return {
table: { ...table, rows: formattedTableInfo.rows },
Expand All @@ -186,6 +192,7 @@ export const getFormattedTablesByLayers = (
table,
formatFactory,
xAccessor,
splitAccessor,
[xAccessor, splitAccessor, ...accessors].filter<string | ExpressionValueVisDimension>(
(a): a is string | ExpressionValueVisDimension => a !== undefined
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ const GranularityHelpPopover = () => {
);
};

function isValidBound(
bounds?: { min?: number; max?: number } | undefined
): bounds is Required<ExtendedBounds> {
return boundsValidation(bounds);
}

function boundsValidation(bounds?: { min?: number; max?: number }) {
return bounds?.min != null && bounds?.max != null && bounds?.min < bounds?.max;
}
Expand Down Expand Up @@ -122,15 +128,8 @@ const BaseRangeEditor = ({
});
const onChangeCustomBoundsWithValidation = useCallback(
(newBounds: { min?: number; max?: number } | undefined) => {
if (
newBounds &&
newBounds.min != null &&
newBounds.max != null &&
boundsValidation(newBounds)
) {
const bounds = { min: newBounds.min, max: newBounds.max };

onChangeCustomBounds(bounds);
if (!newBounds || isValidBound(newBounds)) {
onChangeCustomBounds(newBounds);
}
},
[onChangeCustomBounds]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ export const rangeOperation: OperationDefinition<RangeIndexPatternColumn, 'field
has_extended_bounds: Boolean(params.includeEmptyRows && params.customBounds),
min_doc_count: Boolean(params.includeEmptyRows),
autoExtendBounds: Boolean(params.includeEmptyRows && !params.customBounds),
extended_bounds: extendedBoundsToAst(params.customBounds ?? {}),
extended_bounds: extendedBoundsToAst(
params.includeEmptyRows ? params.customBounds ?? {} : {}
),
}).toAst();
},
paramEditor: ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import {
import { GenericIndexPatternColumn } from './indexpattern';
import { operationDefinitionMap } from './operations';
import { IndexPattern, IndexPatternPrivateState, IndexPatternLayer } from './types';
import { DateHistogramIndexPatternColumn, RangeIndexPatternColumn } from './operations/definitions';
import type {
DateHistogramIndexPatternColumn,
RangeIndexPatternColumn,
TermsIndexPatternColumn,
} from './operations/definitions';
import { FormattedIndexPatternColumn } from './operations/definitions/column_types';
import { isColumnFormatted, isColumnOfType } from './operations/definitions/helpers';

Expand Down Expand Up @@ -265,7 +269,9 @@ function getExpressionForLayer(
};
}

const allDateHistogramFields = Object.values(columns)
const configColumns = Object.values(columns);

const allDateHistogramFields = configColumns
.map((column) =>
isColumnOfType<DateHistogramIndexPatternColumn>('date_histogram', column) &&
!column.params.ignoreTimeRange
Expand All @@ -274,6 +280,17 @@ function getExpressionForLayer(
)
.filter((field): field is string => Boolean(field));

// the combination of Interval with extended bounds + Top values (any combination)
// requires partial rows to be enabled
const shouldEnablePartialRows =
configColumns.some((column) => isColumnOfType<TermsIndexPatternColumn>('terms', column)) &&
configColumns.some(
(column) =>
isColumnOfType<RangeIndexPatternColumn>('range', column) &&
column.params.includeEmptyRows &&
column.params.customBounds
);

return {
type: 'expression',
chain: [
Expand All @@ -287,7 +304,7 @@ function getExpressionForLayer(
]),
aggs,
metricsAtAllLevels: false,
partialRows: false,
partialRows: shouldEnablePartialRows,
timeFields: allDateHistogramFields,
}).toAst(),
{
Expand Down

0 comments on commit 0cc33ae

Please sign in to comment.