diff options
author | Chocobozzz <me@florianbigard.com> | 2022-04-06 08:50:43 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2022-04-15 09:49:35 +0200 |
commit | ac907dc7c158056e9b6a5cb58acd27df5c7c2670 (patch) | |
tree | f9d8bff22e0543a305c64e1a1808c0df6d512f1e /server/lib/views/video-views-manager.ts | |
parent | dfbcefc20dc64f0814b1f2e8e782a4ea1bd24db2 (diff) | |
download | PeerTube-ac907dc7c158056e9b6a5cb58acd27df5c7c2670.tar.gz PeerTube-ac907dc7c158056e9b6a5cb58acd27df5c7c2670.tar.zst PeerTube-ac907dc7c158056e9b6a5cb58acd27df5c7c2670.zip |
Improve viewer counter
More precise, avoid weird decrease, reuse an id to federate viewers
Diffstat (limited to 'server/lib/views/video-views-manager.ts')
-rw-r--r-- | server/lib/views/video-views-manager.ts | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/server/lib/views/video-views-manager.ts b/server/lib/views/video-views-manager.ts index 9382fb482..ea3b35c6c 100644 --- a/server/lib/views/video-views-manager.ts +++ b/server/lib/views/video-views-manager.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' | 1 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
2 | import { MVideo } from '@server/types/models' | 2 | import { MVideo } from '@server/types/models' |
3 | import { VideoViewEvent } from '@shared/models' | 3 | import { VideoViewEvent } from '@shared/models' |
4 | import { VideoViewers, VideoViews } from './shared' | 4 | import { VideoViewerCounters, VideoViewerStats, VideoViews } from './shared' |
5 | 5 | ||
6 | /** | 6 | /** |
7 | * If processing a local view: | 7 | * If processing a local view: |
@@ -27,14 +27,16 @@ export class VideoViewsManager { | |||
27 | 27 | ||
28 | private static instance: VideoViewsManager | 28 | private static instance: VideoViewsManager |
29 | 29 | ||
30 | private videoViewers: VideoViewers | 30 | private videoViewerStats: VideoViewerStats |
31 | private videoViewerCounters: VideoViewerCounters | ||
31 | private videoViews: VideoViews | 32 | private videoViews: VideoViews |
32 | 33 | ||
33 | private constructor () { | 34 | private constructor () { |
34 | } | 35 | } |
35 | 36 | ||
36 | init () { | 37 | init () { |
37 | this.videoViewers = new VideoViewers() | 38 | this.videoViewerStats = new VideoViewerStats() |
39 | this.videoViewerCounters = new VideoViewerCounters() | ||
38 | this.videoViews = new VideoViews() | 40 | this.videoViews = new VideoViews() |
39 | } | 41 | } |
40 | 42 | ||
@@ -48,10 +50,12 @@ export class VideoViewsManager { | |||
48 | 50 | ||
49 | logger.debug('Processing local view for %s and ip %s.', video.url, ip, lTags()) | 51 | logger.debug('Processing local view for %s and ip %s.', video.url, ip, lTags()) |
50 | 52 | ||
51 | const successViewer = await this.videoViewers.addLocalViewer({ video, ip, viewEvent, currentTime }) | 53 | await this.videoViewerStats.addLocalViewer({ video, ip, viewEvent, currentTime }) |
54 | |||
55 | const successViewer = await this.videoViewerCounters.addLocalViewer({ video, ip }) | ||
52 | 56 | ||
53 | // Do it after added local viewer to fetch updated information | 57 | // Do it after added local viewer to fetch updated information |
54 | const watchTime = await this.videoViewers.getWatchTime(video.id, ip) | 58 | const watchTime = await this.videoViewerStats.getWatchTime(video.id, ip) |
55 | 59 | ||
56 | const successView = await this.videoViews.addLocalView({ video, watchTime, ip }) | 60 | const successView = await this.videoViews.addLocalView({ video, watchTime, ip }) |
57 | 61 | ||
@@ -60,26 +64,27 @@ export class VideoViewsManager { | |||
60 | 64 | ||
61 | async processRemoteView (options: { | 65 | async processRemoteView (options: { |
62 | video: MVideo | 66 | video: MVideo |
67 | viewerId: string | null | ||
63 | viewerExpires?: Date | 68 | viewerExpires?: Date |
64 | }) { | 69 | }) { |
65 | const { video, viewerExpires } = options | 70 | const { video, viewerId, viewerExpires } = options |
66 | 71 | ||
67 | logger.debug('Processing remote view for %s.', video.url, { viewerExpires, ...lTags() }) | 72 | logger.debug('Processing remote view for %s.', video.url, { viewerExpires, viewerId, ...lTags() }) |
68 | 73 | ||
69 | if (viewerExpires) await this.videoViewers.addRemoteViewer({ video, viewerExpires }) | 74 | if (viewerExpires) await this.videoViewerCounters.addRemoteViewer({ video, viewerId, viewerExpires }) |
70 | else await this.videoViews.addRemoteView({ video }) | 75 | else await this.videoViews.addRemoteView({ video }) |
71 | } | 76 | } |
72 | 77 | ||
73 | getViewers (video: MVideo) { | 78 | getViewers (video: MVideo) { |
74 | return this.videoViewers.getViewers(video) | 79 | return this.videoViewerCounters.getViewers(video) |
75 | } | 80 | } |
76 | 81 | ||
77 | buildViewerExpireTime () { | 82 | buildViewerExpireTime () { |
78 | return this.videoViewers.buildViewerExpireTime() | 83 | return this.videoViewerCounters.buildViewerExpireTime() |
79 | } | 84 | } |
80 | 85 | ||
81 | processViewers () { | 86 | processViewerStats () { |
82 | return this.videoViewers.processViewerStats() | 87 | return this.videoViewerStats.processViewerStats() |
83 | } | 88 | } |
84 | 89 | ||
85 | static get Instance () { | 90 | static get Instance () { |