aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/stat-manager.ts
blob: 3c5e0a93e0046d3940c26b7c5b83672541d00348 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { CONFIG } from '@server/initializers/config'
import { UserModel } from '@server/models/user/user'
import { ActorFollowModel } from '@server/models/actor/actor-follow'
import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
import { VideoModel } from '@server/models/video/video'
import { VideoChannelModel } from '@server/models/video/video-channel'
import { VideoCommentModel } from '@server/models/video/video-comment'
import { VideoFileModel } from '@server/models/video/video-file'
import { VideoPlaylistModel } from '@server/models/video/video-playlist'
import { ActivityType, ServerStats, VideoRedundancyStrategyWithManual } from '@shared/models'
import * as Bluebird from 'bluebird'

class StatsManager {

  private static instance: StatsManager

  private readonly instanceStartDate = new Date()

  private inboxMessages = {
    processed: 0,
    errors: 0,
    successes: 0,
    waiting: 0,
    errorsPerType: this.buildAPPerType(),
    successesPerType: this.buildAPPerType()
  }

  private constructor () {}

  updateInboxWaiting (inboxMessagesWaiting: number) {
    this.inboxMessages.waiting = inboxMessagesWaiting
  }

  addInboxProcessedSuccess (type: ActivityType) {
    this.inboxMessages.processed++
    this.inboxMessages.successes++
    this.inboxMessages.successesPerType[type]++
  }

  addInboxProcessedError (type: ActivityType) {
    this.inboxMessages.processed++
    this.inboxMessages.errors++
    this.inboxMessages.errorsPerType[type]++
  }

  async getStats () {
    const { totalLocalVideos, totalLocalVideoViews, totalVideos } = await VideoModel.getStats()
    const { totalLocalVideoComments, totalVideoComments } = await VideoCommentModel.getStats()
    const { totalUsers, totalDailyActiveUsers, totalWeeklyActiveUsers, totalMonthlyActiveUsers } = await UserModel.getStats()
    const { totalInstanceFollowers, totalInstanceFollowing } = await ActorFollowModel.getStats()
    const { totalLocalVideoFilesSize } = await VideoFileModel.getStats()
    const {
      totalLocalVideoChannels,
      totalLocalDailyActiveVideoChannels,
      totalLocalWeeklyActiveVideoChannels,
      totalLocalMonthlyActiveVideoChannels
    } = await VideoChannelModel.getStats()
    const { totalLocalPlaylists } = await VideoPlaylistModel.getStats()

    const videosRedundancyStats = await this.buildRedundancyStats()

    const data: ServerStats = {
      totalUsers,
      totalDailyActiveUsers,
      totalWeeklyActiveUsers,
      totalMonthlyActiveUsers,

      totalLocalVideos,
      totalLocalVideoViews,
      totalLocalVideoComments,
      totalLocalVideoFilesSize,

      totalVideos,
      totalVideoComments,

      totalLocalVideoChannels,
      totalLocalDailyActiveVideoChannels,
      totalLocalWeeklyActiveVideoChannels,
      totalLocalMonthlyActiveVideoChannels,

      totalLocalPlaylists,

      totalInstanceFollowers,
      totalInstanceFollowing,

      videosRedundancy: videosRedundancyStats,

      ...this.buildAPStats()
    }

    return data
  }

  private buildActivityPubMessagesProcessedPerSecond () {
    const now = new Date()
    const startedSeconds = (now.getTime() - this.instanceStartDate.getTime()) / 1000

    return this.inboxMessages.processed / startedSeconds
  }

  private buildRedundancyStats () {
    const strategies = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
                                               .map(r => ({
                                                 strategy: r.strategy as VideoRedundancyStrategyWithManual,
                                                 size: r.size
                                               }))

    strategies.push({ strategy: 'manual', size: null })

    return Bluebird.mapSeries(strategies, r => {
      return VideoRedundancyModel.getStats(r.strategy)
        .then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size }))
    })
  }

  private buildAPPerType () {
    return {
      Create: 0,
      Update: 0,
      Delete: 0,
      Follow: 0,
      Accept: 0,
      Reject: 0,
      Announce: 0,
      Undo: 0,
      Like: 0,
      Dislike: 0,
      Flag: 0,
      View: 0
    }
  }

  private buildAPStats () {
    return {
      totalActivityPubMessagesProcessed: this.inboxMessages.processed,

      totalActivityPubMessagesSuccesses: this.inboxMessages.successes,

      // Dirty, but simpler and with type checking
      totalActivityPubCreateMessagesSuccesses: this.inboxMessages.successesPerType.Create,
      totalActivityPubUpdateMessagesSuccesses: this.inboxMessages.successesPerType.Update,
      totalActivityPubDeleteMessagesSuccesses: this.inboxMessages.successesPerType.Delete,
      totalActivityPubFollowMessagesSuccesses: this.inboxMessages.successesPerType.Follow,
      totalActivityPubAcceptMessagesSuccesses: this.inboxMessages.successesPerType.Accept,
      totalActivityPubRejectMessagesSuccesses: this.inboxMessages.successesPerType.Reject,
      totalActivityPubAnnounceMessagesSuccesses: this.inboxMessages.successesPerType.Announce,
      totalActivityPubUndoMessagesSuccesses: this.inboxMessages.successesPerType.Undo,
      totalActivityPubLikeMessagesSuccesses: this.inboxMessages.successesPerType.Like,
      totalActivityPubDislikeMessagesSuccesses: this.inboxMessages.successesPerType.Dislike,
      totalActivityPubFlagMessagesSuccesses: this.inboxMessages.successesPerType.Flag,
      totalActivityPubViewMessagesSuccesses: this.inboxMessages.successesPerType.View,

      totalActivityPubCreateMessagesErrors: this.inboxMessages.errorsPerType.Create,
      totalActivityPubUpdateMessagesErrors: this.inboxMessages.errorsPerType.Update,
      totalActivityPubDeleteMessagesErrors: this.inboxMessages.errorsPerType.Delete,
      totalActivityPubFollowMessagesErrors: this.inboxMessages.errorsPerType.Follow,
      totalActivityPubAcceptMessagesErrors: this.inboxMessages.errorsPerType.Accept,
      totalActivityPubRejectMessagesErrors: this.inboxMessages.errorsPerType.Reject,
      totalActivityPubAnnounceMessagesErrors: this.inboxMessages.errorsPerType.Announce,
      totalActivityPubUndoMessagesErrors: this.inboxMessages.errorsPerType.Undo,
      totalActivityPubLikeMessagesErrors: this.inboxMessages.errorsPerType.Like,
      totalActivityPubDislikeMessagesErrors: this.inboxMessages.errorsPerType.Dislike,
      totalActivityPubFlagMessagesErrors: this.inboxMessages.errorsPerType.Flag,
      totalActivityPubViewMessagesErrors: this.inboxMessages.errorsPerType.View,

      totalActivityPubMessagesErrors: this.inboxMessages.errors,

      activityPubMessagesProcessedPerSecond: this.buildActivityPubMessagesProcessedPerSecond(),
      totalActivityPubMessagesWaiting: this.inboxMessages.waiting
    }
  }

  static get Instance () {
    return this.instance || (this.instance = new this())
  }
}

// ---------------------------------------------------------------------------

export {
  StatsManager
}