]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/stats.ts
a35709c2688a1954a6dd0f1316f40cd883fd8853
[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 doubleFollow,
8 flushAndRunMultipleServers,
9 ServerInfo,
10 setAccessTokensToServers,
11 uploadVideo,
12 viewVideo,
13 wait,
14 waitJobs
15 } from '@shared/extra-utils'
16 import { ActivityType, VideoPlaylistPrivacy } from '@shared/models'
17
18 const expect = chai.expect
19
20 describe('Test stats (excluding redundancy)', function () {
21 let servers: ServerInfo[] = []
22 let channelId
23 const user = {
24 username: 'user1',
25 password: 'super_password'
26 }
27
28 before(async function () {
29 this.timeout(60000)
30
31 servers = await flushAndRunMultipleServers(3)
32
33 await setAccessTokensToServers(servers)
34
35 await doubleFollow(servers[0], servers[1])
36
37 await servers[0].usersCommand.create({ username: user.username, password: user.password })
38
39 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' })
40 const videoUUID = resVideo.body.video.uuid
41
42 await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'comment' })
43
44 await viewVideo(servers[0].url, videoUUID)
45
46 // Wait the video views repeatable job
47 await wait(8000)
48
49 await servers[2].followsCommand.follow({ targets: [ 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].statsCommand.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].statsCommand.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].statsCommand.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].followsCommand.unfollow({ target: servers[0] })
101 await waitJobs(servers)
102
103 const data = await servers[2].statsCommand.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.statsCommand.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.loginCommand.getAccessToken(user)
121
122 const data = await server.statsCommand.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.statsCommand.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.channelsCommand.create({ attributes })
147 channelId = created.id
148
149 const data = await server.statsCommand.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 uploadVideo(server.url, server.accessToken, { fixture: 'video_short.webm', channelId })
158
159 const data = await server.statsCommand.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.statsCommand.get()
172 expect(data.totalLocalPlaylists).to.equal(0)
173 }
174
175 {
176 await server.playlistsCommand.create({
177 attributes: {
178 displayName: 'playlist for count',
179 privacy: VideoPlaylistPrivacy.PUBLIC,
180 videoChannelId: channelId
181 }
182 })
183
184 const data = await server.statsCommand.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].configCommand.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 '240p': false,
205 '360p': false,
206 '480p': false,
207 '720p': false,
208 '1080p': false,
209 '1440p': false,
210 '2160p': false
211 }
212 }
213 }
214 })
215
216 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video', fixture: 'video_short.webm' })
217
218 await waitJobs(servers)
219
220 {
221 const data = await servers[1].statsCommand.get()
222 expect(data.totalLocalVideoFilesSize).to.equal(0)
223 }
224
225 {
226 const data = await servers[0].statsCommand.get()
227 expect(data.totalLocalVideoFilesSize).to.be.greaterThan(500000)
228 expect(data.totalLocalVideoFilesSize).to.be.lessThan(600000)
229 }
230 })
231
232 it('Should have the correct AP stats', async function () {
233 this.timeout(60000)
234
235 await servers[0].configCommand.updateCustomSubConfig({
236 newConfig: {
237 transcoding: {
238 enabled: false
239 }
240 }
241 })
242
243 const first = await servers[1].statsCommand.get()
244
245 for (let i = 0; i < 10; i++) {
246 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
247 }
248
249 await waitJobs(servers)
250
251 await wait(6000)
252
253 const second = await servers[1].statsCommand.get()
254 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
255
256 const apTypes: ActivityType[] = [
257 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
258 ]
259
260 const processed = apTypes.reduce(
261 (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
262 0
263 )
264 expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
265 expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)
266
267 expect(second.totalActivityPubMessagesErrors).to.equal(0)
268
269 for (const apType of apTypes) {
270 expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
271 }
272
273 await wait(6000)
274
275 const third = await servers[1].statsCommand.get()
276 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
277 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
278 })
279
280 after(async function () {
281 await cleanupTests(servers)
282 })
283 })