Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/desktop/electron/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
63 changes: 44 additions & 19 deletions packages/desktop/electron/watchers/active-window.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
1 change: 1 addition & 0 deletions packages/server/dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ CLIENT_ID = "sljflsglirjifiorjglksjfj"
CLIENT_SECRET = "KGJOIJIJIJjoijoijoj"
REDIRECT_URL = "www.console.google.com/playground/"
REFRESH_TOKEN = "ljlkhuihliuhluihiuefuofshwotqiu"
DEBUG = true