import { pick } from 'lodash' ; /**
 * Cached aggregation types
 */ type AggType = 'overallCardinality' | 'maxBucketCardinality' ; type CacheStorage = { [ key in AggType ] : { [ field ] : number ; } ; } ; /**
 * Caches cardinality fields values to avoid
 * unnecessary aggregations on elasticsearch
 */ export const initCardinalityFieldsCache = ( ) => { const cardinalityCache = new Map < string , CacheStorage > ( ) ; return { /**
         * Gets requested values from cache
         */ getValues ( indexPatternName , timeField , earliestMs , latestMs , aggType , fieldNames ) { const cacheKey = indexPatternName + timeField + earliestMs + latestMs ; const cached = cardinalityCache . get ( cacheKey ) ; if ( ! cached ) { return null ; } return pick ( cached [ aggType ] , fieldNames ) ; } , /**
         * Extends cache with provided values
         */ updateValues ( indexPatternName , timeField , earliestMs , latestMs , update ) { const cacheKey = indexPatternName + timeField + earliestMs + latestMs ; const cachedValues = cardinalityCache . get ( cacheKey ) ; if ( cachedValues === undefined ) { cardinalityCache . set ( cacheKey , { overallCardinality : update . overallCardinality ?? { } , maxBucketCardinality : update . maxBucketCardinality ?? { } , } ) ; return ; } Object . assign ( cachedValues . overallCardinality , update . overallCardinality ) ; Object . assign ( cachedValues . maxBucketCardinality , update . maxBucketCardinality ) ; } , } ; } ;