1 import { logger } from '@server/helpers/logger'
2 import { VideoStatsTimeserieGroupInterval } from '@shared/models'
4 function buildGroupByAndBoundaries (startDateString: string, endDateString: string) {
5 const startDate = new Date(startDateString)
6 const endDate = new Date(endDateString)
8 const groupByMatrix: { [ id in VideoStatsTimeserieGroupInterval ]: string } = {
11 ten_minutes: '10 minutes',
12 one_minute: '1 minute'
14 const groupInterval = buildGroupInterval(startDate, endDate)
16 logger.debug('Found "%s" group interval.', groupInterval, { startDate, endDate })
18 // Remove parts of the date we don't need
19 if (groupInterval === 'one_day') {
20 startDate.setHours(0, 0, 0, 0)
21 } else if (groupInterval === 'one_hour') {
22 startDate.setMinutes(0, 0, 0)
24 startDate.setSeconds(0, 0)
29 sqlInterval: groupByMatrix[groupInterval],
35 // ---------------------------------------------------------------------------
38 buildGroupByAndBoundaries
41 // ---------------------------------------------------------------------------
43 function buildGroupInterval (startDate: Date, endDate: Date): VideoStatsTimeserieGroupInterval {
48 const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000
50 if (diffSeconds >= 6 * aDay) return 'one_day'
51 if (diffSeconds >= 6 * anHour) return 'one_hour'
52 if (diffSeconds >= 60 * aMinute) return 'ten_minutes'