-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* 🚚 First move batch to common * 🚚 Second batch of move * 🏷️ Import types only * 🚚 Third batch * 🚚 Fourth batch move * 🚚 Another module moved * 🚚 More function moved * 🚚 Last bit of move * ⚡ Reduce page load bundle size * 🐛 Fix import issue * 🐛 More import fix * ✨ Registered functions on the server * 🐛 Expose datatable_column as well * ✅ Add server side expression test * 🚚 Moved back render functions to public * ✨ Add a timezone arg to time_scale * 🔥 Remove timezone arg * 🔥 Remove server side code for now * 👌 Integrated feedback * 🚚 Move back datatable render function * 🏷️ Fix imports * 🏷️ Fix missing export * 🚚 Move render functions back! Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: Marco Liberati <[email protected]>
- Loading branch information
1 parent
bdceec1
commit 1b92b79
Showing
136 changed files
with
2,123 additions
and
1,792 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
x-pack/plugins/lens/common/expressions/datatable/datatable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* 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 { 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; | ||
direction: 'asc' | 'desc' | 'none'; | ||
} | ||
|
||
export interface DatatableProps { | ||
data: LensMultiTable; | ||
untransposedData?: LensMultiTable; | ||
args: DatatableArgs; | ||
} | ||
|
||
export interface DatatableRender { | ||
type: 'render'; | ||
as: 'lens_datatable_renderer'; | ||
value: DatatableProps; | ||
} | ||
|
||
export interface DatatableArgs { | ||
title: string; | ||
description?: string; | ||
columns: ColumnConfigArg[]; | ||
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, | ||
}, | ||
}; | ||
}, | ||
}); |
85 changes: 85 additions & 0 deletions
85
x-pack/plugins/lens/common/expressions/datatable/datatable_column.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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 type { Direction } from '@elastic/eui'; | ||
import type { | ||
CustomPaletteState, | ||
PaletteOutput, | ||
} from '../../../../../../src/plugins/charts/common'; | ||
import type { | ||
ExpressionFunctionDefinition, | ||
DatatableColumn, | ||
} from '../../../../../../src/plugins/expressions/common'; | ||
import type { CustomPaletteParams } from '../../types'; | ||
|
||
export type LensGridDirection = 'none' | Direction; | ||
|
||
export interface ColumnConfig { | ||
columns: ColumnConfigArg[]; | ||
sortingColumnId: string | undefined; | ||
sortingDirection: LensGridDirection; | ||
} | ||
|
||
export type ColumnConfigArg = Omit<ColumnState, 'palette'> & { | ||
type: 'lens_datatable_column'; | ||
palette?: PaletteOutput<CustomPaletteState>; | ||
summaryRowValue?: unknown; | ||
}; | ||
|
||
export interface ColumnState { | ||
columnId: string; | ||
width?: number; | ||
hidden?: boolean; | ||
isTransposed?: boolean; | ||
// These flags are necessary to transpose columns and map them back later | ||
// They are set automatically and are not user-editable | ||
transposable?: boolean; | ||
originalColumnId?: string; | ||
originalName?: string; | ||
bucketValues?: Array<{ originalBucketColumn: DatatableColumn; value: unknown }>; | ||
alignment?: 'left' | 'right' | 'center'; | ||
palette?: PaletteOutput<CustomPaletteParams>; | ||
colorMode?: 'none' | 'cell' | 'text'; | ||
summaryRow?: 'none' | 'sum' | 'avg' | 'count' | 'min' | 'max'; | ||
summaryLabel?: string; | ||
} | ||
|
||
export type DatatableColumnResult = ColumnState & { type: 'lens_datatable_column' }; | ||
|
||
export const datatableColumn: ExpressionFunctionDefinition< | ||
'lens_datatable_column', | ||
null, | ||
ColumnState, | ||
DatatableColumnResult | ||
> = { | ||
name: 'lens_datatable_column', | ||
aliases: [], | ||
type: 'lens_datatable_column', | ||
help: '', | ||
inputTypes: ['null'], | ||
args: { | ||
columnId: { types: ['string'], help: '' }, | ||
alignment: { types: ['string'], help: '' }, | ||
hidden: { types: ['boolean'], help: '' }, | ||
width: { types: ['number'], help: '' }, | ||
isTransposed: { types: ['boolean'], help: '' }, | ||
transposable: { types: ['boolean'], help: '' }, | ||
colorMode: { types: ['string'], help: '' }, | ||
palette: { | ||
types: ['palette'], | ||
help: '', | ||
}, | ||
summaryRow: { types: ['string'], help: '' }, | ||
summaryLabel: { types: ['string'], help: '' }, | ||
}, | ||
fn: function fn(input: unknown, args: ColumnState) { | ||
return { | ||
type: 'lens_datatable_column', | ||
...args, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export * from './datatable_column'; | ||
export * from './datatable'; | ||
export * from './summary'; | ||
export * from './transpose_helpers'; | ||
export * from './utils'; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.