]>
Commit | Line | Data |
---|---|---|
6b616860 C |
1 | import { Redis } from '../../redis' |
2 | import { logger } from '../../../helpers/logger' | |
3 | import { VideoModel } from '../../../models/video/video' | |
fd261a8d | 4 | import { VideoViewModel } from '../../../models/video/video-view' |
6f0c46be | 5 | import { isTestInstance } from '../../../helpers/core-utils' |
8dc8a34e | 6 | import { federateVideoIfNeeded } from '../../activitypub/videos' |
6b616860 | 7 | |
030177d2 | 8 | async function processVideosViews () { |
6f0c46be C |
9 | const lastHour = new Date() |
10 | ||
11 | // In test mode, we run this function multiple times per hour, so we don't want the values of the previous hour | |
12 | if (!isTestInstance()) lastHour.setHours(lastHour.getHours() - 1) | |
13 | ||
14 | const hour = lastHour.getHours() | |
15 | const startDate = lastHour.setMinutes(0, 0, 0) | |
16 | const endDate = lastHour.setMinutes(59, 59, 999) | |
6b616860 C |
17 | |
18 | const videoIds = await Redis.Instance.getVideosIdViewed(hour) | |
19 | if (videoIds.length === 0) return | |
20 | ||
21 | logger.info('Processing videos views in job for hour %d.', hour) | |
22 | ||
23 | for (const videoId of videoIds) { | |
9431cabf C |
24 | try { |
25 | const views = await Redis.Instance.getVideoViews(videoId, hour) | |
ca6d3622 C |
26 | await Redis.Instance.deleteVideoViews(videoId, hour) |
27 | ||
6040f87d | 28 | if (views) { |
9431cabf C |
29 | logger.debug('Adding %d views to video %d in hour %d.', views, videoId, hour) |
30 | ||
9431cabf | 31 | try { |
63dc5898 C |
32 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) |
33 | if (!video) { | |
34 | logger.debug('Video %d does not exist anymore, skipping videos view addition.', videoId) | |
35 | continue | |
36 | } | |
37 | ||
9431cabf C |
38 | await VideoViewModel.create({ |
39 | startDate, | |
40 | endDate, | |
41 | views, | |
42 | videoId | |
43 | }) | |
030177d2 | 44 | |
dbe6aa69 C |
45 | if (video.isOwned()) { |
46 | // If this is a remote video, the origin instance will send us an update | |
47 | await VideoModel.incrementViews(videoId, views) | |
48 | ||
49 | // Send video update | |
50 | video.views += views | |
51 | await federateVideoIfNeeded(video, false) | |
52 | } | |
9431cabf | 53 | } catch (err) { |
63dc5898 | 54 | logger.error('Cannot create video views for video %d in hour %d.', videoId, hour, { err }) |
9431cabf C |
55 | } |
56 | } | |
9431cabf | 57 | } catch (err) { |
63dc5898 | 58 | logger.error('Cannot update video views of video %d in hour %d.', videoId, hour, { err }) |
6b616860 | 59 | } |
6b616860 C |
60 | } |
61 | } | |
62 | ||
63 | // --------------------------------------------------------------------------- | |
64 | ||
65 | export { | |
030177d2 | 66 | processVideosViews |
6b616860 | 67 | } |