]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Add totalLocalVideoFilesSize in stats
authorChocobozzz <me@florianbigard.com>
Tue, 15 Jan 2019 08:45:54 +0000 (09:45 +0100)
committerChocobozzz <me@florianbigard.com>
Tue, 15 Jan 2019 08:45:54 +0000 (09:45 +0100)
server/controllers/api/server/stats.ts
server/models/redundancy/video-redundancy.ts
server/models/video/video-file.ts
server/tests/api/server/stats.ts
shared/models/server/server-stats.model.ts

index 85803f69ee87e4559f4dbe56bf14f1688122e649..89ffd1717a8e609de92c68823e76f2864f97553f 100644 (file)
@@ -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,
index 8b6cd146abbf90fe232bd64276e232c561ffe37b..8f2ef2d9ac4206ebe1b8ce3905fed6ad4f3bc968 100644 (file)
@@ -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,
index 0fd868cd61a33e9a5fdf2c649bc67e65b4376245..1f1b76c1e10e0621dffe5ef18af13c64fa07747f 100644 (file)
@@ -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 &&
index 517b4e54298c2e06317442118ed2eadf8992cf95..9858e2b15a566d0fbed9f1dfc21fc1192f3e6571 100644 (file)
@@ -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)
index a6bd2d4d35ee2756ba05c2e535f83233bbc629ce..74f3de5d3094724e29d7de55efc6c12e10c6f70d 100644 (file)
@@ -5,6 +5,7 @@ export interface ServerStats {
   totalLocalVideos: number
   totalLocalVideoViews: number
   totalLocalVideoComments: number
+  totalLocalVideoFilesSize: number
 
   totalVideos: number
   totalVideoComments: number