]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/server/stats.ts
Merge branch 'release/2.1.0' into develop
[github/Chocobozzz/PeerTube.git] / server / controllers / api / server / stats.ts
1 import * as express from 'express'
2 import { ServerStats } from '../../../../shared/models/server/server-stats.model'
3 import { asyncMiddleware } from '../../../middlewares'
4 import { UserModel } from '../../../models/account/user'
5 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
6 import { VideoModel } from '../../../models/video/video'
7 import { VideoCommentModel } from '../../../models/video/video-comment'
8 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
9 import { ROUTE_CACHE_LIFETIME } from '../../../initializers/constants'
10 import { cacheRoute } from '../../../middlewares/cache'
11 import { VideoFileModel } from '../../../models/video/video-file'
12 import { CONFIG } from '../../../initializers/config'
13 import { VideoRedundancyStrategyWithManual } from '@shared/models'
14
15 const statsRouter = express.Router()
16
17 statsRouter.get('/stats',
18 asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.STATS)),
19 asyncMiddleware(getStats)
20 )
21
22 async function getStats (req: express.Request, res: express.Response) {
23 const { totalLocalVideos, totalLocalVideoViews, totalVideos } = await VideoModel.getStats()
24 const { totalLocalVideoComments, totalVideoComments } = await VideoCommentModel.getStats()
25 const { totalUsers } = await UserModel.getStats()
26 const { totalInstanceFollowers, totalInstanceFollowing } = await ActorFollowModel.getStats()
27 const { totalLocalVideoFilesSize } = await VideoFileModel.getStats()
28
29 const strategies: { strategy: VideoRedundancyStrategyWithManual, size: number }[] = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
30 .map(r => ({
31 strategy: r.strategy,
32 size: r.size
33 }))
34 strategies.push({ strategy: 'manual', size: null })
35
36 const videosRedundancyStats = await Promise.all(
37 strategies.map(r => {
38 return VideoRedundancyModel.getStats(r.strategy)
39 .then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size }))
40 })
41 )
42
43 const data: ServerStats = {
44 totalLocalVideos,
45 totalLocalVideoViews,
46 totalLocalVideoFilesSize,
47 totalLocalVideoComments,
48 totalVideos,
49 totalVideoComments,
50 totalUsers,
51 totalInstanceFollowers,
52 totalInstanceFollowing,
53 videosRedundancy: videosRedundancyStats
54 }
55
56 return res.json(data).end()
57 }
58
59 // ---------------------------------------------------------------------------
60
61 export {
62 statsRouter
63 }