[Lens] Memoize operationMatrix computation (#101745) (#101892)

Co-authored-by: Marco Liberati <dej611@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2021-06-10 10:58:33 -04:00 committed by GitHub
parent 3eb9b6c392
commit 5c55734ca0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 14 deletions

View file

@ -5,9 +5,10 @@
* 2.0.
*/
import { DatasourceDimensionDropProps } from '../../types';
import memoizeOne from 'memoize-one';
import { DatasourceDimensionDropProps, OperationMetadata } from '../../types';
import { OperationType } from '../indexpattern';
import { memoizedGetAvailableOperationsByMetadata } from '../operations';
import { memoizedGetAvailableOperationsByMetadata, OperationFieldTuple } from '../operations';
import { IndexPatternPrivateState } from '../types';
export interface OperationSupportMatrix {
@ -21,17 +22,16 @@ type Props = Pick<
'layerId' | 'columnId' | 'state' | 'filterOperations'
>;
// TODO: the support matrix should be available outside of the dimension panel
// TODO: This code has historically been memoized, as a potentially performance
// sensitive task. If we can add memoization without breaking the behavior, we should.
export const getOperationSupportMatrix = (props: Props): OperationSupportMatrix => {
const layerId = props.layerId;
const currentIndexPattern = props.state.indexPatterns[props.state.layers[layerId].indexPatternId];
const filteredOperationsByMetadata = memoizedGetAvailableOperationsByMetadata(
currentIndexPattern
).filter((operation) => props.filterOperations(operation.operationMetaData));
function computeOperationMatrix(
operationsByMetadata: Array<{
operationMetaData: OperationMetadata;
operations: OperationFieldTuple[];
}>,
filterOperations: (operation: OperationMetadata) => boolean
) {
const filteredOperationsByMetadata = operationsByMetadata.filter((operation) =>
filterOperations(operation.operationMetaData)
);
const supportedOperationsByField: Partial<Record<string, Set<OperationType>>> = {};
const supportedOperationsWithoutField: Set<OperationType> = new Set();
@ -59,4 +59,16 @@ export const getOperationSupportMatrix = (props: Props): OperationSupportMatrix
operationWithoutField: supportedOperationsWithoutField,
fieldByOperation: supportedFieldsByOperation,
};
}
// memoize based on latest execution. It supports multiple args
const memoizedComputeOperationsMatrix = memoizeOne(computeOperationMatrix);
// TODO: the support matrix should be available outside of the dimension panel
export const getOperationSupportMatrix = (props: Props): OperationSupportMatrix => {
const layerId = props.layerId;
const currentIndexPattern = props.state.indexPatterns[props.state.layers[layerId].indexPatternId];
const operationsByMetadata = memoizedGetAvailableOperationsByMetadata(currentIndexPattern);
return memoizedComputeOperationsMatrix(operationsByMetadata, props.filterOperations);
};

View file

@ -93,7 +93,7 @@ export function isDocumentOperation(type: string) {
return documentOperations.has(type);
}
type OperationFieldTuple =
export type OperationFieldTuple =
| {
type: 'field';
operationType: OperationType;