This repository has been archived by the owner on Aug 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #835 from jacquarg/development
Generate UseTracker doc about cozy usage for MesInfos researchers.
- Loading branch information
Showing
7 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |