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