diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-24 13:36:47 +0100 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-04-15 09:49:35 +0200 |
commit | b211106695bb82f6c32e53306081b5262c3d109d (patch) | |
tree | fa187de1c33b0956665f5362e29af6b0f6d8bb57 /server/lib/views/shared/video-views.ts | |
parent | 69d48ee30c9d47cddf0c3c047dc99a99dcb6e894 (diff) | |
download | PeerTube-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/shared/video-views.ts')
-rw-r--r-- | server/lib/views/shared/video-views.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/server/lib/views/shared/video-views.ts b/server/lib/views/shared/video-views.ts new file mode 100644 index 000000000..19250f993 --- /dev/null +++ b/server/lib/views/shared/video-views.ts | |||
@@ -0,0 +1,60 @@ | |||
1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | ||
2 | import { MVideo } from '@server/types/models' | ||
3 | import { Redis } from '../../redis' | ||
4 | |||
5 | const lTags = loggerTagsFactory('views') | ||
6 | |||
7 | export class VideoViews { | ||
8 | |||
9 | async addLocalView (options: { | ||
10 | video: MVideo | ||
11 | ip: string | ||
12 | watchTime: number | ||
13 | }) { | ||
14 | const { video, ip, watchTime } = options | ||
15 | |||
16 | logger.debug('Adding local view to video %s.', video.uuid, { watchTime, ...lTags(video.uuid) }) | ||
17 | |||
18 | if (!this.hasEnoughWatchTime(video, watchTime)) return false | ||
19 | |||
20 | const viewExists = await Redis.Instance.doesVideoIPViewExist(ip, video.uuid) | ||
21 | if (viewExists) return false | ||
22 | |||
23 | await Redis.Instance.setIPVideoView(ip, video.uuid) | ||
24 | |||
25 | await this.addView(video) | ||
26 | |||
27 | return true | ||
28 | } | ||
29 | |||
30 | async addRemoteView (options: { | ||
31 | video: MVideo | ||
32 | }) { | ||
33 | const { video } = options | ||
34 | |||
35 | logger.debug('Adding remote view to video %s.', video.uuid, { ...lTags(video.uuid) }) | ||
36 | |||
37 | await this.addView(video) | ||
38 | |||
39 | return true | ||
40 | } | ||
41 | |||
42 | private async addView (video: MVideo) { | ||
43 | const promises: Promise<any>[] = [] | ||
44 | |||
45 | if (video.isOwned()) { | ||
46 | promises.push(Redis.Instance.addLocalVideoView(video.id)) | ||
47 | } | ||
48 | |||
49 | promises.push(Redis.Instance.addVideoViewStats(video.id)) | ||
50 | |||
51 | await Promise.all(promises) | ||
52 | } | ||
53 | |||
54 | private hasEnoughWatchTime (video: MVideo, watchTime: number) { | ||
55 | if (video.isLive || video.duration >= 30) return watchTime >= 30 | ||
56 | |||
57 | // Check more than 50% of the video is watched | ||
58 | return video.duration / watchTime < 2 | ||
59 | } | ||
60 | } | ||