]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/timeserie.ts
Translated using Weblate (Japanese)
[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(' day') || groupInterval.endsWith(' days')) {
13 startDate.setHours(0, 0, 0, 0)
14 } else if (groupInterval.endsWith(' hour') || groupInterval.endsWith(' hours')) {
15 startDate.setMinutes(0, 0, 0)
16 } else {
17 startDate.setSeconds(0, 0)
18 }
19
20 return {
21 groupInterval,
22 startDate,
23 endDate
24 }
25 }
26
27 // ---------------------------------------------------------------------------
28
29 export {
30 buildGroupByAndBoundaries
31 }
32
33 // ---------------------------------------------------------------------------
34
35 function buildGroupInterval (startDate: Date, endDate: Date): string {
36 const aDay = 86400
37 const anHour = 3600
38 const aMinute = 60
39
40 const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000
41
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'
47
48 return '1 minute'
49 }