import { share } from 'rxjs/operators' ; import { IUiSettingsClient , SavedObjectsClientContract } from 'src/core/public' ; import { IStorageWrapper } from 'src/plugins/opensearch_dashboards_utils/public' ; import { FilterManager } from './filter_manager' ; import { createAddToQueryLog } from './lib' ; import { TimefilterService , TimefilterSetup } from './timefilter' ; import { createSavedQueryService } from './saved_query/saved_query_service' ; import { createQueryStateObservable } from './state_sync/create_global_query_observable' ; import { QueryStringManager , QueryStringContract } from './query_string' ; import { buildOpenSearchQuery , getOpenSearchQueryConfig } from '../../common' ; import { getUiSettings } from '../services' ; import { IndexPattern } from '..' ; internal interface QueryServiceSetupDependencies { storage : IStorageWrapper ; uiSettings : IUiSettingsClient ; } interface QueryServiceStartDependencies { savedObjectsClient : SavedObjectsClientContract ; storage : IStorageWrapper ; uiSettings : IUiSettingsClient ; } export class QueryService { filterManager ! : FilterManager ; timefilter ! : TimefilterSetup ; queryStringManager ! : QueryStringContract ; state$ ! : ReturnType < typeof createQueryStateObservable > ; public setup ( { storage , uiSettings } ) { this . filterManager = new FilterManager ( uiSettings ) ; const timefilterService = new TimefilterService ( ) ; this . timefilter = timefilterService . setup ( { uiSettings , storage , } ) ; this . queryStringManager = new QueryStringManager ( storage , uiSettings ) ; this . state$ = createQueryStateObservable ( { filterManager : this . filterManager , timefilter : this . timefilter , queryString : this . queryStringManager , } ) . pipe ( share ( ) ) ; return { filterManager : this . filterManager , timefilter : this . timefilter , queryString : this . queryStringManager , state$ : this . state$ , } ; } public start ( { savedObjectsClient , storage , uiSettings } ) { return { addToQueryLog : createAddToQueryLog ( { storage , uiSettings , } ) , filterManager : this . filterManager , queryString : this . queryStringManager , savedQueries : createSavedQueryService ( savedObjectsClient ) , state$ : this . state$ , timefilter : this . timefilter , getOpenSearchQuery : ( indexPattern ) => { const timeFilter = this . timefilter . timefilter . createFilter ( indexPattern ) ; return buildOpenSearchQuery ( indexPattern , this . queryStringManager . getQuery ( ) , [ ... this . filterManager . getFilters ( ) , ... ( timeFilter ? [ timeFilter ] : [ ] ) ] , getOpenSearchQueryConfig ( getUiSettings ( ) ) ) ; } , } ; } public stop ( ) { } } public export type QuerySetup = ReturnType < QueryService [ 'setup' ] > ; export type QueryStart = ReturnType < QueryService [ 'start' ] > ;