import { get } from 'lodash' ; import { LegacyAPICaller } from 'opensearch-dashboards/server' ; import { DEFAULT_QUERY_LANGUAGE , UI_SETTINGS } from '../../../common' ; const defaultSearchQueryLanguageSetting = DEFAULT_QUERY_LANGUAGE ; export interface Usage { optInCount : number ; optOutCount : number ; defaultQueryLanguage : string ; } export function fetchProvider ( index ) { return async ( callCluster ) : Promise < Usage > => { const [ response , config ] = await Promise . all ( [ callCluster ( 'get' , { index , id : 'dql-telemetry:dql-telemetry' , ignore : [ 404 ] , } ) , callCluster ( 'search' , { index , body : { query : { term : { type : 'config' } } } , ignore : [ 404 ] , } ) , ] ) ; const queryLanguageConfigValue = get ( config , `hits.hits[0]._source.config.${ UI_SETTINGS . SEARCH_QUERY_LANGUAGE }` ) ; let defaultLanguage ; if ( queryLanguageConfigValue === undefined ) { defaultLanguage = `default-${ defaultSearchQueryLanguageSetting }` ; } else if ( queryLanguageConfigValue === null ) { defaultLanguage = defaultSearchQueryLanguageSetting ; } else { defaultLanguage = queryLanguageConfigValue ; } const dqlTelemetryDoc = { optInCount : 0 , optOutCount : 0 , ... get ( response , '_source.dql-telemetry' , { } ) , } ; return { ... dqlTelemetryDoc , defaultQueryLanguage : defaultLanguage , } ; } ; }