-
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.
Merge remote-tracking branch 'upstream/master' into nls/obs-bread
- Loading branch information
Showing
65 changed files
with
1,238 additions
and
145 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
11 changes: 11 additions & 0 deletions
11
...ugin-plugins-expressions-public.expressionfunctiondefinitions.overall_metric.md
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,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) > [overall\_metric](./kibana-plugin-plugins-expressions-public.expressionfunctiondefinitions.overall_metric.md) | ||
|
||
## ExpressionFunctionDefinitions.overall\_metric property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
overall_metric: ExpressionFunctionOverallMetric; | ||
``` |
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
11 changes: 11 additions & 0 deletions
11
...ugin-plugins-expressions-server.expressionfunctiondefinitions.overall_metric.md
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,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) > [overall\_metric](./kibana-plugin-plugins-expressions-server.expressionfunctiondefinitions.overall_metric.md) | ||
|
||
## ExpressionFunctionDefinitions.overall\_metric property | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
overall_metric: ExpressionFunctionOverallMetric; | ||
``` |
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
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
168 changes: 168 additions & 0 deletions
168
src/plugins/expressions/common/expression_functions/specs/overall_metric.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,168 @@ | ||
/* | ||
* 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 { Datatable } from '../../expression_types'; | ||
import { buildResultColumns, getBucketIdentifier } from '../series_calculation_helpers'; | ||
|
||
export interface OverallMetricArgs { | ||
by?: string[]; | ||
inputColumnId: string; | ||
outputColumnId: string; | ||
outputColumnName?: string; | ||
metric: 'sum' | 'min' | 'max' | 'average'; | ||
} | ||
|
||
export type ExpressionFunctionOverallMetric = ExpressionFunctionDefinition< | ||
'overall_metric', | ||
Datatable, | ||
OverallMetricArgs, | ||
Datatable | ||
>; | ||
|
||
function getValueAsNumberArray(value: unknown) { | ||
if (Array.isArray(value)) { | ||
return value.map((innerVal) => Number(innerVal)); | ||
} else { | ||
return [Number(value)]; | ||
} | ||
} | ||
|
||
/** | ||
* Calculates the overall metric 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 overall metric 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 overall metric of `inputColumnId` into `outputColumnId` | ||
* * If provided will use `outputColumnName` as name for the newly created column. Otherwise falls back to `outputColumnId` | ||
* * Each cell will contain the calculated metric based on the values of all cells belonging to the current series. | ||
* | ||
* 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 overall metric of | ||
* all cells of the same series. | ||
* * For all values besides `null` and `undefined`, the value will be cast to a number before it's added to the | ||
* overall metric 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 overallMetric: ExpressionFunctionOverallMetric = { | ||
name: 'overall_metric', | ||
type: 'datatable', | ||
|
||
inputTypes: ['datatable'], | ||
|
||
help: i18n.translate('expressions.functions.overallMetric.help', { | ||
defaultMessage: 'Calculates the overall sum, min, max or average of a column in a data table', | ||
}), | ||
|
||
args: { | ||
by: { | ||
help: i18n.translate('expressions.functions.overallMetric.args.byHelpText', { | ||
defaultMessage: 'Column to split the overall calculation by', | ||
}), | ||
multi: true, | ||
types: ['string'], | ||
required: false, | ||
}, | ||
metric: { | ||
help: i18n.translate('expressions.functions.overallMetric.metricHelpText', { | ||
defaultMessage: 'Metric to calculate', | ||
}), | ||
types: ['string'], | ||
options: ['sum', 'min', 'max', 'average'], | ||
}, | ||
inputColumnId: { | ||
help: i18n.translate('expressions.functions.overallMetric.args.inputColumnIdHelpText', { | ||
defaultMessage: 'Column to calculate the overall metric of', | ||
}), | ||
types: ['string'], | ||
required: true, | ||
}, | ||
outputColumnId: { | ||
help: i18n.translate('expressions.functions.overallMetric.args.outputColumnIdHelpText', { | ||
defaultMessage: 'Column to store the resulting overall metric in', | ||
}), | ||
types: ['string'], | ||
required: true, | ||
}, | ||
outputColumnName: { | ||
help: i18n.translate('expressions.functions.overallMetric.args.outputColumnNameHelpText', { | ||
defaultMessage: 'Name of the column to store the resulting overall metric in', | ||
}), | ||
types: ['string'], | ||
required: false, | ||
}, | ||
}, | ||
|
||
fn(input, { by, inputColumnId, outputColumnId, outputColumnName, metric }) { | ||
const resultColumns = buildResultColumns( | ||
input, | ||
outputColumnId, | ||
inputColumnId, | ||
outputColumnName | ||
); | ||
|
||
if (!resultColumns) { | ||
return input; | ||
} | ||
|
||
const accumulators: Partial<Record<string, number>> = {}; | ||
const valueCounter: Partial<Record<string, number>> = {}; | ||
input.rows.forEach((row) => { | ||
const bucketIdentifier = getBucketIdentifier(row, by); | ||
const accumulatorValue = accumulators[bucketIdentifier] ?? 0; | ||
|
||
const currentValue = row[inputColumnId]; | ||
if (currentValue != null) { | ||
const currentNumberValues = getValueAsNumberArray(currentValue); | ||
switch (metric) { | ||
case 'average': | ||
valueCounter[bucketIdentifier] = | ||
(valueCounter[bucketIdentifier] ?? 0) + currentNumberValues.length; | ||
case 'sum': | ||
accumulators[bucketIdentifier] = | ||
accumulatorValue + currentNumberValues.reduce((a, b) => a + b, 0); | ||
break; | ||
case 'min': | ||
accumulators[bucketIdentifier] = Math.min(accumulatorValue, ...currentNumberValues); | ||
break; | ||
case 'max': | ||
accumulators[bucketIdentifier] = Math.max(accumulatorValue, ...currentNumberValues); | ||
break; | ||
} | ||
} | ||
}); | ||
if (metric === 'average') { | ||
Object.keys(accumulators).forEach((bucketIdentifier) => { | ||
accumulators[bucketIdentifier] = | ||
accumulators[bucketIdentifier]! / valueCounter[bucketIdentifier]!; | ||
}); | ||
} | ||
return { | ||
...input, | ||
columns: resultColumns, | ||
rows: input.rows.map((row) => { | ||
const newRow = { ...row }; | ||
const bucketIdentifier = getBucketIdentifier(row, by); | ||
newRow[outputColumnId] = accumulators[bucketIdentifier]; | ||
|
||
return newRow; | ||
}), | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.