diff options
Diffstat (limited to 'server/lib/timeserie.ts')
-rw-r--r-- | server/lib/timeserie.ts | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/server/lib/timeserie.ts b/server/lib/timeserie.ts index d8f700a2f..bd3d1c1ca 100644 --- a/server/lib/timeserie.ts +++ b/server/lib/timeserie.ts | |||
@@ -1,24 +1,17 @@ | |||
1 | import { logger } from '@server/helpers/logger' | 1 | import { logger } from '@server/helpers/logger' |
2 | import { VideoStatsTimeserieGroupInterval } from '@shared/models' | ||
3 | 2 | ||
4 | function buildGroupByAndBoundaries (startDateString: string, endDateString: string) { | 3 | function buildGroupByAndBoundaries (startDateString: string, endDateString: string) { |
5 | const startDate = new Date(startDateString) | 4 | const startDate = new Date(startDateString) |
6 | const endDate = new Date(endDateString) | 5 | const endDate = new Date(endDateString) |
7 | 6 | ||
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) | 7 | const groupInterval = buildGroupInterval(startDate, endDate) |
15 | 8 | ||
16 | logger.debug('Found "%s" group interval.', groupInterval, { startDate, endDate }) | 9 | logger.debug('Found "%s" group interval.', groupInterval, { startDate, endDate }) |
17 | 10 | ||
18 | // Remove parts of the date we don't need | 11 | // Remove parts of the date we don't need |
19 | if (groupInterval === 'one_day') { | 12 | if (groupInterval.endsWith(' day') || groupInterval.endsWith(' days')) { |
20 | startDate.setHours(0, 0, 0, 0) | 13 | startDate.setHours(0, 0, 0, 0) |
21 | } else if (groupInterval === 'one_hour') { | 14 | } else if (groupInterval.endsWith(' hour') || groupInterval.endsWith(' hours')) { |
22 | startDate.setMinutes(0, 0, 0) | 15 | startDate.setMinutes(0, 0, 0) |
23 | } else { | 16 | } else { |
24 | startDate.setSeconds(0, 0) | 17 | startDate.setSeconds(0, 0) |
@@ -26,7 +19,6 @@ function buildGroupByAndBoundaries (startDateString: string, endDateString: stri | |||
26 | 19 | ||
27 | return { | 20 | return { |
28 | groupInterval, | 21 | groupInterval, |
29 | sqlInterval: groupByMatrix[groupInterval], | ||
30 | startDate, | 22 | startDate, |
31 | endDate | 23 | endDate |
32 | } | 24 | } |
@@ -40,16 +32,18 @@ export { | |||
40 | 32 | ||
41 | // --------------------------------------------------------------------------- | 33 | // --------------------------------------------------------------------------- |
42 | 34 | ||
43 | function buildGroupInterval (startDate: Date, endDate: Date): VideoStatsTimeserieGroupInterval { | 35 | function buildGroupInterval (startDate: Date, endDate: Date): string { |
44 | const aDay = 86400 | 36 | const aDay = 86400 |
45 | const anHour = 3600 | 37 | const anHour = 3600 |
46 | const aMinute = 60 | 38 | const aMinute = 60 |
47 | 39 | ||
48 | const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000 | 40 | const diffSeconds = (endDate.getTime() - startDate.getTime()) / 1000 |
49 | 41 | ||
50 | if (diffSeconds >= 6 * aDay) return 'one_day' | 42 | if (diffSeconds >= 15 * aDay) return '1 day' |
51 | if (diffSeconds >= 6 * anHour) return 'one_hour' | 43 | if (diffSeconds >= 8 * aDay) return '12 hours' |
52 | if (diffSeconds >= 60 * aMinute) return 'ten_minutes' | 44 | if (diffSeconds >= 4 * aDay) return '6 hours' |
45 | if (diffSeconds >= 15 * anHour) return '1 hour' | ||
46 | if (diffSeconds >= 180 * aMinute) return '10 minutes' | ||
53 | 47 | ||
54 | return 'one_minute' | 48 | return '1 minute' |
55 | } | 49 | } |