Skip to content

Commit

Permalink
🚚 Move render functions back!
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Jul 21, 2021
1 parent 2bde1e2 commit 8567922
Show file tree
Hide file tree
Showing 32 changed files with 631 additions and 573 deletions.
126 changes: 125 additions & 1 deletion x-pack/plugins/lens/common/expressions/datatable/datatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@
* 2.0.
*/

import type { LensMultiTable } from '../../types';
import { i18n } from '@kbn/i18n';
import { cloneDeep } from 'lodash';
import type {
DatatableColumnMeta,
ExpressionFunctionDefinition,
} from '../../../../../../src/plugins/expressions/common';
import type { FormatFactory, LensMultiTable } from '../../types';
import type { ColumnConfigArg } from './datatable_column';
import { getSortingCriteria } from './sorting';
import { computeSummaryRowForColumn } from './summary';
import { transposeTable } from './transpose_helpers';

export interface SortingState {
columnId: string | undefined;
Expand All @@ -32,3 +41,118 @@ export interface DatatableArgs {
sortingColumnId: SortingState['columnId'];
sortingDirection: SortingState['direction'];
}

function isRange(meta: { params?: { id?: string } } | undefined) {
return meta?.params?.id === 'range';
}

export const getDatatable = ({
formatFactory,
}: {
formatFactory: FormatFactory;
}): ExpressionFunctionDefinition<
'lens_datatable',
LensMultiTable,
DatatableArgs,
DatatableRender
> => ({
name: 'lens_datatable',
type: 'render',
inputTypes: ['lens_multitable'],
help: i18n.translate('xpack.lens.datatable.expressionHelpLabel', {
defaultMessage: 'Datatable renderer',
}),
args: {
title: {
types: ['string'],
help: i18n.translate('xpack.lens.datatable.titleLabel', {
defaultMessage: 'Title',
}),
},
description: {
types: ['string'],
help: '',
},
columns: {
types: ['lens_datatable_column'],
help: '',
multi: true,
},
sortingColumnId: {
types: ['string'],
help: '',
},
sortingDirection: {
types: ['string'],
help: '',
},
},
fn(data, args, context) {
let untransposedData: LensMultiTable | undefined;
// do the sorting at this level to propagate it also at CSV download
const [firstTable] = Object.values(data.tables);
const [layerId] = Object.keys(context.inspectorAdapters.tables || {});
const formatters: Record<string, ReturnType<FormatFactory>> = {};

firstTable.columns.forEach((column) => {
formatters[column.id] = formatFactory(column.meta?.params);
});

const hasTransposedColumns = args.columns.some((c) => c.isTransposed);
if (hasTransposedColumns) {
// store original shape of data separately
untransposedData = cloneDeep(data);
// transposes table and args inplace
transposeTable(args, firstTable, formatters);
}

const { sortingColumnId: sortBy, sortingDirection: sortDirection } = args;

const columnsReverseLookup = firstTable.columns.reduce<
Record<string, { name: string; index: number; meta?: DatatableColumnMeta }>
>((memo, { id, name, meta }, i) => {
memo[id] = { name, index: i, meta };
return memo;
}, {});

const columnsWithSummary = args.columns.filter((c) => c.summaryRow);
for (const column of columnsWithSummary) {
column.summaryRowValue = computeSummaryRowForColumn(
column,
firstTable,
formatters,
formatFactory({ id: 'number' })
);
}

if (sortBy && columnsReverseLookup[sortBy] && sortDirection !== 'none') {
// Sort on raw values for these types, while use the formatted value for the rest
const sortingCriteria = getSortingCriteria(
isRange(columnsReverseLookup[sortBy]?.meta)
? 'range'
: columnsReverseLookup[sortBy]?.meta?.type,
sortBy,
formatters[sortBy],
sortDirection
);
// replace the table here
context.inspectorAdapters.tables[layerId].rows = (firstTable.rows || [])
.slice()
.sort(sortingCriteria);
// replace also the local copy
firstTable.rows = context.inspectorAdapters.tables[layerId].rows;
} else {
args.sortingColumnId = undefined;
args.sortingDirection = 'none';
}
return {
type: 'render',
as: 'lens_datatable_renderer',
value: {
data,
untransposedData,
args,
},
};
},
});
3 changes: 3 additions & 0 deletions x-pack/plugins/lens/common/expressions/datatable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@

export * from './datatable_column';
export * from './datatable';
export * from './summary';
export * from './transpose_helpers';
export * from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import ipaddr from 'ipaddr.js';
import type { IPv4, IPv6 } from 'ipaddr.js';
import type { FieldFormat } from '../../../../../src/plugins/data/common';
import type { FieldFormat } from '../../../../../../src/plugins/data/common';

function isIPv6Address(ip: IPv4 | IPv6): ip is IPv6 {
return ip.kind() === 'ipv6';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

import { i18n } from '@kbn/i18n';
import type { FieldFormat } from '../../../../../src/plugins/data/common';
import type { Datatable } from '../../../../../src/plugins/expressions/common';
import type { ColumnConfigArg } from '../../common/expressions';
import type { FieldFormat } from '../../../../../../src/plugins/data/common';
import type { Datatable } from '../../../../../../src/plugins/expressions/common';
import { ColumnConfigArg } from './datatable_column';
import { getOriginalId } from './transpose_helpers';
import { isNumericFieldForDatatable } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import type { FieldFormat } from 'src/plugins/data/public';
import type { Datatable } from 'src/plugins/expressions';
import { DatatableArgs } from './datatable';

import type { DatatableArgs } from '../../common/expressions';
import { transposeTable } from './transpose_helpers';

describe('transpose_helpes', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import type {
Datatable,
DatatableColumn,
DatatableRow,
} from '../../../../../src/plugins/expressions';
import type { FieldFormat } from '../../../../../src/plugins/data/common';
import type { ColumnConfig, ColumnConfigArg, DatatableArgs } from '../../common/expressions';
} from '../../../../../../src/plugins/expressions';
import type { FieldFormat } from '../../../../../../src/plugins/data/common';
import type { DatatableArgs } from './datatable';
import type { ColumnConfig, ColumnConfigArg } from './datatable_column';

const TRANSPOSE_SEPARATOR = '---';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import type { Datatable } from '../../../../../src/plugins/expressions/common';
import type { Datatable } from '../../../../../../src/plugins/expressions/common';
import { getOriginalId } from './transpose_helpers';

function isValidNumber(value: unknown): boolean {
Expand Down
122 changes: 122 additions & 0 deletions x-pack/plugins/lens/common/expressions/heatmap_chart/heatmap_chart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import type { ExpressionFunctionDefinition } from '../../../../../../src/plugins/expressions/common';
import type { PaletteOutput } from '../../../../../../src/plugins/charts/common';
import type { LensMultiTable, CustomPaletteParams } from '../../types';
import { HeatmapGridConfigResult, HEATMAP_GRID_FUNCTION } from './heatmap_grid';
import { HeatmapLegendConfigResult, HEATMAP_LEGEND_FUNCTION } from './heatmap_legend';

export const HEATMAP_FUNCTION = 'lens_heatmap';
export const HEATMAP_FUNCTION_RENDERER = 'lens_heatmap_renderer';

export type ChartShapes = 'heatmap';

export interface SharedHeatmapLayerState {
shape: ChartShapes;
xAccessor?: string;
yAccessor?: string;
valueAccessor?: string;
legend: HeatmapLegendConfigResult;
gridConfig: HeatmapGridConfigResult;
}

export type HeatmapLayerState = SharedHeatmapLayerState & {
layerId: string;
};

export type HeatmapVisualizationState = HeatmapLayerState & {
// need to store the current accessor to reset the color stops at accessor change
palette?: PaletteOutput<CustomPaletteParams> & { accessor: string };
};

export type HeatmapExpressionArgs = SharedHeatmapLayerState & {
title?: string;
description?: string;
palette: PaletteOutput;
};

export interface HeatmapRender {
type: 'render';
as: typeof HEATMAP_FUNCTION_RENDERER;
value: HeatmapExpressionProps;
}

export interface HeatmapExpressionProps {
data: LensMultiTable;
args: HeatmapExpressionArgs;
}

export const heatmap: ExpressionFunctionDefinition<
typeof HEATMAP_FUNCTION,
LensMultiTable,
HeatmapExpressionArgs,
HeatmapRender
> = {
name: HEATMAP_FUNCTION,
type: 'render',
help: i18n.translate('xpack.lens.heatmap.expressionHelpLabel', {
defaultMessage: 'Heatmap renderer',
}),
args: {
title: {
types: ['string'],
help: i18n.translate('xpack.lens.heatmap.titleLabel', {
defaultMessage: 'Title',
}),
},
description: {
types: ['string'],
help: '',
},
xAccessor: {
types: ['string'],
help: '',
},
yAccessor: {
types: ['string'],
help: '',
},
valueAccessor: {
types: ['string'],
help: '',
},
shape: {
types: ['string'],
help: '',
},
palette: {
default: `{theme "palette" default={system_palette name="default"} }`,
help: '',
types: ['palette'],
},
legend: {
types: [HEATMAP_LEGEND_FUNCTION],
help: i18n.translate('xpack.lens.heatmapChart.legend.help', {
defaultMessage: 'Configure the chart legend.',
}),
},
gridConfig: {
types: [HEATMAP_GRID_FUNCTION],
help: i18n.translate('xpack.lens.heatmapChart.gridConfig.help', {
defaultMessage: 'Configure the heatmap layout.',
}),
},
},
inputTypes: ['lens_multitable'],
fn(data: LensMultiTable, args: HeatmapExpressionArgs) {
return {
type: 'render',
as: HEATMAP_FUNCTION_RENDERER,
value: {
data,
args,
},
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

export * from './heatmap_grid';
export * from './heatmap_legend';
export * from './types';
export * from './heatmap_chart';
20 changes: 0 additions & 20 deletions x-pack/plugins/lens/common/expressions/heatmap_chart/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*/

export * from './types';
export * from './metric_chart';
Loading

0 comments on commit 8567922

Please sign in to comment.