Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 0 additions & 153 deletions x-pack/plugins/lens/public/editor_frame_service/format_column.ts

This file was deleted.

2 changes: 0 additions & 2 deletions x-pack/plugins/lens/public/editor_frame_service/service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
} from '../types';
import { Document } from '../persistence/saved_object_store';
import { mergeTables } from './merge_tables';
import { formatColumn } from './format_column';
import { EmbeddableFactory, LensEmbeddableStartServices } from './embeddable/embeddable_factory';
import { UiActionsStart } from '../../../../../src/plugins/ui_actions/public';
import { DashboardStart } from '../../../../../src/plugins/dashboard/public';
Expand Down Expand Up @@ -86,7 +85,6 @@ export class EditorFrameService {
getAttributeService: () => Promise<LensAttributeService>
): EditorFrameSetup {
plugins.expressions.registerFunction(() => mergeTables);
plugins.expressions.registerFunction(() => formatColumn);

const getStartServices = async (): Promise<LensEmbeddableStartServices> => {
const [coreStart, deps] = await core.getStartServices();
Expand Down
137 changes: 137 additions & 0 deletions x-pack/plugins/lens/public/indexpattern_datasource/format_column.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* 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 {
ExpressionFunctionDefinition,
Datatable,
DatatableColumn,
} from 'src/plugins/expressions/public';

interface FormatColumn {
format: string;
columnId: string;
decimals?: number;
parentFormat?: string;
}

export const supportedFormats: Record<
string,
{ decimalsToPattern: (decimals?: number) => string }
> = {
number: {
decimalsToPattern: (decimals = 2) => {
if (decimals === 0) {
return `0,0`;
}
return `0,0.${'0'.repeat(decimals)}`;
},
},
percent: {
decimalsToPattern: (decimals = 2) => {
if (decimals === 0) {
return `0,0%`;
}
return `0,0.${'0'.repeat(decimals)}%`;
},
},
bytes: {
decimalsToPattern: (decimals = 2) => {
if (decimals === 0) {
return `0,0b`;
}
return `0,0.${'0'.repeat(decimals)}b`;
},
},
};

export const formatColumn: ExpressionFunctionDefinition<
'lens_format_column',
Datatable,
FormatColumn,
Datatable
> = {
name: 'lens_format_column',
type: 'datatable',
help: '',
args: {
format: {
types: ['string'],
help: '',
required: true,
},
columnId: {
types: ['string'],
help: '',
required: true,
},
decimals: {
types: ['number'],
help: '',
},
parentFormat: {
types: ['string'],
help: '',
},
},
inputTypes: ['datatable'],
fn(input, { format, columnId, decimals, parentFormat }: FormatColumn) {
return {
...input,
columns: input.columns.map((col) => {
if (col.id === columnId) {
if (!parentFormat) {
if (supportedFormats[format]) {
return withParams(col, {
id: format,
params: { pattern: supportedFormats[format].decimalsToPattern(decimals) },
});
} else if (format) {
return withParams(col, { id: format });
} else {
return col;
}
}

const parsedParentFormat = JSON.parse(parentFormat);
const parentFormatId = parsedParentFormat.id;
const parentFormatParams = parsedParentFormat.params ?? {};

if (!parentFormatId) {
return col;
}

if (format && supportedFormats[format]) {
return withParams(col, {
id: parentFormatId,
params: {
id: format,
params: {
pattern: supportedFormats[format].decimalsToPattern(decimals),
},
...parentFormatParams,
},
});
}
if (parentFormatParams) {
const innerParams = (col.meta.params?.params as Record<string, unknown>) ?? {};
return withParams(col, {
...col.meta.params,
params: {
...innerParams,
...parentFormatParams,
},
});
}
}
return col;
}),
};
},
};

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 @@ -13,6 +13,7 @@ import {
DataPublicPluginStart,
} from '../../../../../src/plugins/data/public';
import { Datasource, EditorFrameSetup } from '../types';
import { formatColumn } from './format_column';

export interface IndexPatternDatasourceSetupPlugins {
expressions: ExpressionsSetup;
Expand All @@ -35,6 +36,7 @@ export class IndexPatternDatasource {
editorFrame.registerDatasource(async () => {
const { getIndexPatternDatasource, renameColumns } = await import('../async_services');
expressions.registerFunction(renameColumns);
expressions.registerFunction(formatColumn);
return core.getStartServices().then(([coreStart, { data }]) =>
getIndexPatternDatasource({
core: coreStart,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import React from 'react';
import { i18n } from '@kbn/i18n';

import { supportedFormats } from '../../../../editor_frame_service/format_column';
import { UI_SETTINGS } from '../../../../../../../../src/plugins/data/common';
import { Range } from '../../../../../../../../src/plugins/expressions/common/expression_types/index';
import { RangeEditor } from './range_editor';
import { OperationDefinition } from '../index';
import { FieldBasedIndexPatternColumn } from '../column_types';
import { updateColumnParam, changeColumn } from '../../../state_helpers';
import { supportedFormats } from '../../../format_column';
import { MODES, AUTO_BARS, DEFAULT_INTERVAL, MIN_HISTOGRAM_BARS, SLICES } from './constants';
import { IndexPattern, IndexPatternField } from '../../../types';

Expand All @@ -37,7 +37,7 @@ export interface RangeIndexPatternColumn extends FieldBasedIndexPatternColumn {
format?: { id: string; params?: { decimals: number } };
parentFormat?: {
id: string;
params?: { template?: string; id?: string; replaceInfinity?: boolean };
params?: { id?: string; template?: string; replaceInfinity?: boolean };
};
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,43 +92,18 @@ function getExpressionForLayer(
// TODO: improve the type handling here
const parentFormat = 'parentFormat' in col.params ? col.params!.parentFormat! : undefined;
const format = (col as FormattedColumn).params!.format;

const base: ExpressionFunctionAST = {
type: 'function',
function: 'lens_format_column',
arguments: {
format: [(format || parentFormat!).id], // either one of the two is available
format: format ? [format.id] : [''],
columnId: [id],
rangeParameters: [''],
decimals: typeof format?.params?.decimals === 'number' ? [format.params.decimals] : [],
parentFormat: parentFormat ? [JSON.stringify(parentFormat)] : [],
},
};

// Handle nested formatter carefully one parameter at the time
const rangeParameters: Record<string, string | boolean> = {};

if (parentFormat) {
base.arguments.format = [parentFormat.id];
// If a format override is present use it
if (format) {
rangeParameters.nestedFormat = format.id;
}
}
if (parentFormat?.params?.template) {
rangeParameters.template = parentFormat.params?.template;
}

if (parentFormat?.params?.replaceInfinity) {
rangeParameters.replaceInfinity = parentFormat.params?.replaceInfinity;
}

if (typeof format?.params?.decimals === 'number') {
base.arguments.decimals = [format.params.decimals];
}

// To avoid mapping too many ad-hoc arguments, this passes thru a JSON serialization
if (Object.keys(rangeParameters).length) {
base.arguments.rangeParameters = [JSON.stringify(rangeParameters)];
}

return base;
}
);
Expand Down