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] Automatically enable show array values for non-numeric runtime fields #149025

Merged
merged 3 commits into from Jan 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions x-pack/plugins/lens/public/datasources/form_based/mocks.ts
Expand Up @@ -81,6 +81,26 @@ export const createMockedIndexPattern = (someProps?: Partial<IndexPattern>): Ind
lang: 'painless' as const,
script: '1234',
},
{
name: 'runtime-keyword',
displayName: 'Runtime keyword field',
type: 'string',
searchable: true,
aggregatable: true,
runtime: true,
lang: 'painless' as const,
script: 'emit("123")',
},
{
name: 'runtime-number',
displayName: 'Runtime number field',
type: 'number',
searchable: true,
aggregatable: true,
runtime: true,
lang: 'painless' as const,
script: 'emit(123)',
},
];
return {
id: '1',
Expand Down
Expand Up @@ -335,7 +335,7 @@ describe('math completion', () => {
triggerCharacter: '(',
})
);
expect(results.list).toEqual(['bytes', 'memory']);
expect(results.list).toEqual(['bytes', 'memory', 'runtime-number']);
});

it('should autocomplete only operations that provide numeric or date output', async () => {
Expand All @@ -346,7 +346,13 @@ describe('math completion', () => {
triggerCharacter: '(',
})
);
expect(results.list).toEqual(['bytes', 'memory', 'timestamp', 'start_date']);
expect(results.list).toEqual([
'bytes',
'memory',
'runtime-number',
'timestamp',
'start_date',
]);
});

it('should autocomplete shift parameter with relative suggestions and a couple of abs ones', async () => {
Expand Down
Expand Up @@ -290,6 +290,44 @@ describe('last_value', () => {
).params.showArrayValues
).toBeTruthy();
});

it('should set show array values if field is runtime and not of type number', () => {
const oldColumn: LastValueIndexPatternColumn = {
operationType: 'last_value',
sourceField: 'bytes',
label: 'Last value of bytes',
isBucketed: false,
dataType: 'number',
params: {
sortField: 'datefield',
showArrayValues: false,
},
};
const indexPattern = createMockedIndexPattern();
const field = indexPattern.fields.find((i) => i.name === 'runtime-keyword')!;

expect(
lastValueOperation.onFieldChange(oldColumn, field).params.showArrayValues
).toBeTruthy();
});

it('should not set show array values if field is runtime and of type number', () => {
const oldColumn: LastValueIndexPatternColumn = {
operationType: 'last_value',
sourceField: 'bytes',
label: 'Last value of bytes',
isBucketed: false,
dataType: 'number',
params: {
sortField: 'datefield',
showArrayValues: false,
},
};
const indexPattern = createMockedIndexPattern();
const field = indexPattern.fields.find((i) => i.name === 'runtime-number')!;

expect(lastValueOperation.onFieldChange(oldColumn, field).params.showArrayValues).toBeFalsy();
});
});

describe('getPossibleOperationForField', () => {
Expand Down Expand Up @@ -480,11 +518,19 @@ describe('last_value', () => {
);
});

it('should set showArrayValues if field is scripted or comes from existing params', () => {
it('should set showArrayValues if field is scripted, non-numeric runtime or comes from existing params', () => {
const indexPattern = createMockedIndexPattern();

const scriptedField = indexPattern.fields.find((field) => field.scripted);
const nonScriptedField = indexPattern.fields.find((field) => !field.scripted);
const runtimeKeywordField = indexPattern.fields.find(
(field) => field.runtime && field.type !== 'number'
);
const runtimeNumericField = indexPattern.fields.find(
(field) => field.runtime && field.type === 'number'
);
const nonScriptedField = indexPattern.fields.find(
(field) => !field.scripted && !field.runtime
);

const localLayer = {
columns: {
Expand All @@ -508,6 +554,22 @@ describe('last_value', () => {
}).params.showArrayValues
).toBeTruthy();

expect(
lastValueOperation.buildColumn({
indexPattern,
layer: localLayer,
field: runtimeKeywordField!,
}).params.showArrayValues
).toBeTruthy();

expect(
lastValueOperation.buildColumn({
indexPattern,
layer: localLayer,
field: runtimeNumericField!,
}).params.showArrayValues
).toBeFalsy();

expect(
lastValueOperation.buildColumn(
{
Expand Down
Expand Up @@ -30,7 +30,7 @@ import {
} from './helpers';
import { adjustTimeScaleLabelSuffix } from '../time_scale_utils';
import { getDisallowedPreviousShiftMessage } from '../../time_shift_utils';
import { isScriptedField } from './terms/helpers';
import { isRuntimeField, isScriptedField } from './terms/helpers';
import { FormRow } from './shared_components/form_row';
import { getColumnReducedTimeRangeError } from '../../reduced_time_range_utils';
import { getGroupByKey } from './get_group_by_key';
Expand Down Expand Up @@ -107,6 +107,17 @@ function getDateFields(indexPattern: IndexPattern): IndexPatternField[] {
return dateFields;
}

function setDefaultShowArrayValues(
field: IndexPatternField,
oldParams: LastValueIndexPatternColumn['params']
) {
return (
isScriptedField(field) ||
(isRuntimeField(field) && field.type !== 'number') ||
oldParams?.showArrayValues
);
}

export interface LastValueIndexPatternColumn extends FieldBasedIndexPatternColumn {
operationType: 'last_value';
params: {
Expand Down Expand Up @@ -154,7 +165,7 @@ export const lastValueOperation: OperationDefinition<
onFieldChange: (oldColumn, field) => {
const newParams = { ...oldColumn.params };

newParams.showArrayValues = isScriptedField(field) || oldColumn.params.showArrayValues;
newParams.showArrayValues = setDefaultShowArrayValues(field, oldColumn.params);

if ('format' in newParams && field.type !== 'number') {
delete newParams.format;
Expand Down Expand Up @@ -221,7 +232,7 @@ export const lastValueOperation: OperationDefinition<
);
}

const showArrayValues = isScriptedField(field) || lastValueParams?.showArrayValues;
const showArrayValues = setDefaultShowArrayValues(field, lastValueParams);

return {
label: ofName(field.displayName, previousColumn?.timeShift, previousColumn?.reducedTimeRange),
Expand Down
Expand Up @@ -265,6 +265,10 @@ export function isScriptedField(
return fieldName.scripted;
}

export function isRuntimeField(field: IndexPatternField): boolean {
return Boolean(field.runtime);
}

export function getFieldsByValidationState(
newIndexPattern: IndexPattern,
column?: GenericIndexPatternColumn,
Expand Down