aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/views/video-views-overall-stats.ts
blob: ac636961ea64ef342e765163a178eb20562837f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import { FfmpegCommand } from 'fluent-ffmpeg'
import { prepareViewsServers, prepareViewsVideos, processViewersStats } from '@server/tests/shared'
import { cleanupTests, PeerTubeServer, stopFfmpeg, waitJobs } from '@shared/server-commands'
import { wait } from '@shared/core-utils'
import { VideoStatsOverall } from '@shared/models'

/**
 *
 * Simulate 5 sections of viewers
 *  * user0 started and ended before start date
 *  * user1 started before start date and ended in the interval
 *  * user2 started started in the interval and ended after end date
 *  * user3 started and ended in the interval
 *  * user4 started and ended after end date
 */
async function simulateComplexViewers (servers: PeerTubeServer[], videoUUID: string) {
  const user0 = '8.8.8.8,127.0.0.1'
  const user1 = '8.8.8.8,127.0.0.1'
  const user2 = '8.8.8.9,127.0.0.1'
  const user3 = '8.8.8.10,127.0.0.1'
  const user4 = '8.8.8.11,127.0.0.1'

  await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user0 }) // User 0 starts
  await wait(500)

  await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user1 }) // User 1 starts
  await servers[0].views.view({ id: videoUUID, currentTime: 2, xForwardedFor: user0 }) // User 0 ends
  await wait(500)

  const startDate = new Date().toISOString()
  await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user2 }) // User 2 starts
  await wait(500)

  await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user3 }) // User 3 starts
  await wait(500)

  await servers[0].views.view({ id: videoUUID, currentTime: 4, xForwardedFor: user1 }) // User 1 ends
  await wait(500)

  await servers[0].views.view({ id: videoUUID, currentTime: 3, xForwardedFor: user3 }) // User 3 ends
  await wait(500)

  const endDate = new Date().toISOString()
  await servers[0].views.view({ id: videoUUID, currentTime: 0, xForwardedFor: user4 }) // User 4 starts
  await servers[0].views.view({ id: videoUUID, currentTime: 5, xForwardedFor: user2 }) // User 2 ends
  await wait(500)

  await servers[0].views.view({ id: videoUUID, currentTime: 1, xForwardedFor: user4 }) // User 4 ends

  await processViewersStats(servers)

  return { startDate, endDate }
}

