From 5c44fb8e9dac820bcf3d6893eb388b99e07dcfd8 Mon Sep 17 00:00:00 2001 From: M Fahru Date: Tue, 14 May 2024 08:34:18 -0700 Subject: [PATCH] Merge pull request #18281 from overleaf/mf-decaffeinate-server-admin-controller [web][admin panel] Decaf cleanup of AdminController GitOrigin-RevId: fca50565e140435b833c303d2a0dfac7449c3e17 --- .../Features/ServerAdmin/AdminController.js | 162 +++++------------- 1 file changed, 40 insertions(+), 122 deletions(-) diff --git a/services/web/app/src/Features/ServerAdmin/AdminController.js b/services/web/app/src/Features/ServerAdmin/AdminController.js index f3c5a01431..e204638974 100644 --- a/services/web/app/src/Features/ServerAdmin/AdminController.js +++ b/services/web/app/src/Features/ServerAdmin/AdminController.js @@ -1,62 +1,11 @@ -/* eslint-disable - n/handle-callback-err, - max-len -*/ -// TODO: This file was created by bulk-decaffeinate. -// Fix any style issues and re-enable lint. -/* - * decaffeinate suggestions: - * DS101: Remove unnecessary use of Array.from - * DS102: Remove unnecessary code created because of implicit returns - * DS103: Rewrite code to no longer use __guard__ - * DS205: Consider reworking code to avoid use of IIFEs - * DS207: Consider shorter variations of null checks - * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md - */ - -const metrics = require('@overleaf/metrics') const logger = require('@overleaf/logger') -const _ = require('lodash') -const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler') +const http = require('http') +const https = require('https') const Settings = require('@overleaf/settings') const TpdsUpdateSender = require('../ThirdPartyDataStore/TpdsUpdateSender') const TpdsProjectFlusher = require('../ThirdPartyDataStore/TpdsProjectFlusher') const EditorRealTimeController = require('../Editor/EditorRealTimeController') const SystemMessageManager = require('../SystemMessages/SystemMessageManager') -const { - addOptionalCleanupHandlerAfterDrainingConnections, -} = require('../../infrastructure/GracefulShutdown') - -const oneMinInMs = 60 * 1000 - -function updateOpenConnetionsMetrics() { - metrics.gauge( - 'open_connections.socketio', - __guard__( - __guard__( - __guard__(require('../../infrastructure/Server').io, x2 => x2.sockets), - x1 => x1.clients() - ), - x => x.length - ) - ) - metrics.gauge( - 'open_connections.http', - _.size(__guard__(require('http').globalAgent, x3 => x3.sockets)) - ) - metrics.gauge( - 'open_connections.https', - _.size(__guard__(require('https').globalAgent, x4 => x4.sockets)) - ) -} - -const intervalHandle = setInterval(updateOpenConnetionsMetrics, oneMinInMs) -addOptionalCleanupHandlerAfterDrainingConnections( - 'collect connection metrics', - () => { - clearInterval(intervalHandle) - } -) const AdminController = { _sendDisconnectAllUsersMessage: delay => { @@ -67,113 +16,82 @@ const AdminController = { ) }, index: (req, res, next) => { - let agents, url - let agent + let url const openSockets = {} - const object = require('http').globalAgent.sockets - for (url in object) { - agents = object[url] - openSockets[`http://${url}`] = (() => { - const result = [] - for (agent of Array.from(agents)) { - result.push(agent._httpMessage.path) - } - return result - })() - } - const object1 = require('https').globalAgent.sockets - for (url in object1) { - agents = object1[url] - openSockets[`https://${url}`] = (() => { - const result1 = [] - for (agent of Array.from(agents)) { - result1.push(agent._httpMessage.path) - } - return result1 - })() + for (url in http.globalAgent.sockets) { + openSockets[`http://${url}`] = http.globalAgent.sockets[url].map( + socket => socket._httpMessage.path + ) } - return SystemMessageManager.getMessagesFromDB( - function (error, systemMessages) { - if (error != null) { - return next(error) - } - return res.render('admin/index', { - title: 'System Admin', - openSockets, - systemMessages, - }) + for (url in https.globalAgent.sockets) { + openSockets[`https://${url}`] = https.globalAgent.sockets[url].map( + socket => socket._httpMessage.path + ) + } + + SystemMessageManager.getMessagesFromDB(function (error, systemMessages) { + if (error) { + return next(error) } - ) + res.render('admin/index', { + title: 'System Admin', + openSockets, + systemMessages, + }) + }) }, disconnectAllUsers: (req, res) => { logger.warn('disconecting everyone') const delay = (req.query && req.query.delay) > 0 ? req.query.delay : 10 AdminController._sendDisconnectAllUsersMessage(delay) - return res.sendStatus(200) + res.sendStatus(200) }, openEditor(req, res) { logger.warn('opening editor') Settings.editorIsOpen = true - return res.sendStatus(200) + res.sendStatus(200) }, closeEditor(req, res) { logger.warn('closing editor') Settings.editorIsOpen = req.body.isOpen - return res.sendStatus(200) + res.sendStatus(200) }, - writeAllToMongo(req, res) { - logger.debug('writing all docs to mongo') - Settings.mongo.writeAll = true - return DocumentUpdaterHandler.flushAllDocsToMongo(function () { - logger.debug('all docs have been saved to mongo') - return res.sendStatus(200) - }) - }, - - flushProjectToTpds(req, res) { - return TpdsProjectFlusher.flushProjectToTpds(req.body.project_id, err => + flushProjectToTpds(req, res, next) { + TpdsProjectFlusher.flushProjectToTpds(req.body.project_id, error => { + if (error) { + return next(error) + } res.sendStatus(200) - ) + }) }, pollDropboxForUser(req, res) { const { user_id: userId } = req.body - return TpdsUpdateSender.pollDropboxForUser(userId, () => - res.sendStatus(200) - ) + TpdsUpdateSender.pollDropboxForUser(userId, () => res.sendStatus(200)) }, createMessage(req, res, next) { - return SystemMessageManager.createMessage( - req.body.content, - function (error) { - if (error != null) { - return next(error) - } - return res.sendStatus(200) + SystemMessageManager.createMessage(req.body.content, function (error) { + if (error) { + return next(error) } - ) + res.sendStatus(200) + }) }, clearMessages(req, res, next) { - return SystemMessageManager.clearMessages(function (error) { - if (error != null) { + SystemMessageManager.clearMessages(function (error) { + if (error) { return next(error) } - return res.sendStatus(200) + res.sendStatus(200) }) }, } -function __guard__(value, transform) { - return typeof value !== 'undefined' && value !== null - ? transform(value) - : undefined -} - module.exports = AdminController