forked from elastic/kibana
/
percentile.tsx
212 lines (201 loc) · 6.79 KB
/
percentile.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { EuiFieldNumber, EuiFormRow } from '@elastic/eui';
import React, { useCallback, useState } from 'react';
import { i18n } from '@kbn/i18n';
import { AggFunctionsMapping } from 'src/plugins/data/public';
import { buildExpressionFunction } from '../../../../../../../src/plugins/expressions/public';
import { OperationDefinition } from './index';
import {
getFormatFromPreviousColumn,
getInvalidFieldMessage,
getSafeName,
isValidNumber,
useDebounceWithOptions,
getFilter,
} from './helpers';
import { FieldBasedIndexPatternColumn } from './column_types';
export interface PercentileIndexPatternColumn extends FieldBasedIndexPatternColumn {
operationType: 'percentile';
params: {
percentile: number;
format?: {
id: string;
params?: {
decimals: number;
};
};
};
}
function ofName(name: string, percentile: number) {
return i18n.translate('xpack.lens.indexPattern.percentileOf', {
defaultMessage:
'{percentile, selectordinal, one {#st} two {#nd} few {#rd} other {#th}} percentile of {name}',
values: { name, percentile },
});
}
const DEFAULT_PERCENTILE_VALUE = 95;
const supportedFieldTypes = ['number', 'histogram'];
export const percentileOperation: OperationDefinition<PercentileIndexPatternColumn, 'field'> = {
type: 'percentile',
displayName: i18n.translate('xpack.lens.indexPattern.percentile', {
defaultMessage: 'Percentile',
}),
input: 'field',
operationParams: [
{ name: 'percentile', type: 'number', required: false, defaultValue: DEFAULT_PERCENTILE_VALUE },
],
filterable: true,
getPossibleOperationForField: ({ aggregationRestrictions, aggregatable, type: fieldType }) => {
if (supportedFieldTypes.includes(fieldType) && aggregatable && !aggregationRestrictions) {
return {
dataType: 'number',
isBucketed: false,
scale: 'ratio',
};
}
},
isTransferable: (column, newIndexPattern) => {
const newField = newIndexPattern.getFieldByName(column.sourceField);
return Boolean(
newField &&
supportedFieldTypes.includes(newField.type) &&
newField.aggregatable &&
!newField.aggregationRestrictions
);
},
getDefaultLabel: (column, indexPattern, columns) =>
ofName(getSafeName(column.sourceField, indexPattern), column.params.percentile),
buildColumn: ({ field, previousColumn, indexPattern }, columnParams) => {
const existingPercentileParam =
previousColumn?.operationType === 'percentile' &&
previousColumn.params &&
'percentile' in previousColumn.params &&
previousColumn.params.percentile;
const newPercentileParam =
columnParams?.percentile ?? (existingPercentileParam || DEFAULT_PERCENTILE_VALUE);
return {
label: ofName(getSafeName(field.name, indexPattern), newPercentileParam),
dataType: 'number',
operationType: 'percentile',
sourceField: field.name,
isBucketed: false,
scale: 'ratio',
filter: getFilter(previousColumn, columnParams),
params: {
percentile: newPercentileParam,
...getFormatFromPreviousColumn(previousColumn),
},
};
},
onFieldChange: (oldColumn, field) => {
return {
...oldColumn,
label: ofName(field.displayName, oldColumn.params.percentile),
sourceField: field.name,
};
},
toEsAggsFn: (column, columnId, _indexPattern) => {
return buildExpressionFunction<AggFunctionsMapping['aggSinglePercentile']>(
'aggSinglePercentile',
{
id: columnId,
enabled: true,
schema: 'metric',
field: column.sourceField,
percentile: column.params.percentile,
}
).toAst();
},
getErrorMessage: (layer, columnId, indexPattern) =>
getInvalidFieldMessage(layer.columns[columnId] as FieldBasedIndexPatternColumn, indexPattern),
paramEditor: function PercentileParamEditor({
layer,
updateLayer,
currentColumn,
columnId,
indexPattern,
}) {
const [inputValue, setInputValue] = useState(String(currentColumn.params.percentile));
const inputValueAsNumber = Number(inputValue);
// an input is value if it's not an empty string, parses to a valid number, is between 0 and 100 (exclusive)
// and is an integer
const inputValueIsValid = isValidNumber(inputValue, true, 99, 1);
useDebounceWithOptions(
() => {
if (!inputValueIsValid) return;
updateLayer({
...layer,
columns: {
...layer.columns,
[columnId]: {
...currentColumn,
label: currentColumn.customLabel
? currentColumn.label
: ofName(
indexPattern.getFieldByName(currentColumn.sourceField)?.displayName ||
currentColumn.sourceField,
inputValueAsNumber
),
params: {
...currentColumn.params,
percentile: inputValueAsNumber,
},
},
},
});
},
{ skipFirstRender: true },
256,
[inputValue]
);
const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const val = String(e.target.value);
setInputValue(val);
}, []);
return (
<EuiFormRow
label={i18n.translate('xpack.lens.indexPattern.percentile.percentileValue', {
defaultMessage: 'Percentile',
})}
data-test-subj="lns-indexPattern-percentile-form"
display="columnCompressed"
fullWidth
isInvalid={!inputValueIsValid}
error={
!inputValueIsValid &&
i18n.translate('xpack.lens.indexPattern.percentile.errorMessage', {
defaultMessage: 'Percentile has to be an integer between 1 and 99',
})
}
>
<EuiFieldNumber
data-test-subj="lns-indexPattern-percentile-input"
compressed
value={inputValue}
min={1}
max={99}
step={1}
onChange={handleInputChange}
/>
</EuiFormRow>
);
},
documentation: {
section: 'elasticsearch',
signature: i18n.translate('xpack.lens.indexPattern.percentile.signature', {
defaultMessage: 'field: string, [percentile]: number',
}),
description: i18n.translate('xpack.lens.indexPattern.percentile.documentation', {
defaultMessage: `
Returns the specified percentile of the values of a field. This is the value n percent of the values occuring in documents are smaller.
Example: Get the number of bytes larger than 95 % of values:
\`percentile(bytes, percentile=95)\`
`,
}),
},
};