1 import { Transaction } from 'sequelize/types'
2 import { isTestInstance } from '@server/helpers/core-utils'
3 import { GeoIP } from '@server/helpers/geo-ip'
4 import { logger, loggerTagsFactory } from '@server/helpers/logger'
5 import { MAX_LOCAL_VIEWER_WATCH_SECTIONS, VIEW_LIFETIME } from '@server/initializers/constants'
6 import { sequelizeTypescript } from '@server/initializers/database'
7 import { sendCreateWatchAction } from '@server/lib/activitypub/send'
8 import { getLocalVideoViewerActivityPubUrl } from '@server/lib/activitypub/url'
9 import { Redis } from '@server/lib/redis'
10 import { VideoModel } from '@server/models/video/video'
11 import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
12 import { LocalVideoViewerWatchSectionModel } from '@server/models/view/local-video-viewer-watch-section'
13 import { MVideo, MVideoImmutable } from '@server/types/models'
14 import { VideoViewEvent } from '@shared/models'
16 const lTags = loggerTagsFactory('views')
18 type LocalViewerStats = {
19 firstUpdated: number // Date.getTime()
20 lastUpdated: number // Date.getTime()
34 export class VideoViewerStats {
35 private processingViewersStats = false
38 setInterval(() => this.processViewerStats(), VIEW_LIFETIME.VIEWER_STATS)
41 // ---------------------------------------------------------------------------
43 async addLocalViewer (options: {
44 video: MVideoImmutable
47 viewEvent?: VideoViewEvent
49 const { video, ip, viewEvent, currentTime } = options
51 logger.debug('Adding local viewer to video stats %s.', video.uuid, { currentTime, viewEvent, ...lTags(video.uuid) })
53 return this.updateLocalViewerStats({ video, viewEvent, currentTime, ip })
56 // ---------------------------------------------------------------------------
58 async getWatchTime (videoId: number, ip: string) {
59 const stats: LocalViewerStats = await Redis.Instance.getLocalVideoViewer({ ip, videoId })
61 return stats?.watchTime || 0
64 // ---------------------------------------------------------------------------
66 private async updateLocalViewerStats (options: {
67 video: MVideoImmutable
70 viewEvent?: VideoViewEvent
72 const { video, ip, viewEvent, currentTime } = options
73 const nowMs = new Date().getTime()
75 let stats: LocalViewerStats = await Redis.Instance.getLocalVideoViewer({ ip, videoId: video.id })
77 if (stats && stats.watchSections.length >= MAX_LOCAL_VIEWER_WATCH_SECTIONS) {
78 logger.warn('Too much watch section to store for a viewer, skipping this one', { currentTime, viewEvent, ...lTags(video.uuid) })
83 const country = await GeoIP.Instance.safeCountryISOLookup(ip)
98 stats.lastUpdated = nowMs
100 if (viewEvent === 'seek' || stats.watchSections.length === 0) {
101 stats.watchSections.push({
106 const lastSection = stats.watchSections[stats.watchSections.length - 1]
107 lastSection.end = currentTime
110 stats.watchTime = this.buildWatchTimeFromSections(stats.watchSections)
112 logger.debug('Set local video viewer stats for video %s.', video.uuid, { stats, ...lTags(video.uuid) })
114 await Redis.Instance.setLocalVideoViewer(ip, video.id, stats)
117 async processViewerStats () {
118 if (this.processingViewersStats) return
119 this.processingViewersStats = true
121 if (!isTestInstance()) logger.info('Processing viewer statistics.', lTags())
123 const now = new Date().getTime()
126 const allKeys = await Redis.Instance.listLocalVideoViewerKeys()
128 for (const key of allKeys) {
129 const stats: LocalViewerStats = await Redis.Instance.getLocalVideoViewer({ key })
131 // Process expired stats
132 if (stats.lastUpdated > now - VIEW_LIFETIME.VIEWER_STATS) {
137 await sequelizeTypescript.transaction(async t => {
138 const video = await VideoModel.load(stats.videoId, t)
141 const statsModel = await this.saveViewerStats(video, stats, t)
144 await sendCreateWatchAction(statsModel, t)
148 await Redis.Instance.deleteLocalVideoViewersKeys(key)
150 logger.error('Cannot process viewer stats for Redis key %s.', key, { err, ...lTags() })
154 logger.error('Error in video save viewers stats scheduler.', { err, ...lTags() })
157 this.processingViewersStats = false
160 private async saveViewerStats (video: MVideo, stats: LocalViewerStats, transaction: Transaction) {
161 const statsModel = new LocalVideoViewerModel({
162 startDate: new Date(stats.firstUpdated),
163 endDate: new Date(stats.lastUpdated),
164 watchTime: stats.watchTime,
165 country: stats.country,
169 statsModel.url = getLocalVideoViewerActivityPubUrl(statsModel)
170 statsModel.Video = video as VideoModel
172 await statsModel.save({ transaction })
174 statsModel.WatchSections = await LocalVideoViewerWatchSectionModel.bulkCreateSections({
175 localVideoViewerId: statsModel.id,
176 watchSections: stats.watchSections,
183 private buildWatchTimeFromSections (sections: { start: number, end: number }[]) {
184 return sections.reduce((p, current) => p + (current.end - current.start), 0)