Skip to content

Commit

Permalink
[Lens] should register "suffix" field formatter in setup lifecycle (#…
Browse files Browse the repository at this point in the history
…110218)

Closes: #106838

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
alexwizp and kibanamachine authored Aug 30, 2021
1 parent 0082e5a commit 6d874ca
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 33 deletions.
13 changes: 8 additions & 5 deletions x-pack/plugins/lens/common/suffix_formatter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 === '') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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);
});
});
56 changes: 32 additions & 24 deletions x-pack/plugins/lens/public/indexpattern_datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
*/

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';
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,
Expand Down Expand Up @@ -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,
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 6d874ca

Please sign in to comment.