aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/views/video-views-timeserie-stats.ts
blob: 98c041cdfae1b2f74fedc175d0bdf2cb595b550f (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
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import * as chai from 'chai'
import { FfmpegCommand } from 'fluent-ffmpeg'
import { prepareViewsServers, prepareViewsVideos, processViewersStats } from '@server/tests/shared'
import { VideoStatsTimeserie, VideoStatsTimeserieMetric } from '@shared/models'
import { cleanupTests, PeerTubeServer, stopFfmpeg } from '@shared/server-commands'

const expect = chai.expect

describe('Test views timeserie stats', function () {
  const availableMetrics: VideoStatsTimeserieMetric[] = [ 'viewers' ]

  let servers: PeerTubeServer[]

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

    servers = await prepareViewsServers()
  })

  describe('Common metric tests', function () {
    let vodVideoId: string

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

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

    it('Should display empty metric stats', async function () {
      for (const metric of availableMetrics) {
        const { data } = await servers[0].videoStats.getTimeserieStats({ videoId: vodVideoId, metric })

        expect(data).to.have.lengthOf(30)

        for (const d of data) {
          expect(d.value).to.equal(0)
        }
      }
    })
  })

  describe('Test viewer and watch time metrics on live and VOD', function () {
    let vodVideoId: string
    let liveVideoId: string
    let command: FfmpegCommand

    function expectTimeserieData (result: VideoStatsTimeserie, lastValue: number) {
      const { data } = result
      expect(data).to.have.lengthOf(30)

      const last = data[data.length - 1]

      const today = new Date().getDate()
      expect(new Date(last.date).getDate()).to.equal(today)
      expect(last.value).to.equal(lastValue)

      for (let i = 0; i < data.length - 2; i++) {
        expect(data[i].value).to.equal(0)
      }
    }

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

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

    it('Should display appropriate viewers metrics', async function () {
      for (const videoId of [ vodVideoId, liveVideoId ]) {
        await servers[0].views.simulateViewer({ id: videoId, currentTimes: [ 0, 3 ] })
        await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 5 ] })
      }

      await processViewersStats(servers)

      for (const videoId of [ vodVideoId, liveVideoId ]) {
        const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'viewers' })
        expectTimeserieData(result, 2)
      }
    })

    it('Should display appropriate watch time metrics', async function () {
      for (const videoId of [ vodVideoId, liveVideoId ]) {
        const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'aggregateWatchTime' })
        expectTimeserieData(result, 8)

        await servers[1].views.simulateViewer({ id: videoId, currentTimes: [ 0, 1 ] })
      }

      await processViewersStats(servers)

      for (const videoId of [ vodVideoId, liveVideoId ]) {
        const result = await servers[0].videoStats.getTimeserieStats({ videoId, metric: 'aggregateWatchTime' })
        expectTimeserieData(result, 9)
      }
    })

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

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