import { overwrite } from '../../helpers' ; import { getBucketSize , getTimerange } from '../../helpers' ; import { validateField } from '../../../../../common/fields_utils' ; import { search , UI_SETTINGS } from '../../../../../../../../plugins/data/server' ; import type { AnnotationsRequestProcessorsFunction } from './types' ; const { dateHistogramInterval } = search . aggs ; export const dateHistogram = ( { req , panel , annotation , annotationIndex , capabilities , uiSettings , } ) => { return ( next ) => async ( doc ) => { const barTargetUiSettings = await uiSettings . get ( UI_SETTINGS . HISTOGRAM_BAR_TARGET ) ; const timeField = annotation . time_field || annotationIndex . indexPattern ?. timeFieldName || '' ; if ( panel . use_kibana_indexes ) { validateField ( timeField , annotationIndex ) ; } const { bucketSize , intervalString } = getBucketSize ( req , 'auto' , capabilities , barTargetUiSettings ) ; const { from , to } = getTimerange ( req ) ; const { timezone } = capabilities ; overwrite ( doc , `aggs.${ annotation . id }.date_histogram` , { field : timeField , min_doc_count : 0 , time_zone : timezone , extended_bounds : { min : from . valueOf ( ) , max : to . valueOf ( ) - bucketSize * 1000 , } , ... dateHistogramInterval ( intervalString ) , } ) ; return next ( doc ) ; } ; } ;