1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { FfmpegCommand } from 'fluent-ffmpeg'
5 import { prepareViewsServers, prepareViewsVideos, processViewersStats } from '@server/tests/shared'
6 import { cleanupTests, PeerTubeServer, stopFfmpeg, waitJobs } from '@shared/server-commands'
7 import { wait } from '@shared/core-utils'
8 import { VideoStatsOverall } from '@shared/models'
12 * Simulate 5 sections of viewers
13 * * user0 started and ended before start date
14 * * user1 started before start date and ended in the interval
15 * * user2 started started in the interval and ended after end date
16 * * user3 started and ended in the interval
17 * * user4 started and ended after end date
19 async function simulateComplexViewers (servers: PeerTubeServer[], videoUUID: string) {
20 const user0 = '8.8.8.8,127.0.0.1'
21 const user1 = '8.8.8.8,127.0.0.1'
22 const user2 = '8.8.8.9,127.0.0.1'
23 const user3 = '8.8.8.10,127.0.0.1'
24 const user4 = '8.8.8.11,127.0.0.1'
26 await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user0 }) // User 0 starts
29 await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user1 }) // User 1 starts
30 await servers[0].views.view({ id: videoUUID, currentTime: 2, xForwardedFor: user0 }) // User 0 ends
33 const startDate = new Date().toISOString()
34 await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user2 }) // User 2 starts
37 await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user3 }) // User 3 starts
40 await servers[0].views.view({ id: videoUUID, currentTime: 4, xForwardedFor: user1 }) // User 1 ends
43 await servers[0].views.view({ id: videoUUID, currentTime: 3, xForwardedFor: user3 }) // User 3 ends
46 const endDate = new Date().toISOString()
47 await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user4 }) // User 4 starts
48 await servers[0].views.view({ id: videoUUID, currentTime: 5, xForwardedFor: user2 }) // User 2 ends
51 await servers[0].views.view({ id: videoUUID, currentTime: 1, xForwardedFor: user4 }) // User 4 ends
53 await processViewersStats(servers)
55 return { startDate, endDate }
58 describe('Test views overall stats', function () {
59 let servers: PeerTubeServer[]
61 before(async function () {
64 servers = await prepareViewsServers()
67 describe('Test watch time stats of local videos on live and VOD', function () {
68 let vodVideoId: string
69 let liveVideoId: string
70 let command: FfmpegCommand
72 before(async function () {
75 ({ vodVideoId, liveVideoId, ffmpegCommand: command } = await prepareViewsVideos({ servers, live: true, vod: true }))
78 it('Should display overall stats of a video with no viewers', async function () {
79 for (const videoId of [ liveVideoId, vodVideoId ]) {
80 const stats = await servers[0].videoStats.getOverallStats({ videoId })
81 const video = await servers[0].videos.get({ id: videoId })
83 expect(video.views).to.equal(0)
84 expect(stats.averageWatchTime).to.equal(0)
85 expect(stats.totalWatchTime).to.equal(0)
86 expect(stats.totalViewers).to.equal(0)
90 it('Should display overall stats with 1 viewer below the watch time limit', async function () {
93 for (const videoId of [ liveVideoId, vodVideoId ]) {
94 await servers[0].views.simulateViewer({ id: videoId, currentTimes: [ 0, 1 ] })
97 await processViewersStats(servers)
99 for (const videoId of [ liveVideoId, vodVideoId ]) {
100 const stats = await servers[0].videoStats.getOverallStats({ videoId })
101 const video = await servers[0].videos.get({ id: videoId })
103 expect(video.views).to.equal(0)
104 expect(stats.averageWatchTime).to.equal(1)
105 expect(stats.totalWatchTime).to.equal(1)
106 expect(stats.totalViewers).to.equal(1)
110 it('Should display overall stats with 2 viewers', async function () {
114 await servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 3 ] })
115 await servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 35, 40 ] })
117 await processViewersStats(servers)
120 const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId })
121 const video = await servers[0].videos.get({ id: vodVideoId })
123 expect(video.views).to.equal(1)
124 expect(stats.averageWatchTime).to.equal(2)
125 expect(stats.totalWatchTime).to.equal(4)
126 expect(stats.totalViewers).to.equal(2)
130 const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId })
131 const video = await servers[0].videos.get({ id: liveVideoId })
133 expect(video.views).to.equal(1)
134 expect(stats.averageWatchTime).to.equal(21)
135 expect(stats.totalWatchTime).to.equal(41)
136 expect(stats.totalViewers).to.equal(2)
141 it('Should display overall stats with a remote viewer below the watch time limit', async function () {
144 for (const videoId of [ liveVideoId, vodVideoId ]) {
145 await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 2 ] })
148 await processViewersStats(servers)
151 const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId })
152 const video = await servers[0].videos.get({ id: vodVideoId })
154 expect(video.views).to.equal(1)
155 expect(stats.averageWatchTime).to.equal(2)
156 expect(stats.totalWatchTime).to.equal(6)
157 expect(stats.totalViewers).to.equal(3)
161 const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId })
162 const video = await servers[0].videos.get({ id: liveVideoId })
164 expect(video.views).to.equal(1)
165 expect(stats.averageWatchTime).to.equal(14)
166 expect(stats.totalWatchTime).to.equal(43)
167 expect(stats.totalViewers).to.equal(3)
171 it('Should display overall stats with a remote viewer above the watch time limit', async function () {
174 await servers[1].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 5 ] })
175 await servers[1].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 45 ] })
176 await processViewersStats(servers)
179 const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId })
180 const video = await servers[0].videos.get({ id: vodVideoId })
182 expect(video.views).to.equal(2)
183 expect(stats.averageWatchTime).to.equal(3)
184 expect(stats.totalWatchTime).to.equal(11)
185 expect(stats.totalViewers).to.equal(4)
189 const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId })
190 const video = await servers[0].videos.get({ id: liveVideoId })
192 expect(video.views).to.equal(2)
193 expect(stats.averageWatchTime).to.equal(22)
194 expect(stats.totalWatchTime).to.equal(88)
195 expect(stats.totalViewers).to.equal(4)
199 it('Should filter overall stats by date', async function () {
202 const beforeView = new Date()
204 await servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 3 ] })
205 await processViewersStats(servers)
208 const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId, startDate: beforeView.toISOString() })
209 expect(stats.averageWatchTime).to.equal(3)
210 expect(stats.totalWatchTime).to.equal(3)
211 expect(stats.totalViewers).to.equal(1)
215 const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId, endDate: beforeView.toISOString() })
216 expect(stats.averageWatchTime).to.equal(22)
217 expect(stats.totalWatchTime).to.equal(88)
218 expect(stats.totalViewers).to.equal(4)
222 after(async function () {
223 await stopFfmpeg(command)
227 describe('Test watchers peak stats of local videos on VOD', function () {
228 let videoUUID: string
229 let before2Watchers: Date
231 before(async function () {
232 this.timeout(240000);
234 ({ vodVideoId: videoUUID } = await prepareViewsVideos({ servers, live: true, vod: true }))
237 it('Should not have watchers peak', async function () {
238 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID })
240 expect(stats.viewersPeak).to.equal(0)
241 expect(stats.viewersPeakDate).to.be.null
244 it('Should have watcher peak with 1 watcher', async function () {
247 const before = new Date()
248 await servers[0].views.simulateViewer({ id: videoUUID, currentTimes: [ 0, 2 ] })
249 const after = new Date()
251 await processViewersStats(servers)
253 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID })
255 expect(stats.viewersPeak).to.equal(1)
256 expect(new Date(stats.viewersPeakDate)).to.be.above(before).and.below(after)
259 it('Should have watcher peak with 2 watchers', async function () {
262 before2Watchers = new Date()
263 await servers[0].views.view({ id: videoUUID, currentTime: 0 })
264 await servers[1].views.view({ id: videoUUID, currentTime: 0 })
265 await servers[0].views.view({ id: videoUUID, currentTime: 2 })
266 await servers[1].views.view({ id: videoUUID, currentTime: 2 })
267 const after = new Date()
269 await processViewersStats(servers)
271 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID })
273 expect(stats.viewersPeak).to.equal(2)
274 expect(new Date(stats.viewersPeakDate)).to.be.above(before2Watchers).and.below(after)
277 it('Should filter peak viewers stats by date', async function () {
279 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() })
280 expect(stats.viewersPeak).to.equal(0)
281 expect(stats.viewersPeakDate).to.not.exist
285 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, endDate: before2Watchers.toISOString() })
286 expect(stats.viewersPeak).to.equal(1)
287 expect(new Date(stats.viewersPeakDate)).to.be.below(before2Watchers)
291 it('Should complex filter peak viewers by date', async function () {
294 const { startDate, endDate } = await simulateComplexViewers(servers, videoUUID)
296 const expectCorrect = (stats: VideoStatsOverall) => {
297 expect(stats.viewersPeak).to.equal(3)
298 expect(new Date(stats.viewersPeakDate)).to.be.above(new Date(startDate)).and.below(new Date(endDate))
301 expectCorrect(await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate, endDate }))
302 expectCorrect(await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate }))
303 expectCorrect(await servers[0].videoStats.getOverallStats({ videoId: videoUUID, endDate }))
304 expectCorrect(await servers[0].videoStats.getOverallStats({ videoId: videoUUID }))
308 describe('Test countries', function () {
309 let videoUUID: string
311 it('Should not report countries if geoip is disabled', async function () {
314 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
315 await waitJobs(servers)
317 await servers[1].views.view({ id: uuid, xForwardedFor: '8.8.8.8,127.0.0.1', currentTime: 1 })
319 await processViewersStats(servers)
321 const stats = await servers[0].videoStats.getOverallStats({ videoId: uuid })
322 expect(stats.countries).to.have.lengthOf(0)
325 it('Should report countries if geoip is enabled', async function () {
328 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
330 await waitJobs(servers)
337 const config = { geo_ip: { enabled: true } }
339 servers[0].run(config),
340 servers[1].run(config)
343 await servers[0].views.view({ id: uuid, xForwardedFor: '8.8.8.8,127.0.0.1', currentTime: 1 })
344 await servers[1].views.view({ id: uuid, xForwardedFor: '8.8.8.4,127.0.0.1', currentTime: 3 })
345 await servers[1].views.view({ id: uuid, xForwardedFor: '80.67.169.12,127.0.0.1', currentTime: 2 })
347 await processViewersStats(servers)
349 const stats = await servers[0].videoStats.getOverallStats({ videoId: uuid })
350 expect(stats.countries).to.have.lengthOf(2)
352 expect(stats.countries[0].isoCode).to.equal('US')
353 expect(stats.countries[0].viewers).to.equal(2)
355 expect(stats.countries[1].isoCode).to.equal('FR')
356 expect(stats.countries[1].viewers).to.equal(1)
359 it('Should filter countries stats by date', async function () {
360 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() })
361 expect(stats.countries).to.have.lengthOf(0)
365 after(async function () {
366 await cleanupTests(servers)