-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Add cumulative sum expression function #80129
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
17d1e0b
add cumulative sum expression function
flash1293 93dc455
add required flags
flash1293 5d29f58
Merge remote-tracking branch 'upstream/master' into lens/cumulative-sum
flash1293 950e642
review comments
flash1293 e67af0b
fix non number handling
flash1293 f48880d
Simplify tests
wylieconlon 583d42c
Merge branch 'master' into lens/cumulative-sum
kibanamachine 61f6d47
Merge remote-tracking branch 'upstream/master' into lens/cumulative-sum
flash1293 9dca454
review comments
flash1293 6e22969
Merge branch 'master' into lens/cumulative-sum
kibanamachine f9c58b1
Merge branch 'master' into lens/cumulative-sum
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
...ugin-plugins-expressions-public.expressionfunctiondefinitions.cumulative_sum.md
This file contains hidden or 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,11 @@ | ||
| <!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
|
||
| [Home](./index.md) > [kibana-plugin-plugins-expressions-public](./kibana-plugin-plugins-expressions-public.md) > [ExpressionFunctionDefinitions](./kibana-plugin-plugins-expressions-public.expressionfunctiondefinitions.md) > [cumulative\_sum](./kibana-plugin-plugins-expressions-public.expressionfunctiondefinitions.cumulative_sum.md) | ||
|
|
||
| ## ExpressionFunctionDefinitions.cumulative\_sum property | ||
|
|
||
| <b>Signature:</b> | ||
|
|
||
| ```typescript | ||
| cumulative_sum: ExpressionFunctionCumulativeSum; | ||
| ``` |
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
...ugin-plugins-expressions-server.expressionfunctiondefinitions.cumulative_sum.md
This file contains hidden or 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,11 @@ | ||
| <!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
|
||
| [Home](./index.md) > [kibana-plugin-plugins-expressions-server](./kibana-plugin-plugins-expressions-server.md) > [ExpressionFunctionDefinitions](./kibana-plugin-plugins-expressions-server.expressionfunctiondefinitions.md) > [cumulative\_sum](./kibana-plugin-plugins-expressions-server.expressionfunctiondefinitions.cumulative_sum.md) | ||
|
|
||
| ## ExpressionFunctionDefinitions.cumulative\_sum property | ||
|
|
||
| <b>Signature:</b> | ||
|
|
||
| ```typescript | ||
| cumulative_sum: ExpressionFunctionCumulativeSum; | ||
| ``` |
This file contains hidden or 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
170 changes: 170 additions & 0 deletions
170
src/plugins/expressions/common/expression_functions/specs/cumulative_sum.ts
This file contains hidden or 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,170 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| import { i18n } from '@kbn/i18n'; | ||
| import { ExpressionFunctionDefinition } from '../types'; | ||
| import { Datatable, DatatableRow } from '../../expression_types'; | ||
|
|
||
| export interface CumulativeSumArgs { | ||
| by?: string[]; | ||
| inputColumnId: string; | ||
| outputColumnId: string; | ||
| outputColumnName?: string; | ||
| } | ||
|
|
||
| export type ExpressionFunctionCumulativeSum = ExpressionFunctionDefinition< | ||
| 'cumulative_sum', | ||
| Datatable, | ||
| CumulativeSumArgs, | ||
| Datatable | ||
| >; | ||
|
|
||
| /** | ||
| * Returns a string identifying the group of a row by a list of columns to group by | ||
| */ | ||
| function getBucketIdentifier(row: DatatableRow, groupColumns?: string[]) { | ||
| return (groupColumns || []) | ||
| .map((groupColumnId) => (row[groupColumnId] == null ? '' : String(row[groupColumnId]))) | ||
| .join('|'); | ||
| } | ||
|
|
||
| /** | ||
| * Calculates the cumulative sum of a specified column in the data table. | ||
| * | ||
| * Also supports multiple series in a single data table - use the `by` argument | ||
| * to specify the columns to split the calculation by. | ||
| * For each unique combination of all `by` columns a separate cumulative sum will be calculated. | ||
| * The order of rows won't be changed - this function is not modifying any existing columns, it's only | ||
| * adding the specified `outputColumnId` column to every row of the table without adding or removing rows. | ||
| * | ||
| * Behavior: | ||
| * * Will write the cumulative sum of `inputColumnId` into `outputColumnId` | ||
| * * If provided will use `outputColumnName` as name for the newly created column. Otherwise falls back to `outputColumnId` | ||
| * * Cumulative sums always start with 0, a cell will contain its own value plus the values of | ||
| * all cells of the same series further up in the table. | ||
| * | ||
| * Edge cases: | ||
| * * Will return the input table if `inputColumnId` does not exist | ||
| * * Will throw an error if `outputColumnId` exists already in provided data table | ||
| * * If the row value contains `null` or `undefined`, it will be ignored and overwritten with the cumulative sum of | ||
| * all cells of the same series further up in the table. | ||
| * * For all values besides `null` and `undefined`, the value will be cast to a number before it's added to the | ||
| * cumulative sum of the current series - if this results in `NaN` (like in case of objects), all cells of the | ||
| * current series will be set to `NaN`. | ||
| * * To determine separate series defined by the `by` columns, the values of these columns will be cast to strings | ||
| * before comparison. If the values are objects, the return value of their `toString` method will be used for comparison. | ||
| * Missing values (`null` and `undefined`) will be treated as empty strings. | ||
| */ | ||
| export const cumulativeSum: ExpressionFunctionCumulativeSum = { | ||
| name: 'cumulative_sum', | ||
| type: 'datatable', | ||
|
|
||
| inputTypes: ['datatable'], | ||
|
|
||
| help: i18n.translate('expressions.functions.cumulativeSum.help', { | ||
| defaultMessage: 'Calculates the cumulative sum of a column in a data table', | ||
| }), | ||
|
|
||
| args: { | ||
| by: { | ||
| help: i18n.translate('expressions.functions.cumulativeSum.args.byHelpText', { | ||
| defaultMessage: 'Column to split the cumulative sum calculation by', | ||
| }), | ||
| multi: true, | ||
| types: ['string'], | ||
| required: false, | ||
| }, | ||
| inputColumnId: { | ||
| help: i18n.translate('expressions.functions.cumulativeSum.args.inputColumnIdHelpText', { | ||
| defaultMessage: 'Column to calculate the cumulative sum of', | ||
| }), | ||
| types: ['string'], | ||
| required: true, | ||
| }, | ||
| outputColumnId: { | ||
| help: i18n.translate('expressions.functions.cumulativeSum.args.outputColumnIdHelpText', { | ||
| defaultMessage: 'Column to store the resulting cumulative sum in', | ||
| }), | ||
| types: ['string'], | ||
| required: true, | ||
| }, | ||
| outputColumnName: { | ||
| help: i18n.translate('expressions.functions.cumulativeSum.args.outputColumnNameHelpText', { | ||
| defaultMessage: 'Name of the column to store the resulting cumulative sum in', | ||
| }), | ||
| types: ['string'], | ||
| required: false, | ||
| }, | ||
| }, | ||
|
|
||
| fn(input, { by, inputColumnId, outputColumnId, outputColumnName }) { | ||
| if (input.columns.some((column) => column.id === outputColumnId)) { | ||
| throw new Error( | ||
| i18n.translate('expressions.functions.cumulativeSum.columnConflictMessage', { | ||
| defaultMessage: | ||
| 'Specified outputColumnId {columnId} already exists. Please pick another column id.', | ||
| values: { | ||
| columnId: outputColumnId, | ||
| }, | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| const inputColumnDefinition = input.columns.find((column) => column.id === inputColumnId); | ||
|
|
||
| if (!inputColumnDefinition) { | ||
| return input; | ||
| } | ||
|
|
||
| const outputColumnDefinition = { | ||
| ...inputColumnDefinition, | ||
| id: outputColumnId, | ||
| name: outputColumnName || outputColumnId, | ||
| }; | ||
|
|
||
| const resultColumns = [...input.columns]; | ||
| // add output column after input column in the table | ||
| resultColumns.splice( | ||
| resultColumns.indexOf(inputColumnDefinition) + 1, | ||
| 0, | ||
| outputColumnDefinition | ||
| ); | ||
|
|
||
| const accumulators: Partial<Record<string, number>> = {}; | ||
flash1293 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return { | ||
| ...input, | ||
| columns: resultColumns, | ||
| rows: input.rows.map((row) => { | ||
| const newRow = { ...row }; | ||
|
|
||
| const bucketIdentifier = getBucketIdentifier(row, by); | ||
| const accumulatorValue = accumulators[bucketIdentifier] ?? 0; | ||
| const currentValue = newRow[inputColumnId]; | ||
| if (currentValue != null) { | ||
| newRow[outputColumnId] = Number(currentValue) + accumulatorValue; | ||
| accumulators[bucketIdentifier] = newRow[outputColumnId]; | ||
| } else { | ||
| newRow[outputColumnId] = accumulatorValue; | ||
| } | ||
|
|
||
| return newRow; | ||
| }), | ||
| }; | ||
| }, | ||
| }; | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.