aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/server/stats.ts
blob: 6d508a481d7beb67024bb15ce688341309823745 (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
import * as express from 'express'
import { ServerStats } from '../../../../shared/models/server/server-stats.model'
import { asyncMiddleware } from '../../../middlewares'
import { UserModel } from '../../../models/account/user'
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
import { VideoModel } from '../../../models/video/video'
import { VideoCommentModel } from '../../../models/video/video-comment'
import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
import { ROUTE_CACHE_LIFETIME } from '../../../initializers/constants'
import { cacheRoute } from '../../../middlewares/cache'
import { VideoFileModel } from '../../../models/video/video-file'
import { CONFIG } from '../../../initializers/config'
import { VideoRedundancyStrategyWithManual } from '@shared/models'

const statsRouter = express.Router()

statsRouter.get('/stats',
  asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.STATS)),
  asyncMiddleware(getStats)
)

async function getStats (req: express.Request, res: express.Response) {
  const { totalLocalVideos, totalLocalVideoViews, totalVideos } = await VideoModel.getStats()
  const { totalLocalVideoComments, totalVideoComments } = await VideoCommentModel.getStats()
  const { totalUsers } = await UserModel.getStats()
  const { totalInstanceFollowers, totalInstanceFollowing } = await ActorFollowModel.getStats()
  const { totalLocalVideoFilesSize } = await VideoFileModel.getStats()

  const strategies: { strategy: VideoRedundancyStrategyWithManual, size: number }[] = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
                                                                                            .map(r => ({
                                                                                              strategy: r.strategy,
                                                                                              size: r.size
                                                                                            }))
  strategies.push({ strategy: 'manual', size: null })

  const videosRedundancyStats = await Promise.all(
    strategies.map(r => {
      return VideoRedundancyModel.getStats(r.strategy)
        .then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size }))
    })
  )

  const data: ServerStats = {
    totalLocalVideos,
    totalLocalVideoViews,
    totalLocalVideoFilesSize,
    totalLocalVideoComments,
    totalVideos,
    totalVideoComments,
    totalUsers,
    totalInstanceFollowers,
    totalInstanceFollowing,
    videosRedundancy: videosRedundancyStats
  }

  return res.json(data).end()
}

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

export {
  statsRouter
}