import moment from 'moment' ; import { EuiTheme } from 'src/plugins/kibana_react/common' ; import { TimeRangeComparisonType , TimeRangeComparisonEnum , } from '../../../../common/runtime_types/comparison_type_rt' ; import { getDateDifference } from '../../../../common/utils/formatters' ; export function getComparisonChartTheme ( theme ) { return { areaSeriesStyle : { area : { fill : theme . eui . euiColorLightShade , visible : true , opacity : 0.5 , } , line : { stroke : theme . eui . euiColorMediumShade , strokeWidth : 1 , visible : true , } , point : { visible : false , } , } , } ; } const oneDayInMilliseconds = moment . duration ( 1 , 'day' ) . asMilliseconds ( ) ; const oneWeekInMilliseconds = moment . duration ( 1 , 'week' ) . asMilliseconds ( ) ; export function getTimeRangeComparison ( { comparisonEnabled , comparisonType , start , end , } ) { if ( ! comparisonEnabled || ! comparisonType || ! start || ! end ) { return { } ; } const startMoment = moment ( start ) ; const endMoment = moment ( end ) ; const startEpoch = startMoment . valueOf ( ) ; const endEpoch = endMoment . valueOf ( ) ; let diff ; let offset ; switch ( comparisonType ) { case TimeRangeComparisonEnum . DayBefore : diff = oneDayInMilliseconds ; offset = '1d' ; break ; case TimeRangeComparisonEnum . WeekBefore : diff = oneWeekInMilliseconds ; offset = '1w' ; break ; case TimeRangeComparisonEnum . PeriodBefore : diff = getDateDifference ( { start : startMoment , end : endMoment , unitOfTime : 'milliseconds' , precise : true , } ) ; offset = `${ diff }ms` ; break ; default : throw new Error ( 'Unknown comparisonType' ) ; } return { comparisonStart : new Date ( startEpoch - diff ) . toISOString ( ) , comparisonEnd : new Date ( endEpoch - diff ) . toISOString ( ) , offset , } ; }