2022-03-24 13:36:47 +01:00
|
|
|
import { VideoViewModel } from '@server/models/view/video-view'
|
2022-07-06 15:44:14 +02:00
|
|
|
import { isTestOrDevInstance } from '../../../helpers/core-utils'
|
2018-08-29 16:26:25 +02:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { VideoModel } from '../../../models/video/video'
|
2021-11-09 10:11:20 +01:00
|
|
|
import { Redis } from '../../redis'
|
2018-08-29 16:26:25 +02:00
|
|
|
|
2021-11-09 10:11:20 +01:00
|
|
|
async function processVideosViewsStats () {
|
2018-09-13 13:59:41 +02:00
|
|
|
const lastHour = new Date()
|
|
|
|
|
|
|
|
// In test mode, we run this function multiple times per hour, so we don't want the values of the previous hour
|
2022-07-06 15:44:14 +02:00
|
|
|
if (!isTestOrDevInstance()) lastHour.setHours(lastHour.getHours() - 1)
|
2018-09-13 13:59:41 +02:00
|
|
|
|
|
|
|
const hour = lastHour.getHours()
|
|
|
|
const startDate = lastHour.setMinutes(0, 0, 0)
|
|
|
|
const endDate = lastHour.setMinutes(59, 59, 999)
|
2018-08-29 16:26:25 +02:00
|
|
|
|
2021-11-09 10:11:20 +01:00
|
|
|
const videoIds = await Redis.Instance.listVideosViewedForStats(hour)
|
2018-08-29 16:26:25 +02:00
|
|
|
if (videoIds.length === 0) return
|
|
|
|
|
2021-11-09 10:11:20 +01:00
|
|
|
logger.info('Processing videos views stats in job for hour %d.', hour)
|
2018-08-29 16:26:25 +02:00
|
|
|
|
|
|
|
for (const videoId of videoIds) {
|
2018-09-13 10:13:25 +02:00
|
|
|
try {
|
2021-11-09 10:11:20 +01:00
|
|
|
const views = await Redis.Instance.getVideoViewsStats(videoId, hour)
|
|
|
|
await Redis.Instance.deleteVideoViewsStats(videoId, hour)
|
2020-01-30 11:53:38 +01:00
|
|
|
|
2018-12-04 16:02:49 +01:00
|
|
|
if (views) {
|
2021-11-09 10:11:20 +01:00
|
|
|
logger.debug('Adding %d views to video %d stats in hour %d.', views, videoId, hour)
|
2018-09-13 10:13:25 +02:00
|
|
|
|
|
|
|
try {
|
2021-11-09 10:11:20 +01:00
|
|
|
const video = await VideoModel.load(videoId)
|
2019-05-21 13:14:27 +02:00
|
|
|
if (!video) {
|
2021-11-09 10:11:20 +01:00
|
|
|
logger.debug('Video %d does not exist anymore, skipping videos view stats.', videoId)
|
2019-05-21 13:14:27 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-13 10:13:25 +02:00
|
|
|
await VideoViewModel.create({
|
2021-05-12 14:09:04 +02:00
|
|
|
startDate: new Date(startDate),
|
|
|
|
endDate: new Date(endDate),
|
2018-09-13 10:13:25 +02:00
|
|
|
views,
|
|
|
|
videoId
|
|
|
|
})
|
|
|
|
} catch (err) {
|
2021-11-09 10:11:20 +01:00
|
|
|
logger.error('Cannot create video views stats for video %d in hour %d.', videoId, hour, { err })
|
2018-09-13 10:13:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-11-09 10:11:20 +01:00
|
|
|
logger.error('Cannot update video views stats of video %d in hour %d.', videoId, hour, { err })
|
2018-08-29 16:26:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2021-11-09 10:11:20 +01:00
|
|
|
processVideosViewsStats
|
2018-08-29 16:26:25 +02:00
|
|
|
}
|