Просмотр исходного кода

Send edit sessions heartbeat to the analytics service

Alberto Fernández Capel 8 лет назад
Родитель
Сommit
868e32c1bc

+ 13 - 0
services/web/app/coffee/Features/Analytics/AnalyticsController.coffee

@@ -3,6 +3,19 @@ Errors = require "../Errors/Errors"
 AuthenticationController = require("../Authentication/AuthenticationController")
 
 module.exports = AnalyticsController =
+	updateEditSession: (req, res, next) ->
+		userId    = AuthenticationController.getLoggedInUserId(req) or req.sessionID
+		projectId = req.params.projectId
+
+		AnalyticsManager.updateEditSession userId, projectId, {}, (error) ->
+			if error instanceof Errors.ServiceNotConfiguredError
+				# ignore, no-op
+				return res.send(204)
+			else if error?
+				return next(error)
+			else
+				return res.send 204
+
 	recordEvent: (req, res, next) ->
 		user_id = AuthenticationController.getLoggedInUserId(req) or req.sessionID
 		AnalyticsManager.recordEvent user_id, req.params.event, req.body, (error) ->

+ 12 - 0
services/web/app/coffee/Features/Analytics/AnalyticsManager.coffee

@@ -39,6 +39,18 @@ module.exports =
 			url: "/user/#{user_id}/event"
 		makeRequest opts, callback
 
+	updateEditSession: (userId, projectId, segmentation = {}, callback = (error) ->) ->
+		if userId+"" == settings.smokeTest?.userId+""
+			return callback()
+		opts =
+			body:
+				segmentation: segmentation
+			json: true
+			method: "PUT"
+			timeout: 1000
+			url: "/editSession?userId=#{userId}&projectId=#{projectId}"
+		makeRequest opts, callback
+
 
 	getLastOccurance: (user_id, event, callback = (error) ->) ->
 		opts =

+ 1 - 0
services/web/app/coffee/Features/Analytics/AnalyticsRouter.coffee

@@ -5,6 +5,7 @@ AnalyticsProxy = require('./AnalyticsProxy')
 module.exports =
 	apply: (webRouter, privateApiRouter, publicApiRouter) ->
 		webRouter.post '/event/:event', AnalyticsController.recordEvent
+		webRouter.put  '/editSession/:projectId', AnalyticsController.updateEditSession
 		publicApiRouter.use '/analytics/graphs',
 			AuthenticationController.httpAuth,
 			AnalyticsProxy.call('/graphs')

+ 4 - 0
services/web/public/coffee/ide/editor/directives/aceEditor.coffee

@@ -300,6 +300,10 @@ define [
 				updateCount = 0
 				onChange = () ->
 					updateCount++
+
+					projectId = _.last( location.pathname.split("/"))
+					event_tracking.editSessionHeartbeat(projectId)
+
 					if updateCount == 100
 						event_tracking.send 'editor-interaction', 'multi-doc-update'
 					scope.$emit "#{scope.name}:change"

+ 13 - 0
services/web/public/coffee/main/event.coffee

@@ -3,6 +3,7 @@ define [
 	"modules/localStorage"
 ], (App) ->
 	CACHE_KEY = "mbEvents"
+	EDIT_SESSION_HEARTBEAT_INTERVAL = 5 * 60 * 1000 # 5min
 
 	send = (category, action, attributes = {})->
 		ga('send', 'event', category, action)
@@ -34,6 +35,18 @@ define [
 			send: (category, action, label, value)->
 				ga('send', 'event', category, action, label, value)
 
+			editSessionHeartbeat: _.throttle( (projectId, segmentation = {}) ->
+				$http({
+					url: "/editSession/#{projectId}",
+					method: "PUT",
+					data: segmentation,
+					headers: {
+						"X-CSRF-Token": window.csrfToken
+					}
+				})
+			, EDIT_SESSION_HEARTBEAT_INTERVAL)
+
+
 			sendMB: (key, segmentation = {}) ->
 				$http {
 					url: "/event/#{key}",