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] Allow date functions in formula #143632

Merged
merged 15 commits into from
Nov 3, 2022
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 @@ -14,6 +14,7 @@ import { Datatable, DatatableColumn, DatatableColumnType, getType } from '../../
export type MathColumnArguments = MathArguments & {
id: string;
name?: string;
castColumns?: string[];
copyMetaFrom?: string | null;
};

Expand Down Expand Up @@ -52,6 +53,14 @@ export const mathColumn: ExpressionFunctionDefinition<
}),
required: true,
},
castColumns: {
dokmic marked this conversation as resolved.
Show resolved Hide resolved
types: ['string'],
multi: true,
help: i18n.translate('expressions.functions.mathColumn.args.castColumnsHelpText', {
defaultMessage: 'The ids of columns to cast to numbers before applying the formula',
}),
required: false,
},
copyMetaFrom: {
types: ['string', 'null'],
help: i18n.translate('expressions.functions.mathColumn.args.copyMetaFromHelpText', {
Expand All @@ -77,11 +86,31 @@ export const mathColumn: ExpressionFunctionDefinition<

const newRows = await Promise.all(
input.rows.map(async (row) => {
let preparedRow = row;
if (args.castColumns) {
preparedRow = { ...row };
args.castColumns.forEach((columnId) => {
switch (typeof row[columnId]) {
case 'string':
const parsedAsDate = Number(new Date(preparedRow[columnId]));
if (!isNaN(parsedAsDate)) {
preparedRow[columnId] = parsedAsDate;
return;
} else {
preparedRow[columnId] = Number(preparedRow[columnId]);
return;
}
case 'boolean':
preparedRow[columnId] = Number(preparedRow[columnId]);
return;
}
});
}
const result = await math.fn(
{
...input,
columns: input.columns,
rows: [row],
rows: [preparedRow],
},
{
expression: args.expression,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ describe('math completion', () => {
expect(results.list).toEqual(['bytes', 'memory']);
});

it('should autocomplete only operations that provide numeric output', async () => {
it('should autocomplete only operations that provide numeric or date output', async () => {
const results = await suggest({
expression: 'last_value()',
zeroIndexedOffset: 11,
Expand All @@ -366,7 +366,7 @@ describe('math completion', () => {
unifiedSearch: unifiedSearchPluginMock.createStartContract(),
dataViews: dataViewPluginMocks.createStartContract(),
});
expect(results.list).toEqual(['bytes', 'memory']);
expect(results.list).toEqual(['bytes', 'memory', 'timestamp', 'start_date']);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,14 @@ function getArgumentSuggestions(
operationDefinitionMap
);
// TODO: This only allow numeric functions, will reject last_value(string) for example.
const validOperation = available.find(
const validOperation = available.filter(
({ operationMetaData }) =>
operationMetaData.dataType === 'number' && !operationMetaData.isBucketed
(operationMetaData.dataType === 'number' || operationMetaData.dataType === 'date') &&
!operationMetaData.isBucketed
);
if (validOperation) {
const fields = validOperation.operations
if (validOperation.length) {
const fields = validOperation
.flatMap((op) => op.operations)
.filter((op) => op.operationType === operation.type)
.map((op) => ('field' in op ? op.field : undefined))
.filter(nonNullable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('formula', () => {
formulaOperation.buildColumn({
previousColumn: {
...layer.columns.col1,
dataType: 'date',
dataType: 'boolean',
filter: { language: 'kuery', query: 'ABC: DEF' },
},
layer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ export const formulaOperation: OperationDefinition<FormulaIndexPatternColumn, 'm
arguments: {
id: [columnId],
name: [label || defaultLabel],
...(currentColumn.references.length
? {
castColumns: [currentColumn.references[0]],
}
: {}),
expression: [currentColumn.references.length ? `"${currentColumn.references[0]}"` : ''],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ export function generateFormula(
previousFormula += `${previousColumn.operationType}(${metric.operationType}(${fieldName})`;
}
} else {
if (previousColumn && 'sourceField' in previousColumn && previousColumn.dataType === 'number') {
if (
previousColumn &&
'sourceField' in previousColumn &&
(previousColumn.dataType === 'number' || previousColumn.dataType === 'date')
) {
previousFormula += `${previousColumn.operationType}(${getSafeFieldName(previousColumn)}`;
} else {
// couldn't find formula function to call, exit early because adding args is going to fail anyway
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ describe('math operation', () => {
arguments: {
id: ['myColumnId'],
name: ['Math'],
castColumns: [],
expression: [
'(((((((((((((((("columnX0" + "columnX1") + "columnX2") + "columnX3") + "columnX4") + "columnX5") + "columnX6") + "columnX7") + "columnX8") + "columnX9") + "columnX10") + "columnX11") + "columnX12") + "columnX13") + "columnX14") + "columnX15") + "columnX16")',
],
Expand Down Expand Up @@ -243,6 +244,7 @@ describe('math operation', () => {
arguments: {
id: ['myColumnId'],
name: ['Math'],
castColumns: [],
expression: [
`("columnX0" + (("columnX1" - "columnX2") / ("columnX3" - ("columnX4" * "columnX5"))))`,
],
Expand Down Expand Up @@ -298,6 +300,7 @@ describe('math operation', () => {
arguments: {
id: ['myColumnId'],
name: ['Math'],
castColumns: [],
expression: [`max(min("columnX0","columnX1"),abs("columnX2"))`],
onError: ['null'],
},
Expand Down Expand Up @@ -342,6 +345,7 @@ describe('math operation', () => {
arguments: {
id: ['myColumnId'],
name: ['Math'],
castColumns: [],
expression: [`(5 + (3 / 8))`],
onError: ['null'],
},
Expand Down Expand Up @@ -425,6 +429,7 @@ describe('math operation', () => {
arguments: {
id: ['myColumnId'],
name: ['Math'],
castColumns: [],
expression: [
'ifelse(("columnX0" == 0),ifelse(("columnX1" < 0),ifelse(("columnX2" <= 0),"columnX3","columnX4"),"columnX5"),ifelse(("columnX6" > 0),ifelse(("columnX7" >= 0),"columnX8","columnX9"),"columnX10"))',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export const mathOperation: OperationDefinition<MathIndexPatternColumn, 'managed
name: [column.label],
expression: [astToString(column.params.tinymathAst)],
onError: ['null'],
// cast everything into number
castColumns: column.references,
},
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ describe('last_value', () => {
).toEqual({
dataType: 'ip',
isBucketed: false,
scale: 'ratio',
scale: 'ordinal',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is basically fixing a bug

});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ function getExistsFilter(field: string) {
};
}

function getScale(type: string) {
return type === 'string' ||
type === 'ip' ||
type === 'ip_range' ||
type === 'date_range' ||
type === 'number_range'
? 'ordinal'
: 'ratio';
}

export const lastValueOperation: OperationDefinition<
LastValueIndexPatternColumn,
'field',
Expand Down Expand Up @@ -155,7 +165,7 @@ export const lastValueOperation: OperationDefinition<
label: ofName(field.displayName, oldColumn.timeShift, oldColumn.reducedTimeRange),
sourceField: field.name,
params: newParams,
scale: field.type === 'string' ? 'ordinal' : 'ratio',
scale: getScale(field.type),
filter:
oldColumn.filter && isEqual(oldColumn.filter, getExistsFilter(oldColumn.sourceField))
? getExistsFilter(field.name)
Expand All @@ -167,7 +177,7 @@ export const lastValueOperation: OperationDefinition<
return {
dataType: type as DataType,
isBucketed: false,
scale: type === 'string' ? 'ordinal' : 'ratio',
scale: getScale(type),
};
}
},
Expand Down Expand Up @@ -218,7 +228,7 @@ export const lastValueOperation: OperationDefinition<
dataType: field.type as DataType,
operationType: 'last_value',
isBucketed: false,
scale: field.type === 'string' ? 'ordinal' : 'ratio',
scale: getScale(field.type),
sourceField: field.name,
filter: getFilter(previousColumn, columnParams) || getExistsFilter(field.name),
timeShift: columnParams?.shift || previousColumn?.timeShift,
Expand Down