1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
9 flushAndRunMultipleServers,
11 setAccessTokensToServers,
16 } from '@shared/extra-utils'
17 import { ActivityType, VideoPlaylistPrivacy } from '@shared/models'
19 const expect = chai.expect
21 describe('Test stats (excluding redundancy)', function () {
22 let servers: ServerInfo[] = []
26 password: 'super_password'
29 before(async function () {
32 servers = await flushAndRunMultipleServers(3)
34 await setAccessTokensToServers(servers)
36 await doubleFollow(servers[0], servers[1])
38 await createUser({ url: servers[0].url, accessToken: servers[0].accessToken, username: user.username, password: user.password })
40 const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' })
41 const videoUUID = resVideo.body.video.uuid
43 await servers[0].commentsCommand.createThread({ videoId: videoUUID, text: 'comment' })
45 await viewVideo(servers[0].url, videoUUID)
47 // Wait the video views repeatable job
50 await servers[2].followsCommand.follow({ targets: [ servers[0].url ] })
51 await waitJobs(servers)
54 it('Should have the correct stats on instance 1', async function () {
55 const data = await servers[0].statsCommand.get()
57 expect(data.totalLocalVideoComments).to.equal(1)
58 expect(data.totalLocalVideos).to.equal(1)
59 expect(data.totalLocalVideoViews).to.equal(1)
60 expect(data.totalLocalVideoFilesSize).to.equal(218910)
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)
66 expect(data.totalLocalPlaylists).to.equal(0)
69 it('Should have the correct stats on instance 2', async function () {
70 const data = await servers[1].statsCommand.get()
72 expect(data.totalLocalVideoComments).to.equal(0)
73 expect(data.totalLocalVideos).to.equal(0)
74 expect(data.totalLocalVideoViews).to.equal(0)
75 expect(data.totalLocalVideoFilesSize).to.equal(0)
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)
81 expect(data.totalLocalPlaylists).to.equal(0)
84 it('Should have the correct stats on instance 3', async function () {
85 const data = await servers[2].statsCommand.get()
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)
95 expect(data.totalLocalPlaylists).to.equal(0)
98 it('Should have the correct total videos stats after an unfollow', async function () {
101 await servers[2].followsCommand.unfollow({ target: servers[0] })
102 await waitJobs(servers)
104 const data = await servers[2].statsCommand.get()
106 expect(data.totalVideos).to.equal(0)
109 it('Should have the correct active user stats', async function () {
110 const server = servers[0]
113 const data = await server.statsCommand.get()
115 expect(data.totalDailyActiveUsers).to.equal(1)
116 expect(data.totalWeeklyActiveUsers).to.equal(1)
117 expect(data.totalMonthlyActiveUsers).to.equal(1)
121 await server.loginCommand.getAccessToken(user)
123 const data = await server.statsCommand.get()
125 expect(data.totalDailyActiveUsers).to.equal(2)
126 expect(data.totalWeeklyActiveUsers).to.equal(2)
127 expect(data.totalMonthlyActiveUsers).to.equal(2)
131 it('Should have the correct active channel stats', async function () {
132 const server = servers[0]
135 const data = await server.statsCommand.get()
137 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
138 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
139 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
144 name: 'stats_channel',
145 displayName: 'My stats channel'
147 const created = await server.channelsCommand.create({ attributes })
148 channelId = created.id
150 const data = await server.statsCommand.get()
152 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
153 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
154 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
158 await uploadVideo(server.url, server.accessToken, { fixture: 'video_short.webm', channelId })
160 const data = await server.statsCommand.get()
162 expect(data.totalLocalDailyActiveVideoChannels).to.equal(2)
163 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(2)
164 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(2)
168 it('Should have the correct playlist stats', async function () {
169 const server = servers[0]
172 const data = await server.statsCommand.get()
173 expect(data.totalLocalPlaylists).to.equal(0)
177 await server.playlistsCommand.create({
179 displayName: 'playlist for count',
180 privacy: VideoPlaylistPrivacy.PUBLIC,
181 videoChannelId: channelId
185 const data = await server.statsCommand.get()
186 expect(data.totalLocalPlaylists).to.equal(1)
190 it('Should correctly count video file sizes if transcoding is enabled', async function () {
193 await servers[0].configCommand.updateCustomSubConfig({
217 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video', fixture: 'video_short.webm' })
219 await waitJobs(servers)
222 const data = await servers[1].statsCommand.get()
223 expect(data.totalLocalVideoFilesSize).to.equal(0)
227 const data = await servers[0].statsCommand.get()
228 expect(data.totalLocalVideoFilesSize).to.be.greaterThan(500000)
229 expect(data.totalLocalVideoFilesSize).to.be.lessThan(600000)
233 it('Should have the correct AP stats', async function () {
236 await servers[0].configCommand.updateCustomSubConfig({
244 const first = await servers[1].statsCommand.get()
246 for (let i = 0; i < 10; i++) {
247 await uploadVideo(servers[0].url, servers[0].accessToken, { name: 'video' })
250 await waitJobs(servers)
254 const second = await servers[1].statsCommand.get()
255 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
257 const apTypes: ActivityType[] = [
258 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
261 const processed = apTypes.reduce(
262 (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
265 expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
266 expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)
268 expect(second.totalActivityPubMessagesErrors).to.equal(0)
270 for (const apType of apTypes) {
271 expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
276 const third = await servers[1].statsCommand.get()
277 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
278 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
281 after(async function () {
282 await cleanupTests(servers)