aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/views/video-views-timeserie-stats.ts
blob: fd3aba188a33e8f74da18e760056be451c377898 (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
/* 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(120000);

      ({ 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 expectTodayLastValue (result: VideoStatsTimeserie, lastValue: number) {
      const { data } = result

      const last = data[data.length - 1]
      const today = new Date().getDate()
      expect(new Date(last.date).getDate()).to.equal(today)
    }

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

      expectTodayLastValue(result, lastValue)

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

    function expectInterval (result: VideoStatsTimeserie, intervalMs: number) {
      const first = result.data[0]
      const second = result.data[1]
      expect(new Date(second.date).getTime() - new Date(first.date).getTime()).to.equal(intervalMs)
    }

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

      ({ 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)
      }
    })

    it('Should use a custom start/end date', async function () {
      const now = new Date()
      const twentyDaysAgo = new Date()
      twentyDaysAgo.setDate(twentyDaysAgo.getDate() - 19)

      const result = await servers[0].videoStats.getTimeserieStats({
        videoId: vodVideoId,
        metric: 'aggregateWatchTime',
        startDate: twentyDaysAgo,
        endDate: now
      })

      expect(result.groupInterval).to.equal('1 day')
      expect(result.data).to.have.lengthOf(20)

      const first = result.data[0]
      expect(new Date(first.date).toLocaleDateString()).to.equal(twentyDaysAgo.toLocaleDateString())

      expectInterval(result, 24 * 3600 * 1000)
      expectTodayLastValue(result, 9)
    })

    it('Should automatically group by hours', async function () {
      const now = new Date()
      const twoDaysAgo = new Date()
      twoDaysAgo.setDate(twoDaysAgo.getDate() - 1)

      const result = await servers[0].videoStats.getTimeserieStats({
        videoId: vodVideoId,
        metric: 'aggregateWatchTime',
        startDate: twoDaysAgo,
        endDate: now
      })

      expect(result.groupInterval).to.equal('1 hour')
      expect(result.data).to.have.length.above(24).and.below(50)

      expectInterval(result, 3600 * 1000)
      expectTodayLastValue(result, 9)
    })

    it('Should automatically group by ten minutes', async function () {
      const now = new Date()
      const twoHoursAgo = new Date()
      twoHoursAgo.setHours(twoHoursAgo.getHours() - 4)

      const result = await servers[0].videoStats.getTimeserieStats({
        videoId: vodVideoId,
        metric: 'aggregateWatchTime',
        startDate: twoHoursAgo,
        endDate: now
      })

      expect(result.groupInterval).to.equal('10 minutes')
      expect(result.data).to.have.length.above(20).and.below(30)

      expectInterval(result, 60 * 10 * 1000)
      expectTodayLastValue(result, 9)
    })

    it('Should automatically group by one minute', async function () {
      const now = new Date()
      const thirtyAgo = new Date()
      thirtyAgo.setMinutes(thirtyAgo.getMinutes() - 30)

      const result = await servers[0].videoStats.getTimeserieStats({
        videoId: vodVideoId,
        metric: 'aggregateWatchTime',
        startDate: thirtyAgo,
        endDate: now
      })

      expect(result.groupInterval).to.equal('1 minute')
      expect(result.data).to.have.length.above(20).and.below(40)

      expectInterval(result, 60 * 1000)
      expectTodayLastValue(result, 9)
    })

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

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