AnalyticsManager.coffee 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. settings = require "settings-sharelatex"
  2. logger = require "logger-sharelatex"
  3. _ = require "underscore"
  4. request = require "requestretry"
  5. Errors = require '../Errors/Errors'
  6. makeFaultTolerantRequest = (userId, options, callback) ->
  7. if userId+"" == settings.smokeTest?.userId+""
  8. return callback()
  9. options = Object.assign(options, {
  10. delayStrategy: exponentialBackoffStrategy()
  11. timeout: 30000
  12. })
  13. if settings.overleaf?
  14. options.qs = Object.assign({}, options.qs, { fromV2: 1 })
  15. makeRequest options, (err) ->
  16. if err?
  17. logger.err { err: err }, 'Request to analytics failed'
  18. callback() # Do not wait for all the attempts
  19. makeRequest = (opts, callback)->
  20. if settings.apis?.analytics?.url?
  21. urlPath = opts.url
  22. opts.url = "#{settings.apis.analytics.url}#{urlPath}"
  23. request opts, callback
  24. else
  25. callback(new Errors.ServiceNotConfiguredError('Analytics service not configured'))
  26. # Set an exponential backoff to retry calls to analytics. First retry will
  27. # happen after 4s, then 8, 16, 32, 64...
  28. exponentialBackoffStrategy = () ->
  29. attempts = 1 # This won't be called until there has been 1 failure
  30. () ->
  31. attempts += 1
  32. exponentialBackoffDelay(attempts)
  33. exponentialBackoffDelay = (attempts) ->
  34. delay = 2 ** attempts * 1000
  35. logger.warn "Error comunicating with the analytics service. " +
  36. "Will try again attempt #{attempts} in #{delay}ms"
  37. delay
  38. module.exports =
  39. identifyUser: (user_id, old_user_id, callback = (error)->)->
  40. opts =
  41. body:
  42. old_user_id:old_user_id
  43. json:true
  44. method:"POST"
  45. timeout:1000
  46. url: "/user/#{user_id}/identify"
  47. makeRequest opts, callback
  48. recordEvent: (user_id, event, segmentation = {}, callback = (error) ->) ->
  49. opts =
  50. body:
  51. event:event
  52. segmentation:segmentation
  53. json:true
  54. method:"POST"
  55. url: "/user/#{user_id}/event"
  56. maxAttempts: 7 # Give up after ~ 8min
  57. makeFaultTolerantRequest user_id, opts, callback
  58. updateEditingSession: (userId, projectId, countryCode, callback = (error) ->) ->
  59. query =
  60. userId: userId
  61. projectId: projectId
  62. if countryCode
  63. query.countryCode = countryCode
  64. opts =
  65. method: "PUT"
  66. url: "/editingSession"
  67. qs: query
  68. maxAttempts: 6 # Give up after ~ 4min
  69. makeFaultTolerantRequest userId, opts, callback
  70. getLastOccurrence: (user_id, event, callback = (error) ->) ->
  71. opts =
  72. body:
  73. event:event
  74. json:true
  75. method:"POST"
  76. timeout:1000
  77. url: "/user/#{user_id}/event/last_occurrence"
  78. makeRequest opts, (err, response, body)->
  79. if err?
  80. console.log response, opts
  81. logger.err {user_id, err}, "error getting last occurance of event"
  82. return callback err
  83. else
  84. return callback null, body