1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import * as chai 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 const expect = chai.expect
19 describe('Test stats (excluding redundancy)', function () {
20 let servers: PeerTubeServer[] = []
24 password: 'super_password'
27 before(async function () {
30 servers = await createMultipleServers(3)
32 await setAccessTokensToServers(servers)
33 await setDefaultChannelAvatar(servers)
34 await setDefaultAccountAvatar(servers)
36 await doubleFollow(servers[0], servers[1])
38 await servers[0].users.create({ username: user.username, password: user.password })
40 const { uuid } = await servers[0].videos.upload({ attributes: { fixture: 'video_short.webm' } })
42 await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
44 await servers[0].views.simulateView({ id: uuid })
46 // Wait the video views repeatable job
49 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
50 await waitJobs(servers)
53 it('Should have the correct stats on instance 1', async function () {
54 const data = await servers[0].stats.get()
56 expect(data.totalLocalVideoComments).to.equal(1)
57 expect(data.totalLocalVideos).to.equal(1)
58 expect(data.totalLocalVideoViews).to.equal(1)
59 expect(data.totalLocalVideoFilesSize).to.equal(218910)
60 expect(data.totalUsers).to.equal(2)
61 expect(data.totalVideoComments).to.equal(1)
62 expect(data.totalVideos).to.equal(1)
63 expect(data.totalInstanceFollowers).to.equal(2)
64 expect(data.totalInstanceFollowing).to.equal(1)
65 expect(data.totalLocalPlaylists).to.equal(0)
68 it('Should have the correct stats on instance 2', async function () {
69 const data = await servers[1].stats.get()
71 expect(data.totalLocalVideoComments).to.equal(0)
72 expect(data.totalLocalVideos).to.equal(0)
73 expect(data.totalLocalVideoViews).to.equal(0)
74 expect(data.totalLocalVideoFilesSize).to.equal(0)
75 expect(data.totalUsers).to.equal(1)
76 expect(data.totalVideoComments).to.equal(1)
77 expect(data.totalVideos).to.equal(1)
78 expect(data.totalInstanceFollowers).to.equal(1)
79 expect(data.totalInstanceFollowing).to.equal(1)
80 expect(data.totalLocalPlaylists).to.equal(0)
83 it('Should have the correct stats on instance 3', async function () {
84 const data = await servers[2].stats.get()
86 expect(data.totalLocalVideoComments).to.equal(0)
87 expect(data.totalLocalVideos).to.equal(0)
88 expect(data.totalLocalVideoViews).to.equal(0)
89 expect(data.totalUsers).to.equal(1)
90 expect(data.totalVideoComments).to.equal(1)
91 expect(data.totalVideos).to.equal(1)
92 expect(data.totalInstanceFollowing).to.equal(1)
93 expect(data.totalInstanceFollowers).to.equal(0)
94 expect(data.totalLocalPlaylists).to.equal(0)
97 it('Should have the correct total videos stats after an unfollow', async function () {
100 await servers[2].follows.unfollow({ target: servers[0] })
101 await waitJobs(servers)
103 const data = await servers[2].stats.get()
105 expect(data.totalVideos).to.equal(0)
108 it('Should have the correct active user stats', async function () {
109 const server = servers[0]
112 const data = await server.stats.get()
114 expect(data.totalDailyActiveUsers).to.equal(1)
115 expect(data.totalWeeklyActiveUsers).to.equal(1)
116 expect(data.totalMonthlyActiveUsers).to.equal(1)
120 await server.login.getAccessToken(user)
122 const data = await server.stats.get()
124 expect(data.totalDailyActiveUsers).to.equal(2)
125 expect(data.totalWeeklyActiveUsers).to.equal(2)
126 expect(data.totalMonthlyActiveUsers).to.equal(2)
130 it('Should have the correct active channel stats', async function () {
131 const server = servers[0]
134 const data = await server.stats.get()
136 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
137 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
138 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
143 name: 'stats_channel',
144 displayName: 'My stats channel'
146 const created = await server.channels.create({ attributes })
147 channelId = created.id
149 const data = await server.stats.get()
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.totalLocalDailyActiveVideoChannels).to.equal(2)
162 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(2)
163 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(2)
167 it('Should have the correct playlist stats', async function () {
168 const server = servers[0]
171 const data = await server.stats.get()
172 expect(data.totalLocalPlaylists).to.equal(0)
176 await server.playlists.create({
178 displayName: 'playlist for count',
179 privacy: VideoPlaylistPrivacy.PUBLIC,
180 videoChannelId: channelId
184 const data = await server.stats.get()
185 expect(data.totalLocalPlaylists).to.equal(1)
189 it('Should correctly count video file sizes if transcoding is enabled', async function () {
192 await servers[0].config.updateCustomSubConfig({
217 await servers[0].videos.upload({ attributes: { name: 'video', fixture: 'video_short.webm' } })
219 await waitJobs(servers)
222 const data = await servers[1].stats.get()
223 expect(data.totalLocalVideoFilesSize).to.equal(0)
227 const data = await servers[0].stats.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].config.disableTranscoding()
238 const first = await servers[1].stats.get()
240 for (let i = 0; i < 10; i++) {
241 await servers[0].videos.upload({ attributes: { name: 'video' } })
244 await waitJobs(servers)
248 const second = await servers[1].stats.get()
249 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
251 const apTypes: ActivityType[] = [
252 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
255 const processed = apTypes.reduce(
256 (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
259 expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
260 expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)
262 expect(second.totalActivityPubMessagesErrors).to.equal(0)
264 for (const apType of apTypes) {
265 expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
270 const third = await servers[1].stats.get()
271 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
272 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
275 after(async function () {
276 await cleanupTests(servers)