1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { expect } from 'chai'
4 import { wait } from '@shared/core-utils'
5 import { ActivityType, VideoPlaylistPrivacy } from '@shared/models'
11 setAccessTokensToServers,
12 setDefaultAccountAvatar,
13 setDefaultChannelAvatar,
15 } from '@shared/server-commands'
17 describe('Test stats (excluding redundancy)', function () {
18 let servers: PeerTubeServer[] = []
22 password: 'super_password'
25 before(async function () {
28 servers = await createMultipleServers(3)
30 await setAccessTokensToServers(servers)
31 await setDefaultChannelAvatar(servers)
32 await setDefaultAccountAvatar(servers)
34 await doubleFollow(servers[0], servers[1])
36 await servers[0].users.create({ username: user.username, password: user.password })
38 const { uuid } = await servers[0].videos.upload({ attributes: { fixture: 'video_short.webm' } })
40 await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
42 await servers[0].views.simulateView({ id: uuid })
44 // Wait the video views repeatable job
47 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
48 await waitJobs(servers)
51 it('Should have the correct stats on instance 1', async function () {
52 const data = await servers[0].stats.get()
54 expect(data.totalLocalVideoComments).to.equal(1)
55 expect(data.totalLocalVideos).to.equal(1)
56 expect(data.totalLocalVideoViews).to.equal(1)
57 expect(data.totalLocalVideoFilesSize).to.equal(218910)
58 expect(data.totalUsers).to.equal(2)
59 expect(data.totalVideoComments).to.equal(1)
60 expect(data.totalVideos).to.equal(1)
61 expect(data.totalInstanceFollowers).to.equal(2)
62 expect(data.totalInstanceFollowing).to.equal(1)
63 expect(data.totalLocalPlaylists).to.equal(0)
66 it('Should have the correct stats on instance 2', async function () {
67 const data = await servers[1].stats.get()
69 expect(data.totalLocalVideoComments).to.equal(0)
70 expect(data.totalLocalVideos).to.equal(0)
71 expect(data.totalLocalVideoViews).to.equal(0)
72 expect(data.totalLocalVideoFilesSize).to.equal(0)
73 expect(data.totalUsers).to.equal(1)
74 expect(data.totalVideoComments).to.equal(1)
75 expect(data.totalVideos).to.equal(1)
76 expect(data.totalInstanceFollowers).to.equal(1)
77 expect(data.totalInstanceFollowing).to.equal(1)
78 expect(data.totalLocalPlaylists).to.equal(0)
81 it('Should have the correct stats on instance 3', async function () {
82 const data = await servers[2].stats.get()
84 expect(data.totalLocalVideoComments).to.equal(0)
85 expect(data.totalLocalVideos).to.equal(0)
86 expect(data.totalLocalVideoViews).to.equal(0)
87 expect(data.totalUsers).to.equal(1)
88 expect(data.totalVideoComments).to.equal(1)
89 expect(data.totalVideos).to.equal(1)
90 expect(data.totalInstanceFollowing).to.equal(1)
91 expect(data.totalInstanceFollowers).to.equal(0)
92 expect(data.totalLocalPlaylists).to.equal(0)
95 it('Should have the correct total videos stats after an unfollow', async function () {
98 await servers[2].follows.unfollow({ target: servers[0] })
99 await waitJobs(servers)
101 const data = await servers[2].stats.get()
103 expect(data.totalVideos).to.equal(0)
106 it('Should have the correct active user stats', async function () {
107 const server = servers[0]
110 const data = await server.stats.get()
112 expect(data.totalDailyActiveUsers).to.equal(1)
113 expect(data.totalWeeklyActiveUsers).to.equal(1)
114 expect(data.totalMonthlyActiveUsers).to.equal(1)
118 await server.login.getAccessToken(user)
120 const data = await server.stats.get()
122 expect(data.totalDailyActiveUsers).to.equal(2)
123 expect(data.totalWeeklyActiveUsers).to.equal(2)
124 expect(data.totalMonthlyActiveUsers).to.equal(2)
128 it('Should have the correct active channel stats', async function () {
129 const server = servers[0]
132 const data = await server.stats.get()
134 expect(data.totalLocalVideoChannels).to.equal(2)
135 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
136 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
137 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
142 name: 'stats_channel',
143 displayName: 'My stats channel'
145 const created = await server.channels.create({ attributes })
146 channelId = created.id
148 const data = await server.stats.get()
150 expect(data.totalLocalVideoChannels).to.equal(3)
151 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
152 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
153 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
157 await server.videos.upload({ attributes: { fixture: 'video_short.webm', channelId } })
159 const data = await server.stats.get()
161 expect(data.totalLocalVideoChannels).to.equal(3)
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)