2024-04-04 04:30:30 -05:00
|
|
|
import { isTestOrDevInstance, isUsingViewersFederationV2 } from '@peertube/peertube-node-utils'
|
2023-12-01 07:53:17 -06:00
|
|
|
import { exists } from '@server/helpers/custom-validators/misc.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
|
|
|
import { VIEW_LIFETIME } from '@server/initializers/constants.js'
|
|
|
|
import { sendView } from '@server/lib/activitypub/send/send-view.js'
|
|
|
|
import { PeerTubeSocket } from '@server/lib/peertube-socket.js'
|
|
|
|
import { getServerActor } from '@server/models/application/application.js'
|
|
|
|
import { VideoModel } from '@server/models/video/video.js'
|
|
|
|
import { MVideo, MVideoImmutable } from '@server/types/models/index.js'
|
2022-04-06 01:50:43 -05:00
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('views')
|
|
|
|
|
2022-07-27 09:19:25 -05:00
|
|
|
export type ViewerScope = 'local' | 'remote'
|
|
|
|
export type VideoScope = 'local' | 'remote'
|
|
|
|
|
2022-04-06 01:50:43 -05:00
|
|
|
type Viewer = {
|
|
|
|
expires: number
|
|
|
|
id: string
|
2022-07-27 09:19:25 -05:00
|
|
|
viewerScope: ViewerScope
|
|
|
|
videoScope: VideoScope
|
2023-12-01 07:53:17 -06:00
|
|
|
viewerCount: number
|
2022-04-06 01:50:43 -05:00
|
|
|
lastFederation?: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export class VideoViewerCounters {
|
|
|
|
|
|
|
|
// expires is new Date().getTime()
|
|
|
|
private readonly viewersPerVideo = new Map<number, Viewer[]>()
|
|
|
|
private readonly idToViewer = new Map<string, Viewer>()
|
|
|
|
|
|
|
|
private processingViewerCounters = false
|
|
|
|
|
|
|
|
constructor () {
|
|
|
|
setInterval(() => this.cleanViewerCounters(), VIEW_LIFETIME.VIEWER_COUNTER)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async addLocalViewer (options: {
|
2022-06-17 07:34:37 -05:00
|
|
|
video: MVideoImmutable
|
2024-04-04 04:30:30 -05:00
|
|
|
sessionId: string
|
2022-04-06 01:50:43 -05:00
|
|
|
}) {
|
2024-04-04 04:30:30 -05:00
|
|
|
const { video, sessionId } = options
|
2022-04-06 01:50:43 -05:00
|
|
|
|
|
|
|
logger.debug('Adding local viewer to video viewers counter %s.', video.uuid, { ...lTags(video.uuid) })
|
|
|
|
|
2024-04-04 04:30:30 -05:00
|
|
|
const viewerId = sessionId + '-' + video.uuid
|
2022-04-06 01:50:43 -05:00
|
|
|
const viewer = this.idToViewer.get(viewerId)
|
|
|
|
|
|
|
|
if (viewer) {
|
|
|
|
viewer.expires = this.buildViewerExpireTime()
|
|
|
|
await this.federateViewerIfNeeded(video, viewer)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
const newViewer = this.addViewerToVideo({ viewerId, video, viewerScope: 'local', viewerCount: 1 })
|
2022-04-06 01:50:43 -05:00
|
|
|
await this.federateViewerIfNeeded(video, newViewer)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
addRemoteViewerOnLocalVideo (options: {
|
2022-04-06 01:50:43 -05:00
|
|
|
video: MVideo
|
|
|
|
viewerId: string
|
|
|
|
viewerExpires: Date
|
|
|
|
}) {
|
|
|
|
const { video, viewerExpires, viewerId } = options
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
logger.debug('Adding remote viewer to local video %s.', video.uuid, { viewerId, viewerExpires, ...lTags(video.uuid) })
|
2022-04-06 01:50:43 -05:00
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
this.addViewerToVideo({ video, viewerExpires, viewerId, viewerScope: 'remote', viewerCount: 1 })
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
addRemoteViewerOnRemoteVideo (options: {
|
|
|
|
video: MVideo
|
|
|
|
viewerId: string
|
|
|
|
viewerExpires: Date
|
|
|
|
viewerResultCounter?: number
|
|
|
|
}) {
|
|
|
|
const { video, viewerExpires, viewerId, viewerResultCounter } = options
|
|
|
|
|
|
|
|
logger.debug(
|
|
|
|
'Adding remote viewer to remote video %s.', video.uuid,
|
|
|
|
{ viewerId, viewerResultCounter, viewerExpires, ...lTags(video.uuid) }
|
|
|
|
)
|
|
|
|
|
|
|
|
this.addViewerToVideo({
|
|
|
|
video,
|
|
|
|
viewerExpires,
|
|
|
|
viewerId,
|
|
|
|
viewerScope: 'remote',
|
|
|
|
// The origin server sends a summary of all viewers, so we can replace our local copy
|
|
|
|
replaceCurrentViewers: exists(viewerResultCounter),
|
|
|
|
viewerCount: viewerResultCounter ?? 1
|
|
|
|
})
|
2022-04-06 01:50:43 -05:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2022-07-27 09:19:25 -05:00
|
|
|
getTotalViewers (options: {
|
|
|
|
viewerScope: ViewerScope
|
|
|
|
videoScope: VideoScope
|
|
|
|
}) {
|
|
|
|
let total = 0
|
|
|
|
|
|
|
|
for (const viewers of this.viewersPerVideo.values()) {
|
2023-12-01 07:53:17 -06:00
|
|
|
total += viewers.filter(v => v.viewerScope === options.viewerScope && v.videoScope === options.videoScope)
|
|
|
|
.reduce((p, c) => p + c.viewerCount, 0)
|
2022-07-27 09:19:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return total
|
|
|
|
}
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
getTotalViewersOf (video: MVideoImmutable) {
|
2022-04-06 01:50:43 -05:00
|
|
|
const viewers = this.viewersPerVideo.get(video.id)
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
return viewers?.reduce((p, c) => p + c.viewerCount, 0) || 0
|
2022-04-06 01:50:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
buildViewerExpireTime () {
|
|
|
|
return new Date().getTime() + VIEW_LIFETIME.VIEWER_COUNTER
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
private addViewerToVideo (options: {
|
2022-06-17 07:34:37 -05:00
|
|
|
video: MVideoImmutable
|
2022-04-06 01:50:43 -05:00
|
|
|
viewerId: string
|
2022-07-27 09:19:25 -05:00
|
|
|
viewerScope: ViewerScope
|
2023-12-01 07:53:17 -06:00
|
|
|
viewerCount: number
|
|
|
|
replaceCurrentViewers?: boolean
|
2022-04-06 01:50:43 -05:00
|
|
|
viewerExpires?: Date
|
|
|
|
}) {
|
2023-12-01 07:53:17 -06:00
|
|
|
const { video, viewerExpires, viewerId, viewerScope, viewerCount, replaceCurrentViewers } = options
|
2022-04-06 01:50:43 -05:00
|
|
|
|
|
|
|
let watchers = this.viewersPerVideo.get(video.id)
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
if (!watchers || replaceCurrentViewers) {
|
2022-04-06 01:50:43 -05:00
|
|
|
watchers = []
|
|
|
|
this.viewersPerVideo.set(video.id, watchers)
|
|
|
|
}
|
|
|
|
|
|
|
|
const expires = viewerExpires
|
|
|
|
? viewerExpires.getTime()
|
|
|
|
: this.buildViewerExpireTime()
|
|
|
|
|
2022-07-27 09:19:25 -05:00
|
|
|
const videoScope: VideoScope = video.remote
|
|
|
|
? 'remote'
|
|
|
|
: 'local'
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
const viewer = { id: viewerId, expires, videoScope, viewerScope, viewerCount }
|
2022-04-06 01:50:43 -05:00
|
|
|
watchers.push(viewer)
|
|
|
|
|
|
|
|
this.idToViewer.set(viewerId, viewer)
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
this.notifyClients(video)
|
2022-04-06 01:50:43 -05:00
|
|
|
|
|
|
|
return viewer
|
|
|
|
}
|
|
|
|
|
|
|
|
private async cleanViewerCounters () {
|
|
|
|
if (this.processingViewerCounters) return
|
|
|
|
this.processingViewerCounters = true
|
|
|
|
|
2022-07-06 08:44:14 -05:00
|
|
|
if (!isTestOrDevInstance()) logger.info('Cleaning video viewers.', lTags())
|
2022-04-06 01:50:43 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
for (const videoId of this.viewersPerVideo.keys()) {
|
|
|
|
const notBefore = new Date().getTime()
|
|
|
|
|
|
|
|
const viewers = this.viewersPerVideo.get(videoId)
|
|
|
|
|
|
|
|
// Only keep not expired viewers
|
|
|
|
const newViewers: Viewer[] = []
|
|
|
|
|
|
|
|
// Filter new viewers
|
|
|
|
for (const viewer of viewers) {
|
|
|
|
if (viewer.expires > notBefore) {
|
|
|
|
newViewers.push(viewer)
|
|
|
|
} else {
|
|
|
|
this.idToViewer.delete(viewer.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newViewers.length === 0) this.viewersPerVideo.delete(videoId)
|
|
|
|
else this.viewersPerVideo.set(videoId, newViewers)
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
const video = await VideoModel.loadImmutableAttributes(videoId)
|
|
|
|
|
|
|
|
if (video) {
|
|
|
|
this.notifyClients(video)
|
|
|
|
|
|
|
|
// Let total viewers expire on remote instances if there are no more viewers
|
|
|
|
if (video.remote === false && newViewers.length !== 0) {
|
|
|
|
await this.federateTotalViewers(video)
|
|
|
|
}
|
|
|
|
}
|
2022-04-06 01:50:43 -05:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Error in video clean viewers scheduler.', { err, ...lTags() })
|
|
|
|
}
|
|
|
|
|
|
|
|
this.processingViewerCounters = false
|
|
|
|
}
|
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
private notifyClients (video: MVideoImmutable) {
|
|
|
|
const totalViewers = this.getTotalViewersOf(video)
|
|
|
|
PeerTubeSocket.Instance.sendVideoViewsUpdate(video, totalViewers)
|
2022-04-06 01:50:43 -05:00
|
|
|
|
2023-12-01 07:53:17 -06:00
|
|
|
logger.debug('Video viewers update for %s is %d.', video.url, totalViewers, lTags())
|
2022-04-06 01:50:43 -05:00
|
|
|
}
|
|
|
|
|
2022-06-17 07:34:37 -05:00
|
|
|
private async federateViewerIfNeeded (video: MVideoImmutable, viewer: Viewer) {
|
2022-04-06 01:50:43 -05:00
|
|
|
// Federate the viewer if it's been a "long" time we did not
|
|
|
|
const now = new Date().getTime()
|
2022-06-20 03:04:52 -05:00
|
|
|
const federationLimit = now - (VIEW_LIFETIME.VIEWER_COUNTER * 0.75)
|
2022-04-06 01:50:43 -05:00
|
|
|
|
|
|
|
if (viewer.lastFederation && viewer.lastFederation > federationLimit) return
|
2023-12-01 07:53:17 -06:00
|
|
|
if (video.remote === false && isUsingViewersFederationV2()) return
|
|
|
|
|
|
|
|
await sendView({
|
|
|
|
byActor: await getServerActor(),
|
|
|
|
video,
|
|
|
|
viewersCount: 1,
|
|
|
|
viewerIdentifier: viewer.id
|
|
|
|
})
|
2022-04-06 01:50:43 -05:00
|
|
|
|
|
|
|
viewer.lastFederation = now
|
|
|
|
}
|
2023-12-01 07:53:17 -06:00
|
|
|
|
|
|
|
private async federateTotalViewers (video: MVideoImmutable) {
|
|
|
|
if (!isUsingViewersFederationV2()) return
|
|
|
|
|
|
|
|
await sendView({
|
|
|
|
byActor: await getServerActor(),
|
|
|
|
video,
|
|
|
|
viewersCount: this.getTotalViewersOf(video),
|
|
|
|
viewerIdentifier: video.uuid
|
|
|
|
})
|
|
|
|
}
|
2022-04-06 01:50:43 -05:00
|
|
|
}
|