-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
index.ts
72 lines (62 loc) · 2.58 KB
/
index.ts
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
/*
* 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 { KBN_FIELD_TYPES } from '@kbn/field-types';
import {
FieldFormat,
FieldFormatInstanceType,
} from '../../../../../src/plugins/field_formats/common';
import type { FormatFactory } from '../types';
import type { TimeScaleUnit } from '../expressions/time_scale';
const unitSuffixes: Record<TimeScaleUnit, string> = {
s: i18n.translate('xpack.lens.fieldFormats.suffix.s', { defaultMessage: '/s' }),
m: i18n.translate('xpack.lens.fieldFormats.suffix.m', { defaultMessage: '/m' }),
h: i18n.translate('xpack.lens.fieldFormats.suffix.h', { defaultMessage: '/h' }),
d: i18n.translate('xpack.lens.fieldFormats.suffix.d', { defaultMessage: '/d' }),
};
export const unitSuffixesLong: Record<TimeScaleUnit, string> = {
s: i18n.translate('xpack.lens.fieldFormats.longSuffix.s', { defaultMessage: 'per second' }),
m: i18n.translate('xpack.lens.fieldFormats.longSuffix.m', { defaultMessage: 'per minute' }),
h: i18n.translate('xpack.lens.fieldFormats.longSuffix.h', { defaultMessage: 'per hour' }),
d: i18n.translate('xpack.lens.fieldFormats.longSuffix.d', { defaultMessage: 'per day' }),
};
export const suffixFormatterId = 'suffix';
export function getSuffixFormatter(getFormatFactory: () => FormatFactory): FieldFormatInstanceType {
return class SuffixFormatter extends FieldFormat {
static id = suffixFormatterId;
static hidden = true; // Don't want this format to appear in index pattern editor
static title = i18n.translate('xpack.lens.fieldFormats.suffix.title', {
defaultMessage: 'Suffix',
});
static fieldType = KBN_FIELD_TYPES.NUMBER;
allowsNumericalAggregations = true;
getParamDefaults() {
return {
unit: undefined,
nestedParams: {},
};
}
textConvert = (val: unknown) => {
const unit = this.param('unit') as TimeScaleUnit | undefined;
const suffix = unit ? unitSuffixes[unit] : undefined;
const nestedFormatter = this.param('id');
const nestedParams = this.param('params');
const formattedValue = getFormatFactory()({
id: nestedFormatter,
params: nestedParams,
}).convert(val);
// do not add suffixes to empty strings
if (formattedValue === '') {
return '';
}
if (suffix) {
return `${formattedValue}${suffix}`;
}
return formattedValue;
};
};
}