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

import 'mocha'
import * as chai from 'chai'
import {
  cleanupTests,
  doubleFollow,
  flushAndRunMultipleServers,
  ServerInfo,
  setAccessTokensToServers,
  uploadVideo,
  viewVideo,
  wait,
  waitJobs
} from '@shared/extra-utils'
import { ActivityType, VideoPlaylistPrivacy } from '@shared/models'

const expect = chai.expect

describe('Test stats (excluding redundancy)', function () {
  let servers: ServerInfo[] = []
  let channelId
  const user = {
    username: 'user1',
    password: 'super_password'
  }

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

    servers = await flushAndRunMultipleServers(3)

    await setAccessTokensToServers(servers)

    await doubleFollow(servers[0], servers[1])

    await servers[0].usersCommand.create({ username: user.username, password: user.password })

    const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' })
    const videoUUID = resVideo.body.video.uuid

    await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'comment' })

    await viewVideo(servers[0].url, videoUUID)

    // Wait the video views repeatable job
    await wait(8000)

    await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
    await waitJobs(servers)
  })

  it('Should have the correct stats on instance 1', async function () {
    const data = await servers[0].statsCommand.get()

    expect(data.totalLocalVideoComments).to.equal(1)
    expect(data.totalLocalVideos).to.equal(1)
    expect(data.totalLocalVideoViews).to.equal(1)
    expect(data.totalLocalVideoFilesSize).to.equal(218910)
    expect(data.totalUsers).to.equal(2)
    expect(data.totalVideoComments).to.equal(1)
    expect(data.totalVideos).to.equal(1)
    expect(data.totalInstanceFollowers).to.equal(2)
    expect(data.totalInstanceFollowing).to.equal(1)
    expect(data.totalLocalPlaylists).to.equal(0)
  })

  it('Should have the correct stats on instance 2', async function () {
    const data = await servers[1].statsCommand.get()

    expect(data.totalLocalVideoComments).to.equal(0)
    expect(data.totalLocalVideos).to.equal(0)
    expect(data.totalLocalVideoViews).to.equal(0)
    expect(data.totalLocalVideoFilesSize).to.equal(0)
    expect(data.totalUsers).to.equal(1)
    expect(data.totalVideoComments).to.equal(1)
    expect(data.totalVideos).to.equal(1)
    expect(data.totalInstanceFollowers).to.equal(1)
    expect(data.totalInstanceFollowing).to.equal(1)
    expect(data.totalLocalPlaylists).to.equal(0)
  })

  it('Should have the correct stats on instance 3', async function () {
    const data = await servers[2].statsCommand.get()

    expect(data.totalLocalVideoComments).to.equal(0)
    expect(data.totalLocalVideos).to.equal(0)
    expect(data.totalLocalVideoViews).to.equal(0)
    expect(data.totalUsers).to.equal(1)
    expect(data.totalVideoComments).to.equal(1)
    expect(data.totalVideos).to.equal(1)
    expect(data.totalInstanceFollowing).to.equal(1)
    expect(data.totalInstanceFollowers).to.equal(0)
    expect(data.totalLocalPlaylists).to.equal(0)
  })

  it('Should have the correct total videos stats after an unfollow', async function () {
    this.timeout(15000)

    await servers[2].followsCommand.unfollow({ target: servers[0] })
    await waitJobs(servers)

    const data = await servers[2].statsCommand.get()

    expect(data.totalVideos).to.equal(0)
  })

  it('Should have the correct active user stats', async function () {
    const server = servers[0]

    {
      const data = await server.statsCommand.get()

      expect(data.totalDailyActiveUsers).to.equal(1)
      expect(data.totalWeeklyActiveUsers).to.equal(1)
      expect(data.totalMonthlyActiveUsers).to.equal(1)
    }

    {
      await server.loginCommand.getAccessToken(user)

      const data = await server.statsCommand.get()

      expect(data.totalDailyActiveUsers).to.equal(2)
      expect(data.totalWeeklyActiveUsers).to.equal(2)
      expect(data.totalMonthlyActiveUsers).to.equal(2)
    }
  })

  it('Should have the correct active channel stats', async function () {
    const server = servers[0]

    {
      const data = await server.statsCommand.get()

      expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
      expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
      expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
    }

    {
      const attributes = {
        name: 'stats_channel',
        displayName: 'My stats channel'
      }
      const created = await server.channelsCommand.create({ attributes })
      channelId = created.id

      const data = await server.statsCommand.get()

      expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
      expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
      expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
    }

    {
      await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.webm', channelId })

      const data = await server.statsCommand.get()

      expect(data.totalLocalDailyActiveVideoChannels).to.equal(2)
      expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(2)
      expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(2)
    }
  })

  it('Should have the correct playlist stats', async function () {
    const server = servers[0]

    {
      const data = await server.statsCommand.get()
      expect(data.totalLocalPlaylists).to.equal(0)
    }

    {
      await server.playlistsCommand.create({
        attributes: {
          displayName: 'playlist for count',
          privacy: VideoPlaylistPrivacy.PUBLIC,
          videoChannelId: channelId
        }
      })

      const data = await server.statsCommand.get()
      expect(data.totalLocalPlaylists).to.equal(1)
    }
  })

  it('Should correctly count video file sizes if transcoding is enabled', async function () {
    this.timeout(60000)

    await servers[0].configCommand.updateCustomSubConfig({
      newConfig: {
        transcoding: {
          enabled: true,
          webtorrent: {
            enabled: true
          },
          hls: {
            enabled: true
          },
          resolutions: {
            '0p': false,
            '240p': false,
            '360p': false,
            '480p': false,
            '720p': false,
            '1080p': false,
            '1440p': false,
            '2160p': false
          }
        }
      }
    })

    await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video', fixture: 'video_short.webm' })

    await waitJobs(servers)

    {
      const data = await servers[1].statsCommand.get()
      expect(data.totalLocalVideoFilesSize).to.equal(0)
    }

    {
      const data = await servers[0].statsCommand.get()
      expect(data.totalLocalVideoFilesSize).to.be.greaterThan(500000)
      expect(data.totalLocalVideoFilesSize).to.be.lessThan(600000)
    }
  })

  it('Should have the correct AP stats', async function () {
    this.timeout(60000)

    await servers[0].configCommand.updateCustomSubConfig({
      newConfig: {
        transcoding: {
          enabled: false
        }
      }
    })

    const first = await servers[1].statsCommand.get()

    for (let i = 0; i < 10; i++) {
      await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
    }

    await waitJobs(servers)

    await wait(6000)

    const second = await servers[1].statsCommand.get()
    expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)

    const apTypes: ActivityType[] = [
      'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
    ]

    const processed = apTypes.reduce(
      (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
      0
    )
    expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
    expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)

    expect(second.totalActivityPubMessagesErrors).to.equal(0)

    for (const apType of apTypes) {
      expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
    }

    await wait(6000)

    const third = await servers[1].statsCommand.get()
    expect(third.totalActivityPubMessagesWaiting).to.equal(0)
    expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
  })

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