describe('Test views overall stats', function () {
  let servers: PeerTubeServer[]

  before(async function () {
    this.timeout(120000)

    servers = await prepareViewsServers()
  })

  describe('Test watch time stats of local videos on live and VOD', function () {
    let vodVideoId: string
    let liveVideoId: string
    let command: FfmpegCommand

    before(async function () {
      this.timeout(240000);

      ({ vodVideoId, liveVideoId, ffmpegCommand: command } = await prepareViewsVideos({ servers, live: true, vod: true }))
    })

    it('Should display overall stats of a video with no viewers', async function () {
      for (const videoId of [ liveVideoId, vodVideoId ]) {
        const stats = await servers[0].videoStats.getOverallStats({ videoId })
        const video = await servers[0].videos.get({ id: videoId })

        expect(video.views).to.equal(0)
        expect(stats.averageWatchTime).to.equal(0)
        expect(stats.totalWatchTime).to.equal(0)
        expect(stats.totalViewers).to.equal(0)
      }
    })

    it('Should display overall stats with 1 viewer below the watch time limit', async function () {
      this.timeout(60000)

      for (const videoId of [ liveVideoId, vodVideoId ]) {
        await servers[0].views.simulateViewer({ id: videoId, currentTimes: [ 0, 1 ] })
      }

      await processViewersStats(servers)

      for (const videoId of [ liveVideoId, vodVideoId ]) {
        const stats = await servers[0].videoStats.getOverallStats({ videoId })
        const video = await servers[0].videos.get({ id: videoId })

        expect(video.views).to.equal(0)
        expect(stats.averageWatchTime).to.equal(1)
        expect(stats.totalWatchTime).to.equal(1)
        expect(stats.totalViewers).to.equal(1)
      }
    })

    it('Should display overall stats with 2 viewers', async function () {
      this.timeout(60000)

      {
        await servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 3 ] })
        await servers[0].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 35, 40 ] })

        await processViewersStats(servers)

        {
          const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId })
          const video = await servers[0].videos.get({ id: vodVideoId })

          expect(video.views).to.equal(1)
          expect(stats.averageWatchTime).to.equal(2)
          expect(stats.totalWatchTime).to.equal(4)
          expect(stats.totalViewers).to.equal(2)
        }

        {
          const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId })
          const video = await servers[0].videos.get({ id: liveVideoId })

          expect(video.views).to.equal(1)
          expect(stats.averageWatchTime).to.equal(21)
          expect(stats.totalWatchTime).to.equal(41)
          expect(stats.totalViewers).to.equal(2)
        }
      }
    })

    it('Should display overall stats with a remote viewer below the watch time limit', async function () {
      this.timeout(60000)

      for (const videoId of [ liveVideoId, vodVideoId ]) {
        await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 2 ] })
      }

      await processViewersStats(servers)

      {
        const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId })
        const video = await servers[0].videos.get({ id: vodVideoId })

        expect(video.views).to.equal(1)
        expect(stats.averageWatchTime).to.equal(2)
        expect(stats.totalWatchTime).to.equal(6)
        expect(stats.totalViewers).to.equal(3)
      }

      {
        const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId })
        const video = await servers[0].videos.get({ id: liveVideoId })

        expect(video.views).to.equal(1)
        expect(stats.averageWatchTime).to.equal(14)
        expect(stats.totalWatchTime).to.equal(43)
        expect(stats.totalViewers).to.equal(3)
      }
    })

    it('Should display overall stats with a remote viewer above the watch time limit', async function () {
      this.timeout(60000)

      await servers[1].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 5 ] })
      await servers[1].views.simulateViewer({ id: liveVideoId, currentTimes: [ 0, 45 ] })
      await processViewersStats(servers)

      {
        const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId })
        const video = await servers[0].videos.get({ id: vodVideoId })

        expect(video.views).to.equal(2)
        expect(stats.averageWatchTime).to.equal(3)
        expect(stats.totalWatchTime).to.equal(11)
        expect(stats.totalViewers).to.equal(4)
      }

      {
        const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId })
        const video = await servers[0].videos.get({ id: liveVideoId })

        expect(video.views).to.equal(2)
        expect(stats.averageWatchTime).to.equal(22)
        expect(stats.totalWatchTime).to.equal(88)
        expect(stats.totalViewers).to.equal(4)
      }
    })

    it('Should filter overall stats by date', async function () {
      this.timeout(60000)

      const beforeView = new Date()

      await servers[0].views.simulateViewer({ id: vodVideoId, currentTimes: [ 0, 3 ] })
      await processViewersStats(servers)

      {
        const stats = await servers[0].videoStats.getOverallStats({ videoId: vodVideoId, startDate: beforeView.toISOString() })
        expect(stats.averageWatchTime).to.equal(3)
        expect(stats.totalWatchTime).to.equal(3)
        expect(stats.totalViewers).to.equal(1)
      }

      {
        const stats = await servers[0].videoStats.getOverallStats({ videoId: liveVideoId, endDate: beforeView.toISOString() })
        expect(stats.averageWatchTime).to.equal(22)
        expect(stats.totalWatchTime).to.equal(88)
        expect(stats.totalViewers).to.equal(4)
      }
    })

    after(async function () {
      await stopFfmpeg(command)
    })
  })

  describe('Test watchers peak stats of local videos on VOD', function () {
    let videoUUID: string
    let before2Watchers: Date

    before(async function () {
      this.timeout(240000);

      ({ vodVideoId: videoUUID } = await prepareViewsVideos({ servers, live: true, vod: true }))
    })

    it('Should not have watchers peak', async function () {
      const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID })

      expect(stats.viewersPeak).to.equal(0)
      expect(stats.viewersPeakDate).to.be.null
    })

    it('Should have watcher peak with 1 watcher', async function () {
      this.timeout(60000)

      const before = new Date()
      await servers[0].views.simulateViewer({ id: videoUUID, currentTimes: [ 0, 2 ] })
      const after = new Date()

      await processViewersStats(servers)

      const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID })

      expect(stats.viewersPeak).to.equal(1)
      expect(new Date(stats.viewersPeakDate)).to.be.above(before).and.below(after)
    })

    it('Should have watcher peak with 2 watchers', async function () {
      this.timeout(60000)

      before2Watchers = new Date()
      await servers[0].views.view({ id: videoUUID, currentTime: 0 })
      await servers[1].views.view({ id: videoUUID, currentTime: 0 })
      await servers[0].views.view({ id: videoUUID, currentTime: 2 })
      await servers[1].views.view({ id: videoUUID, currentTime: 2 })
      const after = new Date()

      await processViewersStats(servers)

      const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID })

      expect(stats.viewersPeak).to.equal(2)
      expect(new Date(stats.viewersPeakDate)).to.be.above(before2Watchers).and.below(after)
    })

    it('Should filter peak viewers stats by date', async function () {
      {
        const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() })
        expect(stats.viewersPeak).to.equal(0)
        expect(stats.viewersPeakDate).to.not.exist
      }

      {
        const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, endDate: before2Watchers.toISOString() })
        expect(stats.viewersPeak).to.equal(1)
        expect(new Date(stats.viewersPeakDate)).to.be.below(before2Watchers)
      }
    })

    it('Should complex filter peak viewers by date', async function () {
      this.timeout(60000)

      const { startDate, endDate } = await simulateComplexViewers(servers, videoUUID)

      const expectCorrect = (stats: VideoStatsOverall) => {
        expect(stats.viewersPeak).to.equal(3)
        expect(new Date(stats.viewersPeakDate)).to.be.above(new Date(startDate)).and.below(new Date(endDate))
      }

      expectCorrect(await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate, endDate }))
      expectCorrect(await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate }))
      expectCorrect(await servers[0].videoStats.getOverallStats({ videoId: videoUUID, endDate }))
      expectCorrect(await servers[0].videoStats.getOverallStats({ videoId: videoUUID }))
    })
  })

  describe('Test countries', function () {
    let videoUUID: string

    it('Should not report countries if geoip is disabled', async function () {
      this.timeout(120000)

      const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
      await waitJobs(servers)

      await servers[1].views.view({ id: uuid, xForwardedFor: '8.8.8.8,127.0.0.1', currentTime: 1 })

      await processViewersStats(servers)

      const stats = await servers[0].videoStats.getOverallStats({ videoId: uuid })
      expect(stats.countries).to.have.lengthOf(0)
    })

    it('Should report countries if geoip is enabled', async function () {
      this.timeout(240000)

      const { uuid } = await servers[0].videos.quickUpload({ name: 'video' })
      videoUUID = uuid
      await waitJobs(servers)

      await Promise.all([
        servers[0].kill(),
        servers[1].kill()
      ])

      const config = { geo_ip: { enabled: true } }
      await Promise.all([
        servers[0].run(config),
        servers[1].run(config)
      ])

      await servers[0].views.view({ id: uuid, xForwardedFor: '8.8.8.8,127.0.0.1', currentTime: 1 })
      await servers[1].views.view({ id: uuid, xForwardedFor: '8.8.8.4,127.0.0.1', currentTime: 3 })
      await servers[1].views.view({ id: uuid, xForwardedFor: '80.67.169.12,127.0.0.1', currentTime: 2 })

      await processViewersStats(servers)

      const stats = await servers[0].videoStats.getOverallStats({ videoId: uuid })
      expect(stats.countries).to.have.lengthOf(2)

      expect(stats.countries[0].isoCode).to.equal('US')
      expect(stats.countries[0].viewers).to.equal(2)

      expect(stats.countries[1].isoCode).to.equal('FR')
      expect(stats.countries[1].viewers).to.equal(1)
    })

    it('Should filter countries stats by date', async function () {
      const stats = await servers[0].videoStats.getOverallStats({ videoId: videoUUID, startDate: new Date().toISOString() })
      expect(stats.countries).to.have.lengthOf(0)
    })
  })

  after(async function () {
    await cleanupTests(servers)
  })
})