From 1d1dcdfa2f3347c706ae69965d2b3add3f07b6a5 Mon Sep 17 00:00:00 2001 From: James Allen Date: Tue, 4 Mar 2014 13:02:48 +0000 Subject: [PATCH] Add in methods for retrieving updates and doc version --- services/track-changes/app.coffee | 2 + .../app/coffee/DiffManager.coffee | 8 +++ .../app/coffee/DocumentUpdaterManager.coffee | 21 ++++++++ .../app/coffee/MongoManager.coffee | 16 +++++- .../DocumentUpdaterManagerTests.coffee | 52 +++++++++++++++++++ .../MongoManager/MongoManagerTests.coffee | 30 +++++++++++ 6 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 services/track-changes/app/coffee/DiffManager.coffee create mode 100644 services/track-changes/app/coffee/DocumentUpdaterManager.coffee create mode 100644 services/track-changes/test/unit/coffee/DocumentUpdaterManager/DocumentUpdaterManagerTests.coffee diff --git a/services/track-changes/app.coffee b/services/track-changes/app.coffee index 83c768f889..68e11cc486 100644 --- a/services/track-changes/app.coffee +++ b/services/track-changes/app.coffee @@ -2,6 +2,8 @@ Settings = require "settings-sharelatex" logger = require "logger-sharelatex" logger.initialize("track-changes") +require("./app/js/MongoManager").ensureIndices() + HttpController = require "./app/js/HttpController" express = require "express" app = express() diff --git a/services/track-changes/app/coffee/DiffManager.coffee b/services/track-changes/app/coffee/DiffManager.coffee new file mode 100644 index 0000000000..f1186293e8 --- /dev/null +++ b/services/track-changes/app/coffee/DiffManager.coffee @@ -0,0 +1,8 @@ +module.exports = DiffManager = + getDiff: (doc_id, fromDate, toDate, callback = (error, diff) ->) -> + # Flush diff + # Get doc content and version + # Get updates from Mongo + # Check version matches + # Build diff + # Return diff \ No newline at end of file diff --git a/services/track-changes/app/coffee/DocumentUpdaterManager.coffee b/services/track-changes/app/coffee/DocumentUpdaterManager.coffee new file mode 100644 index 0000000000..0b02a9a16c --- /dev/null +++ b/services/track-changes/app/coffee/DocumentUpdaterManager.coffee @@ -0,0 +1,21 @@ +request = require "request" +logger = require "logger-sharelatex" +Settings = require "settings-sharelatex" + +module.exports = DocumentUpdaterManager = + getDocument: (project_id, doc_id, callback = (error, content, version) ->) -> + url = "#{Settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}" + logger.log project_id:project_id, doc_id: doc_id, "getting doc from document updater" + request.get url, (error, res, body)-> + if error? + return callback(error) + if res.statusCode >= 200 and res.statusCode < 300 + try + body = JSON.parse(body) + catch error + return callback(error) + callback null, body.lines, body.version + else + error = new Error("doc updater returned a non-success status code: #{res.statusCode}") + logger.error err: error, project_id:project_id, doc_id:doc_id, url: url, "error accessing doc updater" + callback error \ No newline at end of file diff --git a/services/track-changes/app/coffee/MongoManager.coffee b/services/track-changes/app/coffee/MongoManager.coffee index dc17f12d76..b194188992 100644 --- a/services/track-changes/app/coffee/MongoManager.coffee +++ b/services/track-changes/app/coffee/MongoManager.coffee @@ -37,4 +37,18 @@ module.exports = MongoManager = op: update.op meta: update.meta v: update.v - }, callback \ No newline at end of file + }, callback + + getUpdatesBetweenDates:(doc_id, fromDate, toDate, callback = (error, updates) ->) -> + db.docHistory + .find({ + doc_id: ObjectId(doc_id.toString()) + "meta.start_ts" : { $gte: fromDate } + "meta.end_ts" : { $lte: toDate } + }) + .sort( "meta.end_ts": -1 ) + .toArray callback + + ensureIndices: (callback = (error) ->) -> + db.docHistory.ensureIndex { doc_id: 1, "meta.start_ts": 1, "meta.end_ts": 1 }, callback + diff --git a/services/track-changes/test/unit/coffee/DocumentUpdaterManager/DocumentUpdaterManagerTests.coffee b/services/track-changes/test/unit/coffee/DocumentUpdaterManager/DocumentUpdaterManagerTests.coffee new file mode 100644 index 0000000000..5941eefd8d --- /dev/null +++ b/services/track-changes/test/unit/coffee/DocumentUpdaterManager/DocumentUpdaterManagerTests.coffee @@ -0,0 +1,52 @@ +sinon = require('sinon') +chai = require('chai') +should = chai.should() +expect = chai.expect +modulePath = "../../../../app/js/DocumentUpdaterManager.js" +SandboxedModule = require('sandboxed-module') + +describe "DocumentUpdaterManager", -> + beforeEach -> + @DocumentUpdaterManager = SandboxedModule.require modulePath, requires: + "request": @request = {} + "logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() } + 'settings-sharelatex': @settings = + apis : documentupdater: url : "http://example.com" + @callback = sinon.stub() + @lines = ["one", "two", "three"] + @version = 42 + + describe "getDocument", -> + describe "successfully", -> + beforeEach -> + @body = JSON.stringify + lines: @lines + version: @version + ops: [] + @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body) + @DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback + + it 'should get the document from the document updater', -> + url = "#{@settings.apis.documentupdater.url}/project/#{@project_id}/doc/#{@doc_id}" + @request.get.calledWith(url).should.equal true + + it "should call the callback with the lines and version", -> + @callback.calledWith(null, @lines, @version, @ops).should.equal true + + describe "when the document updater API returns an error", -> + beforeEach -> + @request.get = sinon.stub().callsArgWith(1, @error = new Error("something went wrong"), null, null) + @DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback + + it "should return an error to the callback", -> + @callback.calledWith(@error).should.equal true + + describe "when the document updater returns a failure error code", -> + beforeEach -> + @request.get = sinon.stub().callsArgWith(1, null, { statusCode: 500 }, "") + @DocumentUpdaterManager.getDocument @project_id, @doc_id, @callback + + it "should return the callback with an error", -> + @callback + .calledWith(new Error("doc updater returned failure status code: 500")) + .should.equal true \ No newline at end of file diff --git a/services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee b/services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee index 0aa43677e4..ab9fb73ddf 100644 --- a/services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee +++ b/services/track-changes/test/unit/coffee/MongoManager/MongoManagerTests.coffee @@ -131,3 +131,33 @@ describe "MongoManager", -> it "should call the callback", -> @callback.called.should.equal true + + describe "getUpdatesBetweenDates", -> + beforeEach -> + @updates = ["mock-update"] + @db.docHistory = {} + @db.docHistory.find = sinon.stub().returns @db.docHistory + @db.docHistory.sort = sinon.stub().returns @db.docHistory + @db.docHistory.toArray = sinon.stub().callsArgWith(0, null, @updates) + + @from = new Date(Date.now()) + @to = new Date(Date.now() + 100000) + + @MongoManager.getUpdatesBetweenDates @doc_id, @from, @to, @callback + + it "should find the updates for the doc", -> + @db.docHistory.find + .calledWith({ + doc_id: ObjectId(@doc_id) + "meta.start_ts": { $gte: @from } + "meta.end_ts": { $lte: @to } + }) + .should.equal true + + it "should sort in descending timestamp order", -> + @db.docHistory.sort + .calledWith("meta.end_ts": -1) + .should.equal true + + it "should call the call back with the updates", -> + @callback.calledWith(null, @updates).should.equal true