import { pick } from 'lodash' ; import { UiSettingsServiceStart , SavedObjectsClientContract } from 'src/core/server' ; import { ExpressionsServiceSetup } from 'src/plugins/expressions/common' ; import { AggsCommonService , AggConfigs , AggTypesDependencies , aggsRequiredUiSettings , calculateBounds , TimeRange , } from '../../../common' ; import { FieldFormatsStart } from '../../field_formats' ; import { AggsSetup , AggsStart } from './types' ; internal export interface AggsSetupDependencies { registerFunction : ExpressionsServiceSetup [ 'registerFunction' ] ; } internal export interface AggsStartDependencies { fieldFormats : FieldFormatsStart ; uiSettings : UiSettingsServiceStart ; } /**
 * The aggs service provides a means of modeling and manipulating the various
 * OpenSearch aggregations supported by OpenSearch Dashboards, providing the ability to
 * output the correct DSL when you are ready to send your request to OpenSearch.
 */ export class AggsService { private readonly aggsCommonService = new AggsCommonService ( ) ; /**
     * getForceNow uses window.location on the client, so we must have a
     * separate implementation of calculateBounds on the server.
     */ private calculateBounds = ( timeRange ) => calculateBounds ( timeRange , { } ) ; public setup ( { registerFunction } ) { return this . aggsCommonService . setup ( { registerFunction } ) ; } public start ( { fieldFormats , uiSettings } ) { return { asScopedToClient : async ( savedObjectsClient ) => { const uiSettingsClient = uiSettings . asScopedToClient ( savedObjectsClient ) ; const formats = await fieldFormats . fieldFormatServiceFactory ( uiSettingsClient ) ; const uiSettingsCache = pick ( await uiSettingsClient . getAll ( ) , aggsRequiredUiSettings ) ; const getConfig = < T = any > ( key ) : T => { return uiSettingsCache [ key ] ; } ; const { calculateAutoTimeExpression , types } = this . aggsCommonService . start ( { getConfig } ) ; const aggTypesDependencies = { calculateBounds : this . calculateBounds , getConfig , getFieldFormatsStart : ( ) => ( { deserialize : formats . deserialize , getDefaultInstance : formats . getDefaultInstance , } ) , /**
                     * Date histogram and date range need to know whether we are using the
                     * default timezone, but `isDefault` is not currently offered on the
                     * server, so we need to manually check for the default value.
                     */ isDefaultTimezone : ( ) => getConfig ( 'dateFormat:tz' ) === 'Browser' , } ; const typesRegistry = { get : ( name ) => { const type = types . get ( name ) ; if ( ! type ) { return ; } return type ( aggTypesDependencies ) ; } , getAll : ( ) => { return { buckets : types . getAll ( ) . buckets . map ( ( type ) => type ( aggTypesDependencies ) ) , metrics : types . getAll ( ) . metrics . map ( ( type ) => type ( aggTypesDependencies ) ) , } ; } , } ; return { calculateAutoTimeExpression , createAggConfigs : ( indexPattern , configStates = [ ] , schemas ) => { return new AggConfigs ( indexPattern , configStates , { typesRegistry } ) ; } , types : typesRegistry , } ; } , } ; } public stop ( ) { } }