2022-04-07 04:53:35 -04:00
|
|
|
import { logger } from '@server/helpers/logger'
|
|
|
|
|
|
|
|
function buildGroupByAndBoundaries (startDateString: string, endDateString: string) {
|
|
|
|
const startDate = new Date(startDateString)
|
|
|
|
const endDate = new Date(endDateString)
|
|
|
|
|
|
|
|
const groupInterval = buildGroupInterval(startDate, endDate)
|
|
|
|
|
|
|
|
logger.debug('Found "%s" group interval.', groupInterval, { startDate, endDate })
|
|
|
|
|
|
|
|
// Remove parts of the date we don't need
|
2022-04-08 04:22:56 -04:00
|
|
|
if (groupInterval.endsWith(' day') || groupInterval.endsWith(' days')) {
|
2022-04-07 04:53:35 -04:00
|
|
|
startDate.setHours(0, 0, 0, 0)
|
2022-04-08 04:22:56 -04:00
|
|
|
} else if (groupInterval.endsWith(' hour') || groupInterval.endsWith(' hours')) {
|
2022-04-07 04:53:35 -04:00
|
|
|
startDate.setMinutes(0, 0, 0)
|
|
|
|
} else {
|
|
|
|
startDate.setSeconds(0, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
groupInterval,
|
|
|
|
startDate,
|
|
|
|
endDate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
buildGroupByAndBoundaries
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-04-08 04:22:56 -04:00
|
|
|
function buildGroupInterval (startDate: Date, endDate: Date): string {
|
2022-04-07 04:53:35 -04:00
|
|
|
const aDay = 86400
|
|
|
|
const anHour = 3600
|
|
|
|
const aMinute = 60
|
|
|
|
|
|
|
|
const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000
|
|
|
|
|
2022-04-08 04:22:56 -04:00
|
|
|
if (diffSeconds >= 15 * aDay) return '1 day'
|
|
|
|
if (diffSeconds >= 8 * aDay) return '12 hours'
|
|
|
|
if (diffSeconds >= 4 * aDay) return '6 hours'
|
|
|
|
if (diffSeconds >= 15 * anHour) return '1 hour'
|
|
|
|
if (diffSeconds >= 180 * aMinute) return '10 minutes'
|
2022-04-07 04:53:35 -04:00
|
|
|
|
2022-04-08 04:22:56 -04:00
|
|
|
return '1 minute'
|
2022-04-07 04:53:35 -04:00
|
|
|
}
|