PeerTube/server/controllers/api/server/stats.ts

64 lines
2.7 KiB
TypeScript
Raw Normal View History

2018-02-28 18:04:46 +01:00
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'
2018-09-14 14:57:59 +02:00
import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
2019-04-11 11:33:44 +02:00
import { ROUTE_CACHE_LIFETIME } from '../../../initializers/constants'
2018-09-14 14:57:59 +02:00
import { cacheRoute } from '../../../middlewares/cache'
2019-01-15 09:45:54 +01:00
import { VideoFileModel } from '../../../models/video/video-file'
2019-04-11 11:33:44 +02:00
import { CONFIG } from '../../../initializers/config'
2020-01-10 10:11:28 +01:00
import { VideoRedundancyStrategyWithManual } from '@shared/models'
2018-02-28 18:04:46 +01:00
const statsRouter = express.Router()
statsRouter.get('/stats',
asyncMiddleware(cacheRoute()(ROUTE_CACHE_LIFETIME.STATS)),
2018-02-28 18:04:46 +01:00
asyncMiddleware(getStats)
)
2019-01-15 09:45:54 +01:00
async function getStats (req: express.Request, res: express.Response) {
2018-02-28 18:04:46 +01:00
const { totalLocalVideos, totalLocalVideoViews, totalVideos } = await VideoModel.getStats()
const { totalLocalVideoComments, totalVideoComments } = await VideoCommentModel.getStats()
const { totalUsers } = await UserModel.getStats()
const { totalInstanceFollowers, totalInstanceFollowing } = await ActorFollowModel.getStats()
2019-01-15 09:45:54 +01:00
const { totalLocalVideoFilesSize } = await VideoFileModel.getStats()
2018-02-28 18:04:46 +01:00
2020-01-10 10:11:28 +01:00
const strategies: { strategy: VideoRedundancyStrategyWithManual, size: number }[] = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
.map(r => ({
strategy: r.strategy,
size: r.size
}))
strategies.push({ strategy: 'manual', size: null })
2018-09-14 14:57:59 +02:00
const videosRedundancyStats = await Promise.all(
2020-01-10 10:11:28 +01:00
strategies.map(r => {
2018-09-14 14:57:59 +02:00
return VideoRedundancyModel.getStats(r.strategy)
.then(stats => Object.assign(stats, { strategy: r.strategy, totalSize: r.size }))
})
)
2018-02-28 18:04:46 +01:00
const data: ServerStats = {
totalLocalVideos,
totalLocalVideoViews,
2019-01-15 09:45:54 +01:00
totalLocalVideoFilesSize,
2018-02-28 18:04:46 +01:00
totalLocalVideoComments,
2019-01-15 09:45:54 +01:00
totalVideos,
2018-02-28 18:04:46 +01:00
totalVideoComments,
totalUsers,
totalInstanceFollowers,
2018-09-14 14:57:59 +02:00
totalInstanceFollowing,
videosRedundancy: videosRedundancyStats
2018-02-28 18:04:46 +01:00
}
return res.json(data).end()
}
// ---------------------------------------------------------------------------
export {
statsRouter
}