import moment from 'moment-timezone' ; import { EuiRangeTick } from '@elastic/eui/src/components/form/range/range_ticks' ; import { calcAutoIntervalNear } from '../../../../../../src/plugins/data/common' ; import { getUiSettings } from '../../kibana_services' ; function getTimezone ( ) { const detectedTimezone = moment . tz . guess ( ) ; const dateFormatTZ = getUiSettings ( ) . get ( 'dateFormat:tz' , 'Browser' ) ; return dateFormatTZ === 'Browser' ? detectedTimezone : dateFormatTZ ; } function getScaledDateFormat ( interval ) { if ( interval >= moment . duration ( 1 , 'y' ) . asMilliseconds ( ) ) { return 'YYYY' ; } if ( interval >= moment . duration ( 1 , 'd' ) . asMilliseconds ( ) ) { return 'MMM D' ; } if ( interval >= moment . duration ( 6 , 'h' ) . asMilliseconds ( ) ) { return 'Do HH' ; } if ( interval >= moment . duration ( 1 , 'h' ) . asMilliseconds ( ) ) { return 'HH:mm' ; } if ( interval >= moment . duration ( 1 , 'm' ) . asMilliseconds ( ) ) { return 'HH:mm' ; } if ( interval >= moment . duration ( 1 , 's' ) . asMilliseconds ( ) ) { return 'mm:ss' ; } return 'ss.SSS' ; } export function epochToKbnDateFormat ( epoch ) { const dateFormat = getUiSettings ( ) . get ( 'dateFormat' , 'MMM D, YYYY @ HH:mm:ss.SSS' ) ; const timezone = getTimezone ( ) ; return moment . tz ( epoch , timezone ) . format ( dateFormat ) ; } export function getInterval ( min , max , steps = 6 ) { const duration = max - min ; let interval = calcAutoIntervalNear ( steps , duration ) . asMilliseconds ( ) ; const actualSteps = duration / interval ; if ( actualSteps > steps * 1.5 ) { const factor = Math . round ( actualSteps / steps ) ; interval *= factor ; } else if ( actualSteps < 5 ) { interval *= 0.5 ; } return interval ; } export function getTicks ( min , max , interval ) { const format = getScaledDateFormat ( interval ) ; const timezone = getTimezone ( ) ; let tick = Math . ceil ( min / interval ) * interval ; const ticks = [ ] ; while ( tick < max ) { ticks . push ( { value : tick , label : moment . tz ( tick , timezone ) . format ( format ) , } ) ; tick += interval ; } return ticks ; }