1 import { logger } from '@server/helpers/logger'
3 function buildGroupByAndBoundaries (startDateString: string, endDateString: string) {
4 const startDate = new Date(startDateString)
5 const endDate = new Date(endDateString)
7 const groupInterval = buildGroupInterval(startDate, endDate)
9 logger.debug('Found "%s" group interval.', groupInterval, { startDate, endDate })
11 // Remove parts of the date we don't need
12 if (groupInterval.endsWith(' month') || groupInterval.endsWith(' months')) {
14 startDate.setHours(0, 0, 0, 0)
15 } else if (groupInterval.endsWith(' day') || groupInterval.endsWith(' days')) {
16 startDate.setHours(0, 0, 0, 0)
17 } else if (groupInterval.endsWith(' hour') || groupInterval.endsWith(' hours')) {
18 startDate.setMinutes(0, 0, 0)
20 startDate.setSeconds(0, 0)
30 // ---------------------------------------------------------------------------
33 buildGroupByAndBoundaries
36 // ---------------------------------------------------------------------------
38 function buildGroupInterval (startDate: Date, endDate: Date): string {
39 const aYear = 31536000
40 const aMonth = 2678400
45 const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000
47 if (diffSeconds >= 6 * aYear) return '6 months'
48 if (diffSeconds >= 2 * aYear) return '1 month'
49 if (diffSeconds >= 6 * aMonth) return '7 days'
50 if (diffSeconds >= 2 * aMonth) return '2 days'
52 if (diffSeconds >= 15 * aDay) return '1 day'
53 if (diffSeconds >= 8 * aDay) return '12 hours'
54 if (diffSeconds >= 4 * aDay) return '6 hours'
56 if (diffSeconds >= 15 * anHour) return '1 hour'
58 if (diffSeconds >= 180 * aMinute) return '10 minutes'