From f0777f996c71559543c8331e4ea916deec6402ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Fern=C3=A1ndez=20Capel?= Date: Wed, 24 Jan 2018 15:15:18 +0000 Subject: [PATCH] Space editing sessions heartbeats with an increasing backoff We send the first heartbeat as soon as the session start. The next ones are sent (if there's any activity) when the editing session is about 30 and 60 seconds. Then at 2min, 3min, 4min, 5min and later on every 5min. The backoff is not reset, so if due to inactivity the session expires in the server the backoff will still be the same. --- services/web/public/coffee/main/event.coffee | 26 +++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/services/web/public/coffee/main/event.coffee b/services/web/public/coffee/main/event.coffee index 87b50e49d4..fc203fde26 100644 --- a/services/web/public/coffee/main/event.coffee +++ b/services/web/public/coffee/main/event.coffee @@ -1,10 +1,15 @@ define [ + "moment" "base" "modules/localStorage" -], (App) -> + +], (moment, App) -> CACHE_KEY = "mbEvents" EDIT_SESSION_HEARTBEAT_INTERVAL = 5 * 60 * 1000 # 5min + sessionStart = new Date() + nextHeartbeat = new Date() + send = (category, action, attributes = {})-> ga('send', 'event', category, action) event_name = "#{action}-#{category}" @@ -35,7 +40,22 @@ define [ send: (category, action, label, value)-> ga('send', 'event', category, action, label, value) - editingSessionHeartbeat: _.throttle( (segmentation = {}) -> + + editingSessionHeartbeat: (segmentation = {}) -> + return unless nextHeartbeat <= new Date() + + @_sendEditingSessionHeartbeat(segmentation) + + sessionDuration = (new Date().getTime() - sessionStart.getTime())/1000 + + backoffSecs = switch + when sessionDuration < 60 then 30 + when sessionDuration < 300 then 60 + else 300 + + nextHeartbeat = moment().add(backoffSecs, 'seconds').toDate() + + _sendEditingSessionHeartbeat: (segmentation) -> $http({ url: "/editingSession/#{window.project_id}", method: "PUT", @@ -44,8 +64,6 @@ define [ "X-CSRF-Token": window.csrfToken } }) - , EDIT_SESSION_HEARTBEAT_INTERVAL, trailing: false) - sendMB: (key, segmentation = {}) -> $http {