forked from elastic/kibana
/
count.tsx
107 lines (101 loc) · 3.46 KB
/
count.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
/*
* 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 { i18n } from '@kbn/i18n';
import { AggFunctionsMapping } from '../../../../../../../src/plugins/data/public';
import { buildExpressionFunction } from '../../../../../../../src/plugins/expressions/public';
import { OperationDefinition } from './index';
import { FormattedIndexPatternColumn, FieldBasedIndexPatternColumn } from './column_types';
import { IndexPatternField } from '../../types';
import { getInvalidFieldMessage, getFilter } from './helpers';
import {
adjustTimeScaleLabelSuffix,
adjustTimeScaleOnOtherColumnChange,
} from '../time_scale_utils';
const countLabel = i18n.translate('xpack.lens.indexPattern.countOf', {
defaultMessage: 'Count of records',
});
export type CountIndexPatternColumn = FormattedIndexPatternColumn &
FieldBasedIndexPatternColumn & {
operationType: 'count';
};
export const countOperation: OperationDefinition<CountIndexPatternColumn, 'field'> = {
type: 'count',
priority: 2,
displayName: i18n.translate('xpack.lens.indexPattern.count', {
defaultMessage: 'Count',
}),
input: 'field',
getErrorMessage: (layer, columnId, indexPattern) =>
getInvalidFieldMessage(layer.columns[columnId] as FieldBasedIndexPatternColumn, indexPattern),
onFieldChange: (oldColumn, field) => {
return {
...oldColumn,
label: adjustTimeScaleLabelSuffix(field.displayName, undefined, oldColumn.timeScale),
sourceField: field.name,
};
},
getPossibleOperationForField: (field: IndexPatternField) => {
if (field.type === 'document') {
return {
dataType: 'number',
isBucketed: false,
scale: 'ratio',
};
}
},
getDefaultLabel: (column) => adjustTimeScaleLabelSuffix(countLabel, undefined, column.timeScale),
buildColumn({ field, previousColumn }, columnParams) {
return {
label: adjustTimeScaleLabelSuffix(countLabel, undefined, previousColumn?.timeScale),
dataType: 'number',
operationType: 'count',
isBucketed: false,
scale: 'ratio',
sourceField: field.name,
timeScale: previousColumn?.timeScale,
filter: getFilter(previousColumn, columnParams),
params:
previousColumn?.dataType === 'number' &&
previousColumn.params &&
'format' in previousColumn.params &&
previousColumn.params.format
? { format: previousColumn.params.format }
: undefined,
};
},
onOtherColumnChanged: (layer, thisColumnId, changedColumnId) =>
adjustTimeScaleOnOtherColumnChange<CountIndexPatternColumn>(
layer,
thisColumnId,
changedColumnId
),
toEsAggsFn: (column, columnId) => {
return buildExpressionFunction<AggFunctionsMapping['aggCount']>('aggCount', {
id: columnId,
enabled: true,
schema: 'metric',
}).toAst();
},
isTransferable: () => {
return true;
},
timeScalingMode: 'optional',
filterable: true,
documentation: {
section: 'elasticsearch',
signature: '',
description: i18n.translate('xpack.lens.indexPattern.count.documentation', {
defaultMessage: `
Calculates the number of documents.
Example: Calculate the number of documents:
\`count()\`
Example: Calculate the number of documents matching a certain filter:
\`count(kql='price > 500')\`
`,
}),
},
};