wip: change how indexing of references works.

This commit is contained in:
Shane Kilkelly 2016-01-22 14:23:59 +00:00
parent 307f78b831
commit af75bb8a60
5 changed files with 133 additions and 135 deletions

View file

@ -37,13 +37,17 @@ module.exports = ReferencesSearchController =
return res.send 500 return res.send 500
return res.json data return res.json data
loadReferencesKeys: (req, res) -> index: (req, res) ->
project_id = req.params.Project_id projectId = req.params.Project_id
shouldBroadcast = req.body.shouldBroadcast shouldBroadcast = req.body.shouldBroadcast
logger.log {project_id}, "loading project references keys" docIds = req.body.docIds
ReferencesSearchHandler.loadReferencesKeys project_id, (err, data) -> if (not docIds instanceof Array) and (docIds != "ALL")
logger.err {projectId, docIds}, "docIds is not valid, should be either Array or String 'ALL'"
return res.send 400
logger.log {projectId, docIds}, "index references for project"
ReferencesSearchHandler.index projectId, docIds, (err, data) ->
if err if err
logger.err {err, project_id}, "error getting references keys" logger.err {err, projectId}, "error indexing references"
return res.send 500 return res.send 500
# TODO: optionally broadcast to all connected clients # TODO: optionally broadcast to all connected clients
return res.json data return res.json data

View file

@ -29,35 +29,40 @@ module.exports = ReferencesSearchHandler =
return ids return ids
_isFullIndex: (project, callback = (err, result) ->) -> _isFullIndex: (project, callback = (err, result) ->) ->
UserGetter.getUser project.owner_ref, {features: 1}, (err, owner) -> owner = project.owner_ref
return callback(err) if err
callback(null, owner.features.references == true) callback(null, owner.features.references == true)
loadReferencesKeys: (projectId, callback=(err, data)->) -> # projectId: String, docIds: List[String]|Null
logger.log {projectId}, "load references keys for project" index: (projectId, docIds, callback=(err, data)->) ->
Project.findPopulatedById projectId, (err, project) -> Project.findPopulatedById projectId, (err, project) ->
if err if err
logger.err {err, projectId}, "error finding project"
return callback(err) return callback(err)
if docIds == "ALL"
logger.log {projectId}, "indexing all bib files in project"
docIds = ReferencesSearchHandler._findBibDocIds(project)
ReferencesSearchHandler._isFullIndex project, (err, isFullIndex) -> ReferencesSearchHandler._isFullIndex project, (err, isFullIndex) ->
if err if err
logger.err {err, projectId}, "error checking whether to do full index"
return callback(err) return callback(err)
bibDocIds = ReferencesSearchHandler._findBibDocIds(project) bibDocUrls = docIds.map (docId) ->
bibDocUrls = bibDocIds.map (docId) ->
ReferencesSearchHandler._buildDocUrl projectId, docId ReferencesSearchHandler._buildDocUrl projectId, docId
logger.log {projectId, isFullIndex, bibDocIds}, "sending request to references service" logger.log {projectId, isFullIndex, docIds}, "sending request to references service"
request.post { request.post {
url: "#{settings.apis.references.url}/project/#{projectId}/loadreferenceskeys" url: "#{settings.apis.references.url}/project/#{projectId}/index"
json: json:
docUrls: bibDocUrls docUrls: bibDocUrls
fullIndex: isFullIndex fullIndex: isFullIndex
}, (err, res, result) -> }, (err, res, data) ->
if err if err
logger.err {err, projectId}, "error communicating with references api"
return callback(err) return callback(err)
if 200 <= res.statusCode < 300 if 200 <= res.statusCode < 300
return callback(null) logger.log {projectId}, "got keys from references api"
return callback(null, data)
else else
err = new Error("references api responded with non-success code: #{res.statusCode}") err = new Error("references api responded with non-success code: #{res.statusCode}")
logger.log {err, projectId, fileUrl}, "error updating references" logger.log {err, projectId}, "error updating references"
return callback(err) return callback(err)
## ## ## ## ## ## ## ##

View file

@ -173,6 +173,7 @@ module.exports = class Router
webRouter.post "/project/:Project_id/references", SecurityManager.requestCanAccessProject, ReferencesSearchController.indexFile webRouter.post "/project/:Project_id/references", SecurityManager.requestCanAccessProject, ReferencesSearchController.indexFile
webRouter.get "/project/:Project_id/references/keys", SecurityManager.requestCanAccessProject, ReferencesSearchController.getKeys webRouter.get "/project/:Project_id/references/keys", SecurityManager.requestCanAccessProject, ReferencesSearchController.getKeys
webRouter.post "/project/:Project_id/references/index", SecurityManager.requestCanAccessProject, ReferencesSearchController.index
#Admin Stuff #Admin Stuff
webRouter.get '/admin', SecurityManager.requestIsAdmin, AdminController.index webRouter.get '/admin', SecurityManager.requestIsAdmin, AdminController.index

