Browse Source

Add totalLocalVideoFilesSize in stats

Chocobozzz 5 years ago
parent
commit
44b9c0ba31

+ 5 - 2
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,

+ 1 - 1
server/models/redundancy/video-redundancy.ts

@@ -395,7 +395,7 @@ export class VideoRedundancyModel extends Model<VideoRedundancyModel> {
       ]
     }
 
-    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,

+ 20 - 0
server/models/video/video-file.ts

@@ -120,6 +120,26 @@ export class VideoFileModel extends Model<VideoFileModel> {
     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 &&

+ 2 - 1
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)

+ 1 - 0
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