]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/video-views.ts
Stricter models typing
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-views.ts
index 038ef43e2c7ea624aea5841c93e44567c44f39c1..86d0a271f12a52d33111c56b6630ba986b9f8b22 100644 (file)
@@ -1,9 +1,9 @@
 import { Redis } from '../../redis'
 import { logger } from '../../../helpers/logger'
 import { VideoModel } from '../../../models/video/video'
-import { VideoViewModel } from '../../../models/video/video-views'
+import { VideoViewModel } from '../../../models/video/video-view'
 import { isTestInstance } from '../../../helpers/core-utils'
-import { federateVideoIfNeeded } from '../../activitypub'
+import { federateVideoIfNeeded } from '../../activitypub/videos'
 
 async function processVideosViews () {
   const lastHour = new Date()
@@ -23,20 +23,25 @@ async function processVideosViews () {
   for (const videoId of videoIds) {
     try {
       const views = await Redis.Instance.getVideoViews(videoId, hour)
-      if (isNaN(views)) {
-        logger.error('Cannot process videos views of video %d in hour %d: views number is NaN (%s).', videoId, hour, views)
-      } else {
+      await Redis.Instance.deleteVideoViews(videoId, hour)
+
+      if (views) {
         logger.debug('Adding %d views to video %d in hour %d.', views, videoId, hour)
 
         try {
+          const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
+          if (!video) {
+            logger.debug('Video %d does not exist anymore, skipping videos view addition.', videoId)
+            continue
+          }
+
           await VideoViewModel.create({
-            startDate,
-            endDate,
+            startDate: new Date(startDate),
+            endDate: new Date(endDate),
             views,
             videoId
           })
 
-          const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
           if (video.isOwned()) {
             // If this is a remote video, the origin instance will send us an update
             await VideoModel.incrementViews(videoId, views)
@@ -46,13 +51,11 @@ async function processVideosViews () {
             await federateVideoIfNeeded(video, false)
           }
         } catch (err) {
-          logger.debug('Cannot create video views for video %d in hour %d. Maybe the video does not exist anymore?', videoId, hour)
+          logger.error('Cannot create video views for video %d in hour %d.', videoId, hour, { err })
         }
       }
-
-      await Redis.Instance.deleteVideoViews(videoId, hour)
     } catch (err) {
-      logger.error('Cannot update video views of video %d in hour %d.', videoId, hour)
+      logger.error('Cannot update video views of video %d in hour %d.', videoId, hour, { err })
     }
   }
 }