aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/job-queue/handlers/video-views.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-11-09 10:11:20 +0100
committerChocobozzz <chocobozzz@cpy.re>2021-11-09 15:00:31 +0100
commit51353d9a035fb6b81f903a8b5f391292841649fd (patch)
tree75acb6eea5e043bf2e15a6a5a92e9a3c5967b156 /server/lib/job-queue/handlers/video-views.ts
parent221ee1adc916684d4881d2a9c4c01954dcde986e (diff)
downloadPeerTube-51353d9a035fb6b81f903a8b5f391292841649fd.tar.gz
PeerTube-51353d9a035fb6b81f903a8b5f391292841649fd.tar.zst
PeerTube-51353d9a035fb6b81f903a8b5f391292841649fd.zip
Refactor video views
Introduce viewers attribute for live videos Count views for live videos Reduce delay to see the viewer update for lives Add ability to configure video views buffer interval and view ip expiration
Diffstat (limited to 'server/lib/job-queue/handlers/video-views.ts')
-rw-r--r--server/lib/job-queue/handlers/video-views.ts67
1 files changed, 0 insertions, 67 deletions
diff --git a/server/lib/job-queue/handlers/video-views.ts b/server/lib/job-queue/handlers/video-views.ts
deleted file mode 100644
index 86d0a271f..000000000
--- a/server/lib/job-queue/handlers/video-views.ts
+++ /dev/null
@@ -1,67 +0,0 @@
1import { Redis } from '../../redis'
2import { logger } from '../../../helpers/logger'
3import { VideoModel } from '../../../models/video/video'
4import { VideoViewModel } from '../../../models/video/video-view'
5import { isTestInstance } from '../../../helpers/core-utils'
6import { federateVideoIfNeeded } from '../../activitypub/videos'
7
8async function processVideosViews () {
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)
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) {
24 try {
25 const views = await Redis.Instance.getVideoViews(videoId, hour)
26 await Redis.Instance.deleteVideoViews(videoId, hour)
27
28 if (views) {
29 logger.debug('Adding %d views to video %d in hour %d.', views, videoId, hour)
30
31 try {
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
38 await VideoViewModel.create({
39 startDate: new Date(startDate),
40 endDate: new Date(endDate),
41 views,
42 videoId
43 })
44
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 }
53 } catch (err) {
54 logger.error('Cannot create video views for video %d in hour %d.', videoId, hour, { err })
55 }
56 }
57 } catch (err) {
58 logger.error('Cannot update video views of video %d in hour %d.', videoId, hour, { err })
59 }
60 }
61}
62
63// ---------------------------------------------------------------------------
64
65export {
66 processVideosViews
67}