import moment from 'moment' ; import dateMath , { Unit } from '@elastic/datemath' ; import { parseOpenSearchInterval } from '../../../utils' ; const unitsDesc = dateMath . unitsDesc ; const largeMax = unitsDesc . indexOf ( 'M' ) ; export interface OpenSearchInterval { expression : string ; unit : Unit ; value : number ; } param { moment . duration } duration return { object } export function convertDurationToNormalizedOpenSearchInterval ( duration ) { for ( let i = 0 ; i < unitsDesc . length ; i ++ ) { const unit = unitsDesc [ i ] ; const val = duration . as ( unit ) ; if ( val >= 1 && Math . floor ( val ) === val ) { if ( i <= largeMax && val !== 1 ) { continue ; } return { value : val , unit , expression : val + unit , } ; } } const ms = duration . as ( 'ms' ) ; return { value : ms , unit : 'ms' , expression : ms + 'ms' , } ; } export function convertIntervalToOpenSearchInterval ( interval ) { const { value , unit } = parseOpenSearchInterval ( interval ) ; return { value , unit , expression : interval , } ; }