View file

@ -10,41 +10,29 @@ define [
entity = @ide.fileTreeManager.findEntityById doc.doc_id entity = @ide.fileTreeManager.findEntityById doc.doc_id
if entity?.name?.match /.*\.bib$/ if entity?.name?.match /.*\.bib$/
@$scope.$emit 'references:changed', entity @$scope.$emit 'references:changed', entity
@indexReferences doc.doc_id @indexReferences([doc.doc_id], true)
@$scope.$on 'project:joined', (e) => @$scope.$on 'project:joined', (e) =>
@loadReferencesKeys() @indexReferences("ALL", false)
loadReferencesKeys: () -> # docIds: List[String]|String('ALL'), shouldBroadcast: Bool
indexReferences: (docIds, shouldBroadcast) ->
if window._ENABLE_REFERENCES_AUTOCOMPLETE != true if window._ENABLE_REFERENCES_AUTOCOMPLETE != true
return return
$.post( opts =
"/project/#{@$scope.project_id}/referenceskeys", docIds: docIds
{ shouldBroadcast: shouldBroadcast
shouldBroadcast: false
_csrf: window.csrfToken _csrf: window.csrfToken
}, console.log ">>", opts
$.post(
"/project/#{@$scope.project_id}/references/index",
opts,
(data) => (data) =>
console.log ">> ", data console.log ">> done ", data
@$scope.$root._references.keys = data.keys
) )
indexReferences: (doc_id) -> getReferenceKeys: (callback=(keys)->) ->
if window._ENABLE_REFERENCES_AUTOCOMPLETE != true
return
$.post(
"/project/#{@$scope.project_id}/references",
{
docId: doc_id,
_csrf: window.csrfToken
},
(data) =>
setTimeout(
( () -> @getReferenceKeys() ).bind(this),
500
)
)
getReferenceKeys: (callback) ->
if window._ENABLE_REFERENCES_AUTOCOMPLETE != true if window._ENABLE_REFERENCES_AUTOCOMPLETE != true
return return
$.get( $.get(

View file

@ -7,115 +7,115 @@ modulePath = "../../../../app/js/Features/ReferencesSearch/ReferencesSearchHandl
describe 'ReferencesSearchHandler', -> describe 'ReferencesSearchHandler', ->
beforeEach -> # beforeEach ->
@project_id = '222' # @project_id = '222'
@file_id = '111111' # @file_id = '111111'
@handler = SandboxedModule.require modulePath, requires: # @handler = SandboxedModule.require modulePath, requires:
'logger-sharelatex': { # 'logger-sharelatex': {
log: -> # log: ->
err: -> # err: ->
} # }
'settings-sharelatex': @settings = { # 'settings-sharelatex': @settings = {
apis: # apis:
references: {url: 'http://some.url'} # references: {url: 'http://some.url'}
web: {url: 'http://some.url'} # web: {url: 'http://some.url'}
} # }
'request': @request = { # 'request': @request = {
get: sinon.stub() # get: sinon.stub()
post: sinon.stub() # post: sinon.stub()
} # }
'../../models/Project': @Project = { # '../../models/Project': @Project = {
Project: { # Project: {
findById: sinon.stub().callsArgWith(2, null, {owner_ref: '111'}) # findById: sinon.stub().callsArgWith(2, null, {owner_ref: '111'})
} # }
} # }
'../User/UserGetter': @UserGetter = { # '../User/UserGetter': @UserGetter = {
getUser: sinon.stub().callsArgWith(2, null, {features: {references: false}}) # getUser: sinon.stub().callsArgWith(2, null, {features: {references: false}})
} # }
describe 'indexFile', -> # describe 'indexFile', ->
describe 'full index or not', -> # describe 'full index or not', ->
beforeEach -> # beforeEach ->
@request.post.callsArgWith(1, null, {statusCode: 200}, {}) # @request.post.callsArgWith(1, null, {statusCode: 200}, {})
describe 'when full index is not required', -> # describe 'when full index is not required', ->
beforeEach -> # beforeEach ->
@UserGetter.getUser.callsArgWith(2, null, {features: {references: false}}) # @UserGetter.getUser.callsArgWith(2, null, {features: {references: false}})
it 'should set fullIndex to true', (done) -> # it 'should set fullIndex to true', (done) ->
@handler.indexFile @project_id, @file_id, (err) => # @handler.indexFile @project_id, @file_id, (err) =>
@request.post.calledOnce.should.equal true # @request.post.calledOnce.should.equal true
options = @request.post.firstCall.args[0] # options = @request.post.firstCall.args[0]
options.json.fullIndex.should.equal false # options.json.fullIndex.should.equal false
done() # done()
describe 'when full index is required', -> # describe 'when full index is required', ->
beforeEach -> # beforeEach ->
@UserGetter.getUser.callsArgWith(2, null, {features: {references: true}}) # @UserGetter.getUser.callsArgWith(2, null, {features: {references: true}})
it 'should set fullIndex to true', (done) -> # it 'should set fullIndex to true', (done) ->
@handler.indexFile @project_id, @file_id, (err) => # @handler.indexFile @project_id, @file_id, (err) =>
@request.post.calledOnce.should.equal true # @request.post.calledOnce.should.equal true
options = @request.post.firstCall.args[0] # options = @request.post.firstCall.args[0]
options.json.fullIndex.should.equal true # options.json.fullIndex.should.equal true
done() # done()
describe 'when index operation is successful', -> # describe 'when index operation is successful', ->
beforeEach -> # beforeEach ->
@request.post.callsArgWith(1, null, {statusCode: 201}, {}) # @request.post.callsArgWith(1, null, {statusCode: 201}, {})
it 'should not produce an error', (done) -> # it 'should not produce an error', (done) ->
@handler.indexFile @project_id, @file_id, (err) => # @handler.indexFile @project_id, @file_id, (err) =>
expect(err).to.equal null # expect(err).to.equal null
@request.post.calledOnce.should.equal true # @request.post.calledOnce.should.equal true
options = @request.post.firstCall.args[0] # options = @request.post.firstCall.args[0]
options.json.fullIndex.should.equal false # options.json.fullIndex.should.equal false
options.json.referencesUrl.should.not.be.undefined # options.json.referencesUrl.should.not.be.undefined
options.url.should.not.be.undefined # options.url.should.not.be.undefined
done() # done()
describe 'when index operation fails', -> # describe 'when index operation fails', ->
beforeEach -> # beforeEach ->
@request.post.callsArgWith(1, null, {statusCode: 500}, {}) # @request.post.callsArgWith(1, null, {statusCode: 500}, {})
it 'should produce an error', (done) -> # it 'should produce an error', (done) ->
@handler.indexFile @project_id, @file_id, (err) => # @handler.indexFile @project_id, @file_id, (err) =>
expect(err).to.not.equal null # expect(err).to.not.equal null
done() # done()
describe 'getKeys', -> # describe 'getKeys', ->
describe 'when request is successful', -> # describe 'when request is successful', ->
beforeEach -> # beforeEach ->
@data = # @data =
projectId: @projectId # projectId: @projectId
keys: ['a', 'b', 'c'] # keys: ['a', 'b', 'c']
@request.get.callsArgWith(1, null, {statusCode: 200}, @data) # @request.get.callsArgWith(1, null, {statusCode: 200}, @data)
it 'should not produce an error', -> # it 'should not produce an error', ->
@handler.getKeys @project_id, (err, result) => # @handler.getKeys @project_id, (err, result) =>
expect(err).to.equal null # expect(err).to.equal null
it 'should produce a result object', -> # it 'should produce a result object', ->
@handler.getKeys @project_id, (err, result) => # @handler.getKeys @project_id, (err, result) =>
expect(result).to.not.equal null # expect(result).to.not.equal null
expect(result).to.deep.equal @data # expect(result).to.deep.equal @data
describe 'when request fails', -> # describe 'when request fails', ->
beforeEach -> # beforeEach ->
@data = # @data =
projectId: @project_Id # projectId: @project_Id
keys: ['a', 'b', 'c'] # keys: ['a', 'b', 'c']
@request.get.callsArgWith(1, null, {statusCode: 500}, null) # @request.get.callsArgWith(1, null, {statusCode: 500}, null)
it 'should produce an error', -> # it 'should produce an error', ->
@handler.getKeys @project_id, (err, result) => # @handler.getKeys @project_id, (err, result) =>
expect(err).to.not.equal null # expect(err).to.not.equal null
it 'should not produce a result', -> # it 'should not produce a result', ->
@handler.getKeys @project_id, (err, result) => # @handler.getKeys @project_id, (err, result) =>
expect(result).to.not.equal null # expect(result).to.not.equal null