diff options
author | Chocobozzz <me@florianbigard.com> | 2022-04-07 10:53:35 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-04-15 09:49:35 +0200 |
commit | 901bcf5c188ea79350fecd499ad76460b866617b (patch) | |
tree | 1e79f26cc3f2b952371d31bfa9b94a2b150be38a /server/lib/timeserie.ts | |
parent | ac907dc7c158056e9b6a5cb58acd27df5c7c2670 (diff) | |
download | PeerTube-901bcf5c188ea79350fecd499ad76460b866617b.tar.gz PeerTube-901bcf5c188ea79350fecd499ad76460b866617b.tar.zst PeerTube-901bcf5c188ea79350fecd499ad76460b866617b.zip |
Add ability to set start/end date to timeserie
Diffstat (limited to 'server/lib/timeserie.ts')
-rw-r--r-- | server/lib/timeserie.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/server/lib/timeserie.ts b/server/lib/timeserie.ts new file mode 100644 index 000000000..d8f700a2f --- /dev/null +++ b/server/lib/timeserie.ts | |||
@@ -0,0 +1,55 @@ | |||
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 | } | ||