diff --git a/services/web/app/coffee/Features/Compile/ClsiManager.coffee b/services/web/app/coffee/Features/Compile/ClsiManager.coffee index 22598d7dfa..f5c79f12df 100755 --- a/services/web/app/coffee/Features/Compile/ClsiManager.coffee +++ b/services/web/app/coffee/Features/Compile/ClsiManager.coffee @@ -105,4 +105,19 @@ module.exports = ClsiManager = rootResourcePath: rootResourcePath resources: resources } - + + wordCount: (project_id, file, options, callback = (error, response) ->) -> + ClsiManager._buildRequest project_id, options, (error, req) -> + compilerUrl = ClsiManager._getCompilerUrl(options?.compileGroup) + filename = file || req.compile.rootResourcePath + request.get { + url: "#{compilerUrl}/project/#{project_id}/wordcount?file=#{filename}" + }, (error, response, body) -> + return callback(error) if error? + if 200 <= response.statusCode < 300 + callback null, body + else + error = new Error("CLSI returned non-success code: #{response.statusCode}") + logger.error err: error, project_id: project_id, "CLSI returned failure code" + callback error, body + diff --git a/services/web/app/coffee/Features/Compile/CompileController.coffee b/services/web/app/coffee/Features/Compile/CompileController.coffee index 667accd65a..d00a4375c7 100755 --- a/services/web/app/coffee/Features/Compile/CompileController.coffee +++ b/services/web/app/coffee/Features/Compile/CompileController.coffee @@ -102,3 +102,11 @@ module.exports = CompileController = proxy.pipe(res) proxy.on "error", (error) -> logger.warn err: error, url: url, "CLSI proxy error" + + wordCount: (req, res, next) -> + project_id = req.params.Project_id + file = req.query.file || false + CompileManager.wordCount project_id, file, (error, body) -> + return next(error) if error? + res.contentType("application/json") + res.send 200, body diff --git a/services/web/app/coffee/Features/Compile/CompileManager.coffee b/services/web/app/coffee/Features/Compile/CompileManager.coffee index 6e7056813d..7cac827789 100755 --- a/services/web/app/coffee/Features/Compile/CompileManager.coffee +++ b/services/web/app/coffee/Features/Compile/CompileManager.coffee @@ -91,3 +91,7 @@ module.exports = CompileManager = else ProjectRootDocManager.setRootDocAutomatically project_id, callback + wordCount: (project_id, file, callback = (error) ->) -> + CompileManager.getProjectCompileLimits project_id, (error, limits) -> + return callback(error) if error? + ClsiManager.wordCount project_id, file, limits, callback diff --git a/services/web/app/coffee/router.coffee b/services/web/app/coffee/router.coffee index 22c1fbfe54..cbad929e67 100644 --- a/services/web/app/coffee/router.coffee +++ b/services/web/app/coffee/router.coffee @@ -111,6 +111,7 @@ module.exports = class Router webRouter.delete "/project/:Project_id/output", SecurityManager.requestCanAccessProject, CompileController.deleteAuxFiles webRouter.get "/project/:Project_id/sync/code", SecurityManager.requestCanAccessProject, CompileController.proxySync webRouter.get "/project/:Project_id/sync/pdf", SecurityManager.requestCanAccessProject, CompileController.proxySync + webRouter.get "/project/:Project_id/wordcount", SecurityManager.requestCanAccessProject, CompileController.wordCount webRouter.delete '/Project/:Project_id', SecurityManager.requestIsOwner, ProjectController.deleteProject webRouter.post '/Project/:Project_id/restore', SecurityManager.requestIsOwner, ProjectController.restoreProject diff --git a/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee b/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee index 8eda10fa56..66c21c2400 100644 --- a/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee @@ -259,4 +259,31 @@ describe "ClsiManager", -> url: url json: @req jar: false - }).should.equal true \ No newline at end of file + }).should.equal true + + describe "wordCount", -> + beforeEach -> + @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body = { mock: "foo" }) + @ClsiManager._buildRequest = sinon.stub().callsArgWith(2, null, { compile: { rootResourcePath: "rootfile.text" } }) + @ClsiManager._getCompilerUrl = sinon.stub().returns "compiler.url" + + describe "with root file", -> + beforeEach -> + @ClsiManager.wordCount @project_id, false, {}, @callback + + it "should call wordCount with root file", -> + @request.get + .calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=rootfile.text" }) + .should.equal true + + it "should call the callback", -> + @callback.called.should.equal true + + describe "with param file", -> + beforeEach -> + @ClsiManager.wordCount @project_id, "main.tex", {}, @callback + + it "should call wordCount with param file", -> + @request.get + .calledWith({ url: "compiler.url/project/#{@project_id}/wordcount?file=main.tex" }) + .should.equal true diff --git a/services/web/test/UnitTests/coffee/Compile/CompileControllerTests.coffee b/services/web/test/UnitTests/coffee/Compile/CompileControllerTests.coffee index 33239f53a6..7ac3bb0006 100644 --- a/services/web/test/UnitTests/coffee/Compile/CompileControllerTests.coffee +++ b/services/web/test/UnitTests/coffee/Compile/CompileControllerTests.coffee @@ -352,3 +352,22 @@ describe "CompileController", -> @CompileController.compileAndDownloadPdf @req, @res @CompileController.proxyToClsi.calledWith(@project_id, "/project/#{@project_id}/output/output.pdf", @req, @res).should.equal true done() + + describe "wordCount", -> + beforeEach -> + @CompileManager.wordCount = sinon.stub().callsArgWith(2, null, {content:"body"}) + @req.params = + Project_id: @project_id + @res.send = sinon.stub() + @res.contentType = sinon.stub() + @CompileController.wordCount @req, @res, @next + + it "should proxy to the CLSI", -> + @CompileManager.wordCount + .calledWith(@project_id, false) + .should.equal true + + it "should return a 200 and body", -> + @res.send + .calledWith(200, {content:"body"}) + .should.equal true diff --git a/services/web/test/UnitTests/coffee/Compile/CompileManagerTests.coffee b/services/web/test/UnitTests/coffee/Compile/CompileManagerTests.coffee index 9f4031e776..e68f5fa422 100644 --- a/services/web/test/UnitTests/coffee/Compile/CompileManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Compile/CompileManagerTests.coffee @@ -259,3 +259,22 @@ describe "CompileManager", -> @CompileManager._checkIfAutoCompileLimitHasBeenHit true, (err, canCompile)=> canCompile.should.equal false done() + + describe "wordCount", -> + beforeEach -> + @CompileManager.getProjectCompileLimits = sinon.stub().callsArgWith 1, null, @limits = { compileGroup: "mock-compile-group" } + @ClsiManager.wordCount = sinon.stub().callsArg(3) + @CompileManager.wordCount @project_id, false, @callback + + it "should look up the compile group to use", -> + @CompileManager.getProjectCompileLimits + .calledWith(@project_id) + .should.equal true + + it "should call wordCount for project", -> + @ClsiManager.wordCount + .calledWith(@project_id, false, @limits) + .should.equal true + + it "should call the callback", -> + @callback.called.should.equal true