Skip to content

Commit

Permalink
[#295] Update experience sampling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SRichner committed Mar 18, 2024
1 parent 7c1dd1d commit 8c530ad
Showing 1 changed file with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { WindowService } from '../WindowService';
import { Tracker } from './Tracker';
import { getLogger } from '../../../shared/Logger';
import { Settings } from '../../entities/Settings';
import { powerMonitor } from 'electron';

const LOG = getLogger('ExperienceSamplingTracker');

Expand Down Expand Up @@ -41,15 +42,7 @@ export class ExperienceSamplingTracker implements Tracker {
LOG.info(
'Next invocation is in the past, scheduling job to fire in 30 minutes + randomization'
);
const subtractOrAdd: 1 | -1 = Math.random() < 0.5 ? -1 : 1;
const randomization =
this.intervalInMs * this.samplingRandomization * Math.random() * subtractOrAdd;
const nextInvocation = new Date(Date.now() + 30 * 60 * 1000 + randomization);

this.forcedExperienceSamplingJob = schedule.scheduleJob(nextInvocation, async () => {
await this.handleExperienceSamplingJob(nextInvocation);
});
LOG.info(`Resume, scheduled to fire at ${nextInvocation}`);
await this.scheduleNextForcedExperienceSamplingJob();
} else {
await this.startExperienceSamplingJob();
}
Expand Down Expand Up @@ -83,6 +76,39 @@ export class ExperienceSamplingTracker implements Tracker {
await settings.save();
}

private async scheduleNextForcedExperienceSamplingJob(): Promise<void> {
const subtractOrAdd: 1 | -1 = Math.random() < 0.5 ? -1 : 1;
const scheduleAfterResumeInMs = 30 * 60 * 1000;
const randomization =
scheduleAfterResumeInMs * this.samplingRandomization * Math.random() * subtractOrAdd;
const nextInvocation = new Date(Date.now() + scheduleAfterResumeInMs + randomization);

this.forcedExperienceSamplingJob = schedule.scheduleJob(nextInvocation, async () => {
const systemIdleState: 'active' | 'idle' | 'locked' | 'unknown' =
powerMonitor.getSystemIdleState(10 * 60);
LOG.debug(
`scheduleNextForcedExperienceSamplingJob(): System idle state: ${systemIdleState}, assuming idle after 10 minutes of inactivity`
);

const systemIdleTimeInSeconds: number = powerMonitor.getSystemIdleTime();
LOG.debug(
`scheduleNextForcedExperienceSamplingJob(): System idle time: ${systemIdleTimeInSeconds}`
);
if (systemIdleState !== 'active') {
LOG.info(
`scheduleNextForcedExperienceSamplingJob(): System idle time is greater than 30 minutes, not starting experience sampling job`
);
await this.scheduleNextForcedExperienceSamplingJob();
return;
}
await this.handleExperienceSamplingJob(nextInvocation);
});
LOG.info(`scheduleNextForcedExperienceSamplingJob(): scheduled to fire at ${nextInvocation}`);

await this.scheduleNextJob();
await this.startExperienceSamplingJob();
}

private getRandomNextInvocationDate(): Date {
const subtractOrAdd: 1 | -1 = Math.random() < 0.5 ? -1 : 1;
const randomization =
Expand Down

0 comments on commit 8c530ad

Please sign in to comment.