]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/stats.ts
Allow to specify transcoding and import jobs concurrency
[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 follow,
11 ServerInfo,
12 unfollow,
13 updateCustomSubConfig,
14 uploadVideo,
15 userLogin,
16 viewVideo,
17 wait
18 } from '../../../../shared/extra-utils'
19 import { setAccessTokensToServers } from '../../../../shared/extra-utils/index'
20 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
21 import { getStats } from '../../../../shared/extra-utils/server/stats'
22 import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
23 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
24
25 const expect = chai.expect
26
27 describe('Test stats (excluding redundancy)', function () {
28 let servers: ServerInfo[] = []
29 const user = {
30 username: 'user1',
31 password: 'super_password'
32 }
33
34 before(async function () {
35 this.timeout(60000)
36
37 servers = await flushAndRunMultipleServers(3)
38
39 await setAccessTokensToServers(servers)
40
41 await doubleFollow(servers[0], servers[1])
42
43 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
44
45 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' })
46 const videoUUID = resVideo.body.video.uuid
47
48 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment')
49
50 await viewVideo(servers[0].url, videoUUID)
51
52 // Wait the video views repeatable job
53 await wait(8000)
54
55 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
56 await waitJobs(servers)
57 })
58
59 it('Should have the correct stats on instance 1', async function () {
60 const res = await getStats(servers[0].url)
61 const data: ServerStats = res.body
62
63 expect(data.totalLocalVideoComments).to.equal(1)
64 expect(data.totalLocalVideos).to.equal(1)
65 expect(data.totalLocalVideoViews).to.equal(1)
66 expect(data.totalLocalVideoFilesSize).to.equal(218910)
67 expect(data.totalUsers).to.equal(2)
68 expect(data.totalVideoComments).to.equal(1)
69 expect(data.totalVideos).to.equal(1)
70 expect(data.totalInstanceFollowers).to.equal(2)
71 expect(data.totalInstanceFollowing).to.equal(1)
72 })
73
74 it('Should have the correct stats on instance 2', async function () {
75 const res = await getStats(servers[1].url)
76 const data: ServerStats = res.body
77
78 expect(data.totalLocalVideoComments).to.equal(0)
79 expect(data.totalLocalVideos).to.equal(0)
80 expect(data.totalLocalVideoViews).to.equal(0)
81 expect(data.totalLocalVideoFilesSize).to.equal(0)
82 expect(data.totalUsers).to.equal(1)
83 expect(data.totalVideoComments).to.equal(1)
84 expect(data.totalVideos).to.equal(1)
85 expect(data.totalInstanceFollowers).to.equal(1)
86 expect(data.totalInstanceFollowing).to.equal(1)
87 })
88
89 it('Should have the correct stats on instance 3', async function () {
90 const res = await getStats(servers[2].url)
91 const data: ServerStats = res.body
92
93 expect(data.totalLocalVideoComments).to.equal(0)
94 expect(data.totalLocalVideos).to.equal(0)
95 expect(data.totalLocalVideoViews).to.equal(0)
96 expect(data.totalUsers).to.equal(1)
97 expect(data.totalVideoComments).to.equal(1)
98 expect(data.totalVideos).to.equal(1)
99 expect(data.totalInstanceFollowing).to.equal(1)
100 expect(data.totalInstanceFollowers).to.equal(0)
101 })
102
103 it('Should have the correct total videos stats after an unfollow', async function () {
104 this.timeout(15000)
105
106 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
107 await waitJobs(servers)
108
109 const res = await getStats(servers[2].url)
110 const data: ServerStats = res.body
111
112 expect(data.totalVideos).to.equal(0)
113 })
114
115 it('Should have the correct active users stats', async function () {
116 const server = servers[0]
117
118 {
119 const res = await getStats(server.url)
120 const data: ServerStats = res.body
121 expect(data.totalDailyActiveUsers).to.equal(1)
122 expect(data.totalWeeklyActiveUsers).to.equal(1)
123 expect(data.totalMonthlyActiveUsers).to.equal(1)
124 }
125
126 {
127 await userLogin(server, user)
128
129 const res = await getStats(server.url)
130 const data: ServerStats = res.body
131 expect(data.totalDailyActiveUsers).to.equal(2)
132 expect(data.totalWeeklyActiveUsers).to.equal(2)
133 expect(data.totalMonthlyActiveUsers).to.equal(2)
134 }
135 })
136
137 it('Should correctly count video file sizes if transcoding is enabled', async function () {
138 this.timeout(60000)
139
140 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
141 transcoding: {
142 enabled: true,
143 webtorrent: {
144 enabled: true
145 },
146 hls: {
147 enabled: true
148 },
149 resolutions: {
150 '0p': false,
151 '240p': false,
152 '360p': false,
153 '480p': false,
154 '720p': false,
155 '1080p': false,
156 '1440p': false,
157 '2160p': false
158 }
159 }
160 })
161
162 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video', fixture: 'video_short.webm' })
163
164 await waitJobs(servers)
165
166 {
167 const res = await getStats(servers[1].url)
168 const data: ServerStats = res.body
169 expect(data.totalLocalVideoFilesSize).to.equal(0)
170 }
171
172 {
173 const res = await getStats(servers[0].url)
174 const data: ServerStats = res.body
175 expect(data.totalLocalVideoFilesSize).to.be.greaterThan(300000)
176 expect(data.totalLocalVideoFilesSize).to.be.lessThan(400000)
177 }
178 })
179
180 it('Should have the correct AP stats', async function () {
181 this.timeout(60000)
182
183 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
184 transcoding: {
185 enabled: false
186 }
187 })
188
189 const res1 = await getStats(servers[1].url)
190 const first = res1.body as ServerStats
191
192 for (let i = 0; i < 10; i++) {
193 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
194 }
195
196 await waitJobs(servers)
197
198 const res2 = await getStats(servers[1].url)
199 const second: ServerStats = res2.body
200
201 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
202
203 await wait(5000)
204
205 const res3 = await getStats(servers[1].url)
206 const third: ServerStats = res3.body
207
208 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
209 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
210 })
211
212 after(async function () {
213 await cleanupTests(servers)
214 })
215 })