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,
14 } from '@shared/server-commands'
16 const expect = chai.expect
18 describe('Test stats (excluding redundancy)', function () {
19 let servers: PeerTubeServer[] = []
23 password: 'super_password'
26 before(async function () {
29 servers = await createMultipleServers(3)
31 await setAccessTokensToServers(servers)
33 await doubleFollow(servers[0], servers[1])
35 await servers[0].users.create({ username: user.username, password: user.password })
37 const { uuid } = await servers[0].videos.upload({ attributes: { fixture: 'video_short.webm' } })
39 await servers[0].comments.createThread({ videoId: uuid, text: 'comment' })
41 await servers[0].videos.view({ id: uuid })
43 // Wait the video views repeatable job
46 await servers[2].follows.follow({ hosts: [ servers[0].url ] })
47 await waitJobs(servers)
50 it('Should have the correct stats on instance 1', async function () {
51 const data = await servers[0].stats.get()
53 expect(data.totalLocalVideoComments).to.equal(1)
54 expect(data.totalLocalVideos).to.equal(1)
55 expect(data.totalLocalVideoViews).to.equal(1)
56 expect(data.totalLocalVideoFilesSize).to.equal(218910)
57 expect(data.totalUsers).to.equal(2)
58 expect(data.totalVideoComments).to.equal(1)
59 expect(data.totalVideos).to.equal(1)
60 expect(data.totalInstanceFollowers).to.equal(2)
61 expect(data.totalInstanceFollowing).to.equal(1)
62 expect(data.totalLocalPlaylists).to.equal(0)
65 it('Should have the correct stats on instance 2', async function () {
66 const data = await servers[1].stats.get()
68 expect(data.totalLocalVideoComments).to.equal(0)
69 expect(data.totalLocalVideos).to.equal(0)
70 expect(data.totalLocalVideoViews).to.equal(0)
71 expect(data.totalLocalVideoFilesSize).to.equal(0)
72 expect(data.totalUsers).to.equal(1)
73 expect(data.totalVideoComments).to.equal(1)
74 expect(data.totalVideos).to.equal(1)
75 expect(data.totalInstanceFollowers).to.equal(1)
76 expect(data.totalInstanceFollowing).to.equal(1)
77 expect(data.totalLocalPlaylists).to.equal(0)
80 it('Should have the correct stats on instance 3', async function () {
81 const data = await servers[2].stats.get()
83 expect(data.totalLocalVideoComments).to.equal(0)
84 expect(data.totalLocalVideos).to.equal(0)
85 expect(data.totalLocalVideoViews).to.equal(0)
86 expect(data.totalUsers).to.equal(1)
87 expect(data.totalVideoComments).to.equal(1)
88 expect(data.totalVideos).to.equal(1)
89 expect(data.totalInstanceFollowing).to.equal(1)
90 expect(data.totalInstanceFollowers).to.equal(0)
91 expect(data.totalLocalPlaylists).to.equal(0)
94 it('Should have the correct total videos stats after an unfollow', async function () {
97 await servers[2].follows.unfollow({ target: servers[0] })
98 await waitJobs(servers)
100 const data = await servers[2].stats.get()
102 expect(data.totalVideos).to.equal(0)
105 it('Should have the correct active user stats', async function () {
106 const server = servers[0]
109 const data = await server.stats.get()
111 expect(data.totalDailyActiveUsers).to.equal(1)
112 expect(data.totalWeeklyActiveUsers).to.equal(1)
113 expect(data.totalMonthlyActiveUsers).to.equal(1)
117 await server.login.getAccessToken(user)
119 const data = await server.stats.get()
121 expect(data.totalDailyActiveUsers).to.equal(2)
122 expect(data.totalWeeklyActiveUsers).to.equal(2)
123 expect(data.totalMonthlyActiveUsers).to.equal(2)
127 it('Should have the correct active channel stats', async function () {
128 const server = servers[0]
131 const data = await server.stats.get()
133 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
134 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
135 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
140 name: 'stats_channel',
141 displayName: 'My stats channel'
143 const created = await server.channels.create({ attributes })
144 channelId = created.id
146 const data = await server.stats.get()
148 expect(data.totalLocalDailyActiveVideoChannels).to.equal(1)
149 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(1)
150 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(1)
154 await server.videos.upload({ attributes: { fixture: 'video_short.webm', channelId } })
156 const data = await server.stats.get()
158 expect(data.totalLocalDailyActiveVideoChannels).to.equal(2)
159 expect(data.totalLocalWeeklyActiveVideoChannels).to.equal(2)
160 expect(data.totalLocalMonthlyActiveVideoChannels).to.equal(2)
164 it('Should have the correct playlist stats', async function () {
165 const server = servers[0]
168 const data = await server.stats.get()
169 expect(data.totalLocalPlaylists).to.equal(0)
173 await server.playlists.create({
175 displayName: 'playlist for count',
176 privacy: VideoPlaylistPrivacy.PUBLIC,
177 videoChannelId: channelId
181 const data = await server.stats.get()
182 expect(data.totalLocalPlaylists).to.equal(1)
186 it('Should correctly count video file sizes if transcoding is enabled', async function () {
189 await servers[0].config.updateCustomSubConfig({
214 await servers[0].videos.upload({ attributes: { name: 'video', fixture: 'video_short.webm' } })
216 await waitJobs(servers)
219 const data = await servers[1].stats.get()
220 expect(data.totalLocalVideoFilesSize).to.equal(0)
224 const data = await servers[0].stats.get()
225 expect(data.totalLocalVideoFilesSize).to.be.greaterThan(500000)
226 expect(data.totalLocalVideoFilesSize).to.be.lessThan(600000)
230 it('Should have the correct AP stats', async function () {
233 await servers[0].config.updateCustomSubConfig({
241 const first = await servers[1].stats.get()
243 for (let i = 0; i < 10; i++) {
244 await servers[0].videos.upload({ attributes: { name: 'video' } })
247 await waitJobs(servers)
251 const second = await servers[1].stats.get()
252 expect(second.totalActivityPubMessagesProcessed).to.be.greaterThan(first.totalActivityPubMessagesProcessed)
254 const apTypes: ActivityType[] = [
255 'Create', 'Update', 'Delete', 'Follow', 'Accept', 'Announce', 'Undo', 'Like', 'Reject', 'View', 'Dislike', 'Flag'
258 const processed = apTypes.reduce(
259 (previous, type) => previous + second['totalActivityPub' + type + 'MessagesSuccesses'],
262 expect(second.totalActivityPubMessagesProcessed).to.equal(processed)
263 expect(second.totalActivityPubMessagesSuccesses).to.equal(processed)
265 expect(second.totalActivityPubMessagesErrors).to.equal(0)
267 for (const apType of apTypes) {
268 expect(second['totalActivityPub' + apType + 'MessagesErrors']).to.equal(0)
273 const third = await servers[1].stats.get()
274 expect(third.totalActivityPubMessagesWaiting).to.equal(0)
275 expect(third.activityPubMessagesProcessedPerSecond).to.be.lessThan(second.activityPubMessagesProcessedPerSecond)
278 after(async function () {
279 await cleanupTests(servers)