]>
Commit | Line | Data |
---|---|---|
6b616860 C |
1 | import { Redis } from '../../redis' |
2 | import { logger } from '../../../helpers/logger' | |
3 | import { VideoModel } from '../../../models/video/video' | |
4 | import { VideoViewModel } from '../../../models/video/video-views' | |
6f0c46be | 5 | import { isTestInstance } from '../../../helpers/core-utils' |
6b616860 C |
6 | |
7 | async function processVideosViewsViews () { | |
6f0c46be C |
8 | const lastHour = new Date() |
9 | ||
10 | // In test mode, we run this function multiple times per hour, so we don't want the values of the previous hour | |
11 | if (!isTestInstance()) lastHour.setHours(lastHour.getHours() - 1) | |
12 | ||
13 | const hour = lastHour.getHours() | |
14 | const startDate = lastHour.setMinutes(0, 0, 0) | |
15 | const endDate = lastHour.setMinutes(59, 59, 999) | |
6b616860 C |
16 | |
17 | const videoIds = await Redis.Instance.getVideosIdViewed(hour) | |
18 | if (videoIds.length === 0) return | |
19 | ||
20 | logger.info('Processing videos views in job for hour %d.', hour) | |
21 | ||
22 | for (const videoId of videoIds) { | |
9431cabf C |
23 | try { |
24 | const views = await Redis.Instance.getVideoViews(videoId, hour) | |
25 | if (isNaN(views)) { | |
26 | logger.error('Cannot process videos views of video %d in hour %d: views number is NaN.', videoId, hour) | |
27 | } else { | |
28 | logger.debug('Adding %d views to video %d in hour %d.', views, videoId, hour) | |
29 | ||
30 | await VideoModel.incrementViews(videoId, views) | |
31 | ||
32 | try { | |
33 | await VideoViewModel.create({ | |
34 | startDate, | |
35 | endDate, | |
36 | views, | |
37 | videoId | |
38 | }) | |
39 | } catch (err) { | |
40 | logger.debug('Cannot create video views for video %d in hour %d. Maybe the video does not exist anymore?', videoId, hour) | |
41 | } | |
42 | } | |
43 | ||
44 | await Redis.Instance.deleteVideoViews(videoId, hour) | |
45 | } catch (err) { | |
46 | logger.error('Cannot update video views of video %d in hour %d.', videoId, hour) | |
6b616860 | 47 | } |
6b616860 C |
48 | } |
49 | } | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | ||
53 | export { | |
54 | processVideosViewsViews | |
55 | } |