From 99afa081bc6ae7f34b2105075bd43e3625434fa8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 15 Dec 2020 13:34:58 +0100 Subject: Add AP stats --- server/lib/stat-manager.ts | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 server/lib/stat-manager.ts (limited to 'server/lib/stat-manager.ts') diff --git a/server/lib/stat-manager.ts b/server/lib/stat-manager.ts new file mode 100644 index 000000000..f9d69b0dc --- /dev/null +++ b/server/lib/stat-manager.ts @@ -0,0 +1,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 +} -- cgit v1.2.3