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 { ActivityType, ServerStats, VideoRedundancyStrategyWithManual } from '@shared/models'
12 private static instance: StatsManager
14 private readonly instanceStartDate = new Date()
16 private inboxMessages = {
21 errorsPerType: this.buildAPPerType(),
22 successesPerType: this.buildAPPerType()
25 private constructor () {}
27 updateInboxWaiting (inboxMessagesWaiting: number) {
28 this.inboxMessages.waiting = inboxMessagesWaiting
31 addInboxProcessedSuccess (type: ActivityType) {
32 this.inboxMessages.processed++
33 this.inboxMessages.successes++
34 this.inboxMessages.successesPerType[type]++
37 addInboxProcessedError (type: ActivityType) {
38 this.inboxMessages.processed++
39 this.inboxMessages.errors++
40 this.inboxMessages.errorsPerType[type]++
44 const { totalLocalVideos, totalLocalVideoViews, totalVideos } = await VideoModel.getStats()
45 const { totalLocalVideoComments, totalVideoComments } = await VideoCommentModel.getStats()
46 const { totalUsers, totalDailyActiveUsers, totalWeeklyActiveUsers, totalMonthlyActiveUsers } = await UserModel.getStats()
47 const { totalInstanceFollowers, totalInstanceFollowing } = await ActorFollowModel.getStats()
48 const { totalLocalVideoFilesSize } = await VideoFileModel.getStats()
50 const videosRedundancyStats = await this.buildRedundancyStats()
52 const data: ServerStats = {
55 totalLocalVideoFilesSize,
56 totalLocalVideoComments,
61 totalDailyActiveUsers,
62 totalWeeklyActiveUsers,
63 totalMonthlyActiveUsers,
65 totalInstanceFollowers,
66 totalInstanceFollowing,
68 videosRedundancy: videosRedundancyStats,
70 ...this.buildAPStats()
76 private buildActivityPubMessagesProcessedPerSecond () {
77 const now = new Date()
78 const startedSeconds = (now.getTime() - this.instanceStartDate.getTime()) / 1000
80 return this.inboxMessages.processed / startedSeconds
83 private buildRedundancyStats () {
84 const strategies = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
86 strategy: r.strategy as VideoRedundancyStrategyWithManual,
90 strategies.push({ strategy: 'manual', size: null })
94 return VideoRedundancyModel.getStats(r.strategy)
95 .then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size }))
100 private buildAPPerType () {
117 private buildAPStats () {
119 totalActivityPubMessagesProcessed: this.inboxMessages.processed,
121 totalActivityPubMessagesSuccesses: this.inboxMessages.successes,
123 // Dirty, but simpler and with type checking
124 totalActivityPubCreateMessagesSuccesses: this.inboxMessages.successesPerType.Create,
125 totalActivityPubUpdateMessagesSuccesses: this.inboxMessages.successesPerType.Update,
126 totalActivityPubDeleteMessagesSuccesses: this.inboxMessages.successesPerType.Delete,
127 totalActivityPubFollowMessagesSuccesses: this.inboxMessages.successesPerType.Follow,
128 totalActivityPubAcceptMessagesSuccesses: this.inboxMessages.successesPerType.Accept,
129 totalActivityPubRejectMessagesSuccesses: this.inboxMessages.successesPerType.Reject,
130 totalActivityPubAnnounceMessagesSuccesses: this.inboxMessages.successesPerType.Announce,
131 totalActivityPubUndoMessagesSuccesses: this.inboxMessages.successesPerType.Undo,
132 totalActivityPubLikeMessagesSuccesses: this.inboxMessages.successesPerType.Like,
133 totalActivityPubDislikeMessagesSuccesses: this.inboxMessages.successesPerType.Dislike,
134 totalActivityPubFlagMessagesSuccesses: this.inboxMessages.successesPerType.Flag,
135 totalActivityPubViewMessagesSuccesses: this.inboxMessages.successesPerType.View,
137 totalActivityPubCreateMessagesErrors: this.inboxMessages.errorsPerType.Create,
138 totalActivityPubUpdateMessagesErrors: this.inboxMessages.errorsPerType.Update,
139 totalActivityPubDeleteMessagesErrors: this.inboxMessages.errorsPerType.Delete,
140 totalActivityPubFollowMessagesErrors: this.inboxMessages.errorsPerType.Follow,
141 totalActivityPubAcceptMessagesErrors: this.inboxMessages.errorsPerType.Accept,
142 totalActivityPubRejectMessagesErrors: this.inboxMessages.errorsPerType.Reject,
143 totalActivityPubAnnounceMessagesErrors: this.inboxMessages.errorsPerType.Announce,
144 totalActivityPubUndoMessagesErrors: this.inboxMessages.errorsPerType.Undo,
145 totalActivityPubLikeMessagesErrors: this.inboxMessages.errorsPerType.Like,
146 totalActivityPubDislikeMessagesErrors: this.inboxMessages.errorsPerType.Dislike,
147 totalActivityPubFlagMessagesErrors: this.inboxMessages.errorsPerType.Flag,
148 totalActivityPubViewMessagesErrors: this.inboxMessages.errorsPerType.View,
150 totalActivityPubMessagesErrors: this.inboxMessages.errors,
152 activityPubMessagesProcessedPerSecond: this.buildActivityPubMessagesProcessedPerSecond(),
153 totalActivityPubMessagesWaiting: this.inboxMessages.waiting
157 static get Instance () {
158 return this.instance || (this.instance = new this())
162 // ---------------------------------------------------------------------------