[Lens] should register "suffix" field formatter in setup lifecycle (#110218)

Closes: #106838

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Alexey Antonov 2021-08-30 15:17:09 +03:00 committed by GitHub
parent 0082e5ac6b
commit 6d874caef5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 33 deletions

View file

@ -28,9 +28,11 @@ export const unitSuffixesLong: Record<TimeScaleUnit, string> = {
d: i18n.translate('xpack.lens.fieldFormats.longSuffix.d', { defaultMessage: 'per day' }),
};
export function getSuffixFormatter(formatFactory: FormatFactory): FieldFormatInstanceType {
export const suffixFormatterId = 'suffix';
export function getSuffixFormatter(getFormatFactory: () => FormatFactory): FieldFormatInstanceType {
return class SuffixFormatter extends FieldFormat {
static id = 'suffix';
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',
@ -51,9 +53,10 @@ export function getSuffixFormatter(formatFactory: FormatFactory): FieldFormatIns
const nestedFormatter = this.param('id');
const nestedParams = this.param('params');
const formattedValue = formatFactory({ id: nestedFormatter, params: nestedParams }).convert(
val
);
const formattedValue = getFormatFactory()({
id: nestedFormatter,
params: nestedParams,
}).convert(val);
// do not add suffixes to empty strings
if (formattedValue === '') {

View file

@ -12,7 +12,7 @@ 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 SuffixFormatter = getSuffixFormatter(() => (formatFactory as unknown) as FormatFactory);
const nestedParams = { abc: 123 };
const formatterInstance = new SuffixFormatter({
unit: 'h',
@ -30,7 +30,7 @@ describe('suffix formatter', () => {
it('should not add suffix to empty strings', () => {
const convertMock = jest.fn((x) => '');
const formatFactory = jest.fn(() => ({ convert: convertMock }));
const SuffixFormatter = getSuffixFormatter((formatFactory as unknown) as FormatFactory);
const SuffixFormatter = getSuffixFormatter(() => (formatFactory as unknown) as FormatFactory);
const nestedParams = { abc: 123 };
const formatterInstance = new SuffixFormatter({
unit: 'h',
@ -46,7 +46,7 @@ describe('suffix formatter', () => {
it('should be a hidden formatter', () => {
const convertMock = jest.fn((x) => '');
const formatFactory = jest.fn(() => ({ convert: convertMock }));
const SuffixFormatter = getSuffixFormatter((formatFactory as unknown) as FormatFactory);
const SuffixFormatter = getSuffixFormatter(() => (formatFactory as unknown) as FormatFactory);
expect(SuffixFormatter.hidden).toBe(true);
});
});

View file

@ -6,7 +6,7 @@
*/
import type { CoreSetup } from 'kibana/public';
import { Storage } from '../../../../../src/plugins/kibana_utils/public';
import { createStartServicesGetter, Storage } from '../../../../../src/plugins/kibana_utils/public';
import type { ExpressionsSetup } from '../../../../../src/plugins/expressions/public';
import type { ChartsPluginSetup } from '../../../../../src/plugins/charts/public';
import type { IndexPatternFieldEditorStart } from '../../../../../src/plugins/index_pattern_field_editor/public';
@ -14,7 +14,7 @@ import type {
DataPublicPluginSetup,
DataPublicPluginStart,
} from '../../../../../src/plugins/data/public';
import type { Datasource, EditorFrameSetup } from '../types';
import type { EditorFrameSetup } from '../types';
import type { UiActionsStart } from '../../../../../src/plugins/ui_actions/public';
import type {
FieldFormatsStart,
@ -57,29 +57,37 @@ export class IndexPatternDatasource {
counterRate,
getTimeScale,
getSuffixFormatter,
suffixFormatterId,
} = await import('../async_services');
return core
.getStartServices()
.then(([coreStart, { indexPatternFieldEditor, uiActions, data, fieldFormats }]) => {
const suffixFormatter = getSuffixFormatter(fieldFormats.deserialize);
if (!fieldFormats.has(suffixFormatter.id)) {
// todo: this code should be executed on setup phase.
fieldFormatsSetup.register([suffixFormatter]);
}
expressions.registerFunction(getTimeScale(() => getTimeZone(core.uiSettings)));
expressions.registerFunction(counterRate);
expressions.registerFunction(renameColumns);
expressions.registerFunction(formatColumn);
return getIndexPatternDatasource({
core: coreStart,
fieldFormats,
storage: new Storage(localStorage),
data,
charts,
indexPatternFieldEditor,
uiActions,
});
}) as Promise<Datasource>;
if (!fieldFormatsSetup.has(suffixFormatterId)) {
const startServices = createStartServicesGetter(core.getStartServices);
const suffixFormatter = getSuffixFormatter(
() => startServices().plugins.fieldFormats.deserialize
);
fieldFormatsSetup.register([suffixFormatter]);
}
expressions.registerFunction(getTimeScale(() => getTimeZone(core.uiSettings)));
expressions.registerFunction(counterRate);
expressions.registerFunction(renameColumns);
expressions.registerFunction(formatColumn);
const [
coreStart,
{ indexPatternFieldEditor, uiActions, data, fieldFormats },
] = await core.getStartServices();
return getIndexPatternDatasource({
core: coreStart,
fieldFormats,
storage: new Storage(localStorage),
data,
charts,
indexPatternFieldEditor,
uiActions,
});
});
}
}

View file

@ -76,7 +76,11 @@ export {
counterRate,
} from '../../common/expressions';
export { FormatColumnArgs, supportedFormats, formatColumn } from '../../common/expressions';
export { getSuffixFormatter, unitSuffixesLong } from '../../common/suffix_formatter';
export {
getSuffixFormatter,
unitSuffixesLong,
suffixFormatterId,
} from '../../common/suffix_formatter';
export { getTimeScale, TimeScaleArgs } from '../../common/expressions';
export { renameColumns } from '../../common/expressions';