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