aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/views/video-views-manager.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-24 13:36:47 +0100
committerChocobozzz <chocobozzz@cpy.re>2022-04-15 09:49:35 +0200
commitb211106695bb82f6c32e53306081b5262c3d109d (patch)
treefa187de1c33b0956665f5362e29af6b0f6d8bb57 /server/lib/views/video-views-manager.ts
parent69d48ee30c9d47cddf0c3c047dc99a99dcb6e894 (diff)
downloadPeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.gz
PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.tar.zst
PeerTube-b211106695bb82f6c32e53306081b5262c3d109d.zip
Support video views/viewers stats in server
* Add "currentTime" and "event" body params to view endpoint * Merge watching and view endpoints * Introduce WatchAction AP activity * Add tables to store viewer information of local videos * Add endpoints to fetch video views/viewers stats of local videos * Refactor views/viewers handlers * Support "views" and "viewers" counters for both VOD and live videos
Diffstat (limited to 'server/lib/views/video-views-manager.ts')
-rw-r--r--server/lib/views/video-views-manager.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/server/lib/views/video-views-manager.ts b/server/lib/views/video-views-manager.ts
new file mode 100644
index 000000000..e07af1ca9
--- /dev/null
+++ b/server/lib/views/video-views-manager.ts
@@ -0,0 +1,70 @@
1import { logger, loggerTagsFactory } from '@server/helpers/logger'
2import { MVideo } from '@server/types/models'
3import { VideoViewEvent } from '@shared/models'
4import { VideoViewers, VideoViews } from './shared'
5
6const lTags = loggerTagsFactory('views')
7
8export class VideoViewsManager {
9
10 private static instance: VideoViewsManager
11
12 private videoViewers: VideoViewers
13 private videoViews: VideoViews
14
15 private constructor () {
16 }
17
18 init () {
19 this.videoViewers = new VideoViewers()
20 this.videoViews = new VideoViews()
21 }
22
23 async processLocalView (options: {
24 video: MVideo
25 currentTime: number
26 ip: string | null
27 viewEvent?: VideoViewEvent
28 }) {
29 const { video, ip, viewEvent, currentTime } = options
30
31 logger.debug('Processing local view for %s and ip %s.', video.url, ip, lTags())
32
33 const successViewer = await this.videoViewers.addLocalViewer({ video, ip, viewEvent, currentTime })
34
35 // Do it after added local viewer to fetch updated information
36 const watchTime = await this.videoViewers.getWatchTime(video.id, ip)
37
38 const successView = await this.videoViews.addLocalView({ video, watchTime, ip })
39
40 return { successView, successViewer }
41 }
42
43 async processRemoteView (options: {
44 video: MVideo
45 viewerExpires?: Date
46 }) {
47 const { video, viewerExpires } = options
48
49 logger.debug('Processing remote view for %s.', video.url, { viewerExpires, ...lTags() })
50
51 if (viewerExpires) await this.videoViewers.addRemoteViewer({ video, viewerExpires })
52 else await this.videoViews.addRemoteView({ video })
53 }
54
55 getViewers (video: MVideo) {
56 return this.videoViewers.getViewers(video)
57 }
58
59 buildViewerExpireTime () {
60 return this.videoViewers.buildViewerExpireTime()
61 }
62
63 processViewers () {
64 return this.videoViewers.processViewerStats()
65 }
66
67 static get Instance () {
68 return this.instance || (this.instance = new this())
69 }
70}