r1451 - in wit/js: . components services
Author: smaisonneuve Date: 2015-05-22 15:53:43 +0000 (Fri, 22 May 2015) New Revision: 1451 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1451 Log: [UserActivity] Refactoring service by moving sessions utilities to a new service Modified: wit/js/components/Timeline.js wit/js/main.js wit/js/services/UserActivityService.js Modified: wit/js/components/Timeline.js =================================================================== --- wit/js/components/Timeline.js 2015-05-22 15:31:41 UTC (rev 1450) +++ wit/js/components/Timeline.js 2015-05-22 15:53:43 UTC (rev 1451) @@ -24,7 +24,7 @@ if (startDate && endDate) { var setState = this.setState.bind(this); - scope.user.getSessions(startDate, endDate, true) + scope.sessions.getSessions(startDate, endDate, true) .then(function(sessions) { setState({ sessions: sessions Modified: wit/js/main.js =================================================================== --- wit/js/main.js 2015-05-22 15:31:41 UTC (rev 1450) +++ wit/js/main.js 2015-05-22 15:53:43 UTC (rev 1451) @@ -14,6 +14,9 @@ global.scope = window.scope = {}; // Services // +var sessions = require("./js/services/SessionsService.js"); +scope.sessions = sessions; + var timer = require("./js/services/TimerService.js"); scope.timer = timer; Modified: wit/js/services/UserActivityService.js =================================================================== --- wit/js/services/UserActivityService.js 2015-05-22 15:31:41 UTC (rev 1450) +++ wit/js/services/UserActivityService.js 2015-05-22 15:53:43 UTC (rev 1451) @@ -4,12 +4,10 @@ var xprop = require("xprop"); var moment = require("moment"); var log = require("loglevel"); -var db = require("./database.js"); var events = require("events"); var eventEmitter = new events.EventEmitter(); -var crtSession; var activityChangeTimer; var disableActivityDetection = false; @@ -144,119 +142,31 @@ // Let's create a new session but with same color attributes than the last one, // since the active window has not really changed, only some of its attributes - if (crtSession.name !== windowName) { - createNewSession(windowName, crtSession.color); + var crtSession = scope.sessions.getCrtSession(); + if (crtSession && crtSession.name !== windowName) { + scope.sessions.createNewSession(windowName, crtSession.color); } }); }; -var createNewSession = function(windowName, color, startDate) { - var now = moment(); - - // Stop current session - - if (crtSession) { - crtSession.endDate = now.valueOf(); - db.insertSession(crtSession); - } - - // Check if a session corresponding to the given name already exists, - // so that we can retrieve some properties - - var prev = db.getSessionByName(windowName); - - // Create new session - - crtSession = { - name: windowName, - color: color || prev && prev.color || "#" + (Math.floor(Math.random() * (999 - 100 + 1)) + 100), - startDate: startDate || now.valueOf() - }; -}; - -var createFakeSession = function(startDate, endDate) { - var d1 = moment(startDate), d2 = moment(endDate); - - return { - name: "", - color: "transparent", - startDate: d1, - endDate: d2 - }; -}; - /** * Create a new window session */ var addWindowSession = function(xClient, ev) { - var crt = crtSession; + var ss = scope.sessions; + var crtSession = ss.getCrtSession(); return getActiveWindowId(xClient, ev) .then(getWindowName) - .then(createNewSession) - .then(setupActivityDetection.bind(null, crt)); + .then(ss.createNewSession.bind(ss)) + .then(setupActivityDetection.bind(null, crtSession)); }; /////////////// // Exports // /////////////// -/** - * Retrieve window activity sessions for the period [startDAte; endDate] - * If withEmptySession parameter equals true, then if there is a gap between 2 sessions in the results, - * it will be filled with a fake session - */ -exports.getSessions = function(startDate, endDate, withEmptySession) { - var rst; - if (!startDate || !endDate) { - log.error("[UserActivityService] getSessions : parameter missing !"); - - } else { - var d1 = moment(startDate), d2 = moment(endDate); - - rst = db.getSessions(d1.toDate(), d2.toDate()) - .then(function(sessions) { - - // Append the current active session to the sessions list - - return crtSession ? sessions.concat([crtSession]) : sessions; - }) - .then(function(sessions) { - var result = JSON.parse(JSON.stringify(sessions)); - - // Convert results - - var lastSessionEnd = d1, s; - for (var i = 0; i < result.length; i++) { - - s = result[i]; - - // Convert timestamp to moment - - s.startDate = moment(s.startDate); - s.endDate = s.endDate ? moment(s.endDate) : d2; - - // Create an empty session when there is a hole between 2 sessions - - if (withEmptySession && lastSessionEnd.isBefore(s.startDate)) { - result.splice(i, 0, createFakeSession(lastSessionEnd, s.startDate)); - i++; - } - lastSessionEnd = s.endDate; - } - - if (withEmptySession && lastSessionEnd.isBefore(d2)) { - result.push(createFakeSession(lastSessionEnd, d2)); - } - - return result; - }); - } - - return rst; -}; - /** * Offers an api for services interesting in activity change detection */
participants (1)
-
smaisonneuveďź users.nuiton.org