1 import { CONFIG } from '@server/initializers/config'
2 import { UserModel } from '@server/models/account/user'
3 import { ActorFollowModel } from '@server/models/activitypub/actor-follow'
4 import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
5 import { VideoModel } from '@server/models/video/video'
6 import { VideoCommentModel } from '@server/models/video/video-comment'
7 import { VideoFileModel } from '@server/models/video/video-file'
8 import { ServerStats, VideoRedundancyStrategyWithManual } from '@shared/models'
12 private static instance: StatsManager
14 private readonly instanceStartDate = new Date()
16 private inboxMessagesProcessed = 0
17 private inboxMessagesWaiting = 0
19 private constructor () {}
21 updateInboxStats (inboxMessagesProcessed: number, inboxMessagesWaiting: number) {
22 this.inboxMessagesProcessed = inboxMessagesProcessed
23 this.inboxMessagesWaiting = inboxMessagesWaiting
27 const { totalLocalVideos, totalLocalVideoViews, totalVideos } = await VideoModel.getStats()
28 const { totalLocalVideoComments, totalVideoComments } = await VideoCommentModel.getStats()
29 const { totalUsers, totalDailyActiveUsers, totalWeeklyActiveUsers, totalMonthlyActiveUsers } = await UserModel.getStats()
30 const { totalInstanceFollowers, totalInstanceFollowing } = await ActorFollowModel.getStats()
31 const { totalLocalVideoFilesSize } = await VideoFileModel.getStats()
33 const videosRedundancyStats = await this.buildRedundancyStats()
35 const data: ServerStats = {
38 totalLocalVideoFilesSize,
39 totalLocalVideoComments,
44 totalDailyActiveUsers,
45 totalWeeklyActiveUsers,
46 totalMonthlyActiveUsers,
48 totalInstanceFollowers,
49 totalInstanceFollowing,
51 videosRedundancy: videosRedundancyStats,
53 totalActivityPubMessagesProcessed: this.inboxMessagesProcessed,
54 activityPubMessagesProcessedPerSecond: this.buildActivityPubMessagesProcessedPerSecond(),
55 totalActivityPubMessagesWaiting: this.inboxMessagesWaiting
61 private buildActivityPubMessagesProcessedPerSecond () {
62 const now = new Date()
63 const startedSeconds = (now.getTime() - this.instanceStartDate.getTime()) / 1000
65 return this.inboxMessagesProcessed / startedSeconds
68 private buildRedundancyStats () {
69 const strategies = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
71 strategy: r.strategy as VideoRedundancyStrategyWithManual,
75 strategies.push({ strategy: 'manual', size: null })
79 return VideoRedundancyModel.getStats(r.strategy)
80 .then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size }))
85 static get Instance () {
86 return this.instance || (this.instance = new this())
90 // ---------------------------------------------------------------------------