]>
Commit | Line | Data |
---|---|---|
1 | import { logger } from '@server/helpers/logger' | |
2 | import { VideoStatsTimeserieGroupInterval } from '@shared/models' | |
3 | ||
4 | function buildGroupByAndBoundaries (startDateString: string, endDateString: string) { | |
5 | const startDate = new Date(startDateString) | |
6 | const endDate = new Date(endDateString) | |
7 | ||
8 | const groupByMatrix: { [ id in VideoStatsTimeserieGroupInterval ]: string } = { | |
9 | one_day: '1 day', | |
10 | one_hour: '1 hour', | |
11 | ten_minutes: '10 minutes', | |
12 | one_minute: '1 minute' | |
13 | } | |
14 | const groupInterval = buildGroupInterval(startDate, endDate) | |
15 | ||
16 | logger.debug('Found "%s" group interval.', groupInterval, { startDate, endDate }) | |
17 | ||
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) | |
23 | } else { | |
24 | startDate.setSeconds(0, 0) | |
25 | } | |
26 | ||
27 | return { | |
28 | groupInterval, | |
29 | sqlInterval: groupByMatrix[groupInterval], | |
30 | startDate, | |
31 | endDate | |
32 | } | |
33 | } | |
34 | ||
35 | // --------------------------------------------------------------------------- | |
36 | ||
37 | export { | |
38 | buildGroupByAndBoundaries | |
39 | } | |
40 | ||
41 | // --------------------------------------------------------------------------- | |
42 | ||
43 | function buildGroupInterval (startDate: Date, endDate: Date): VideoStatsTimeserieGroupInterval { | |
44 | const aDay = 86400 | |
45 | const anHour = 3600 | |
46 | const aMinute = 60 | |
47 | ||
48 | const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000 | |
49 | ||
50 | if (diffSeconds >= 6 * aDay) return 'one_day' | |
51 | if (diffSeconds >= 6 * anHour) return 'one_hour' | |
52 | if (diffSeconds >= 60 * aMinute) return 'ten_minutes' | |
53 | ||
54 | return 'one_minute' | |
55 | } |