Check follow constraints when getting a video
[oweals/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 { CONFIG, ROUTE_CACHE_LIFETIME } from '../../../initializers/constants'
10 import { cacheRoute } from '../../../middlewares/cache'
11
12 const statsRouter = express.Router()
13
14 statsRouter.get('/stats',
15   asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.STATS)),
16   asyncMiddleware(getStats)
17 )
18
19 async function getStats (req: express.Request, res: express.Response, next: express.NextFunction) {
20   const { totalLocalVideos, totalLocalVideoViews, totalVideos } = await VideoModel.getStats()
21   const { totalLocalVideoComments, totalVideoComments } = await VideoCommentModel.getStats()
22   const { totalUsers } = await UserModel.getStats()
23   const { totalInstanceFollowers, totalInstanceFollowing } = await ActorFollowModel.getStats()
24
25   const videosRedundancyStats = await Promise.all(
26     CONFIG.REDUNDANCY.VIDEOS.STRATEGIES.map(r => {
27       return VideoRedundancyModel.getStats(r.strategy)
28         .then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size }))
29     })
30   )
31
32   const data: ServerStats = {
33     totalLocalVideos,
34     totalLocalVideoViews,
35     totalVideos,
36     totalLocalVideoComments,
37     totalVideoComments,
38     totalUsers,
39     totalInstanceFollowers,
40     totalInstanceFollowing,
41     videosRedundancy: videosRedundancyStats
42   }
43
44   return res.json(data).end()
45 }
46
47 // ---------------------------------------------------------------------------
48
49 export {
50   statsRouter
51 }