Skip to content

Commit

Permalink
[Lens] Add suffix formatter (elastic#82852)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Nov 12, 2020
1 parent d283252 commit 65b0e09
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,18 @@ export const formatColumn: ExpressionFunctionDefinition<
});
}
if (parentFormatParams) {
const innerParams = (col.meta.params?.params as Record<string, unknown>) ?? {};
// if original format is already a nested one, we are just replacing the wrapper params
// otherwise wrapping it inside parentFormatId/parentFormatParams
const isNested = isNestedFormat(col.meta.params);
const innerParams = isNested
? col.meta.params?.params
: { id: col.meta.params?.id, params: col.meta.params?.params };

const formatId = isNested ? col.meta.params?.id : parentFormatId;

return withParams(col, {
...col.meta.params,
id: formatId,
params: {
...innerParams,
...parentFormatParams,
Expand All @@ -132,6 +141,11 @@ export const formatColumn: ExpressionFunctionDefinition<
},
};

function isNestedFormat(params: DatatableColumn['meta']['params']) {
// if there is a nested params object with an id, it's a nested format
return !!params?.params?.id;
}

function withParams(col: DatatableColumn, params: Record<string, unknown>) {
return { ...col, meta: { ...col.meta, params } };
}
2 changes: 2 additions & 0 deletions x-pack/plugins/lens/public/indexpattern_datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ export class IndexPatternDatasource {
renameColumns,
formatColumn,
getTimeScaleFunction,
getSuffixFormatter,
} = await import('../async_services');
return core.getStartServices().then(([coreStart, { data }]) => {
data.fieldFormats.register([getSuffixFormatter(data.fieldFormats.deserialize)]);
expressions.registerFunction(getTimeScaleFunction(data));
expressions.registerFunction(renameColumns);
expressions.registerFunction(formatColumn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function columnToOperation(column: IndexPatternColumn, uniqueLabel?: stri
export * from './rename_columns';
export * from './format_column';
export * from './time_scale';
export * from './suffix_formatter';

export function getIndexPatternDatasource({
core,
Expand Down
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 });
});
});
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;
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { DataPublicPluginStart } from 'src/plugins/data/public';
import { search } from '../../../../../src/plugins/data/public';
import { buildResultColumns } from '../../../../../src/plugins/expressions/common';

type TimeScaleUnit = 's' | 'm' | 'h' | 'd';
export type TimeScaleUnit = 's' | 'm' | 'h' | 'd';

export interface TimeScaleArgs {
dateColumnId: string;
Expand Down

0 comments on commit 65b0e09

Please sign in to comment.