overleaf/services/track-changes/app/coffee/HttpController.coffee

138 lines
4.6 KiB
CoffeeScript
Raw Normal View History

UpdatesManager = require "./UpdatesManager"
DiffManager = require "./DiffManager"
2015-06-04 11:23:49 -04:00
PackManager = require "./PackManager"
2014-03-10 12:58:26 -04:00
RestoreManager = require "./RestoreManager"
logger = require "logger-sharelatex"
2015-10-19 05:59:39 -04:00
HealthChecker = require "./HealthChecker"
2016-01-27 10:14:23 -05:00
_ = require "underscore"
module.exports = HttpController =
2014-03-21 11:57:17 -04:00
flushDoc: (req, res, next = (error) ->) ->
doc_id = req.params.doc_id
2014-03-19 12:40:55 -04:00
project_id = req.params.project_id
2014-03-21 11:57:17 -04:00
logger.log project_id: project_id, doc_id: doc_id, "compressing doc history"
2014-03-19 12:40:55 -04:00
UpdatesManager.processUncompressedUpdatesWithLock project_id, doc_id, (error) ->
return next(error) if error?
res.send 204
2014-03-21 11:57:17 -04:00
flushProject: (req, res, next = (error) ->) ->
project_id = req.params.project_id
logger.log project_id: project_id, "compressing project history"
UpdatesManager.processUncompressedUpdatesForProject project_id, (error) ->
return next(error) if error?
res.send 204
flushAll: (req, res, next = (error) ->) ->
2017-04-20 06:01:46 -04:00
# limit on projects to flush or -1 for all (default)
limit = if req.query.limit? then parseInt(req.query.limit, 10) else -1
logger.log {limit: limit}, "flushing all projects"
UpdatesManager.flushAll limit, (error, result) ->
return next(error) if error?
2017-04-20 06:01:46 -04:00
{failed, succeeded, all} = result
status = "#{succeeded.length} succeeded, #{failed.length} failed"
2017-04-20 06:01:46 -04:00
if limit == 0
res.status(200).send "#{status}\nwould flush:\n#{all.join('\n')}\n"
else if failed.length > 0
logger.log {failed: failed, succeeded: succeeded}, "error flushing projects"
res.status(500).send "#{status}\nfailed to flush:\n#{failed.join('\n')}\n"
else
2017-04-20 06:55:32 -04:00
res.status(200).send "#{status}\nflushed #{succeeded.length} projects of #{all.length}\n"
2017-04-13 06:31:45 -04:00
checkDanglingUpdates: (req, res, next = (error) ->) ->
logger.log "checking dangling updates"
UpdatesManager.getDanglingUpdates (error, result) ->
return next(error) if error?
if result.length > 0
logger.log {dangling: result}, "found dangling updates"
res.status(500).send "dangling updates:\n#{result.join('\n')}\n"
else
res.status(200).send "no dangling updates found\n"
2015-12-09 09:57:04 -05:00
checkDoc: (req, res, next = (error) ->) ->
doc_id = req.params.doc_id
project_id = req.params.project_id
logger.log project_id: project_id, doc_id: doc_id, "checking doc history"
DiffManager.getDocumentBeforeVersion project_id, doc_id, 1, (error, document, rewoundUpdates) ->
return next(error) if error?
broken = []
for update in rewoundUpdates
for op in update.op when op.broken is true
broken.push op
if broken.length > 0
res.send broken
else
res.send 204
getDiff: (req, res, next = (error) ->) ->
doc_id = req.params.doc_id
project_id = req.params.project_id
if req.query.from?
from = parseInt(req.query.from, 10)
else
from = null
if req.query.to?
to = parseInt(req.query.to, 10)
else
to = null
logger.log {project_id, doc_id, from, to}, "getting diff"
DiffManager.getDiff project_id, doc_id, from, to, (error, diff) ->
return next(error) if error?
res.json {diff: diff}
getUpdates: (req, res, next = (error) ->) ->
project_id = req.params.project_id
if req.query.before?
before = parseInt(req.query.before, 10)
if req.query.min_count?
min_count = parseInt(req.query.min_count, 10)
UpdatesManager.getSummarizedProjectUpdates project_id, before: before, min_count: min_count, (error, updates, nextBeforeTimestamp) ->
return next(error) if error?
res.json {
updates: updates
nextBeforeTimestamp: nextBeforeTimestamp
}
2014-03-11 13:39:40 -04:00
2014-03-10 12:58:26 -04:00
restore: (req, res, next = (error) ->) ->
{doc_id, project_id, version} = req.params
user_id = req.headers["x-user-id"]
2014-03-10 12:58:26 -04:00
version = parseInt(version, 10)
RestoreManager.restoreToBeforeVersion project_id, doc_id, version, user_id, (error) ->
2014-03-10 12:58:26 -04:00
return next(error) if error?
res.send 204
2015-08-06 10:11:43 -04:00
2016-03-09 11:56:49 -05:00
pushDocHistory: (req, res, next = (error) ->) ->
project_id = req.params.project_id
doc_id = req.params.doc_id
logger.log {project_id, doc_id}, "pushing all finalised changes to s3"
PackManager.pushOldPacks project_id, doc_id, (error) ->
return next(error) if error?
res.send 204
pullDocHistory: (req, res, next = (error) ->) ->
project_id = req.params.project_id
doc_id = req.params.doc_id
logger.log {project_id, doc_id}, "pulling all packs from s3"
PackManager.pullOldPacks project_id, doc_id, (error) ->
return next(error) if error?
res.send 204
2015-10-19 05:59:39 -04:00
healthCheck: (req, res)->
HealthChecker.check (err)->
if err?
logger.err err:err, "error performing health check"
res.send 500
else
res.send 200
2015-10-29 06:52:23 -04:00
checkLock: (req, res)->
HealthChecker.checkLock (err) ->
2015-10-29 06:52:23 -04:00
if err?
logger.err err:err, "error performing lock check"
res.send 500
else
res.send 200