1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { FfmpegCommand } from 'fluent-ffmpeg'
6 import { prepareViewsServers, prepareViewsVideos, processViewersStats } from '@server/tests/shared'
7 import { cleanupTests, PeerTubeServer, stopFfmpeg, waitJobs } from '@shared/server-commands'
9 const expect = chai.expect
11 describe('Test views overall stats', function () {
12 let servers: PeerTubeServer[]
14 before(async function () {
17 servers = await prepareViewsServers()
20 describe('Test watch time stats of local videos on live and VOD', function () {
21 let vodVideoId: string
22 let liveVideoId: string
23 let command: FfmpegCommand
25 before(async function () {
28 ({ vodVideoId, liveVideoId, ffmpegCommand: command } = await prepareViewsVideos({ servers, live: true, vod: true }))
31 it('Should display overall stats of a video with no viewers', async function () {
32 for (const videoId of [ liveVideoId, vodVideoId ]) {
33 const stats = await servers[0].videoStats.getOverallStats({ videoId })
34 const video = await servers[0].videos.get({ id: videoId })
36 expect(video.views).to.equal(0)
37 expect(stats.averageWatchTime).to.equal(0)
38 expect(stats.totalWatchTime).to.equal(0)
39 expect(stats.totalViewers).to.equal(0)
43 it('Should display overall stats with 1 viewer below the watch time limit', async function () {
46 for (const videoId of [ liveVideoId, vodVideoId ]) {
47 await servers[0].views.simulateViewer({ id: videoId, currentTimes: [ 0, 1 ] })
50 await processViewersStats(servers)
52 for (const videoId of [ liveVideoId, vodVideoId ]) {
53 const stats = await servers[0].videoStats.getOverallStats({ videoId })
54 const video = await servers[0].videos.get({ id: videoId })
56 expect(video.views).to.equal(0)
57 expect(stats.averageWatchTime).to.equal(1)
58 expect(stats.totalWatchTime).to.equal(1)
59 expect(stats.totalViewers).to.equal(1)
63 it('Should display overall stats with 2 viewers', async function () {
67 await servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 3 ] })
68 await servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 35, 40 ] })
70 await processViewersStats(servers)
73 const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId })
74 const video = await servers[0].videos.get({ id: vodVideoId })
76 expect(video.views).to.equal(1)
77 expect(stats.averageWatchTime).to.equal(2)
78 expect(stats.totalWatchTime).to.equal(4)
79 expect(stats.totalViewers).to.equal(2)
83 const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId })
84 const video = await servers[0].videos.get({ id: liveVideoId })
86 expect(video.views).to.equal(1)
87 expect(stats.averageWatchTime).to.equal(21)
88 expect(stats.totalWatchTime).to.equal(41)
89 expect(stats.totalViewers).to.equal(2)
94 it('Should display overall stats with a remote viewer below the watch time limit', async function () {
97 for (const videoId of [ liveVideoId, vodVideoId ]) {
98 await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 2 ] })
101 await processViewersStats(servers)
104 const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId })
105 const video = await servers[0].videos.get({ id: vodVideoId })
107 expect(video.views).to.equal(1)
108 expect(stats.averageWatchTime).to.equal(2)
109 expect(stats.totalWatchTime).to.equal(6)
110 expect(stats.totalViewers).to.equal(3)
114 const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId })
115 const video = await servers[0].videos.get({ id: liveVideoId })
117 expect(video.views).to.equal(1)
118 expect(stats.averageWatchTime).to.equal(14)
119 expect(stats.totalWatchTime).to.equal(43)
120 expect(stats.totalViewers).to.equal(3)
124 it('Should display overall stats with a remote viewer above the watch time limit', async function () {
127 await servers[1].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 5 ] })
128 await servers[1].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 45 ] })
129 await processViewersStats(servers)
132 const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId })
133 const video = await servers[0].videos.get({ id: vodVideoId })
135 expect(video.views).to.equal(2)
136 expect(stats.averageWatchTime).to.equal(3)
137 expect(stats.totalWatchTime).to.equal(11)
138 expect(stats.totalViewers).to.equal(4)
142 const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId })
143 const video = await servers[0].videos.get({ id: liveVideoId })
145 expect(video.views).to.equal(2)
146 expect(stats.averageWatchTime).to.equal(22)
147 expect(stats.totalWatchTime).to.equal(88)
148 expect(stats.totalViewers).to.equal(4)
152 it('Should filter overall stats by date', async function () {
155 const beforeView = new Date()
157 await servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 3 ] })
158 await processViewersStats(servers)
161 const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId, startDate: beforeView.toISOString() })
162 expect(stats.averageWatchTime).to.equal(3)
163 expect(stats.totalWatchTime).to.equal(3)
164 expect(stats.totalViewers).to.equal(1)
168 const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId, endDate: beforeView.toISOString() })
169 expect(stats.averageWatchTime).to.equal(22)
170 expect(stats.totalWatchTime).to.equal(88)
171 expect(stats.totalViewers).to.equal(4)
175 after(async function () {
176 await stopFfmpeg(command)
180 describe('Test watchers peak stats of local videos on VOD', function () {
181 let videoUUID: string
182 let before2Watchers: Date
184 before(async function () {
185 this.timeout(120000);
187 ({ vodVideoId: videoUUID } = await prepareViewsVideos({ servers, live: true, vod: true }))
190 it('Should not have watchers peak', async function () {
191 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID })
193 expect(stats.viewersPeak).to.equal(0)
194 expect(stats.viewersPeakDate).to.be.null
197 it('Should have watcher peak with 1 watcher', async function () {
200 const before = new Date()
201 await servers[0].views.simulateViewer({ id: videoUUID, currentTimes: [ 0, 2 ] })
202 const after = new Date()
204 await processViewersStats(servers)
206 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID })
208 expect(stats.viewersPeak).to.equal(1)
209 expect(new Date(stats.viewersPeakDate)).to.be.above(before).and.below(after)
212 it('Should have watcher peak with 2 watchers', async function () {
215 before2Watchers = new Date()
216 await servers[0].views.view({ id: videoUUID, currentTime: 0 })
217 await servers[1].views.view({ id: videoUUID, currentTime: 0 })
218 await servers[0].views.view({ id: videoUUID, currentTime: 2 })
219 await servers[1].views.view({ id: videoUUID, currentTime: 2 })
220 const after = new Date()
222 await processViewersStats(servers)
224 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID })
226 expect(stats.viewersPeak).to.equal(2)
227 expect(new Date(stats.viewersPeakDate)).to.be.above(before2Watchers).and.below(after)
230 it('Should filter peak viewers stats by date', async function () {
232 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() })
233 expect(stats.viewersPeak).to.equal(0)
234 expect(stats.viewersPeakDate).to.not.exist
238 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, endDate: before2Watchers.toISOString() })
239 expect(stats.viewersPeak).to.equal(1)
240 expect(new Date(stats.viewersPeakDate)).to.be.below(before2Watchers)
245 describe('Test countries', function () {
246 let videoUUID: string
248 it('Should not report countries if geoip is disabled', async function () {
251 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
252 await waitJobs(servers)
254 await servers[1].views.view({ id: uuid, xForwardedFor: '8.8.8.8,127.0.0.1', currentTime: 1 })
256 await processViewersStats(servers)
258 const stats = await servers[0].videoStats.getOverallStats({ videoId: uuid })
259 expect(stats.countries).to.have.lengthOf(0)
262 it('Should report countries if geoip is enabled', async function () {
265 const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
267 await waitJobs(servers)
274 const config = { geo_ip: { enabled: true } }
276 servers[0].run(config),
277 servers[1].run(config)
280 await servers[0].views.view({ id: uuid, xForwardedFor: '8.8.8.8,127.0.0.1', currentTime: 1 })
281 await servers[1].views.view({ id: uuid, xForwardedFor: '8.8.8.4,127.0.0.1', currentTime: 3 })
282 await servers[1].views.view({ id: uuid, xForwardedFor: '80.67.169.12,127.0.0.1', currentTime: 2 })
284 await processViewersStats(servers)
286 const stats = await servers[0].videoStats.getOverallStats({ videoId: uuid })
287 expect(stats.countries).to.have.lengthOf(2)
289 expect(stats.countries[0].isoCode).to.equal('US')
290 expect(stats.countries[0].viewers).to.equal(2)
292 expect(stats.countries[1].isoCode).to.equal('FR')
293 expect(stats.countries[1].viewers).to.equal(1)
296 it('Should filter countries stats by date', async function () {
297 const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() })
298 expect(stats.countries).to.have.lengthOf(0)
302 after(async function () {
303 await cleanupTests(servers)