AnalyticsManager.coffee 2.5 KB

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