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