-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] Create mathColumn function to improve performance (#101908)
* [Lens] Create mathColumn function to improve performance * Fix empty formula case * Fix tinymath memoization Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
0db70be
commit 7baab61
Showing
8 changed files
with
248 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/plugins/expressions/common/expression_functions/specs/math_column.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { ExpressionFunctionDefinition } from '../types'; | ||
import { math, MathArguments } from './math'; | ||
import { Datatable, DatatableColumn, getType } from '../../expression_types'; | ||
|
||
export type MathColumnArguments = MathArguments & { | ||
id: string; | ||
name?: string; | ||
copyMetaFrom?: string | null; | ||
}; | ||
|
||
export const mathColumn: ExpressionFunctionDefinition< | ||
'mathColumn', | ||
Datatable, | ||
MathColumnArguments, | ||
Datatable | ||
> = { | ||
name: 'mathColumn', | ||
type: 'datatable', | ||
inputTypes: ['datatable'], | ||
help: i18n.translate('expressions.functions.mathColumnHelpText', { | ||
defaultMessage: | ||
'Adds a column calculated as the result of other columns. ' + | ||
'Changes are made only when you provide arguments.' + | ||
'See also {alterColumnFn} and {staticColumnFn}.', | ||
values: { | ||
alterColumnFn: '`alterColumn`', | ||
staticColumnFn: '`staticColumn`', | ||
}, | ||
}), | ||
args: { | ||
...math.args, | ||
id: { | ||
types: ['string'], | ||
help: i18n.translate('expressions.functions.mathColumn.args.idHelpText', { | ||
defaultMessage: 'id of the resulting column. Must be unique.', | ||
}), | ||
required: true, | ||
}, | ||
name: { | ||
types: ['string'], | ||
aliases: ['_', 'column'], | ||
help: i18n.translate('expressions.functions.mathColumn.args.nameHelpText', { | ||
defaultMessage: 'The name of the resulting column. Names are not required to be unique.', | ||
}), | ||
required: true, | ||
}, | ||
copyMetaFrom: { | ||
types: ['string', 'null'], | ||
help: i18n.translate('expressions.functions.mathColumn.args.copyMetaFromHelpText', { | ||
defaultMessage: | ||
"If set, the meta object from the specified column id is copied over to the specified target column. If the column doesn't exist it silently fails.", | ||
}), | ||
required: false, | ||
default: null, | ||
}, | ||
}, | ||
fn: (input, args, context) => { | ||
const columns = [...input.columns]; | ||
const existingColumnIndex = columns.findIndex(({ id }) => { | ||
return id === args.id; | ||
}); | ||
if (existingColumnIndex > -1) { | ||
throw new Error('ID must be unique'); | ||
} | ||
|
||
const newRows = input.rows.map((row) => { | ||
return { | ||
...row, | ||
[args.id]: math.fn( | ||
{ | ||
type: 'datatable', | ||
columns: input.columns, | ||
rows: [row], | ||
}, | ||
{ | ||
expression: args.expression, | ||
onError: args.onError, | ||
}, | ||
context | ||
), | ||
}; | ||
}); | ||
const type = newRows.length ? getType(newRows[0][args.id]) : 'null'; | ||
const newColumn: DatatableColumn = { | ||
id: args.id, | ||
name: args.name ?? args.id, | ||
meta: { type, params: { id: type } }, | ||
}; | ||
if (args.copyMetaFrom) { | ||
const metaSourceFrom = columns.find(({ id }) => id === args.copyMetaFrom); | ||
newColumn.meta = { ...newColumn.meta, ...(metaSourceFrom?.meta || {}) }; | ||
} | ||
|
||
columns.push(newColumn); | ||
|
||
return { | ||
type: 'datatable', | ||
columns, | ||
rows: newRows, | ||
} as Datatable; | ||
}, | ||
}; |
74 changes: 74 additions & 0 deletions
74
src/plugins/expressions/common/expression_functions/specs/tests/math_column.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { mathColumn } from '../math_column'; | ||
import { functionWrapper, testTable } from './utils'; | ||
|
||
describe('mathColumn', () => { | ||
const fn = functionWrapper(mathColumn); | ||
|
||
it('throws if the id is used', () => { | ||
expect(() => fn(testTable, { id: 'price', name: 'price', expression: 'price * 2' })).toThrow( | ||
`ID must be unique` | ||
); | ||
}); | ||
|
||
it('applies math to each row by id', () => { | ||
const result = fn(testTable, { id: 'output', name: 'output', expression: 'quantity * price' }); | ||
expect(result.columns).toEqual([ | ||
...testTable.columns, | ||
{ id: 'output', name: 'output', meta: { params: { id: 'number' }, type: 'number' } }, | ||
]); | ||
expect(result.rows[0]).toEqual({ | ||
in_stock: true, | ||
name: 'product1', | ||
output: 60500, | ||
price: 605, | ||
quantity: 100, | ||
time: 1517842800950, | ||
}); | ||
}); | ||
|
||
it('handles onError', () => { | ||
const args = { | ||
id: 'output', | ||
name: 'output', | ||
expression: 'quantity / 0', | ||
}; | ||
expect(() => fn(testTable, args)).toThrowError(`Cannot divide by 0`); | ||
expect(() => fn(testTable, { ...args, onError: 'throw' })).toThrow(); | ||
expect(fn(testTable, { ...args, onError: 'zero' }).rows[0].output).toEqual(0); | ||
expect(fn(testTable, { ...args, onError: 'false' }).rows[0].output).toEqual(false); | ||
expect(fn(testTable, { ...args, onError: 'null' }).rows[0].output).toEqual(null); | ||
}); | ||
|
||
it('should copy over the meta information from the specified column', async () => { | ||
const result = await fn( | ||
{ | ||
...testTable, | ||
columns: [ | ||
...testTable.columns, | ||
{ | ||
id: 'myId', | ||
name: 'myName', | ||
meta: { type: 'date', params: { id: 'number', params: { digits: 2 } } }, | ||
}, | ||
], | ||
rows: testTable.rows.map((row) => ({ ...row, myId: Date.now() })), | ||
}, | ||
{ id: 'output', name: 'name', copyMetaFrom: 'myId', expression: 'price + 2' } | ||
); | ||
|
||
expect(result.type).toBe('datatable'); | ||
expect(result.columns[result.columns.length - 1]).toEqual({ | ||
id: 'output', | ||
name: 'name', | ||
meta: { type: 'date', params: { id: 'number', params: { digits: 2 } } }, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters