import dateMath from '@elastic/datemath' ; import { buildRangeFilter , IIndexPattern , TimeRange , TimeRangeBounds } from '../..' ; interface CalculateBoundsOptions { forceNow ? : Date ; } export function calculateBounds ( timeRange , options = { } ) { return { min : dateMath . parse ( timeRange . from , { forceNow : options . forceNow } ) , max : dateMath . parse ( timeRange . to , { roundUp : true , forceNow : options . forceNow } ) , } ; } export function getTime ( indexPattern , timeRange , options ? ) { return createTimeRangeFilter ( indexPattern , timeRange , options ?. fieldName || indexPattern ?. timeFieldName , options ?. forceNow ) ; } function createTimeRangeFilter ( indexPattern , timeRange , fieldName ? , forceNow ? ) { if ( ! indexPattern ) { return ; } const field = indexPattern . fields . find ( ( f ) => f . name === ( fieldName || indexPattern . timeFieldName ) ) ; if ( ! field ) { return ; } const bounds = calculateBounds ( timeRange , { forceNow } ) ; if ( ! bounds ) { return ; } return buildRangeFilter ( field , { ... ( bounds . min && { gte : bounds . min . toISOString ( ) } ) , ... ( bounds . max && { lte : bounds . max . toISOString ( ) } ) , format : 'strict_date_optional_time' , } , indexPattern ) ; }