Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Carry over filter extra fields with in incomplete state #102509

Merged
merged 4 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,11 @@ export function DimensionEditor(props: DimensionEditorProps) {
};

const incompleteInfo = (state.layers[layerId].incompleteColumns ?? {})[columnId];
const incompleteOperation = incompleteInfo?.operationType;
const incompleteField = incompleteInfo?.sourceField ?? null;
const {
operationType: incompleteOperation,
sourceField: incompleteField = null,
...incompleteParams
} = incompleteInfo || {};

const ParamEditor = selectedOperationDefinition?.paramEditor;

Expand Down Expand Up @@ -486,6 +489,7 @@ export function DimensionEditor(props: DimensionEditorProps) {
field: currentIndexPattern.getFieldByName(choice.field),
visualizationGroups: dimensionGroups,
targetGroup: props.groupId,
incompleteParams,
})
);
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,46 @@ describe('state_helpers', () => {
).toEqual(expect.objectContaining({ columnOrder: ['col1', 'col2', 'col3'] }));
});

it('should inherit filters from the incomplete column when passed', () => {
expect(
insertNewColumn({
layer: {
indexPatternId: '1',
columnOrder: ['col1'],
columns: {
col1: {
label: 'Date histogram of timestamp',
dataType: 'date',
isBucketed: true,

// Private
operationType: 'date_histogram',
sourceField: 'timestamp',
params: {
interval: 'h',
},
},
},
},
columnId: 'col2',
indexPattern,
op: 'average',
field: indexPattern.fields[2],
visualizationGroups: [],
incompleteParams: { filter: { language: 'kuery', query: '' }, timeShift: '3d' },
})
).toEqual(
expect.objectContaining({
columns: expect.objectContaining({
col2: expect.objectContaining({
filter: { language: 'kuery', query: '' },
timeShift: '3d',
}),
}),
})
);
});

describe('inserting a new reference', () => {
it('should throw if the required references are impossible to match', () => {
// @ts-expect-error this function is not valid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { partition, mapValues, pickBy } from 'lodash';
import { CoreStart } from 'kibana/public';
import { Query } from 'src/plugins/data/common';
import type {
FramePublicAPI,
OperationMetadata,
Expand All @@ -18,6 +19,7 @@ import {
OperationType,
IndexPatternColumn,
RequiredReference,
GenericOperationDefinition,
} from './definitions';
import type {
IndexPattern,
Expand All @@ -29,6 +31,13 @@ import { getSortScoreByPriority } from './operations';
import { generateId } from '../../id_generator';
import { ReferenceBasedIndexPatternColumn } from './definitions/column_types';
import { FormulaIndexPatternColumn, regenerateLayerFromAst } from './definitions/formula';
import { TimeScaleUnit } from '../time_scale';

interface ColumnAdvancedParams {
filter?: Query | undefined;
timeShift?: string | undefined;
timeScale?: TimeScaleUnit | undefined;
}

interface ColumnChange {
op: OperationType;
Expand All @@ -39,6 +48,7 @@ interface ColumnChange {
visualizationGroups: VisualizationDimensionGroupConfig[];
targetGroup?: string;
shouldResetLabel?: boolean;
incompleteParams?: ColumnAdvancedParams;
}

interface ColumnCopy {
Expand Down Expand Up @@ -141,6 +151,24 @@ export function insertOrReplaceColumn(args: ColumnChange): IndexPatternLayer {
return insertNewColumn(args);
}

function ensureCompatibleFiltersAreMoved<T extends ColumnAdvancedParams>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ensureCompatibleParamsAreMoved

column: T,
referencedOperation: GenericOperationDefinition,
previousColumn: ColumnAdvancedParams
) {
const newColumn = { ...column };
if (referencedOperation.filterable) {
newColumn.filter = (previousColumn as ReferenceBasedIndexPatternColumn).filter;
}
if (referencedOperation.shiftable) {
newColumn.timeShift = (previousColumn as ReferenceBasedIndexPatternColumn).timeShift;
}
if (referencedOperation.timeScalingMode !== 'disabled') {
newColumn.timeScale = (previousColumn as ReferenceBasedIndexPatternColumn).timeScale;
}
return newColumn;
}

// Insert a column into an empty ID. The field parameter is required when constructing
// a field-based operation, but will cause the function to fail for any other type of operation.
export function insertNewColumn({
Expand All @@ -152,6 +180,7 @@ export function insertNewColumn({
visualizationGroups,
targetGroup,
shouldResetLabel,
incompleteParams,
}: ColumnChange): IndexPatternLayer {
const operationDefinition = operationDefinitionMap[op];

Expand All @@ -163,7 +192,10 @@ export function insertNewColumn({
throw new Error(`Can't insert a column with an ID that is already in use`);
}

const baseOptions = { indexPattern, previousColumn: layer.columns[columnId] };
const baseOptions = {
indexPattern,
previousColumn: { ...incompleteParams, ...layer.columns[columnId] },
};

if (operationDefinition.input === 'none' || operationDefinition.input === 'managedReference') {
if (field) {
Expand Down Expand Up @@ -414,15 +446,13 @@ export function replaceColumn({
indexPattern,
});

const column = copyCustomLabel({ ...referenceColumn }, previousColumn);
// do not forget to move over also any filter/shift/anything (if compatible)
// from the reference definition to the new operation.
if (referencedOperation.filterable) {
column.filter = (previousColumn as ReferenceBasedIndexPatternColumn).filter;
}
if (referencedOperation.shiftable) {
column.timeShift = (previousColumn as ReferenceBasedIndexPatternColumn).timeShift;
}
const column = ensureCompatibleFiltersAreMoved(
copyCustomLabel({ ...referenceColumn }, previousColumn),
referencedOperation,
previousColumn as ReferenceBasedIndexPatternColumn
);

tempLayer = {
...tempLayer,
Expand Down Expand Up @@ -529,15 +559,30 @@ export function replaceColumn({
}

if (!field) {
let incompleteColumn: {
operationType: OperationType;
} & ColumnAdvancedParams = { operationType: op };
// if no field is available perform a full clean of the column from the layer
if (previousDefinition.input === 'fullReference') {
tempLayer = deleteColumn({ layer: tempLayer, columnId, indexPattern });
const previousReferenceId = (previousColumn as ReferenceBasedIndexPatternColumn)
.references[0];
const referenceColumn = layer.columns[previousReferenceId];
if (referenceColumn) {
const referencedOperation = operationDefinitionMap[referenceColumn.operationType];

incompleteColumn = ensureCompatibleFiltersAreMoved(
incompleteColumn,
referencedOperation,
previousColumn
);
}
}
return {
...tempLayer,
incompleteColumns: {
...(tempLayer.incompleteColumns ?? {}),
[columnId]: { operationType: op },
[columnId]: incompleteColumn,
},
};
}
Expand Down