AdminController.js 4.9 KB

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