import { i18n } from '@osd/i18n' ; import { createAction , ACTION_VISUALIZE_FIELD , VisualizeFieldContext , } from '../../../ui_actions/public' ; import { getApplication , getUISettings , getIndexPatterns , getQueryService , getShareService , } from '../services' ; import { VISUALIZE_APP_URL_GENERATOR , VisualizeUrlGeneratorState } from '../url_generator' ; import { AGGS_TERMS_SIZE_SETTING } from '../../common/constants' ; export const visualizeFieldAction = createAction < typeof ACTION_VISUALIZE_FIELD > ( { type : ACTION_VISUALIZE_FIELD , id : ACTION_VISUALIZE_FIELD , getDisplayName : ( ) => i18n . translate ( 'visualize.discover.visualizeFieldLabel' , { defaultMessage : 'Visualize field' , } ) , isCompatible : async ( ) => ! ! getApplication ( ) . capabilities . visualize . show , getHref : async ( context ) => { const url = await getVisualizeUrl ( context ) ; return url ; } , execute : async ( context ) => { const url = await getVisualizeUrl ( context ) ; const hash = url . split ( '#' ) [ 1 ] ; getApplication ( ) . navigateToApp ( 'visualize' , { path : `/#${ hash }` , } ) ; } , } ) ; const getVisualizeUrl = async ( context ) => { const indexPattern = await getIndexPatterns ( ) . get ( context . indexPatternId ) ; const field = indexPattern . fields . find ( ( fld ) => fld . name === context . fieldName ) ; const aggsTermSize = getUISettings ( ) . get ( AGGS_TERMS_SIZE_SETTING ) ; let agg ; if ( field ?. type === 'date' && indexPattern . timeFieldName === context . fieldName ) { agg = { type : 'date_histogram' , schema : 'segment' , params : { field : context . fieldName , interval : 'auto' , } , } ; } else { agg = { type : 'terms' , schema : 'segment' , params : { field : context . fieldName , size : parseInt ( aggsTermSize , 10 ) , orderBy : '1' , } , } ; } const generator = getShareService ( ) . urlGenerators . getUrlGenerator ( VISUALIZE_APP_URL_GENERATOR ) ; const urlState = { filters : getQueryService ( ) . filterManager . getFilters ( ) , query : getQueryService ( ) . queryString . getQuery ( ) , timeRange : getQueryService ( ) . timefilter . timefilter . getTime ( ) , indexPatternId : context . indexPatternId , type : 'histogram' , vis : { type : 'histogram' , aggs : [ { schema : 'metric' , type : 'count' , id : '1' } , agg ] , } , } ; return generator . createUrl ( urlState ) ; } ;