]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/stat-manager.ts
Refactor auth flow
[github/Chocobozzz/PeerTube.git] / server / lib / stat-manager.ts
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'
9
10 class StatsManager {
11
12 private static instance: StatsManager
13
14 private readonly instanceStartDate = new Date()
15
16 private inboxMessages = {
17 processed: 0,
18 errors: 0,
19 successes: 0,
20 waiting: 0,
21 errorsPerType: this.buildAPPerType(),
22 successesPerType: this.buildAPPerType()
23 }
24
25 private constructor () {}
26
27 updateInboxWaiting (inboxMessagesWaiting: number) {
28 this.inboxMessages.waiting = inboxMessagesWaiting
29 }
30
31 addInboxProcessedSuccess (type: ActivityType) {
32 this.inboxMessages.processed++
33 this.inboxMessages.successes++
34 this.inboxMessages.successesPerType[type]++
35 }
36
37 addInboxProcessedError (type: ActivityType) {
38 this.inboxMessages.processed++
39 this.inboxMessages.errors++
40 this.inboxMessages.errorsPerType[type]++
41 }
42
43 async getStats () {
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()
49
50 const videosRedundancyStats = await this.buildRedundancyStats()
51
52 const data: ServerStats = {
53 totalLocalVideos,
54 totalLocalVideoViews,
55 totalLocalVideoFilesSize,
56 totalLocalVideoComments,
57 totalVideos,
58 totalVideoComments,
59
60 totalUsers,
61 totalDailyActiveUsers,
62 totalWeeklyActiveUsers,
63 totalMonthlyActiveUsers,
64
65 totalInstanceFollowers,
66 totalInstanceFollowing,
67
68 videosRedundancy: videosRedundancyStats,
69
70 ...this.buildAPStats()
71 }
72
73 return data
74 }
75
76 private buildActivityPubMessagesProcessedPerSecond () {
77 const now = new Date()
78 const startedSeconds = (now.getTime() - this.instanceStartDate.getTime()) / 1000
79
80 return this.inboxMessages.processed / startedSeconds
81 }
82
83 private buildRedundancyStats () {
84 const strategies = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
85 .map(r => ({
86 strategy: r.strategy as VideoRedundancyStrategyWithManual,
87 size: r.size
88 }))
89
90 strategies.push({ strategy: 'manual', size: null })
91
92 return Promise.all(
93 strategies.map(r => {
94 return VideoRedundancyModel.getStats(r.strategy)
95 .then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size }))
96 })
97 )
98 }
99
100 private buildAPPerType () {
101 return {
102 Create: 0,
103 Update: 0,
104 Delete: 0,
105 Follow: 0,
106 Accept: 0,
107 Reject: 0,
108 Announce: 0,
109 Undo: 0,
110 Like: 0,
111 Dislike: 0,
112 Flag: 0,
113 View: 0
114 }
115 }
116
117 private buildAPStats () {
118 return {
119 totalActivityPubMessagesProcessed: this.inboxMessages.processed,
120
121 totalActivityPubMessagesSuccesses: this.inboxMessages.successes,
122
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,
136
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,
149
150 totalActivityPubMessagesErrors: this.inboxMessages.errors,
151
152 activityPubMessagesProcessedPerSecond: this.buildActivityPubMessagesProcessedPerSecond(),
153 totalActivityPubMessagesWaiting: this.inboxMessages.waiting
154 }
155 }
156
157 static get Instance () {
158 return this.instance || (this.instance = new this())
159 }
160 }
161
162 // ---------------------------------------------------------------------------
163
164 export {
165 StatsManager
166 }