Skip to content
This repository has been archived by the owner on Aug 27, 2018. It is now read-only.

Commit

Permalink
Merge pull request #835 from jacquarg/development
Browse files Browse the repository at this point in the history
Generate UseTracker doc about cozy usage for MesInfos researchers.
  • Loading branch information
nono authored Nov 25, 2016
2 parents 5dc448d + 8bbaa23 commit 65cde25
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client/app/initialize.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ window.onerror = (msg, url, line, col, error) ->
# start application when DOM is ready (scripts execution is defered)
document.addEventListener 'DOMContentLoaded', ->
try
@mesinfosUseTracker = window.cozy_user.mesinfosUseTracker

@instance = window.cozy_instance or {}
@locale = @instance?.locale or 'en'

Expand Down
17 changes: 17 additions & 0 deletions client/app/lib/usetracker.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
INTERVAL = 1 * 1000 # 1s

module.exports = class UseTracker

constructor: ->
if app.mesinfosUseTracker
setInterval @_heartbeat, INTERVAL

setApp: (appName) ->
@appName = appName

_heartbeat: =>
if document.hasFocus()
$.post 'usetrackers',
timestamp: new Date().toISOString()
appName: @appName
, 'json'
5 changes: 5 additions & 0 deletions client/app/views/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ApplicationsListView = require 'views/home'
SocketListener = require 'lib/socket_listener'
User = require 'models/user'
IntentManager = require 'lib/intent_manager'
UseTracker = require 'lib/usetracker'

client = require 'helpers/client'

Expand Down Expand Up @@ -39,6 +40,8 @@ module.exports = class HomeView extends BaseView
SocketListener.watch @apps
SocketListener.watch @notifications
SocketListener.watch @devices
@useTracker = new UseTracker()

super


Expand Down Expand Up @@ -113,6 +116,7 @@ module.exports = class HomeView extends BaseView

window.document.title = "Cozy - #{title}"
$('#current-application').html title
@useTracker.setApp title

if view is @applicationListView
@backButton.hide()
Expand Down Expand Up @@ -256,6 +260,7 @@ module.exports = class HomeView extends BaseView

@$("#app-btn-#{slug} .spinner").hide()
@$("#app-btn-#{slug} .icon").show()
@useTracker.setApp slug

unless frame.length
@createApplicationIframe slug, hash, id: iframeID
Expand Down
3 changes: 3 additions & 0 deletions server/controllers/routes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ photo = require './photo'
logs = require './logs'
backgrounds = require './backgrounds'
help = require './help'
usetrackers = require './usetrackers'


module.exports =
Expand Down Expand Up @@ -126,3 +127,5 @@ module.exports =
'photos/thumbs/:photoid.jpg' : get : photo.thumb
'photos/raws/:photoid.jpg' :
get : photo.raw

'usetrackers': post: usetrackers.heartbeat
41 changes: 41 additions & 0 deletions server/controllers/usetrackers.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
###
App usage tracking
###

moment = require 'moment-timezone'
UseTracker = require '../models/usetracker'

TIMING = 10 * 1000 # 10s
appTracker = {}

module.exports =
heartbeat: (req, res, next) ->
appName = req.body.appName

# Tracking logic
if not appTracker[appName]?
appTracker[appName] =
dateStart: req.body.timestamp
dateEnd: null
timeout: null

appInfo = appTracker[appName]
appInfo.dateEnd = req.body.timestamp
clearTimeout appInfo.timeout
# When the timeout proc, the session is considered terminated
appInfo.timeout = setTimeout ->
duration = moment(appInfo.dateEnd) - moment(appInfo.dateStart)
# Actually, precision is just up to 1s.
duration = Math.round(duration / 1000) * 1000

data =
app: appName
dateStart: appInfo.dateStart
dateEnd: appInfo.dateEnd
duration: duration
delete appTracker[appName]
UseTracker.create data, (err, res, body) ->
console.log "Couldn't add app tracking info -- #{err}" if err?
, TIMING

res.status(201).send('ok')
1 change: 1 addition & 0 deletions server/models/user.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = User = cozydb.getModel 'User',
encryptedOtpKey: String
hotpCounter: Number
encryptedRecoveryCodes: String
mesinfosUseTracker: {type: Boolean, default: false}


# Request methods
Expand Down
10 changes: 10 additions & 0 deletions server/models/usetracker.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cozydb = require 'cozydb'
request = require 'request'
async = require 'async'

module.exports = UseTracker = cozydb.getModel 'UseTracker',
app: String
dateStart: Date
dateEnd: Date
duration: Number
sent: { type: Boolean, default: false }

0 comments on commit 65cde25

Please sign in to comment.