]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/timeserie.ts
Add test on AP hooks
[github/Chocobozzz/PeerTube.git] / server / lib / timeserie.ts
1 import { logger } from '@server/helpers/logger'
2
3 function buildGroupByAndBoundaries (startDateString: string, endDateString: string) {
4 const startDate = new Date(startDateString)
5 const endDate = new Date(endDateString)
6
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
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')) {
16 startDate.setHours(0, 0, 0, 0)
17 } else if (groupInterval.endsWith(' hour') || groupInterval.endsWith(' hours')) {
18 startDate.setMinutes(0, 0, 0)
19 } else {
20 startDate.setSeconds(0, 0)
21 }
22
23 return {
24 groupInterval,
25 startDate,
26 endDate
27 }
28 }
29
30 // ---------------------------------------------------------------------------
31
32 export {
33 buildGroupByAndBoundaries
34 }
35
36 // ---------------------------------------------------------------------------
37
38 function buildGroupInterval (startDate: Date, endDate: Date): string {
39 const aYear = 31536000
40 const aMonth = 2678400
41 const aDay = 86400
42 const anHour = 3600
43 const aMinute = 60
44
45 const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000
46
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
52 if (diffSeconds >= 15 * aDay) return '1 day'
53 if (diffSeconds >= 8 * aDay) return '12 hours'
54 if (diffSeconds >= 4 * aDay) return '6 hours'
55
56 if (diffSeconds >= 15 * anHour) return '1 hour'
57
58 if (diffSeconds >= 180 * aMinute) return '10 minutes'
59
60 return '1 minute'
61 }