diff --git a/services/track-changes/app/coffee/HttpController.coffee b/services/track-changes/app/coffee/HttpController.coffee index 215d440129..204ba68e48 100644 --- a/services/track-changes/app/coffee/HttpController.coffee +++ b/services/track-changes/app/coffee/HttpController.coffee @@ -38,42 +38,10 @@ module.exports = HttpController = if req.query.limit? limit = parseInt(req.query.limit, 10) - UpdatesManager.getUpdatesWithUserInfo doc_id, to: to, limit: limit, (error, updates) -> + UpdatesManager.getSummarizedUpdates doc_id, to: to, limit: limit, (error, updates) -> return next(error) if error? - res.send JSON.stringify updates: HttpController._buildUpdatesView(updates) + res.send JSON.stringify updates: updates - TIME_BETWEEN_DISTINCT_UPDATES: fiveMinutes = 5 * 60 * 1000 - _buildUpdatesView: (updates) -> - view = [] - for update in updates.slice().reverse() - lastUpdate = view[view.length - 1] - if lastUpdate and update.meta.start_ts - lastUpdate.meta.end_ts < @TIME_BETWEEN_DISTINCT_UPDATES - if update.meta.user? - userExists = false - for user in lastUpdate.meta.users - if user.id == update.meta.user.id - userExists = true - break - if !userExists - lastUpdate.meta.users.push update.meta.user - lastUpdate.meta.start_ts = Math.min(lastUpdate.meta.start_ts, update.meta.start_ts) - lastUpdate.meta.end_ts = Math.max(lastUpdate.meta.end_ts, update.meta.end_ts) - lastUpdate.toV = update.v - else - newUpdate = - meta: - users: [] - start_ts: update.meta.start_ts - end_ts: update.meta.end_ts - fromV: update.v - toV: update.v - - if update.meta.user? - newUpdate.meta.users.push update.meta.user - - view.push newUpdate - - return view.reverse() restore: (req, res, next = (error) ->) -> {doc_id, project_id, version} = req.params diff --git a/services/track-changes/app/coffee/UpdatesManager.coffee b/services/track-changes/app/coffee/UpdatesManager.coffee index 7261b3f341..bb8c9069b2 100644 --- a/services/track-changes/app/coffee/UpdatesManager.coffee +++ b/services/track-changes/app/coffee/UpdatesManager.coffee @@ -76,6 +76,11 @@ module.exports = UpdatesManager = return callback(error) if error? callback null, updates + getSummarizedUpdates: (doc_id, options = {}, callback = (error, updates) ->) -> + UpdatesManager.getUpdatesWithUserInfo doc_id, options, (error, updates) -> + return callback(error) if error? + callback null, UpdatesManager._summarizeUpdates(updates) + fillUserInfo: (updates, callback = (error, updates) ->) -> users = {} for update in updates @@ -105,3 +110,37 @@ module.exports = UpdatesManager = return false else return !!user_id.match(/^[a-f0-9]{24}$/) + + + TIME_BETWEEN_DISTINCT_UPDATES: fiveMinutes = 5 * 60 * 1000 + _summarizeUpdates: (updates) -> + view = [] + for update in updates.slice().reverse() + lastUpdate = view[view.length - 1] + if lastUpdate and update.meta.start_ts - lastUpdate.meta.end_ts < @TIME_BETWEEN_DISTINCT_UPDATES + if update.meta.user? + userExists = false + for user in lastUpdate.meta.users + if user.id == update.meta.user.id + userExists = true + break + if !userExists + lastUpdate.meta.users.push update.meta.user + lastUpdate.meta.start_ts = Math.min(lastUpdate.meta.start_ts, update.meta.start_ts) + lastUpdate.meta.end_ts = Math.max(lastUpdate.meta.end_ts, update.meta.end_ts) + lastUpdate.toV = update.v + else + newUpdate = + meta: + users: [] + start_ts: update.meta.start_ts + end_ts: update.meta.end_ts + fromV: update.v + toV: update.v + + if update.meta.user? + newUpdate.meta.users.push update.meta.user + + view.push newUpdate + + return view.reverse() diff --git a/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee b/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee index f6ec9bdee8..e628736f84 100644 --- a/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee +++ b/services/track-changes/test/unit/coffee/HttpController/HttpControllerTests.coffee @@ -74,77 +74,17 @@ describe "HttpController", -> limit: @limit.toString() @res = send: sinon.stub() - @rawUpdates = ["raw-updates"] - @updatesView = ["updates-view"] - @HttpController._buildUpdatesView = sinon.stub().returns(@updatesView) - @UpdatesManager.getUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @rawUpdates) + @updates = ["mock-summarized-updates"] + @UpdatesManager.getSummarizedUpdates = sinon.stub().callsArgWith(2, null, @updates) @HttpController.getUpdates @req, @res, @next it "should get the updates", -> - @UpdatesManager.getUpdatesWithUserInfo + @UpdatesManager.getSummarizedUpdates .calledWith(@doc_id, to: @to, limit: @limit) .should.equal true - it "should build the updates view", -> - @HttpController._buildUpdatesView - .calledWith(@rawUpdates) - .should.equal true - it "should return the formatted updates", -> - @res.send.calledWith(JSON.stringify(updates: @updatesView)).should.equal true - - describe "_buildUpdatesView", -> - it "should concat updates that are close in time", -> - expect(@HttpController._buildUpdatesView [{ - meta: - user: @user_2 = { id: "mock-user-2" } - start_ts: @now + 20 - end_ts: @now + 30 - v: 5 - }, { - meta: - user: @user_1 = { id: "mock-user-1" } - start_ts: @now - end_ts: @now + 10 - v: 4 - }]).to.deep.equal [{ - meta: - users: [@user_1, @user_2] - start_ts: @now - end_ts: @now + 30 - fromV: 4 - toV: 5 - }] - - it "should leave updates that are far apart in time", -> - oneDay = 1000 * 60 * 60 * 24 - expect(@HttpController._buildUpdatesView [{ - meta: - user: @user_2 = { id: "mock-user-2" } - start_ts: @now + oneDay - end_ts: @now + oneDay + 10 - v: 5 - }, { - meta: - user: @user_1 = { id: "mock-user-2" } - start_ts: @now - end_ts: @now + 10 - v: 4 - }]).to.deep.equal [{ - meta: - users: [@user_2] - start_ts: @now + oneDay - end_ts: @now + oneDay + 10 - fromV: 5 - toV: 5 - }, { - meta: - users: [@user_1] - start_ts: @now - end_ts: @now + 10 - fromV: 4 - toV: 4 - }] + @res.send.calledWith(JSON.stringify(updates: @updates)).should.equal true describe "RestoreManager", -> beforeEach -> diff --git a/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee b/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee index 67fc7b0d14..0a37cb9934 100644 --- a/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee +++ b/services/track-changes/test/unit/coffee/UpdatesManager/UpdatesManagerTests.coffee @@ -242,6 +242,29 @@ describe "UpdatesManager", -> it "shoudl return the updates with the filled details", -> @callback.calledWith(null, @updatesWithUserInfo).should.equal true + describe "getSummarizedUpdates", -> + beforeEach -> + @to = 42 + @limit = 10 + @updates = ["mock-updates"] + @summarizedUpdates = ["summarized-updates"] + @UpdatesManager._summarizeUpdates = sinon.stub().returns(@summarizedUpdates) + @UpdatesManager.getUpdatesWithUserInfo = sinon.stub().callsArgWith(2, null, @updates) + @UpdatesManager.getSummarizedUpdates @doc_id, { to: @to, limit: @limit }, @callback + + it "should get the updates", -> + @UpdatesManager.getUpdatesWithUserInfo + .calledWith(@doc_id, { to: @to, limit: @limit }) + .should.equal true + + it "should summarize the updates", -> + @UpdatesManager._summarizeUpdates + .calledWith(@updates) + .should.equal true + + it "should call the callback with the summarized updates", -> + @callback.calledWith(null, @summarizedUpdates).should.equal true + describe "fillUserInfo", -> describe "with valid users", -> beforeEach (done) -> @@ -330,5 +353,60 @@ describe "UpdatesManager", -> op: "mock-op-2" }] + describe "_buildUpdatesView", -> + beforeEach -> + @now = Date.now() + + it "should concat updates that are close in time", -> + expect(@UpdatesManager._summarizeUpdates [{ + meta: + user: @user_2 = { id: "mock-user-2" } + start_ts: @now + 20 + end_ts: @now + 30 + v: 5 + }, { + meta: + user: @user_1 = { id: "mock-user-1" } + start_ts: @now + end_ts: @now + 10 + v: 4 + }]).to.deep.equal [{ + meta: + users: [@user_1, @user_2] + start_ts: @now + end_ts: @now + 30 + fromV: 4 + toV: 5 + }] + + it "should leave updates that are far apart in time", -> + oneDay = 1000 * 60 * 60 * 24 + expect(@UpdatesManager._summarizeUpdates [{ + meta: + user: @user_2 = { id: "mock-user-2" } + start_ts: @now + oneDay + end_ts: @now + oneDay + 10 + v: 5 + }, { + meta: + user: @user_1 = { id: "mock-user-2" } + start_ts: @now + end_ts: @now + 10 + v: 4 + }]).to.deep.equal [{ + meta: + users: [@user_2] + start_ts: @now + oneDay + end_ts: @now + oneDay + 10 + fromV: 5 + toV: 5 + }, { + meta: + users: [@user_1] + start_ts: @now + end_ts: @now + 10 + fromV: 4 + toV: 4 + }]