1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
11 flushAndRunMultipleServers,
15 updateCustomSubConfig,
20 } from '../../../../shared/extra-utils'
21 import { setAccessTokensToServers } from '../../../../shared/extra-utils/index'
22 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
23 import { getStats } from '../../../../shared/extra-utils/server/stats'
24 import { addVideoCommentThread } from '../../../../shared/extra-utils/videos/video-comments'
25 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
26 import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model'
27 import { ActivityType } from '@shared/models'
29 const expect = chai.expect
31 describe('Test stats (excluding redundancy)', function () {
32 let servers: ServerInfo[] = []
36 password: 'super_password'
39 before(async function () {
42 servers = await flushAndRunMultipleServers(3)
44 await setAccessTokensToServers(servers)
46 await doubleFollow(servers[0], servers[1])
48 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
50 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' })
51 const videoUUID = resVideo.body.video.uuid
53 await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment')
55 await viewVideo(servers[0].url, videoUUID)
57 // Wait the video views repeatable job
60 await follow(servers[2].url, [ servers[0].url ], servers[2].accessToken)
61 await waitJobs(servers)
64 it('Should have the correct stats on instance 1', async function () {
65 const res = await getStats(servers[0].url)
66 const data: ServerStats = res.body
68 expect(data.totalLocalVideoComments).to.equal(1)
69 expect(data.totalLocalVideos).to.equal(1)
70 expect(data.totalLocalVideoViews).to.equal(1)
71 expect(data.totalLocalVideoFilesSize).to.equal(218910)
72 expect(data.totalUsers).to.equal(2)
73 expect(data.totalVideoComments).to.equal(1)
74 expect(data.totalVideos).to.equal(1)
75 expect(data.totalInstanceFollowers).to.equal(2)
76 expect(data.totalInstanceFollowing).to.equal(1)
77 expect(data.totalLocalPlaylists).to.equal(0)
80 it('Should have the correct stats on instance 2', async function () {
81 const res = await getStats(servers[1].url)
82 const data: ServerStats = res.body
84 expect(data.totalLocalVideoComments).to.equal(0)
85 expect(data.totalLocalVideos).to.equal(0)
86 expect(data.totalLocalVideoViews).to.equal(0)
87 expect(data.totalLocalVideoFilesSize).to.equal(0)
88 expect(data.totalUsers).to.equal(1)
89 expect(data.totalVideoComments).to.equal(1)
90 expect(data.totalVideos).to.equal(1)
91 expect(data.totalInstanceFollowers).to.equal(1)
92 expect(data.totalInstanceFollowing).to.equal(1)
93 expect(data.totalLocalPlaylists).to.equal(0)
96 it('Should have the correct stats on instance 3', async function () {
97 const res = await getStats(servers[2].url)
98 const data: ServerStats = res.body
100 expect(data.totalLocalVideoComments).to.equal(0)
101 expect(data.totalLocalVideos).to.equal(0)
102 expect(data.totalLocalVideoViews).to.equal(0)
103 expect(data.totalUsers).to.equal(1)
104 expect(data.totalVideoComments).to.equal(1)
105 expect(data.totalVideos).to.equal(1)
106 expect(data.totalInstanceFollowing).to.equal(1)
107 expect(data.totalInstanceFollowers).to.equal(0)
108 expect(data.totalLocalPlaylists).to.equal(0)
111 it('Should have the correct total videos stats after an unfollow', async function () {
114 await unfollow(servers[2].url, servers[2].accessToken, servers[0])
115 await waitJobs(servers)
117 const res = await getStats(servers[2].url)
118 const data: ServerStats = res.body
120 expect(data.totalVideos).to.equal(0)
123 it('Should have the correct active user stats', async function () {
124 const server = servers[0]
127 const res = await getStats(server.url)
128 const data: ServerStats = res.body
129 expect(data.totalDailyActiveUsers).to.equal(1)
130 expect(data.totalWeeklyActiveUsers).to.equal(1)
131 expect(data.totalMonthlyActiveUsers).to.equal(1)
135 await userLogin(server, user)
137 const res = await getStats(server.url)
138 const data: ServerStats = res.body
139 expect(data.totalDailyActiveUsers).to.equal(2)
140 expect(data.totalWeeklyActiveUsers).to.equal(2)
141 expect(data.totalMonthlyActiveUsers).to.equal(2)
145 it('Should have the correct active channel stats', async function () {
146 const server = servers[0]
149 const res = await getStats(server.url)
150 const data: ServerStats = res.body
151 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
152 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
153 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
157 const channelAttributes = {
158 name: 'stats_channel',
159 displayName: 'My stats channel'
161 const resChannel = await addVideoChannel(server.url, server.accessToken, channelAttributes)
162 channelId = resChannel.body.videoChannel.id
164 const res = await getStats(server.url)
165 const data: ServerStats = res.body
166 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
167 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
168 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
172 await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.webm', channelId })
174 const res = await getStats(server.url)
175 const data: ServerStats = res.body
176 expect(data.totalLocalDailyActiveVideoChannels).to.equal(2)
177 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(2)
178 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(2)
182 it('Should have the correct playlist stats', async function () {
183 const server = servers[0]
186 const resStats = await getStats(server.url)
187 const dataStats: ServerStats = resStats.body
188 expect(dataStats.totalLocalPlaylists).to.equal(0)
192 await createVideoPlaylist({
194 token: server.accessToken,
196 displayName: 'playlist for count',
197 privacy: VideoPlaylistPrivacy.PUBLIC,
198 videoChannelId: channelId
202 const resStats = await getStats(server.url)
203 const dataStats: ServerStats = resStats.body
204 expect(dataStats.totalLocalPlaylists).to.equal(1)
208 it('Should correctly count video file sizes if transcoding is enabled', async function () {
211 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
233 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video', fixture: 'video_short.webm' })
235 await waitJobs(servers)
238 const res = await getStats(servers[1].url)
239 const data: ServerStats = res.body
240 expect(data.totalLocalVideoFilesSize).to.equal(0)
244 const res = await getStats(servers[0].url)
245 const data: ServerStats = res.body
246 expect(data.totalLocalVideoFilesSize).to.be.greaterThan(500000)
247 expect(data.totalLocalVideoFilesSize).to.be.lessThan(600000)
251 it('Should have the correct AP stats', async function () {
254 await updateCustomSubConfig(servers[0].url, servers[0].accessToken, {
260 const res1 = await getStats(servers[1].url)
261 const first = res1.body as ServerStats
263 for (let i = 0; i < 10; i++) {
264 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
267 await waitJobs(servers)
271 const res2 = await getStats(servers[1].url)
272 const second: ServerStats = res2.body
274 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
275 const apTypes: ActivityType[] = [
276 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
279 const processed = apTypes.reduce(
280 (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
283 expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
284 expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)
286 expect(second.totalActivityPubMessagesErrors).to.equal(0)
288 for (const apType of apTypes) {
289 expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
294 const res3 = await getStats(servers[1].url)
295 const third: ServerStats = res3.body
297 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
298 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
301 after(async function () {
302 await cleanupTests(servers)