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] Fix bug in terms formatting #82776

Merged
merged 1 commit into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -34,6 +34,13 @@ export interface TermsIndexPatternColumn extends FieldBasedIndexPatternColumn {
size: number;
orderBy: { type: 'alphabetical' } | { type: 'column'; columnId: string };
orderDirection: 'asc' | 'desc';
// Terms on numeric fields can be formatted
format?: {
id: string;
params?: {
decimals: number;
};
};
};
}

Expand Down Expand Up @@ -105,10 +112,16 @@ export const termsOperation: OperationDefinition<TermsIndexPatternColumn, 'field
};
},
onFieldChange: (oldColumn, indexPattern, field) => {
const newParams = { ...oldColumn.params };
if ('format' in newParams && field.type !== 'number') {
delete newParams.format;
}
return {
...oldColumn,
dataType: field.type as DataType,
label: ofName(field.displayName),
sourceField: field.name,
params: newParams,
};
},
onOtherColumnChanged: (currentColumn, columns) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,40 @@ describe('terms', () => {
},
};
const indexPattern = createMockedIndexPattern();
const newDateField = indexPattern.fields.find((i) => i.name === 'dest')!;
const newNumberField = indexPattern.fields.find((i) => i.name === 'bytes')!;

const column = termsOperation.onFieldChange(oldColumn, indexPattern, newDateField);
expect(column).toHaveProperty('sourceField', 'dest');
const column = termsOperation.onFieldChange(oldColumn, indexPattern, newNumberField);
expect(column).toHaveProperty('dataType', 'number');
expect(column).toHaveProperty('sourceField', 'bytes');
expect(column).toHaveProperty('params.size', 5);
expect(column).toHaveProperty('params.orderBy.type', 'alphabetical');
expect(column).toHaveProperty('params.orderDirection', 'asc');
expect(column.label).toContain('dest');
expect(column.label).toContain('bytes');
});

it('should remove numeric parameters when changing away from number', () => {
const oldColumn: TermsIndexPatternColumn = {
operationType: 'terms',
sourceField: 'bytes',
label: 'Top values of bytes',
isBucketed: true,
dataType: 'number',
params: {
size: 5,
orderBy: {
type: 'alphabetical',
},
orderDirection: 'asc',
format: { id: 'number', params: { decimals: 0 } },
},
};
const indexPattern = createMockedIndexPattern();
const newStringField = indexPattern.fields.find((i) => i.name === 'source')!;

const column = termsOperation.onFieldChange(oldColumn, indexPattern, newStringField);
expect(column).toHaveProperty('dataType', 'string');
expect(column).toHaveProperty('sourceField', 'source');
expect(column.params.format).toBeUndefined();
});
});

Expand Down