Skip to content

Commit 245e231

Browse files
committed
Buffer heartbeats and use bulk api endpoint on web extension
1 parent 32daa8f commit 245e231

File tree

4 files changed

+180
-146
lines changed

4 files changed

+180
-146
lines changed

src/constants.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,18 @@ export enum LogLevel {
1818
export const AI_RECENT_PASTES_TIME_MS = 500;
1919
export const TIME_BETWEEN_HEARTBEATS_MS = 120000;
2020
export const SEND_BUFFER_SECONDS = 30;
21+
22+
export interface Heartbeat {
23+
time: number;
24+
entity: string;
25+
is_write: boolean;
26+
lineno: number;
27+
cursorpos: number;
28+
lines_in_file: number;
29+
alternate_project?: string;
30+
project_folder?: string;
31+
project_root_count?: number;
32+
language?: string;
33+
category?: 'debugging' | 'ai coding' | 'building' | 'code reviewing';
34+
is_unsaved_entity?: boolean;
35+
}

src/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ export class Utils {
140140
return e.contentChanges.length === 1 && e.contentChanges?.[0].text.trim().length > 2;
141141
}
142142

143+
public static getFocusedFile(document?: vscode.TextDocument): string | undefined {
144+
const doc = document ?? vscode.window.activeTextEditor?.document;
145+
if (doc) {
146+
const file = doc.fileName;
147+
if (Utils.isRemoteUri(doc.uri)) {
148+
return `${doc.uri.authority}${doc.uri.path}`.replace('ssh-remote+', 'ssh://');
149+
// TODO: how to support 'dev-container', 'attached-container', 'wsl', and 'codespaces' schemes?
150+
}
151+
return file;
152+
}
153+
}
154+
143155
public static isPossibleHumanCodeInsert(e: vscode.TextDocumentChangeEvent): boolean {
144156
if (e.contentChanges.length !== 1) return false;
145157
if (

src/wakatime.ts

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as vscode from 'vscode';
77
import {
88
AI_RECENT_PASTES_TIME_MS,
99
COMMAND_DASHBOARD,
10+
Heartbeat,
1011
LogLevel,
1112
SEND_BUFFER_SECONDS,
1213
} from './constants';
@@ -26,19 +27,6 @@ interface FileSelectionMap {
2627
[key: string]: FileSelection;
2728
}
2829

29-
interface Heartbeat {
30-
time: number;
31-
entity: string;
32-
is_write: boolean;
33-
lineno: number;
34-
cursorpos: number;
35-
lines_in_file: number;
36-
alternate_project?: string;
37-
project_folder?: string;
38-
category?: 'debugging' | 'ai coding' | 'building' | 'code reviewing';
39-
is_unsaved_entity?: boolean;
40-
}
41-
4230
export class WakaTime {
4331
private agentName: string;
4432
private extension: any;
@@ -544,45 +532,45 @@ export class WakaTime {
544532
if (Date.now() - this.lastSent > SEND_BUFFER_SECONDS * 1000) {
545533
this.sendHeartbeats();
546534
}
547-
548535
clearTimeout(this.debounceTimeoutId);
549536
this.debounceTimeoutId = setTimeout(() => {
550537
if (this.disabled) return;
551-
let editor = vscode.window.activeTextEditor;
538+
const editor = vscode.window.activeTextEditor;
552539
if (editor) {
553-
let doc = editor.document;
540+
const doc = editor.document;
554541
if (doc) {
555-
let file: string = doc.fileName;
556-
if (file) {
557-
if (this.currentlyFocusedFile !== file) {
558-
this.updateTeamStatusBarFromJson();
559-
this.updateTeamStatusBar(doc);
560-
}
542+
const file = Utils.getFocusedFile(doc);
543+
if (!file) {
544+
return;
545+
}
546+
if (this.currentlyFocusedFile !== file) {
547+
this.updateTeamStatusBarFromJson();
548+
this.updateTeamStatusBar(doc);
549+
}
561550

562-
let time: number = Date.now();
563-
if (
564-
isWrite ||
565-
Utils.enoughTimePassed(this.lastHeartbeat, time) ||
566-
this.lastFile !== file ||
567-
this.lastDebug !== this.isDebugging ||
568-
this.lastCompile !== this.isCompiling ||
569-
this.lastAICodeGenerating !== this.isAICodeGenerating
570-
) {
571-
this.appendHeartbeat(
572-
doc,
573-
time,
574-
editor.selection.start,
575-
isWrite,
576-
this.isCompiling,
577-
this.isDebugging,
578-
this.isAICodeGenerating,
579-
);
580-
this.lastFile = file;
581-
this.lastHeartbeat = time;
582-
this.lastDebug = this.isDebugging;
583-
this.lastCompile = this.isCompiling;
584-
this.lastAICodeGenerating = this.isAICodeGenerating;
585-
}
551+
const time: number = Date.now();
552+
if (
553+
isWrite ||
554+
Utils.enoughTimePassed(this.lastHeartbeat, time) ||
555+
this.lastFile !== file ||
556+
this.lastDebug !== this.isDebugging ||
557+
this.lastCompile !== this.isCompiling ||
558+
this.lastAICodeGenerating !== this.isAICodeGenerating
559+
) {
560+
this.appendHeartbeat(
561+
doc,
562+
time,
563+
editor.selection.start,
564+
isWrite,
565+
this.isCompiling,
566+
this.isDebugging,
567+
this.isAICodeGenerating,
568+
);
569+
this.lastFile = file;
570+
this.lastHeartbeat = time;
571+
this.lastDebug = this.isDebugging;
572+
this.lastCompile = this.isCompiling;
573+
this.lastAICodeGenerating = this.isAICodeGenerating;
586574
}
587575
}
588576
}
@@ -600,12 +588,8 @@ export class WakaTime {
600588
): Promise<void> {
601589
if (!this.dependencies.isCliInstalled()) return;
602590

603-
let file = doc.fileName;
604-
if (Utils.isRemoteUri(doc.uri)) {
605-
file = `${doc.uri.authority}${doc.uri.path}`;
606-
file = file.replace('ssh-remote+', 'ssh://');
607-
// TODO: how to support 'dev-container', 'attached-container', 'wsl', and 'codespaces' schemes?
608-
}
591+
const file = Utils.getFocusedFile(doc);
592+
if (!file) return;
609593

610594
// prevent sending the same heartbeat (https://github.com/wakatime/vscode-wakatime/issues/163)
611595
if (isWrite && this.isDuplicateHeartbeat(file, time, selection)) return;
@@ -905,11 +889,9 @@ export class WakaTime {
905889
if (!doc) return;
906890
}
907891

908-
let file = doc.fileName;
909-
if (Utils.isRemoteUri(doc.uri)) {
910-
file = `${doc.uri.authority}${doc.uri.path}`;
911-
file = file.replace('ssh-remote+', 'ssh://');
912-
// TODO: how to support 'dev-container', 'attached-container', 'wsl', and 'codespaces' schemes?
892+
const file = Utils.getFocusedFile(doc);
893+
if (!file) {
894+
return;
913895
}
914896

915897
this.currentlyFocusedFile = file;

0 commit comments

Comments
 (0)