1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
9 flushAndRunMultipleServers,
13 updateCustomSubConfig,
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'
26 const expect = chai.expect
28 describe('Test stats (excluding redundancy)', function () {
29 let servers: ServerInfo[] = []
32 password: 'super_password'
35 before(async function () {
38 servers = await flushAndRunMultipleServers(3)
40 await setAccessTokensToServers(servers)
42 await doubleFollow(servers[0], servers[1])
44 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
46 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' })
47 const videoUUID = resVideo.body.video.uuid
49 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment')
51 await viewVideo(servers[0].url, videoUUID)
53 // Wait the video views repeatable job
56 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
57 await waitJobs(servers)
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
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)
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
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)
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
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)
104 it('Should have the correct total videos stats after an unfollow', async function () {
107 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
108 await waitJobs(servers)
110 const res = await getStats(servers[2].url)
111 const data: ServerStats = res.body
113 expect(data.totalVideos).to.equal(0)
116 it('Should have the correct active users stats', async function () {
117 const server = servers[0]
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)
128 await userLogin(server, user)
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)
138 it('Should correctly count video file sizes if transcoding is enabled', async function () {
141 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
163 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video', fixture: 'video_short.webm' })
165 await waitJobs(servers)
168 const res = await getStats(servers[1].url)
169 const data: ServerStats = res.body
170 expect(data.totalLocalVideoFilesSize).to.equal(0)
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)
181 it('Should have the correct AP stats', async function () {
184 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
190 const res1 = await getStats(servers[1].url)
191 const first = res1.body as ServerStats
193 for (let i = 0; i < 10; i++) {
194 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
197 await waitJobs(servers)
201 const res2 = await getStats(servers[1].url)
202 const second: ServerStats = res2.body
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'
209 const processed = apTypes.reduce(
210 (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
213 expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
214 expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)
216 expect(second.totalActivityPubMessagesErrors).to.equal(0)
218 for (const apType of apTypes) {
219 expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
224 const res3 = await getStats(servers[1].url)
225 const third: ServerStats = res3.body
227 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
228 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
231 after(async function () {
232 await cleanupTests(servers)