]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/timeserie.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / timeserie.ts
CommitLineData
901bcf5c 1import { logger } from '@server/helpers/logger'
901bcf5c
C
2
3function 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
f40712ab
C
12 if (groupInterval.endsWith(' month') || groupInterval.endsWith(' months')) {
13 startDate.setDate(1)
14 startDate.setHours(0, 0, 0, 0)
15 } else if (groupInterval.endsWith(' day') || groupInterval.endsWith(' days')) {
901bcf5c 16 startDate.setHours(0, 0, 0, 0)
3eda9b77 17 } else if (groupInterval.endsWith(' hour') || groupInterval.endsWith(' hours')) {
901bcf5c
C
18 startDate.setMinutes(0, 0, 0)
19 } else {
20 startDate.setSeconds(0, 0)
21 }
22
23 return {
24 groupInterval,
901bcf5c
C
25 startDate,
26 endDate
27 }
28}
29
30// ---------------------------------------------------------------------------
31
32export {
33 buildGroupByAndBoundaries
34}
35
36// ---------------------------------------------------------------------------
37
3eda9b77 38function buildGroupInterval (startDate: Date, endDate: Date): string {
f40712ab
C
39 const aYear = 31536000
40 const aMonth = 2678400
901bcf5c
C
41 const aDay = 86400
42 const anHour = 3600
43 const aMinute = 60
44
45 const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000
46
f40712ab
C
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'
51
3eda9b77
C
52 if (diffSeconds >= 15 * aDay) return '1 day'
53 if (diffSeconds >= 8 * aDay) return '12 hours'
54 if (diffSeconds >= 4 * aDay) return '6 hours'
f40712ab 55
3eda9b77 56 if (diffSeconds >= 15 * anHour) return '1 hour'
f40712ab 57
3eda9b77 58 if (diffSeconds >= 180 * aMinute) return '10 minutes'
901bcf5c 59
3eda9b77 60 return '1 minute'
901bcf5c 61}