forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] Add suffix formatter (elastic#82852)
- Loading branch information
Showing
6 changed files
with
98 additions
and
2 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
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
28 changes: 28 additions & 0 deletions
28
x-pack/plugins/lens/public/indexpattern_datasource/suffix_formatter.test.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,28 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { FormatFactory } from '../types'; | ||
import { getSuffixFormatter } from './suffix_formatter'; | ||
|
||
describe('suffix formatter', () => { | ||
it('should call nested formatter and apply suffix', () => { | ||
const convertMock = jest.fn((x) => x); | ||
const formatFactory = jest.fn(() => ({ convert: convertMock })); | ||
const SuffixFormatter = getSuffixFormatter((formatFactory as unknown) as FormatFactory); | ||
const nestedParams = { abc: 123 }; | ||
const formatterInstance = new SuffixFormatter({ | ||
unit: 'h', | ||
id: 'nestedFormatter', | ||
params: nestedParams, | ||
}); | ||
|
||
const result = formatterInstance.convert(12345); | ||
|
||
expect(result).toEqual('12345/h'); | ||
expect(convertMock).toHaveBeenCalledWith(12345); | ||
expect(formatFactory).toHaveBeenCalledWith({ id: 'nestedFormatter', params: nestedParams }); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
x-pack/plugins/lens/public/indexpattern_datasource/suffix_formatter.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,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { FieldFormat, KBN_FIELD_TYPES } from '../../../../../src/plugins/data/public'; | ||
import { FormatFactory } from '../types'; | ||
import { TimeScaleUnit } from './time_scale'; | ||
|
||
const unitSuffixes: Record<TimeScaleUnit, string> = { | ||
s: i18n.translate('xpack.lens.fieldFormats.suffix.s', { defaultMessage: '/h' }), | ||
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 function getSuffixFormatter(formatFactory: FormatFactory) { | ||
return class SuffixFormatter extends FieldFormat { | ||
static id = 'suffix'; | ||
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 = formatFactory({ id: nestedFormatter, params: nestedParams }).convert( | ||
val | ||
); | ||
|
||
if (suffix) { | ||
return `${formattedValue}${suffix}`; | ||
} | ||
return formattedValue; | ||
}; | ||
}; | ||
} |
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