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