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