]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/server/debug.ts
Improve viewer counter
[github/Chocobozzz/PeerTube.git] / server / controllers / api / server / debug.ts
index 4450038f6a71467615a041a1ecc9f2541957cff4..e09510dc3754cfb08657dcb03a1da188a91aba53 100644 (file)
@@ -1,13 +1,25 @@
-import * as express from 'express'
+import express from 'express'
+import { InboxManager } from '@server/lib/activitypub/inbox-manager'
+import { RemoveDanglingResumableUploadsScheduler } from '@server/lib/schedulers/remove-dangling-resumable-uploads-scheduler'
+import { VideoViewsBufferScheduler } from '@server/lib/schedulers/video-views-buffer-scheduler'
+import { VideoViewsManager } from '@server/lib/views/video-views-manager'
+import { Debug, SendDebugCommand } from '@shared/models'
+import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
 import { UserRight } from '../../../../shared/models/users'
-import { asyncMiddleware, authenticate, ensureUserHasRight } from '../../../middlewares'
+import { authenticate, ensureUserHasRight } from '../../../middlewares'
 
 const debugRouter = express.Router()
 
 debugRouter.get('/debug',
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_DEBUG),
-  asyncMiddleware(getDebug)
+  getDebug
+)
+
+debugRouter.post('/debug/run-command',
+  authenticate,
+  ensureUserHasRight(UserRight.MANAGE_DEBUG),
+  runCommand
 )
 
 // ---------------------------------------------------------------------------
@@ -18,8 +30,23 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function getDebug (req: express.Request, res: express.Response) {
+function getDebug (req: express.Request, res: express.Response) {
   return res.json({
-    ip: req.ip
-  }).end()
+    ip: req.ip,
+    activityPubMessagesWaiting: InboxManager.Instance.getActivityPubMessagesWaiting()
+  } as Debug)
+}
+
+async function runCommand (req: express.Request, res: express.Response) {
+  const body: SendDebugCommand = req.body
+
+  const processors: { [id in SendDebugCommand['command']]: () => Promise<any> } = {
+    'remove-dandling-resumable-uploads': () => RemoveDanglingResumableUploadsScheduler.Instance.execute(),
+    'process-video-views-buffer': () => VideoViewsBufferScheduler.Instance.execute(),
+    'process-video-viewers': () => VideoViewsManager.Instance.processViewerStats()
+  }
+
+  await processors[body.command]()
+
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }