aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/stat-manager.ts
blob: f9d69b0dc733d26b258efa7c3ea768e5f5f79549 (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
import { CONFIG } from '@server/initializers/config'
import { UserModel } from '@server/models/account/user'
import { ActorFollowModel } from '@server/models/activitypub/actor-follow'
import { VideoRedundancyModel } from '@server/models/redundancy/video-redundancy'
import { VideoModel } from '@server/models/video/video'
import { VideoCommentModel } from '@server/models/video/video-comment'
import { VideoFileModel } from '@server/models/video/video-file'
import { ServerStats, VideoRedundancyStrategyWithManual } from '@shared/models'

class StatsManager {

  private static instance: StatsManager

  private readonly instanceStartDate = new Date()

  private inboxMessagesProcessed = 0
  private inboxMessagesWaiting = 0

  private constructor () {}

  updateInboxStats (inboxMessagesProcessed: number, inboxMessagesWaiting: number) {
    this.inboxMessagesProcessed = inboxMessagesProcessed
    this.inboxMessagesWaiting = inboxMessagesWaiting
  }

  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 videosRedundancyStats = await this.buildRedundancyStats()

    const data: ServerStats = {
      totalLocalVideos,
      totalLocalVideoViews,
      totalLocalVideoFilesSize,
      totalLocalVideoComments,
      totalVideos,
      totalVideoComments,

      totalUsers,
      totalDailyActiveUsers,
      totalWeeklyActiveUsers,
      totalMonthlyActiveUsers,

      totalInstanceFollowers,
      totalInstanceFollowing,

      videosRedundancy: videosRedundancyStats,

      totalActivityPubMessagesProcessed: this.inboxMessagesProcessed,
      activityPubMessagesProcessedPerSecond: this.buildActivityPubMessagesProcessedPerSecond(),
      totalActivityPubMessagesWaiting: this.inboxMessagesWaiting
    }

    return data
  }

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

    return this.inboxMessagesProcessed / 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 Promise.all(
      strategies.map(r => {
        return VideoRedundancyModel.getStats(r.strategy)
          .then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size }))
      })
    )
  }

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

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

export {
  StatsManager
}