From 75ec645d3d0cdaf9ccf1dee078c49aef1ab0a04c Mon Sep 17 00:00:00 2001 From: KShivendu Date: Wed, 9 Feb 2022 16:56:53 +0530 Subject: [PATCH 1/2] feat(desktop): Add system idle detection and set tracker interval to 0.5s --- packages/desktop/electron/storage/server.js | 2 +- packages/desktop/electron/tracker.js | 2 +- .../electron/watchers/active-window.js | 63 +++++++++++++------ 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/packages/desktop/electron/storage/server.js b/packages/desktop/electron/storage/server.js index ed9484b..03ab0ba 100644 --- a/packages/desktop/electron/storage/server.js +++ b/packages/desktop/electron/storage/server.js @@ -67,7 +67,7 @@ export class ServerStorage { saveActivity(activity) { if (!this.jwtToken) { - console.log('User not authenticated, unable to store data on server'); + // console.log('NO_JWT'); // User not authenticated, unable to store data on server // TODO: Open authentication page in browser (and local express server) return; } diff --git a/packages/desktop/electron/tracker.js b/packages/desktop/electron/tracker.js index cfe0cbe..2f767f4 100644 --- a/packages/desktop/electron/tracker.js +++ b/packages/desktop/electron/tracker.js @@ -5,7 +5,7 @@ import getIcon from './getIcon.js'; import { ignoreList } from './ignorelist.js'; export function startTracker() { - const interval = 2000; + const interval = 500; const storages = [ new JSONStorage(), diff --git a/packages/desktop/electron/watchers/active-window.js b/packages/desktop/electron/watchers/active-window.js index bdfac9c..c903a47 100644 --- a/packages/desktop/electron/watchers/active-window.js +++ b/packages/desktop/electron/watchers/active-window.js @@ -1,30 +1,33 @@ import activeWin from 'active-win'; +import { powerMonitor } from 'electron'; +const IDLE_TIME_THRESHOLD = 60; export class ActiveWindowWatcher { /** * @param {number} interval Polling interval * @param {(activity) => void} changeCallback */ constructor(interval = 1000, changeCallback) { - this.startTime = null; - this.app = null; + this.prevWindowStartTime = null; + this.prevWindow = null; this.changeCallback = changeCallback; this.interval = interval; + this.prevActive = false; } /** * Storing the start time of the active window * Collecting data of the window which will be active */ - storeTime() { - const endTime = Date.now(); - const startTime = this.startTime; + endPrevWindow(endTime) { + endTime = endTime ?? Date.now(); + const startTime = this.prevWindowStartTime; const { owner: { name, path }, title, url, - } = this.app; + } = this.prevWindow; const data = { name, @@ -40,28 +43,50 @@ export class ActiveWindowWatcher { /** * Checks the active window is specific time interval - * and whenever the active window changes stores the time difference by calling {@link ActiveWindowWatcher.storeTime} function + * and whenever the active window changes stores the time difference by calling {@link ActiveWindowWatcher.endPrevWindow} function */ tracker() { setInterval(async () => { + const now = Date.now(); const activeWindow = await activeWin(); + const idleTime = powerMonitor.getSystemIdleTime(); + // process.stdout.write(idleTime + '\n'); - if (activeWindow === undefined) return; + const startNewActivity = () => { + this.prevWindow = activeWindow; + this.prevWindowStartTime = now; + process.stdout.write('\n' + activeWindow.title); + }; - if (!this.app) { - this.startTime = Date.now(); - this.app = activeWindow; - process.stdout.write(activeWindow.title); + if (!this.prevActive && idleTime == 0) { + // System was previously idle and is now active + + // Start new activity and store that the system is now active + startNewActivity(); + this.prevActive = true; + return; + } else if ( + (this.prevActive && idleTime >= IDLE_TIME_THRESHOLD) || + activeWindow === undefined + ) { + // System was previously active and is now idle for a while + // OR there's no active window at the moment. + + // End previous block and store that the system is now idle + this.endPrevWindow(now); + this.prevActive = false; + return; } - process.stdout.write('.'); - //If the active window is changed store the used time data. - if (activeWindow.title !== this.app.title) { - console.log(''); - this.storeTime(); - this.app = null; + if (activeWindow.title !== this.prevWindow.title) { + // If active window has changed + + // End previous activity block and start a new one + this.endPrevWindow(now); + startNewActivity(); + } else { + process.stdout.write('.'); } - process.stdout.write('.'); }, this.interval); } From d6f70fb62d3c30cca9a3c245ece7327eb64811f4 Mon Sep 17 00:00:00 2001 From: KShivendu Date: Wed, 9 Feb 2022 17:14:50 +0530 Subject: [PATCH 2/2] fix(desktop/storage/server.js): Remove unreleated change in logging --- packages/desktop/electron/storage/server.js | 2 +- packages/server/config.js | 4 ++-- packages/server/dev.env | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/desktop/electron/storage/server.js b/packages/desktop/electron/storage/server.js index 03ab0ba..ed9484b 100644 --- a/packages/desktop/electron/storage/server.js +++ b/packages/desktop/electron/storage/server.js @@ -67,7 +67,7 @@ export class ServerStorage { saveActivity(activity) { if (!this.jwtToken) { - // console.log('NO_JWT'); // User not authenticated, unable to store data on server + console.log('User not authenticated, unable to store data on server'); // TODO: Open authentication page in browser (and local express server) return; } diff --git a/packages/server/config.js b/packages/server/config.js index 39854e7..1f26260 100644 --- a/packages/server/config.js +++ b/packages/server/config.js @@ -11,5 +11,5 @@ try { dotenv.config({ path: dirname + `/dev.env` }); -export const useLocal = - process.env.LOCAL_STORAGE && process.env.LOCAL_STORAGE === 'true'; +export const DEBUG = process.env.DEBUG === 'true'; +export const useLocal = process.env.LOCAL_STORAGE === 'true'; diff --git a/packages/server/dev.env b/packages/server/dev.env index 2be39b6..f302330 100644 --- a/packages/server/dev.env +++ b/packages/server/dev.env @@ -12,3 +12,4 @@ CLIENT_ID = "sljflsglirjifiorjglksjfj" CLIENT_SECRET = "KGJOIJIJIJjoijoijoj" REDIRECT_URL = "www.console.google.com/playground/" REFRESH_TOKEN = "ljlkhuihliuhluihiuefuofshwotqiu" +DEBUG = true \ No newline at end of file