Skip to content

Commit

Permalink
add window param
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Nov 20, 2020
1 parent 7ef2558 commit 724e7c3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
*/

import { i18n } from '@kbn/i18n';
import { useState } from 'react';
import React from 'react';
import { EuiFormRow } from '@elastic/eui';
import { EuiFieldNumber } from '@elastic/eui';
import { FormattedIndexPatternColumn, ReferenceBasedIndexPatternColumn } from '../column_types';
import { IndexPatternLayer } from '../../../types';
import { checkForDateHistogram, dateBasedOperationToExpression, hasDateField } from './utils';
import { OperationDefinition } from '..';
import { updateColumnParam } from '../..';
import { useDebounceWithOptions } from '../helpers';
import { OperationDefinition, ParamEditorProps } from '..';

const ofName = (name: string) => {
return i18n.translate('xpack.lens.indexPattern.movingAverageOf', {
Expand All @@ -20,6 +26,9 @@ const ofName = (name: string) => {
export type MovingAverageIndexPatternColumn = FormattedIndexPatternColumn &
ReferenceBasedIndexPatternColumn & {
operationType: 'moving_average';
params: {
window: number;
};
};

export const movingAverageOperation: OperationDefinition<
Expand Down Expand Up @@ -53,7 +62,9 @@ export const movingAverageOperation: OperationDefinition<
});
},
toExpression: (layer, columnId) => {
return dateBasedOperationToExpression(layer, columnId, 'moving_average');
return dateBasedOperationToExpression(layer, columnId, 'moving_average', {
window: [(layer.columns[columnId] as MovingAverageIndexPatternColumn).params.window],
});
},
buildColumn: ({ referenceIds, previousColumn, layer }) => {
const metric = layer.columns[referenceIds[0]];
Expand All @@ -69,14 +80,60 @@ export const movingAverageOperation: OperationDefinition<
previousColumn.params &&
'format' in previousColumn.params &&
previousColumn.params.format
? { format: previousColumn.params.format }
: undefined,
? { format: previousColumn.params.format, window: 5 }
: { window: 5 },
};
},
paramEditor: MovingAverageParamEditor,
isTransferable: (column, newIndexPattern) => {
return hasDateField(newIndexPattern);
},
getErrorMessage: (layer: IndexPatternLayer) => {
return checkForDateHistogram(layer);
},
};

function MovingAverageParamEditor({
state,
setState,
currentColumn,
layerId,
}: ParamEditorProps<MovingAverageIndexPatternColumn>) {
const [inputValue, setInputValue] = useState(String(currentColumn.params.window));

useDebounceWithOptions(
() => {
if (inputValue === '') {
return;
}
const inputNumber = Number(inputValue);
setState(
updateColumnParam({
state,
layerId,
currentColumn,
paramName: 'window',
value: inputNumber,
})
);
},
{ skipFirstRender: true },
256,
[inputValue]
);
return (
<EuiFormRow
label={i18n.translate('xpack.lens.indexPattern.movingAverage.window', {
defaultMessage: 'Window size',
})}
display="columnCompressed"
fullWidth
>
<EuiFieldNumber
compressed
value={inputValue}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => setInputValue(e.target.value)}
/>
</EuiFormRow>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export function hasDateField(indexPattern: IndexPattern) {
export function dateBasedOperationToExpression(
layer: IndexPatternLayer,
columnId: string,
functionName: string
functionName: string,
additionalArgs: Record<string, unknown[]> = {}
): ExpressionFunctionAST[] {
const currentColumn = (layer.columns[columnId] as unknown) as ReferenceBasedIndexPatternColumn;
const buckets = layer.columnOrder.filter((colId) => layer.columns[colId].isBucketed);
Expand All @@ -54,6 +55,7 @@ export function dateBasedOperationToExpression(
by: buckets,
inputColumnId: [currentColumn.references[0]],
outputColumnId: [columnId],
...additionalArgs,
},
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface BaseIndexPatternColumn extends Operation {
// export interface FormattedIndexPatternColumn extends BaseIndexPatternColumn {
export type FormattedIndexPatternColumn = BaseIndexPatternColumn & {
params?: {
format: {
format?: {
id: string;
params?: {
decimals: number;
Expand Down

0 comments on commit 724e7c3

Please sign in to comment.