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