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