1 import { logger, loggerTagsFactory } from '@server/helpers/logger'
2 import { MVideo, MVideoImmutable } from '@server/types/models'
3 import { VideoViewEvent } from '@shared/models'
4 import { VideoScope, VideoViewerCounters, VideoViewerStats, VideoViews, ViewerScope } from './shared'
7 * If processing a local view:
8 * - We update viewer information (segments watched, watch time etc)
9 * - We add +1 to video viewers counter if this is a new viewer
10 * - We add +1 to video views counter if this is a new view and if the user watched enough seconds
11 * - We send AP message to notify about this viewer and this view
12 * - We update last video time for the user if authenticated
14 * If processing a remote view:
15 * - We add +1 to video viewers counter
16 * - We add +1 to video views counter
18 * A viewer is a someone that watched one or multiple sections of a video
19 * A viewer that watched only a few seconds of a video may not increment the video views counter
20 * Viewers statistics are sent to origin instance using the `WatchAction` ActivityPub object
24 const lTags = loggerTagsFactory('views')
26 export class VideoViewsManager {
28 private static instance: VideoViewsManager
30 private videoViewerStats: VideoViewerStats
31 private videoViewerCounters: VideoViewerCounters
32 private videoViews: VideoViews
34 private constructor () {
38 this.videoViewerStats = new VideoViewerStats()
39 this.videoViewerCounters = new VideoViewerCounters()
40 this.videoViews = new VideoViews()
43 async processLocalView (options: {
44 video: MVideoImmutable
47 viewEvent?: VideoViewEvent
49 const { video, ip, viewEvent, currentTime } = options
51 logger.debug('Processing local view for %s and ip %s.', video.url, ip, lTags())
53 await this.videoViewerStats.addLocalViewer({ video, ip, viewEvent, currentTime })
55 const successViewer = await this.videoViewerCounters.addLocalViewer({ video, ip })
57 // Do it after added local viewer to fetch updated information
58 const watchTime = await this.videoViewerStats.getWatchTime(video.id, ip)
60 const successView = await this.videoViews.addLocalView({ video, watchTime, ip })
62 return { successView, successViewer }
65 async processRemoteView (options: {
67 viewerId: string | null
70 const { video, viewerId, viewerExpires } = options
72 logger.debug('Processing remote view for %s.', video.url, { viewerExpires, viewerId, ...lTags() })
74 if (viewerExpires) await this.videoViewerCounters.addRemoteViewer({ video, viewerId, viewerExpires })
75 else await this.videoViews.addRemoteView({ video })
78 getViewers (video: MVideo) {
79 return this.videoViewerCounters.getViewers(video)
82 getTotalViewers (options: {
83 viewerScope: ViewerScope
84 videoScope: VideoScope
86 return this.videoViewerCounters.getTotalViewers(options)
89 buildViewerExpireTime () {
90 return this.videoViewerCounters.buildViewerExpireTime()
93 processViewerStats () {
94 return this.videoViewerStats.processViewerStats()
97 static get Instance () {
98 return this.instance || (this.instance = new this())