import { getRumPageLoadTransactionsProjection } from '../../projections/rum_page_load_transactions' ; import { mergeProjection } from '../../projections/util/merge_projection' ; import { SetupUX } from './route' ; import { BreakdownItem } from '../../../typings/ui_filters' ; export async function getPageViewTrends ( { setup , breakdowns , urlQuery , start , end , } ) { const projection = getRumPageLoadTransactionsProjection ( { setup , urlQuery , checkFetchStartFieldExists : false , start , end , } ) ; let breakdownItem = null ; if ( breakdowns ) { breakdownItem = JSON . parse ( breakdowns ) ; } const params = mergeProjection ( projection , { body : { size : 0 , query : { bool : projection . body . query . bool , } , aggs : { pageViews : { auto_date_histogram : { field : '@timestamp' , buckets : 50 , } , aggs : breakdownItem ? { breakdown : { terms : { field : breakdownItem . fieldName , size : 9 , missing : 'Others' , } , } , } : undefined , } , ... ( breakdownItem ? { topBreakdowns : { terms : { field : breakdownItem . fieldName , size : 9 , } , } , } : { } ) , } , } , } ) ; const { apmEventClient } = setup ; const response = await apmEventClient . search ( 'get_page_view_trends' , params ) ; const { topBreakdowns } = response . aggregations ?? { } ; const topItems = ( topBreakdowns ?. buckets ?? [ ] ) . map ( ( { key } ) => key as string ) ; const result = response . aggregations ?. pageViews . buckets ?? [ ] ; return { topItems , items : result . map ( ( bucket ) => { const { key : xVal , doc_count : bCount } = bucket ; const res = { x : xVal , y : bCount , } ; if ( 'breakdown' in bucket ) { let top9Count = 0 ; const categoryBuckets = bucket . breakdown . buckets ; categoryBuckets . forEach ( ( { key , doc_count : docCount } ) => { if ( topItems . includes ( key as string ) ) { if ( res [ key ] ) { res [ key ] += docCount ; } else { res [ key ] = docCount ; } top9Count += docCount ; } } ) ; if ( bCount > top9Count ) { res . Others = bCount - top9Count ; } } return res ; } ) , } ; }