From 44b9c0ba31c4a97e3d874f33226ad935c3a90dd5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 15 Jan 2019 09:45:54 +0100 Subject: [PATCH] Add totalLocalVideoFilesSize in stats --- server/controllers/api/server/stats.ts | 7 +++++-- server/models/redundancy/video-redundancy.ts | 2 +- server/models/video/video-file.ts | 20 ++++++++++++++++++++ server/tests/api/server/stats.ts | 3 ++- shared/models/server/server-stats.model.ts | 1 + 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/server/controllers/api/server/stats.ts b/server/controllers/api/server/stats.ts index 85803f69e..89ffd1717 100644 --- a/server/controllers/api/server/stats.ts +++ b/server/controllers/api/server/stats.ts @@ -8,6 +8,7 @@ import { VideoCommentModel } from '../../../models/video/video-comment' import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../../initializers/constants' import { cacheRoute } from '../../../middlewares/cache' +import { VideoFileModel } from '../../../models/video/video-file' const statsRouter = express.Router() @@ -16,11 +17,12 @@ statsRouter.get('/stats', asyncMiddleware(getStats) ) -async function getStats (req: express.Request, res: express.Response, next: express.NextFunction) { +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 videosRedundancyStats = await Promise.all( CONFIG.REDUNDANCY.VIDEOS.STRATEGIES.map(r => { @@ -32,8 +34,9 @@ async function getStats (req: express.Request, res: express.Response, next: expr const data: ServerStats = { totalLocalVideos, totalLocalVideoViews, - totalVideos, + totalLocalVideoFilesSize, totalLocalVideoComments, + totalVideos, totalVideoComments, totalUsers, totalInstanceFollowers, diff --git a/server/models/redundancy/video-redundancy.ts b/server/models/redundancy/video-redundancy.ts index 8b6cd146a..8f2ef2d9a 100644 --- a/server/models/redundancy/video-redundancy.ts +++ b/server/models/redundancy/video-redundancy.ts @@ -395,7 +395,7 @@ export class VideoRedundancyModel extends Model { ] } - return VideoRedundancyModel.find(query as any) // FIXME: typings + return VideoRedundancyModel.findOne(query as any) // FIXME: typings .then((r: any) => ({ totalUsed: parseInt(r.totalUsed.toString(), 10), totalVideos: r.totalVideos, diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts index 0fd868cd6..1f1b76c1e 100644 --- a/server/models/video/video-file.ts +++ b/server/models/video/video-file.ts @@ -120,6 +120,26 @@ export class VideoFileModel extends Model { return VideoFileModel.findById(id, options) } + static async getStats () { + let totalLocalVideoFilesSize = await VideoFileModel.sum('size', { + include: [ + { + attributes: [], + model: VideoModel.unscoped(), + where: { + remote: false + } + } + ] + } as any) + // Sequelize could return null... + if (!totalLocalVideoFilesSize) totalLocalVideoFilesSize = 0 + + return { + totalLocalVideoFilesSize + } + } + hasSameUniqueKeysThan (other: VideoFileModel) { return this.fps === other.fps && this.resolution === other.resolution && diff --git a/server/tests/api/server/stats.ts b/server/tests/api/server/stats.ts index 517b4e542..9858e2b15 100644 --- a/server/tests/api/server/stats.ts +++ b/server/tests/api/server/stats.ts @@ -39,7 +39,7 @@ describe('Test stats (excluding redundancy)', function () { } await createUser(servers[0].url, servers[0].accessToken, user.username, user.password) - const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, {}) + const resVideo = await uploadVideo(servers[0].url, servers[0].accessToken, { fixture: 'video_short.webm' }) const videoUUID = resVideo.body.video.uuid await addVideoCommentThread(servers[0].url, servers[0].accessToken, videoUUID, 'comment') @@ -60,6 +60,7 @@ describe('Test stats (excluding redundancy)', function () { expect(data.totalLocalVideoComments).to.equal(1) expect(data.totalLocalVideos).to.equal(1) expect(data.totalLocalVideoViews).to.equal(1) + expect(data.totalLocalVideoFilesSize).to.equal(218910) expect(data.totalUsers).to.equal(2) expect(data.totalVideoComments).to.equal(1) expect(data.totalVideos).to.equal(1) diff --git a/shared/models/server/server-stats.model.ts b/shared/models/server/server-stats.model.ts index a6bd2d4d3..74f3de5d3 100644 --- a/shared/models/server/server-stats.model.ts +++ b/shared/models/server/server-stats.model.ts @@ -5,6 +5,7 @@ export interface ServerStats { totalLocalVideos: number totalLocalVideoViews: number totalLocalVideoComments: number + totalLocalVideoFilesSize: number totalVideos: number totalVideoComments: number -- 2.25.1