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