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