]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-views.ts
Fix thumbnail processing
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-views.ts
CommitLineData
6b616860
C
1import { Redis } from '../../redis'
2import { logger } from '../../../helpers/logger'
3import { VideoModel } from '../../../models/video/video'
4import { VideoViewModel } from '../../../models/video/video-views'
6f0c46be 5import { isTestInstance } from '../../../helpers/core-utils'
030177d2 6import { federateVideoIfNeeded } from '../../activitypub'
6b616860 7
030177d2 8async 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)
26 if (isNaN(views)) {
dbe6aa69 27 logger.error('Cannot process videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, views)
9431cabf
C
28 } else {
29 logger.debug('Adding %d views to video %d in hour %d.', views, videoId, hour)
30
9431cabf
C
31 try {
32 await VideoViewModel.create({
33 startDate,
34 endDate,
35 views,
36 videoId
37 })
030177d2
C
38
39 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
dbe6aa69
C
40 if (video.isOwned()) {
41 // If this is a remote video, the origin instance will send us an update
42 await VideoModel.incrementViews(videoId, views)
43
44 // Send video update
45 video.views += views
46 await federateVideoIfNeeded(video, false)
47 }
9431cabf
C
48 } catch (err) {
49 logger.debug('Cannot create video views for video %d in hour %d. Maybe the video does not exist anymore?', videoId, hour)
50 }
51 }
52
53 await Redis.Instance.deleteVideoViews(videoId, hour)
54 } catch (err) {
55 logger.error('Cannot update video views of video %d in hour %d.', videoId, hour)
6b616860 56 }
6b616860
C
57 }
58}
59
60// ---------------------------------------------------------------------------
61
62export {
030177d2 63 processVideosViews
6b616860 64}