]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/stats.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / stats.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4 import * as chai from 'chai'
5 import { wait } from '@shared/core-utils'
6 import { ActivityType, VideoPlaylistPrivacy } from '@shared/models'
7 import {
8 cleanupTests,
9 createMultipleServers,
10 doubleFollow,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 waitJobs
14 } from '@shared/server-commands'
15
16 const expect = chai.expect
17
18 describe('Test stats (excluding redundancy)', function () {
19 let servers: PeerTubeServer[] = []
20 let channelId
21 const user = {
22 username: 'user1',
23 password: 'super_password'
24 }
25
26 before(async function () {
27 this.timeout(60000)
28
29 servers = await createMultipleServers(3)
30
31 await setAccessTokensToServers(servers)
32
33 await doubleFollow(servers[0], servers[1])
34
35 await servers[0].users.create({ username: user.username, password: user.password })
36
37 const { uuid } = await servers[0].videos.upload({ attributes: { fixture: 'video_short.webm' } })
38
39 await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
40
41 await servers[0].videos.view({ id: uuid })
42
43 // Wait the video views repeatable job
44 await wait(8000)
45
46 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
47 await waitJobs(servers)
48 })
49
50 it('Should have the correct stats on instance 1', async function () {
51 const data = await servers[0].stats.get()
52
53 expect(data.totalLocalVideoComments).to.equal(1)
54 expect(data.totalLocalVideos).to.equal(1)
55 expect(data.totalLocalVideoViews).to.equal(1)
56 expect(data.totalLocalVideoFilesSize).to.equal(218910)
57 expect(data.totalUsers).to.equal(2)
58 expect(data.totalVideoComments).to.equal(1)
59 expect(data.totalVideos).to.equal(1)
60 expect(data.totalInstanceFollowers).to.equal(2)
61 expect(data.totalInstanceFollowing).to.equal(1)
62 expect(data.totalLocalPlaylists).to.equal(0)
63 })
64
65 it('Should have the correct stats on instance 2', async function () {
66 const data = await servers[1].stats.get()
67
68 expect(data.totalLocalVideoComments).to.equal(0)
69 expect(data.totalLocalVideos).to.equal(0)
70 expect(data.totalLocalVideoViews).to.equal(0)
71 expect(data.totalLocalVideoFilesSize).to.equal(0)
72 expect(data.totalUsers).to.equal(1)
73 expect(data.totalVideoComments).to.equal(1)
74 expect(data.totalVideos).to.equal(1)
75 expect(data.totalInstanceFollowers).to.equal(1)
76 expect(data.totalInstanceFollowing).to.equal(1)
77 expect(data.totalLocalPlaylists).to.equal(0)
78 })
79
80 it('Should have the correct stats on instance 3', async function () {
81 const data = await servers[2].stats.get()
82
83 expect(data.totalLocalVideoComments).to.equal(0)
84 expect(data.totalLocalVideos).to.equal(0)
85 expect(data.totalLocalVideoViews).to.equal(0)
86 expect(data.totalUsers).to.equal(1)
87 expect(data.totalVideoComments).to.equal(1)
88 expect(data.totalVideos).to.equal(1)
89 expect(data.totalInstanceFollowing).to.equal(1)
90 expect(data.totalInstanceFollowers).to.equal(0)
91 expect(data.totalLocalPlaylists).to.equal(0)
92 })
93
94 it('Should have the correct total videos stats after an unfollow', async function () {
95 this.timeout(15000)
96
97 await servers[2].follows.unfollow({ target: servers[0] })
98 await waitJobs(servers)
99
100 const data = await servers[2].stats.get()
101
102 expect(data.totalVideos).to.equal(0)
103 })
104
105 it('Should have the correct active user stats', async function () {
106 const server = servers[0]
107
108 {
109 const data = await server.stats.get()
110
111 expect(data.totalDailyActiveUsers).to.equal(1)
112 expect(data.totalWeeklyActiveUsers).to.equal(1)
113 expect(data.totalMonthlyActiveUsers).to.equal(1)
114 }
115
116 {
117 await server.login.getAccessToken(user)
118
119 const data = await server.stats.get()
120
121 expect(data.totalDailyActiveUsers).to.equal(2)
122 expect(data.totalWeeklyActiveUsers).to.equal(2)
123 expect(data.totalMonthlyActiveUsers).to.equal(2)
124 }
125 })
126
127 it('Should have the correct active channel stats', async function () {
128 const server = servers[0]
129
130 {
131 const data = await server.stats.get()
132
133 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
134 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
135 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
136 }
137
138 {
139 const attributes = {
140 name: 'stats_channel',
141 displayName: 'My stats channel'
142 }
143 const created = await server.channels.create({ attributes })
144 channelId = created.id
145
146 const data = await server.stats.get()
147
148 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
149 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
150 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
151 }
152
153 {
154 await server.videos.upload({ attributes: { fixture: 'video_short.webm', channelId } })
155
156 const data = await server.stats.get()
157
158 expect(data.totalLocalDailyActiveVideoChannels).to.equal(2)
159 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(2)
160 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(2)
161 }
162 })
163
164 it('Should have the correct playlist stats', async function () {
165 const server = servers[0]
166
167 {
168 const data = await server.stats.get()
169 expect(data.totalLocalPlaylists).to.equal(0)
170 }
171
172 {
173 await server.playlists.create({
174 attributes: {
175 displayName: 'playlist for count',
176 privacy: VideoPlaylistPrivacy.PUBLIC,
177 videoChannelId: channelId
178 }
179 })
180
181 const data = await server.stats.get()
182 expect(data.totalLocalPlaylists).to.equal(1)
183 }
184 })
185
186 it('Should correctly count video file sizes if transcoding is enabled', async function () {
187 this.timeout(60000)
188
189 await servers[0].config.updateCustomSubConfig({
190 newConfig: {
191 transcoding: {
192 enabled: true,
193 webtorrent: {
194 enabled: true
195 },
196 hls: {
197 enabled: true
198 },
199 resolutions: {
200 '0p': false,
201 '144p': false,
202 '240p': false,
203 '360p': false,
204 '480p': false,
205 '720p': false,
206 '1080p': false,
207 '1440p': false,
208 '2160p': false
209 }
210 }
211 }
212 })
213
214 await servers[0].videos.upload({ attributes: { name: 'video', fixture: 'video_short.webm' } })
215
216 await waitJobs(servers)
217
218 {
219 const data = await servers[1].stats.get()
220 expect(data.totalLocalVideoFilesSize).to.equal(0)
221 }
222
223 {
224 const data = await servers[0].stats.get()
225 expect(data.totalLocalVideoFilesSize).to.be.greaterThan(500000)
226 expect(data.totalLocalVideoFilesSize).to.be.lessThan(600000)
227 }
228 })
229
230 it('Should have the correct AP stats', async function () {
231 this.timeout(60000)
232
233 await servers[0].config.updateCustomSubConfig({
234 newConfig: {
235 transcoding: {
236 enabled: false
237 }
238 }
239 })
240
241 const first = await servers[1].stats.get()
242
243 for (let i = 0; i < 10; i++) {
244 await servers[0].videos.upload({ attributes: { name: 'video' } })
245 }
246
247 await waitJobs(servers)
248
249 await wait(6000)
250
251 const second = await servers[1].stats.get()
252 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
253
254 const apTypes: ActivityType[] = [
255 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
256 ]
257
258 const processed = apTypes.reduce(
259 (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
260 0
261 )
262 expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
263 expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)
264
265 expect(second.totalActivityPubMessagesErrors).to.equal(0)
266
267 for (const apType of apTypes) {
268 expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
269 }
270
271 await wait(6000)
272
273 const third = await servers[1].stats.get()
274 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
275 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
276 })
277
278 after(async function () {
279 await cleanupTests(servers)
280 })
281 })