2019-05-29 05:21:06 -04:00
|
|
|
const settings = require('settings-sharelatex')
|
2019-06-21 05:57:43 -04:00
|
|
|
const FaultTolerantRequest = require('../../infrastructure/FaultTolerantRequest')
|
2019-05-29 05:21:06 -04:00
|
|
|
const Errors = require('../Errors/Errors')
|
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
// check that the request should be made: ignore smoke test user and ensure the
|
|
|
|
// analytics service is configured
|
|
|
|
const checkAnalyticsRequest = function(userId) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (
|
2019-06-21 05:57:43 -04:00
|
|
|
settings.smokeTest &&
|
|
|
|
settings.smokeTest.userId &&
|
|
|
|
settings.smokeTest.userId.toString() === userId.toString()
|
2019-05-29 05:21:06 -04:00
|
|
|
) {
|
2019-06-21 05:57:43 -04:00
|
|
|
// ignore smoke test user
|
|
|
|
return { error: null, skip: true }
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
if (!settings.apis.analytics) {
|
|
|
|
return {
|
|
|
|
error: new Errors.ServiceNotConfiguredError(
|
|
|
|
'Analytics service not configured'
|
|
|
|
),
|
|
|
|
skip: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { error: null, skip: false }
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
// prepare the request: set `fromv2` param and full URL
|
|
|
|
const prepareAnalyticsRequest = function(options) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (settings.overleaf != null) {
|
|
|
|
options.qs = Object.assign({}, options.qs, { fromV2: 1 })
|
|
|
|
}
|
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
const urlPath = options.url
|
|
|
|
options.url = `${settings.apis.analytics.url}${urlPath}`
|
|
|
|
|
|
|
|
options.timeout = options.timeout || 30000
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
return options
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
// make the request to analytics after checking and preparing it.
|
|
|
|
// request happens asynchronously in the background and will be retried on error
|
|
|
|
const makeAnalyticsBackgroundRequest = function(userId, options, callback) {
|
|
|
|
let { error, skip } = checkAnalyticsRequest(userId)
|
|
|
|
if (error || skip) {
|
|
|
|
return callback(error)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-21 05:57:43 -04:00
|
|
|
prepareAnalyticsRequest(options)
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
// With the tweaked parameter values (BACKOFF_BASE=3000, BACKOFF_MULTIPLIER=3):
|
|
|
|
// - the 6th attempt (maxAttempts=6) will run after 5.5 to 11.5 minutes
|
|
|
|
// - the 9th attempt (maxAttempts=9) will run after 86 to 250 minutes
|
|
|
|
options.maxAttempts = options.maxAttempts || 9
|
|
|
|
options.backoffBase = options.backoffBase || 3000
|
|
|
|
options.backoffMultiplier = options.backoffMultiplier || 3
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
FaultTolerantRequest.backgroundRequest(options, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2019-06-21 05:57:43 -04:00
|
|
|
identifyUser(userId, oldUserId, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
// callback is optional
|
|
|
|
callback = () => {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-21 05:57:43 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
const opts = {
|
|
|
|
body: {
|
2019-06-21 05:57:43 -04:00
|
|
|
old_user_id: oldUserId
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
method: 'POST',
|
2019-06-21 05:57:43 -04:00
|
|
|
url: `/user/${userId}/identify`
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-21 05:57:43 -04:00
|
|
|
makeAnalyticsBackgroundRequest(userId, opts, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
recordEvent(userId, event, segmentation, callback) {
|
2019-05-29 05:21:06 -04:00
|
|
|
if (segmentation == null) {
|
2019-06-21 05:57:43 -04:00
|
|
|
// segmentation is optional
|
2019-05-29 05:21:06 -04:00
|
|
|
segmentation = {}
|
|
|
|
}
|
2019-06-21 05:57:43 -04:00
|
|
|
if (!callback) {
|
|
|
|
// callback is optional
|
|
|
|
callback = () => {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-21 05:57:43 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
const opts = {
|
|
|
|
body: {
|
|
|
|
event,
|
|
|
|
segmentation
|
|
|
|
},
|
|
|
|
json: true,
|
|
|
|
method: 'POST',
|
2019-06-21 05:57:43 -04:00
|
|
|
url: `/user/${userId}/event`
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
makeAnalyticsBackgroundRequest(userId, opts, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
updateEditingSession(userId, projectId, countryCode, callback) {
|
2019-06-21 05:57:43 -04:00
|
|
|
if (!callback) {
|
|
|
|
// callback is optional
|
|
|
|
callback = () => {}
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2019-06-21 05:57:43 -04:00
|
|
|
|
2019-05-29 05:21:06 -04:00
|
|
|
const query = {
|
|
|
|
userId,
|
|
|
|
projectId
|
|
|
|
}
|
|
|
|
|
|
|
|
if (countryCode) {
|
|
|
|
query.countryCode = countryCode
|
|
|
|
}
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
method: 'PUT',
|
|
|
|
url: '/editingSession',
|
|
|
|
qs: query,
|
2019-06-21 05:57:43 -04:00
|
|
|
maxAttempts: 6 // dont retry for too long as session ping timestamp are
|
|
|
|
// recorded when the request is received on the analytics
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
|
2019-06-21 05:57:43 -04:00
|
|
|
makeAnalyticsBackgroundRequest(userId, opts, callback)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
}
|