1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
9 flushAndRunMultipleServers,
11 setAccessTokensToServers,
17 } from '@shared/extra-utils'
18 import { ActivityType, VideoPlaylistPrivacy } from '@shared/models'
20 const expect = chai.expect
22 describe('Test stats (excluding redundancy)', function () {
23 let servers: ServerInfo[] = []
27 password: 'super_password'
30 before(async function () {
33 servers = await flushAndRunMultipleServers(3)
35 await setAccessTokensToServers(servers)
37 await doubleFollow(servers[0], servers[1])
39 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
41 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' })
42 const videoUUID = resVideo.body.video.uuid
44 await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'comment' })
46 await viewVideo(servers[0].url, videoUUID)
48 // Wait the video views repeatable job
51 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
52 await waitJobs(servers)
55 it('Should have the correct stats on instance 1', async function () {
56 const data = await servers[0].statsCommand.get()
58 expect(data.totalLocalVideoComments).to.equal(1)
59 expect(data.totalLocalVideos).to.equal(1)
60 expect(data.totalLocalVideoViews).to.equal(1)
61 expect(data.totalLocalVideoFilesSize).to.equal(218910)
62 expect(data.totalUsers).to.equal(2)
63 expect(data.totalVideoComments).to.equal(1)
64 expect(data.totalVideos).to.equal(1)
65 expect(data.totalInstanceFollowers).to.equal(2)
66 expect(data.totalInstanceFollowing).to.equal(1)
67 expect(data.totalLocalPlaylists).to.equal(0)
70 it('Should have the correct stats on instance 2', async function () {
71 const data = await servers[1].statsCommand.get()
73 expect(data.totalLocalVideoComments).to.equal(0)
74 expect(data.totalLocalVideos).to.equal(0)
75 expect(data.totalLocalVideoViews).to.equal(0)
76 expect(data.totalLocalVideoFilesSize).to.equal(0)
77 expect(data.totalUsers).to.equal(1)
78 expect(data.totalVideoComments).to.equal(1)
79 expect(data.totalVideos).to.equal(1)
80 expect(data.totalInstanceFollowers).to.equal(1)
81 expect(data.totalInstanceFollowing).to.equal(1)
82 expect(data.totalLocalPlaylists).to.equal(0)
85 it('Should have the correct stats on instance 3', async function () {
86 const data = await servers[2].statsCommand.get()
88 expect(data.totalLocalVideoComments).to.equal(0)
89 expect(data.totalLocalVideos).to.equal(0)
90 expect(data.totalLocalVideoViews).to.equal(0)
91 expect(data.totalUsers).to.equal(1)
92 expect(data.totalVideoComments).to.equal(1)
93 expect(data.totalVideos).to.equal(1)
94 expect(data.totalInstanceFollowing).to.equal(1)
95 expect(data.totalInstanceFollowers).to.equal(0)
96 expect(data.totalLocalPlaylists).to.equal(0)
99 it('Should have the correct total videos stats after an unfollow', async function () {
102 await servers[2].followsCommand.unfollow({ target: servers[0] })
103 await waitJobs(servers)
105 const data = await servers[2].statsCommand.get()
107 expect(data.totalVideos).to.equal(0)
110 it('Should have the correct active user stats', async function () {
111 const server = servers[0]
114 const data = await server.statsCommand.get()
116 expect(data.totalDailyActiveUsers).to.equal(1)
117 expect(data.totalWeeklyActiveUsers).to.equal(1)
118 expect(data.totalMonthlyActiveUsers).to.equal(1)
122 await userLogin(server, user)
124 const data = await server.statsCommand.get()
126 expect(data.totalDailyActiveUsers).to.equal(2)
127 expect(data.totalWeeklyActiveUsers).to.equal(2)
128 expect(data.totalMonthlyActiveUsers).to.equal(2)
132 it('Should have the correct active channel stats', async function () {
133 const server = servers[0]
136 const data = await server.statsCommand.get()
138 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
139 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
140 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
145 name: 'stats_channel',
146 displayName: 'My stats channel'
148 const created = await server.channelsCommand.create({ attributes })
149 channelId = created.id
151 const data = await server.statsCommand.get()
153 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
154 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
155 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
159 await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.webm', channelId })
161 const data = await server.statsCommand.get()
163 expect(data.totalLocalDailyActiveVideoChannels).to.equal(2)
164 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(2)
165 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(2)
169 it('Should have the correct playlist stats', async function () {
170 const server = servers[0]
173 const data = await server.statsCommand.get()
174 expect(data.totalLocalPlaylists).to.equal(0)
178 await server.playlistsCommand.create({
180 displayName: 'playlist for count',
181 privacy: VideoPlaylistPrivacy.PUBLIC,
182 videoChannelId: channelId
186 const data = await server.statsCommand.get()
187 expect(data.totalLocalPlaylists).to.equal(1)
191 it('Should correctly count video file sizes if transcoding is enabled', async function () {
194 await servers[0].configCommand.updateCustomSubConfig({
218 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video', fixture: 'video_short.webm' })
220 await waitJobs(servers)
223 const data = await servers[1].statsCommand.get()
224 expect(data.totalLocalVideoFilesSize).to.equal(0)
228 const data = await servers[0].statsCommand.get()
229 expect(data.totalLocalVideoFilesSize).to.be.greaterThan(500000)
230 expect(data.totalLocalVideoFilesSize).to.be.lessThan(600000)
234 it('Should have the correct AP stats', async function () {
237 await servers[0].configCommand.updateCustomSubConfig({
245 const first = await servers[1].statsCommand.get()
247 for (let i = 0; i < 10; i++) {
248 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
251 await waitJobs(servers)
255 const second = await servers[1].statsCommand.get()
256 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
258 const apTypes: ActivityType[] = [
259 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
262 const processed = apTypes.reduce(
263 (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
266 expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
267 expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)
269 expect(second.totalActivityPubMessagesErrors).to.equal(0)
271 for (const apType of apTypes) {
272 expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
277 const third = await servers[1].statsCommand.get()
278 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
279 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
282 after(async function () {
283 await cleanupTests(servers)