AdminController.js 4.8 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 EditorRealTimeController = require('../Editor/EditorRealTimeController')
  32. const SystemMessageManager = require('../SystemMessages/SystemMessageManager')
  33. const oneMinInMs = 60 * 1000
  34. var updateOpenConnetionsMetrics = function() {
  35. metrics.gauge(
  36. 'open_connections.socketio',
  37. __guard__(
  38. __guard__(
  39. __guard__(require('../../infrastructure/Server').io, x2 => x2.sockets),
  40. x1 => x1.clients()
  41. ),
  42. x => x.length
  43. )
  44. )
  45. metrics.gauge(
  46. 'open_connections.http',
  47. _.size(__guard__(require('http').globalAgent, x3 => x3.sockets))
  48. )
  49. metrics.gauge(
  50. 'open_connections.https',
  51. _.size(__guard__(require('https').globalAgent, x4 => x4.sockets))
  52. )
  53. return setTimeout(updateOpenConnetionsMetrics, oneMinInMs)
  54. }
  55. setTimeout(updateOpenConnetionsMetrics, oneMinInMs)
  56. module.exports = AdminController = {
  57. index: (req, res, next) => {
  58. let agents, url
  59. let agent
  60. const http = require('http')
  61. const openSockets = {}
  62. const object = require('http').globalAgent.sockets
  63. for (url in object) {
  64. agents = object[url]
  65. openSockets[`http://${url}`] = (() => {
  66. const result = []
  67. for (agent of Array.from(agents)) {
  68. result.push(agent._httpMessage.path)
  69. }
  70. return result
  71. })()
  72. }
  73. const object1 = require('https').globalAgent.sockets
  74. for (url in object1) {
  75. agents = object1[url]
  76. openSockets[`https://${url}`] = (() => {
  77. const result1 = []
  78. for (agent of Array.from(agents)) {
  79. result1.push(agent._httpMessage.path)
  80. }
  81. return result1
  82. })()
  83. }
  84. return SystemMessageManager.getMessagesFromDB(function(
  85. error,
  86. systemMessages
  87. ) {
  88. if (error != null) {
  89. return next(error)
  90. }
  91. return res.render('admin/index', {
  92. title: 'System Admin',
  93. openSockets,
  94. systemMessages
  95. })
  96. })
  97. },
  98. registerNewUser(req, res, next) {
  99. return res.render('admin/register')
  100. },
  101. dissconectAllUsers: (req, res) => {
  102. logger.warn('disconecting everyone')
  103. EditorRealTimeController.emitToAll(
  104. 'forceDisconnect',
  105. 'Sorry, we are performing a quick update to the editor and need to close it down. Please refresh the page to continue.'
  106. )
  107. return res.sendStatus(200)
  108. },
  109. closeEditor(req, res) {
  110. logger.warn('closing editor')
  111. Settings.editorIsOpen = req.body.isOpen
  112. return res.sendStatus(200)
  113. },
  114. writeAllToMongo(req, res) {
  115. logger.log('writing all docs to mongo')
  116. Settings.mongo.writeAll = true
  117. return DocumentUpdaterHandler.flushAllDocsToMongo(function() {
  118. logger.log('all docs have been saved to mongo')
  119. return res.send()
  120. })
  121. },
  122. flushProjectToTpds(req, res) {
  123. return projectEntityHandler.flushProjectToThirdPartyDataStore(
  124. req.body.project_id,
  125. err => 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. }