Author: smaisonneuve Date: 2015-05-22 16:19:29 +0000 (Fri, 22 May 2015) New Revision: 1452 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1452 Log: [UserActivity] Refactoring service by moving sessions utilities to a new service Added: wit/js/services/SessionsService.js Modified: wit/js/components/FilterLogs.js Modified: wit/js/components/FilterLogs.js =================================================================== --- wit/js/components/FilterLogs.js 2015-05-22 15:53:43 UTC (rev 1451) +++ wit/js/components/FilterLogs.js 2015-05-22 16:19:29 UTC (rev 1452) @@ -1,5 +1,7 @@ "use strict"; +var nonce = require("nonce")(); + var FilterLogs = React.createClass({ getCSV: function() { @@ -167,7 +169,6 @@ }, renderTd: function(date, tags, tagObject) { - var nonce = require("nonce")(); var r = []; if (tagObject.duration) { @@ -176,7 +177,7 @@ return state.tags[tag]; }); - var key = date + nonce(); + var key = nonce(); if (filterTags.length) { r.push( @@ -210,7 +211,6 @@ }, render: function() { - var nonce = require("nonce")(); var items = []; var classString = ""; Added: wit/js/services/SessionsService.js =================================================================== --- wit/js/services/SessionsService.js (rev 0) +++ wit/js/services/SessionsService.js 2015-05-22 16:19:29 UTC (rev 1452) @@ -0,0 +1,108 @@ +/** This module is meant to manage activity sessions and keep track of the currently active session **/ + +"use strict"; + +var moment = require("moment"); +var log = require("loglevel"); +var db = require("./database.js"); + +var crtSession; + +var createFakeSession = function(startDate, endDate) { + var d1 = moment(startDate), d2 = moment(endDate); + + return { + name: "", + color: "transparent", + startDate: d1, + endDate: d2 + }; +}; + +exports.getCrtSession = function() { + return crtSession; +}; + +exports.setCrtSession = function(session) { + crtSession = session; +}; + +exports.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() + }; +}; + +/** + * 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; +};