]>
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' | |
5 | ||
6 | async function processVideosViewsViews () { | |
7 | const hour = new Date().getHours() | |
8 | const startDate = new Date().setMinutes(0, 0, 0) | |
9 | const endDate = new Date().setMinutes(59, 59, 999) | |
10 | ||
11 | const videoIds = await Redis.Instance.getVideosIdViewed(hour) | |
12 | if (videoIds.length === 0) return | |
13 | ||
14 | logger.info('Processing videos views in job for hour %d.', hour) | |
15 | ||
16 | for (const videoId of videoIds) { | |
9431cabf C |
17 | try { |
18 | const views = await Redis.Instance.getVideoViews(videoId, hour) | |
19 | if (isNaN(views)) { | |
20 | logger.error('Cannot process videos views of video %d in hour %d: views number is NaN.', videoId, hour) | |
21 | } else { | |
22 | logger.debug('Adding %d views to video %d in hour %d.', views, videoId, hour) | |
23 | ||
24 | await VideoModel.incrementViews(videoId, views) | |
25 | ||
26 | try { | |
27 | await VideoViewModel.create({ | |
28 | startDate, | |
29 | endDate, | |
30 | views, | |
31 | videoId | |
32 | }) | |
33 | } catch (err) { | |
34 | logger.debug('Cannot create video views for video %d in hour %d. Maybe the video does not exist anymore?', videoId, hour) | |
35 | } | |
36 | } | |
37 | ||
38 | await Redis.Instance.deleteVideoViews(videoId, hour) | |
39 | } catch (err) { | |
40 | logger.error('Cannot update video views of video %d in hour %d.', videoId, hour) | |
6b616860 | 41 | } |
6b616860 C |
42 | } |
43 | } | |
44 | ||
45 | // --------------------------------------------------------------------------- | |
46 | ||
47 | export { | |
48 | processVideosViewsViews | |
49 | } |