]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/views/shared/video-viewer-stats.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / views / shared / video-viewer-stats.ts
index a9ba25b475b221c4b564890dc1af05212f1a1476..ebd963e598f57e90a4939f672d715a15d2693d54 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,17 @@ export class VideoViewerStats {
       })
     } else {
       const lastSection = stats.watchSections[stats.watchSections.length - 1]
-      lastSection.end = currentTime
+
+      if (lastSection.start > currentTime) {
+        logger.debug('Invalid end watch section %d. Last start record was at %d. Starting a new section.', currentTime, lastSection.start)
+
+        stats.watchSections.push({
+          start: currentTime,
+          end: currentTime
+        })
+      } else {
+        lastSection.end = currentTime
+      }
     }
 
     stats.watchTime = this.buildWatchTimeFromSections(stats.watchSections)
@@ -118,7 +128,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()