Skip to content

Commit

Permalink
Buffer heartbeats in offline db and only send to API when we have 25 …
Browse files Browse the repository at this point in the history
…heartbeats
  • Loading branch information
alanhamlett committed Jul 20, 2024
1 parent b153917 commit 7638c99
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/wakatime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export class WakaTime {
private resourcesLocation: string;
private lastApiKeyPrompted: number = 0;
private isMetricsEnabled: boolean = false;
private heartbeatsOfflineOnlySentAt: number = 0;
private heartbeatsOfflineOnlyCount: number = 0;

constructor(extensionPath: string, logger: Logger) {
this.extensionPath = extensionPath;
Expand Down Expand Up @@ -368,7 +370,9 @@ export class WakaTime {
if (apiUrl.value?.trim()) {
try {
const parsedUrl = new URL(apiUrl.value);
url = `${parsedUrl.protocol}//${parsedUrl.hostname}${parsedUrl.port ? `:${parsedUrl.port}` : ''}`;
url = `${parsedUrl.protocol}//${parsedUrl.hostname}${
parsedUrl.port ? `:${parsedUrl.port}` : ''
}`;
} catch (e) {
this.logger.warnException(e);
}
Expand Down Expand Up @@ -549,6 +553,14 @@ export class WakaTime {

args.push('--entity', Utils.quote(file));

if (this._useOfflineOnly()) {
args.push('--offline-only');
this.heartbeatsOfflineOnlyCount += 1;
} else {
this.heartbeatsOfflineOnlySentAt = Date.now();
this.heartbeatsOfflineOnlyCount = 0;
}

let user_agent =
this.agentName + '/' + vscode.version + ' vscode-wakatime/' + this.extension.version;
args.push('--plugin', Utils.quote(user_agent));
Expand Down Expand Up @@ -645,6 +657,13 @@ export class WakaTime {
});
}

private _useOfflineOnly() {
if (!this.heartbeatsOfflineOnlySentAt) return false;
if (this.heartbeatsOfflineOnlyCount >= 24) return false;
if (this.heartbeatsOfflineOnlySentAt + 120000 < Date.now()) return false;
return true;
}

private getCodingActivity() {
if (!this.showStatusBar) return;

Expand Down

0 comments on commit 7638c99

Please sign in to comment.