]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/views/shared/video-viewer-stats.ts
Relax log level
[github/Chocobozzz/PeerTube.git] / server / lib / views / shared / video-viewer-stats.ts
index fd66fd5c7eee7b82a280775c3e12c01ed99889ff..39e34fb9095d9c303242beb93e87c3dcb3bc0b28 100644 (file)
@@ -1,5 +1,5 @@
 import { Transaction } from 'sequelize/types'
-import { isTestInstance } from '@server/helpers/core-utils'
+import { isTestOrDevInstance } from '@server/helpers/core-utils'
 import { GeoIP } from '@server/helpers/geo-ip'
 import { logger, loggerTagsFactory } from '@server/helpers/logger'
 import { MAX_LOCAL_VIEWER_WATCH_SECTIONS, VIEW_LIFETIME } from '@server/initializers/constants'
@@ -10,7 +10,7 @@ import { Redis } from '@server/lib/redis'
 import { VideoModel } from '@server/models/video/video'
 import { LocalVideoViewerModel } from '@server/models/view/local-video-viewer'
 import { LocalVideoViewerWatchSectionModel } from '@server/models/view/local-video-viewer-watch-section'
-import { MVideo } from '@server/types/models'
+import { MVideo, MVideoImmutable } from '@server/types/models'
 import { VideoViewEvent } from '@shared/models'
 
 const lTags = loggerTagsFactory('views')
@@ -41,7 +41,7 @@ export class VideoViewerStats {
   // ---------------------------------------------------------------------------
 
   async addLocalViewer (options: {
-    video: MVideo
+    video: MVideoImmutable
     currentTime: number
     ip: string
     viewEvent?: VideoViewEvent
@@ -64,7 +64,7 @@ export class VideoViewerStats {
   // ---------------------------------------------------------------------------
 
   private async updateLocalViewerStats (options: {
-    video: MVideo
+    video: MVideoImmutable
     ip: string
     currentTime: number
     viewEvent?: VideoViewEvent
@@ -104,7 +104,12 @@ export class VideoViewerStats {
       })
     } else {
       const lastSection = stats.watchSections[stats.watchSections.length - 1]
-      lastSection.end = currentTime
+
+      if (lastSection.start > currentTime) {
+        logger.warn('Invalid end watch section %d. Last start record was at %d.', currentTime, lastSection.start)
+      } else {
+        lastSection.end = currentTime
+      }
     }
 
     stats.watchTime = this.buildWatchTimeFromSections(stats.watchSections)
@@ -118,7 +123,7 @@ export class VideoViewerStats {
     if (this.processingViewersStats) return
     this.processingViewersStats = true
 
-    if (!isTestInstance()) logger.info('Processing viewer statistics.', lTags())
+    if (!isTestOrDevInstance()) logger.info('Processing viewer statistics.', lTags())
 
     const now = new Date().getTime()
 
@@ -136,6 +141,7 @@ export class VideoViewerStats {
         try {
           await sequelizeTypescript.transaction(async t => {
             const video = await VideoModel.load(stats.videoId, t)
+            if (!video) return
 
             const statsModel = await this.saveViewerStats(video, stats, t)