AdminController.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* eslint-disable
  2. camelcase,
  3. node/handle-callback-err,
  4. max-len
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS103: Rewrite code to no longer use __guard__
  13. * DS205: Consider reworking code to avoid use of IIFEs
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. const metrics = require('@overleaf/metrics')
  18. const logger = require('logger-sharelatex')
  19. const _ = require('underscore')
  20. const DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
  21. const Settings = require('@overleaf/settings')
  22. const TpdsUpdateSender = require('../ThirdPartyDataStore/TpdsUpdateSender')
  23. const TpdsProjectFlusher = require('../ThirdPartyDataStore/TpdsProjectFlusher')
  24. const EditorRealTimeController = require('../Editor/EditorRealTimeController')
  25. const SystemMessageManager = require('../SystemMessages/SystemMessageManager')
  26. const oneMinInMs = 60 * 1000
  27. var updateOpenConnetionsMetrics = function () {
  28. metrics.gauge(
  29. 'open_connections.socketio',
  30. __guard__(
  31. __guard__(
  32. __guard__(require('../../infrastructure/Server').io, x2 => x2.sockets),
  33. x1 => x1.clients()
  34. ),
  35. x => x.length
  36. )
  37. )
  38. metrics.gauge(
  39. 'open_connections.http',
  40. _.size(__guard__(require('http').globalAgent, x3 => x3.sockets))
  41. )
  42. metrics.gauge(
  43. 'open_connections.https',
  44. _.size(__guard__(require('https').globalAgent, x4 => x4.sockets))
  45. )
  46. return setTimeout(updateOpenConnetionsMetrics, oneMinInMs)
  47. }
  48. setTimeout(updateOpenConnetionsMetrics, oneMinInMs)
  49. const AdminController = {
  50. index: (req, res, next) => {
  51. let agents, url
  52. let agent
  53. const openSockets = {}
  54. const object = require('http').globalAgent.sockets
  55. for (url in object) {
  56. agents = object[url]
  57. openSockets[`http://${url}`] = (() => {
  58. const result = []
  59. for (agent of Array.from(agents)) {
  60. result.push(agent._httpMessage.path)
  61. }
  62. return result
  63. })()
  64. }
  65. const object1 = require('https').globalAgent.sockets
  66. for (url in object1) {
  67. agents = object1[url]
  68. openSockets[`https://${url}`] = (() => {
  69. const result1 = []
  70. for (agent of Array.from(agents)) {
  71. result1.push(agent._httpMessage.path)
  72. }
  73. return result1
  74. })()
  75. }
  76. return SystemMessageManager.getMessagesFromDB(function (
  77. error,
  78. systemMessages
  79. ) {
  80. if (error != null) {
  81. return next(error)
  82. }
  83. return res.render('admin/index', {
  84. title: 'System Admin',
  85. openSockets,
  86. systemMessages,
  87. })
  88. })
  89. },
  90. registerNewUser(req, res, next) {
  91. return res.render('admin/register')
  92. },
  93. disconnectAllUsers: (req, res) => {
  94. logger.warn('disconecting everyone')
  95. const delay = (req.query && req.query.delay) > 0 ? req.query.delay : 10
  96. EditorRealTimeController.emitToAll(
  97. 'forceDisconnect',
  98. 'Sorry, we are performing a quick update to the editor and need to close it down. Please refresh the page to continue.',
  99. delay
  100. )
  101. return res.sendStatus(200)
  102. },
  103. unregisterServiceWorker: (req, res) => {
  104. logger.warn('unregistering service worker for all users')
  105. EditorRealTimeController.emitToAll('unregisterServiceWorker')
  106. return res.sendStatus(200)
  107. },
  108. openEditor(req, res) {
  109. logger.warn('opening editor')
  110. Settings.editorIsOpen = true
  111. return res.sendStatus(200)
  112. },
  113. closeEditor(req, res) {
  114. logger.warn('closing editor')
  115. Settings.editorIsOpen = req.body.isOpen
  116. return res.sendStatus(200)
  117. },
  118. writeAllToMongo(req, res) {
  119. logger.log('writing all docs to mongo')
  120. Settings.mongo.writeAll = true
  121. return DocumentUpdaterHandler.flushAllDocsToMongo(function () {
  122. logger.log('all docs have been saved to mongo')
  123. return res.sendStatus(200)
  124. })
  125. },
  126. flushProjectToTpds(req, res) {
  127. return TpdsProjectFlusher.flushProjectToTpds(req.body.project_id, err =>
  128. res.sendStatus(200)
  129. )
  130. },
  131. pollDropboxForUser(req, res) {
  132. const { user_id } = req.body
  133. return TpdsUpdateSender.pollDropboxForUser(user_id, () =>
  134. res.sendStatus(200)
  135. )
  136. },
  137. createMessage(req, res, next) {
  138. return SystemMessageManager.createMessage(
  139. req.body.content,
  140. function (error) {
  141. if (error != null) {
  142. return next(error)
  143. }
  144. return res.sendStatus(200)
  145. }
  146. )
  147. },
  148. clearMessages(req, res, next) {
  149. return SystemMessageManager.clearMessages(function (error) {
  150. if (error != null) {
  151. return next(error)
  152. }
  153. return res.sendStatus(200)
  154. })
  155. },
  156. }
  157. function __guard__(value, transform) {
  158. return typeof value !== 'undefined' && value !== null
  159. ? transform(value)
  160. : undefined
  161. }
  162. module.exports = AdminController