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); } 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