diff options
author | Chocobozzz <me@florianbigard.com> | 2022-07-27 16:19:25 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-07-27 16:19:25 +0200 |
commit | adc94cf09c86112051f72055852efcc977e4a04a (patch) | |
tree | ae6f25824c1e66004fa69aa9f685642d8d7ad8b4 /server/lib/views | |
parent | 50cc1ee48aacb6e7d6513c0f108492a589b515ea (diff) | |
download | PeerTube-adc94cf09c86112051f72055852efcc977e4a04a.tar.gz PeerTube-adc94cf09c86112051f72055852efcc977e4a04a.tar.zst PeerTube-adc94cf09c86112051f72055852efcc977e4a04a.zip |
Add live and viewers otel metrics
Diffstat (limited to 'server/lib/views')
-rw-r--r-- | server/lib/views/shared/video-viewer-counters.ts | 31 | ||||
-rw-r--r-- | server/lib/views/video-views-manager.ts | 9 |
2 files changed, 35 insertions, 5 deletions
diff --git a/server/lib/views/shared/video-viewer-counters.ts b/server/lib/views/shared/video-viewer-counters.ts index f851ce050..f5b83130e 100644 --- a/server/lib/views/shared/video-viewer-counters.ts +++ b/server/lib/views/shared/video-viewer-counters.ts | |||
@@ -10,9 +10,14 @@ import { buildUUID, sha256 } from '@shared/extra-utils' | |||
10 | 10 | ||
11 | const lTags = loggerTagsFactory('views') | 11 | const lTags = loggerTagsFactory('views') |
12 | 12 | ||
13 | export type ViewerScope = 'local' | 'remote' | ||
14 | export type VideoScope = 'local' | 'remote' | ||
15 | |||
13 | type Viewer = { | 16 | type Viewer = { |
14 | expires: number | 17 | expires: number |
15 | id: string | 18 | id: string |
19 | viewerScope: ViewerScope | ||
20 | videoScope: VideoScope | ||
16 | lastFederation?: number | 21 | lastFederation?: number |
17 | } | 22 | } |
18 | 23 | ||
@@ -50,7 +55,7 @@ export class VideoViewerCounters { | |||
50 | return false | 55 | return false |
51 | } | 56 | } |
52 | 57 | ||
53 | const newViewer = await this.addViewerToVideo({ viewerId, video }) | 58 | const newViewer = await this.addViewerToVideo({ viewerId, video, viewerScope: 'local' }) |
54 | await this.federateViewerIfNeeded(video, newViewer) | 59 | await this.federateViewerIfNeeded(video, newViewer) |
55 | 60 | ||
56 | return true | 61 | return true |
@@ -65,13 +70,26 @@ export class VideoViewerCounters { | |||
65 | 70 | ||
66 | logger.debug('Adding remote viewer to video %s.', video.uuid, { ...lTags(video.uuid) }) | 71 | logger.debug('Adding remote viewer to video %s.', video.uuid, { ...lTags(video.uuid) }) |
67 | 72 | ||
68 | await this.addViewerToVideo({ video, viewerExpires, viewerId }) | 73 | await this.addViewerToVideo({ video, viewerExpires, viewerId, viewerScope: 'remote' }) |
69 | 74 | ||
70 | return true | 75 | return true |
71 | } | 76 | } |
72 | 77 | ||
73 | // --------------------------------------------------------------------------- | 78 | // --------------------------------------------------------------------------- |
74 | 79 | ||
80 | getTotalViewers (options: { | ||
81 | viewerScope: ViewerScope | ||
82 | videoScope: VideoScope | ||
83 | }) { | ||
84 | let total = 0 | ||
85 | |||
86 | for (const viewers of this.viewersPerVideo.values()) { | ||
87 | total += viewers.filter(v => v.viewerScope === options.viewerScope && v.videoScope === options.videoScope).length | ||
88 | } | ||
89 | |||
90 | return total | ||
91 | } | ||
92 | |||
75 | getViewers (video: MVideo) { | 93 | getViewers (video: MVideo) { |
76 | const viewers = this.viewersPerVideo.get(video.id) | 94 | const viewers = this.viewersPerVideo.get(video.id) |
77 | if (!viewers) return 0 | 95 | if (!viewers) return 0 |
@@ -88,9 +106,10 @@ export class VideoViewerCounters { | |||
88 | private async addViewerToVideo (options: { | 106 | private async addViewerToVideo (options: { |
89 | video: MVideoImmutable | 107 | video: MVideoImmutable |
90 | viewerId: string | 108 | viewerId: string |
109 | viewerScope: ViewerScope | ||
91 | viewerExpires?: Date | 110 | viewerExpires?: Date |
92 | }) { | 111 | }) { |
93 | const { video, viewerExpires, viewerId } = options | 112 | const { video, viewerExpires, viewerId, viewerScope } = options |
94 | 113 | ||
95 | let watchers = this.viewersPerVideo.get(video.id) | 114 | let watchers = this.viewersPerVideo.get(video.id) |
96 | 115 | ||
@@ -103,7 +122,11 @@ export class VideoViewerCounters { | |||
103 | ? viewerExpires.getTime() | 122 | ? viewerExpires.getTime() |
104 | : this.buildViewerExpireTime() | 123 | : this.buildViewerExpireTime() |
105 | 124 | ||
106 | const viewer = { id: viewerId, expires } | 125 | const videoScope: VideoScope = video.remote |
126 | ? 'remote' | ||
127 | : 'local' | ||
128 | |||
129 | const viewer = { id: viewerId, expires, videoScope, viewerScope } | ||
107 | watchers.push(viewer) | 130 | watchers.push(viewer) |
108 | 131 | ||
109 | this.idToViewer.set(viewerId, viewer) | 132 | this.idToViewer.set(viewerId, viewer) |
diff --git a/server/lib/views/video-views-manager.ts b/server/lib/views/video-views-manager.ts index 86758e8d8..c088dad5e 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, MVideoImmutable } from '@server/types/models' | 2 | import { MVideo, MVideoImmutable } from '@server/types/models' |
3 | import { VideoViewEvent } from '@shared/models' | 3 | import { VideoViewEvent } from '@shared/models' |
4 | import { VideoViewerCounters, VideoViewerStats, VideoViews } from './shared' | 4 | import { VideoScope, VideoViewerCounters, VideoViewerStats, VideoViews, ViewerScope } from './shared' |
5 | 5 | ||
6 | /** | 6 | /** |
7 | * If processing a local view: | 7 | * If processing a local view: |
@@ -79,6 +79,13 @@ export class VideoViewsManager { | |||
79 | return this.videoViewerCounters.getViewers(video) | 79 | return this.videoViewerCounters.getViewers(video) |
80 | } | 80 | } |
81 | 81 | ||
82 | getTotalViewers (options: { | ||
83 | viewerScope: ViewerScope | ||
84 | videoScope: VideoScope | ||
85 | }) { | ||
86 | return this.videoViewerCounters.getTotalViewers(options) | ||
87 | } | ||
88 | |||
82 | buildViewerExpireTime () { | 89 | buildViewerExpireTime () { |
83 | return this.videoViewerCounters.buildViewerExpireTime() | 90 | return this.videoViewerCounters.buildViewerExpireTime() |
84 | } | 91 | } |