2016-11-11 12:03:01 -05:00
|
|
|
settings = require "settings-sharelatex"
|
2016-08-10 11:42:56 -04:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
_ = require "underscore"
|
2018-02-12 10:16:21 -05:00
|
|
|
request = require "requestretry"
|
2017-03-21 06:57:39 -04:00
|
|
|
Errors = require '../Errors/Errors'
|
2016-08-10 11:42:56 -04:00
|
|
|
|
|
|
|
|
2018-03-19 06:09:45 -04:00
|
|
|
makeFaultTolerantRequest = (userId, options, callback) ->
|
|
|
|
if userId+"" == settings.smokeTest?.userId+""
|
|
|
|
return callback()
|
|
|
|
|
|
|
|
options = Object.assign(options, {
|
|
|
|
delayStrategy: exponentialBackoffStrategy()
|
2018-03-19 10:46:18 -04:00
|
|
|
timeout: 30000
|
2018-03-19 06:09:45 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
if settings.overleaf?
|
|
|
|
options.qs = Object.assign({}, options.qs, { fromV2: 1 })
|
|
|
|
|
2018-03-23 06:02:11 -04:00
|
|
|
makeRequest options, (err) ->
|
|
|
|
if err?
|
|
|
|
logger.err { err: err }, 'Request to analytics failed'
|
|
|
|
|
|
|
|
callback() # Do not wait for all the attempts
|
2018-03-19 06:09:45 -04:00
|
|
|
|
2016-11-16 09:40:38 -05:00
|
|
|
makeRequest = (opts, callback)->
|
2016-11-15 05:54:48 -05:00
|
|
|
if settings.apis?.analytics?.url?
|
2016-11-16 09:40:38 -05:00
|
|
|
urlPath = opts.url
|
|
|
|
opts.url = "#{settings.apis.analytics.url}#{urlPath}"
|
2018-03-23 06:02:11 -04:00
|
|
|
request opts, callback
|
2016-11-15 05:54:48 -05:00
|
|
|
else
|
2017-03-21 06:57:39 -04:00
|
|
|
callback(new Errors.ServiceNotConfiguredError('Analytics service not configured'))
|
2016-11-15 05:54:48 -05:00
|
|
|
|
2016-08-10 11:42:56 -04:00
|
|
|
|
2018-03-15 11:11:11 -04:00
|
|
|
# Set an exponential backoff to retry calls to analytics. First retry will
|
|
|
|
# happen after 4s, then 8, 16, 32, 64...
|
|
|
|
exponentialBackoffStrategy = () ->
|
|
|
|
attempts = 1 # This won't be called until there has been 1 failure
|
|
|
|
|
|
|
|
() ->
|
|
|
|
attempts += 1
|
|
|
|
exponentialBackoffDelay(attempts)
|
|
|
|
|
|
|
|
exponentialBackoffDelay = (attempts) ->
|
|
|
|
delay = 2 ** attempts * 1000
|
|
|
|
|
|
|
|
logger.warn "Error comunicating with the analytics service. " +
|
|
|
|
"Will try again attempt #{attempts} in #{delay}ms"
|
|
|
|
|
|
|
|
delay
|
|
|
|
|
|
|
|
|
2016-11-11 12:03:01 -05:00
|
|
|
module.exports =
|
|
|
|
|
2017-03-22 12:01:26 -04:00
|
|
|
identifyUser: (user_id, old_user_id, callback = (error)->)->
|
2017-03-08 12:51:35 -05:00
|
|
|
opts =
|
|
|
|
body:
|
|
|
|
old_user_id:old_user_id
|
|
|
|
json:true
|
|
|
|
method:"POST"
|
|
|
|
timeout:1000
|
2017-03-22 11:50:49 -04:00
|
|
|
url: "/user/#{user_id}/identify"
|
|
|
|
makeRequest opts, callback
|
2016-11-15 05:54:48 -05:00
|
|
|
|
2016-11-11 12:03:01 -05:00
|
|
|
recordEvent: (user_id, event, segmentation = {}, callback = (error) ->) ->
|
|
|
|
opts =
|
|
|
|
body:
|
|
|
|
event:event
|
|
|
|
segmentation:segmentation
|
|
|
|
json:true
|
|
|
|
method:"POST"
|
2016-11-16 09:40:38 -05:00
|
|
|
url: "/user/#{user_id}/event"
|
2018-03-15 11:11:11 -04:00
|
|
|
maxAttempts: 7 # Give up after ~ 8min
|
2018-03-19 06:09:45 -04:00
|
|
|
|
|
|
|
makeFaultTolerantRequest user_id, opts, callback
|
|
|
|
|
2016-11-11 12:03:01 -05:00
|
|
|
|
2018-03-05 06:15:41 -05:00
|
|
|
updateEditingSession: (userId, projectId, countryCode, callback = (error) ->) ->
|
2018-03-05 05:46:51 -05:00
|
|
|
query =
|
|
|
|
userId: userId
|
|
|
|
projectId: projectId
|
2018-03-19 06:09:45 -04:00
|
|
|
|
2018-03-05 05:46:51 -05:00
|
|
|
if countryCode
|
|
|
|
query.countryCode = countryCode
|
2018-03-19 06:09:45 -04:00
|
|
|
|
2018-01-22 10:10:52 -05:00
|
|
|
opts =
|
|
|
|
method: "PUT"
|
2018-01-23 07:47:51 -05:00
|
|
|
url: "/editingSession"
|
2018-03-05 05:46:51 -05:00
|
|
|
qs: query
|
2018-03-15 11:11:11 -04:00
|
|
|
maxAttempts: 6 # Give up after ~ 4min
|
2018-03-19 06:09:45 -04:00
|
|
|
|
|
|
|
makeFaultTolerantRequest userId, opts, callback
|
2018-01-22 10:10:52 -05:00
|
|
|
|
2016-11-11 12:03:01 -05:00
|
|
|
|
2018-02-07 04:33:28 -05:00
|
|
|
getLastOccurrence: (user_id, event, callback = (error) ->) ->
|
2016-11-11 12:03:01 -05:00
|
|
|
opts =
|
|
|
|
body:
|
|
|
|
event:event
|
|
|
|
json:true
|
|
|
|
method:"POST"
|
|
|
|
timeout:1000
|
2018-02-07 04:33:28 -05:00
|
|
|
url: "/user/#{user_id}/event/last_occurrence"
|
2016-11-15 10:36:26 -05:00
|
|
|
makeRequest opts, (err, response, body)->
|
2018-01-22 10:00:56 -05:00
|
|
|
if err?
|
2016-11-11 12:03:01 -05:00
|
|
|
console.log response, opts
|
|
|
|
logger.err {user_id, err}, "error getting last occurance of event"
|
|
|
|
return callback err
|
|
|
|
else
|
2016-11-16 09:40:38 -05:00
|
|
|
return callback null, body
|