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'
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')
// ---------------------------------------------------------------------------
async addLocalViewer (options: {
- video: MVideo
+ video: MVideoImmutable
currentTime: number
ip: string
viewEvent?: VideoViewEvent
// ---------------------------------------------------------------------------
private async updateLocalViewerStats (options: {
- video: MVideo
+ video: MVideoImmutable
ip: string
currentTime: number
viewEvent?: VideoViewEvent
})
} 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)
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()
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)