-
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.
- Loading branch information
Showing
19 changed files
with
362 additions
and
277 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
x-pack/plugins/lens/common/expressions/counter_rate/counter_rate_fn.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,54 @@ | ||
/* | ||
* 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 { | ||
buildResultColumns, | ||
getBucketIdentifier, | ||
} from '../../../../../../src/plugins/expressions/common'; | ||
import type { CounterRateExpressionFunction } from './types'; | ||
|
||
export const counterRateFn: CounterRateExpressionFunction['fn'] = ( | ||
input, | ||
{ by, inputColumnId, outputColumnId, outputColumnName } | ||
) => { | ||
const resultColumns = buildResultColumns(input, outputColumnId, inputColumnId, outputColumnName); | ||
|
||
if (!resultColumns) { | ||
return input; | ||
} | ||
const previousValues: Partial<Record<string, number>> = {}; | ||
|
||
return { | ||
...input, | ||
columns: resultColumns, | ||
rows: input.rows.map((row) => { | ||
const newRow = { ...row }; | ||
|
||
const bucketIdentifier = getBucketIdentifier(row, by); | ||
const previousValue = previousValues[bucketIdentifier]; | ||
const currentValue = newRow[inputColumnId]; | ||
if (currentValue != null && previousValue != null) { | ||
const currentValueAsNumber = Number(currentValue); | ||
if (currentValueAsNumber >= previousValue) { | ||
newRow[outputColumnId] = currentValueAsNumber - previousValue; | ||
} else { | ||
newRow[outputColumnId] = currentValueAsNumber; | ||
} | ||
} else { | ||
newRow[outputColumnId] = undefined; | ||
} | ||
|
||
if (currentValue != null) { | ||
previousValues[bucketIdentifier] = Number(currentValue); | ||
} else { | ||
previousValues[bucketIdentifier] = undefined; | ||
} | ||
|
||
return newRow; | ||
}), | ||
}; | ||
}; |
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
16 changes: 16 additions & 0 deletions
16
x-pack/plugins/lens/common/expressions/counter_rate/types.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,16 @@ | ||
/* | ||
* 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 { Datatable, ExpressionFunctionDefinition } from '../../../../../../src/plugins/expressions'; | ||
import { CounterRateArgs } from './index'; | ||
|
||
export type CounterRateExpressionFunction = ExpressionFunctionDefinition< | ||
'lens_counter_rate', | ||
Datatable, | ||
CounterRateArgs, | ||
Datatable | Promise<Datatable> | ||
>; |
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
94 changes: 94 additions & 0 deletions
94
x-pack/plugins/lens/common/expressions/datatable/datatable_fn.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,94 @@ | ||
/* | ||
* 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 { cloneDeep } from 'lodash'; | ||
import { FormatFactory, LensMultiTable } from '../../types'; | ||
import { transposeTable } from './transpose_helpers'; | ||
import { computeSummaryRowForColumn } from './summary'; | ||
import { getSortingCriteria } from './sorting'; | ||
import type { | ||
DatatableColumnMeta, | ||
ExecutionContext, | ||
} from '../../../../../../src/plugins/expressions'; | ||
import type { DatatableExpressionFunction } from './types'; | ||
|
||
function isRange(meta: { params?: { id?: string } } | undefined) { | ||
return meta?.params?.id === 'range'; | ||
} | ||
|
||
export const datatableFn = ( | ||
getFormatFactory: (context: ExecutionContext) => FormatFactory | Promise<FormatFactory> | ||
) => | ||
(async (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>> = {}; | ||
const formatFactory = await getFormatFactory(context); | ||
|
||
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, | ||
}, | ||
}; | ||
}) as DatatableExpressionFunction['fn']; |
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,17 @@ | ||
/* | ||
* 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 { ExpressionFunctionDefinition } from '../../../../../../src/plugins/expressions'; | ||
import type { LensMultiTable } from '../../types'; | ||
import type { DatatableArgs, DatatableRender } from './datatable'; | ||
|
||
export type DatatableExpressionFunction = ExpressionFunctionDefinition< | ||
'lens_datatable', | ||
LensMultiTable, | ||
DatatableArgs, | ||
Promise<DatatableRender> | ||
>; |
Oops, something went wrong.