r1275 - in wit/js: components services
Author: smaisonneuve Date: 2015-04-28 09:09:02 +0000 (Tue, 28 Apr 2015) New Revision: 1275 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1275 Log: [Wit] - Create a database for window sessions Modified: wit/js/components/Timeline.js wit/js/services/UserActivityService.js wit/js/services/database.js Modified: wit/js/components/Timeline.js =================================================================== --- wit/js/components/Timeline.js 2015-04-27 16:12:35 UTC (rev 1274) +++ wit/js/components/Timeline.js 2015-04-28 09:09:02 UTC (rev 1275) @@ -4,14 +4,15 @@ poolingInterval: 5 * 1000, getInitialState: function() { - return this._refreshSessions(); + return { + sessions: [], + startDate: moment().subtract(1, "m"), + endDate: moment() + }; }, componentDidMount: function () { - var self = this; - this.pollingTimer = setInterval(function () { - self.setState(self._refreshSessions()); - }, this.poolingInterval); + this.pollingTimer = setInterval(this._refreshSessions.bind(this), this.poolingInterval); }, componentWillUnmount: function () { @@ -19,18 +20,21 @@ }, _refreshSessions: function () { - // Retrieve window focus activity + var self = this; - var startDate = moment().subtract(1, "h"), + var startDate = moment().subtract(1, "m"), endDate = moment(); + + // Retrieve window focus activity - var sessions = user.getSessions(startDate, endDate); - - return { - sessions: sessions, - startDate: startDate, - endDate: endDate - }; + user.getSessions(startDate, endDate) + .then(function (sessions) { + self.setState({ + sessions: sessions, + startDate: startDate, + endDate: endDate + }); + }); }, render: function() { @@ -42,9 +46,9 @@ var items = [], data; - if (sessions[0].startDate.isAfter(startDate)) { + if (sessions.length && startDate.isBefore(sessions[0].startDate)) { - data = { width: (sessions[0].startDate.diff(startDate) / endDate.diff(startDate) * 100 ) +"%" } + data = { width: Math.abs(startDate.diff(sessions[0].startDate) / endDate.diff(startDate) * 100 ) +"%" } items = [<TimelineSession key={startDate.unix()} data={data}/>]; } @@ -58,7 +62,7 @@ grow: !session.duration ? "1":"0" }; - return <TimelineSession key={session.startDate.unix()} data={data} /> + return <TimelineSession key={session.startDate} data={data} /> }) ); Modified: wit/js/services/UserActivityService.js =================================================================== --- wit/js/services/UserActivityService.js 2015-04-27 16:12:35 UTC (rev 1274) +++ wit/js/services/UserActivityService.js 2015-04-28 09:09:02 UTC (rev 1275) @@ -1,29 +1,22 @@ var x11 = require('x11'); var xprop = require('xprop'); var moment = require('moment'); +var db = require("./database.js"); -var windowSessions = []; +var crtSession; const _NET_ACTIVE_WINDOW = 334; exports.getSessions = function (startDate, endDate) { - var result = windowSessions, m; + var d1 = moment(startDate), d2 = moment(endDate); - if (startDate) { - m = moment(startDate); - result = result.filter(function(s) { - return m.isBefore(s.endDate); - }); - } - - if (endDate) { - m = moment(endDate); - result = result.filter(function(s) { - return m.isAfter(s.startDate); - }); - } - - return result; + return db.getSessions(d1.toDate(), d2.toDate()) + .then(function (sessions) { + + // Append the current active session to the sessions list + + return crtSession ? sessions.concat([crtSession]) : sessions + }); } /* Inspired by : @@ -85,10 +78,10 @@ return getWindowName(wid) .then(function (windowName) { - // Let's create a new seesion but with same color attributes as the last one, since the active window has not change, only some of its attibutes + // Let's create a new seesion but with same color attributes than the last one, + // since the active window has not really changed, only some of its attibutes - var last = windowSessions[windowSessions.length - 1]; - if (last.name != windowName) { + if (currentSession.name != windowName) { createNewSession(windowName, last.color); } @@ -133,25 +126,21 @@ }; var createNewSession = function (windowName, color, startDate) { - if (windowSessions.length) { - var now = Date.now(); - var last = windowSessions[windowSessions.length - 1]; - last.endDate = moment(); - last.duration = last.endDate.diff(last.startDate); + if (crtSession) { + var now = moment(); + crtSession.endDate = now.valueOf(); + crtSession.duration = now.diff(crtSession.startDate); + db.insertSession(crtSession); } // Check if a session corresponding to the given name already exists, // so that we can retrieve some properties - var prev = getSessionByName(windowName); + var prev = db.getSessionByName(windowName); - windowSessions.push({ + crtSession = { name: windowName, color: color || prev && prev.color || "#" + (Math.floor(Math.random() * (999 - 100 + 1)) + 100), - startDate: startDate || moment() - }); -}; - -var getSessionByName = function (name) { - return windowSessions.filter(function (s) { return s.name == name })[0]; + startDate: startDate || moment().valueOf() + }; }; \ No newline at end of file Modified: wit/js/services/database.js =================================================================== --- wit/js/services/database.js 2015-04-27 16:12:35 UTC (rev 1274) +++ wit/js/services/database.js 2015-04-28 09:09:02 UTC (rev 1275) @@ -1,11 +1,18 @@ -var path = (process.env.HOME || process.env.USERPROFILE) + "/.wit/wit.json"; +var path = (process.env.HOME || process.env.USERPROFILE) + "/.wit"; var Datastore = require('nedb'); -var db = new Datastore({filename : path, autoload: true}); +var db = { + logs: new Datastore({filename : path + "/logs.json", autoload: true}), + sessions: new Datastore({filename : path + "/sessions.json", autoload: true}) +}; +/******************** + * Logs db management + *********************/ + exports.insertLog = function(tags, startDate, endDate) { return new Promise(function(resolve, reject) { - db.insert({ + db.logs.insert({ tags: tags, startDate: startDate.getTime(), endDate: endDate.getTime() @@ -27,7 +34,7 @@ if (endDate) { query.endDate = { $lte: endDate.getTime() } } return new Promise(function(resolve, reject) { - db.find(query).sort({ startDate: -1 }).exec(function(err, docs) { + db.logs.find(query).sort({ startDate: -1 }).exec(function(err, docs) { if (!err) { resolve(docs); } else { @@ -40,7 +47,7 @@ exports.getHistory = function(index) { return new Promise(function(resolve, reject) { - db.find({}).sort({ startDate: -1 }).skip(index).limit(1).exec(function (err, docs) { + db.logs.find({}).sort({ startDate: -1 }).skip(index).limit(1).exec(function (err, docs) { if (!err) { resolve(docs); } else { @@ -49,3 +56,51 @@ }); }); }; + +/******************************** + * Window sessions db management + ********************************/ + +exports.insertSession = function (session) { + return new Promise(function(resolve, reject) { + db.sessions.insert(session, + function(err, newDocs) { + if (!err) { + resolve(newDocs); + } else { + reject(err); + } + }); + }); +}; + +exports.getSessionByName = function (name) { + return new Promise(function(resolve, reject) { + db.sessions.findOne({ + name: name + }, function(err, docs) { + if (!err) { + resolve(docs); + } else { + reject(err); + } + }); + }); +}; + +exports.getSessions = function (startDate, endDate) { + var query = {}; + + if (endDate) { query.startDate = { $lte: endDate.getTime() }; } + if (startDate) { query.endDate = { $gte : startDate.getTime() }; } + + return new Promise(function(resolve, reject) { + db.sessions.find(query).sort({ startDate: 1 }).exec(function(err, docs) { + if (!err) { + resolve(docs); + } else { + reject(err); + } + }); + }); +}; \ No newline at end of file
participants (1)
-
smaisonneuveďź users.nuiton.org