1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import * as chai from 'chai'
5 import { wait } from '@shared/core-utils'
6 import { ActivityType, VideoPlaylistPrivacy } from '@shared/models'
12 setAccessTokensToServers,
13 setDefaultAccountAvatar,
14 setDefaultChannelAvatar,
16 } from '@shared/server-commands'
18 const expect = chai.expect
20 describe('Test stats (excluding redundancy)', function () {
21 let servers: PeerTubeServer[] = []
25 password: 'super_password'
28 before(async function () {
31 servers = await createMultipleServers(3)
33 await setAccessTokensToServers(servers)
34 await setDefaultChannelAvatar(servers)
35 await setDefaultAccountAvatar(servers)
37 await doubleFollow(servers[0], servers[1])
39 await servers[0].users.create({ username: user.username, password: user.password })
41 const { uuid } = await servers[0].videos.upload({ attributes: { fixture: 'video_short.webm' } })
43 await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
45 await servers[0].views.simulateView({ id: uuid })
47 // Wait the video views repeatable job
50 await servers[2].follows.follow({ hosts: [ 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].stats.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].stats.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].stats.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].follows.unfollow({ target: servers[0] })
102 await waitJobs(servers)
104 const data = await servers[2].stats.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.stats.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.login.getAccessToken(user)
123 const data = await server.stats.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.stats.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.channels.create({ attributes })
148 channelId = created.id
150 const data = await server.stats.get()
152 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
153 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
154 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
158 await server.videos.upload({ attributes: { fixture: 'video_short.webm', channelId } })
160 const data = await server.stats.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.stats.get()
173 expect(data.totalLocalPlaylists).to.equal(0)
177 await server.playlists.create({
179 displayName: 'playlist for count',
180 privacy: VideoPlaylistPrivacy.PUBLIC,
181 videoChannelId: channelId
185 const data = await server.stats.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].config.updateCustomSubConfig({
218 await servers[0].videos.upload({ attributes: { name: 'video', fixture: 'video_short.webm' } })
220 await waitJobs(servers)
223 const data = await servers[1].stats.get()
224 expect(data.totalLocalVideoFilesSize).to.equal(0)
228 const data = await servers[0].stats.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].config.disableTranscoding()
239 const first = await servers[1].stats.get()
241 for (let i = 0; i < 10; i++) {
242 await servers[0].videos.upload({ attributes: { name: 'video' } })
245 await waitJobs(servers)
249 const second = await servers[1].stats.get()
250 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
252 const apTypes: ActivityType[] = [
253 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
256 const processed = apTypes.reduce(
257 (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
260 expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
261 expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)
263 expect(second.totalActivityPubMessagesErrors).to.equal(0)
265 for (const apType of apTypes) {
266 expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
271 const third = await servers[1].stats.get()
272 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
273 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
276 after(async function () {
277 await cleanupTests(servers)