]>
Commit | Line | Data |
---|---|---|
901bcf5c | 1 | import { logger } from '@server/helpers/logger' |
901bcf5c C |
2 | |
3 | function buildGroupByAndBoundaries (startDateString: string, endDateString: string) { | |
4 | const startDate = new Date(startDateString) | |
5 | const endDate = new Date(endDateString) | |
6 | ||
901bcf5c C |
7 | const groupInterval = buildGroupInterval(startDate, endDate) |
8 | ||
9 | logger.debug('Found "%s" group interval.', groupInterval, { startDate, endDate }) | |
10 | ||
11 | // Remove parts of the date we don't need | |
3eda9b77 | 12 | if (groupInterval.endsWith(' day') || groupInterval.endsWith(' days')) { |
901bcf5c | 13 | startDate.setHours(0, 0, 0, 0) |
3eda9b77 | 14 | } else if (groupInterval.endsWith(' hour') || groupInterval.endsWith(' hours')) { |
901bcf5c C |
15 | startDate.setMinutes(0, 0, 0) |
16 | } else { | |
17 | startDate.setSeconds(0, 0) | |
18 | } | |
19 | ||
20 | return { | |
21 | groupInterval, | |
901bcf5c C |
22 | startDate, |
23 | endDate | |
24 | } | |
25 | } | |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | ||
29 | export { | |
30 | buildGroupByAndBoundaries | |
31 | } | |
32 | ||
33 | // --------------------------------------------------------------------------- | |
34 | ||
3eda9b77 | 35 | function buildGroupInterval (startDate: Date, endDate: Date): string { |
901bcf5c C |
36 | const aDay = 86400 |
37 | const anHour = 3600 | |
38 | const aMinute = 60 | |
39 | ||
40 | const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000 | |
41 | ||
3eda9b77 C |
42 | if (diffSeconds >= 15 * aDay) return '1 day' |
43 | if (diffSeconds >= 8 * aDay) return '12 hours' | |
44 | if (diffSeconds >= 4 * aDay) return '6 hours' | |
45 | if (diffSeconds >= 15 * anHour) return '1 hour' | |
46 | if (diffSeconds >= 180 * aMinute) return '10 minutes' | |
901bcf5c | 47 | |
3eda9b77 | 48 | return '1 minute' |
901bcf5c | 49 | } |