Skip to content

Commit 37cc919

Browse files
committed
fix: heartbeats incorrectly displayed as total imported days
1 parent 6d0de0e commit 37cc919

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

server/utils/wakapi.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async function fetchWakApiHeartbeats(
5050
headers: any,
5151
startDate: Date,
5252
endDate: Date,
53-
userId: string,
53+
userId: string
5454
) {
5555
const today = new Date();
5656
const adjustedEndDate = new Date();
@@ -81,12 +81,12 @@ async function fetchWakApiHeartbeats(
8181
{
8282
params: { date: dateStr },
8383
headers,
84-
},
84+
}
8585
);
8686

8787
if (heartbeatsResponse?.data && heartbeatsResponse.data.length > 0) {
8888
const heartbeats = heartbeatsResponse.data.map((h) =>
89-
processWakaApiHeartbeat(h, userId),
89+
processWakaApiHeartbeat(h, userId)
9090
);
9191

9292
heartbeatsByDate.set(dateStr, heartbeats);
@@ -100,7 +100,7 @@ export async function prepareWakApiData(
100100
apiKey: string,
101101
instanceUrl: string,
102102
userId: string,
103-
job?: ImportJob,
103+
job?: ImportJob
104104
): Promise<Map<string, any[]>> {
105105
if (job) {
106106
updateJob(job, {
@@ -126,7 +126,7 @@ export async function prepareWakApiData(
126126

127127
if (!allTimeResponse?.data?.range) {
128128
throw new Error(
129-
`Failed to fetch activity date range from WakAPI for user ${userId}`,
129+
`Failed to fetch activity date range from WakAPI for user ${userId}`
130130
);
131131
}
132132

@@ -145,7 +145,7 @@ export async function prepareWakApiData(
145145
headers,
146146
startDate,
147147
endDate,
148-
userId,
148+
userId
149149
);
150150

151151
return heartbeatsByDate;
@@ -154,14 +154,14 @@ export async function prepareWakApiData(
154154
export async function handleWakApiSequentialImport(
155155
heartbeatsByDate: Map<string, any[]>,
156156
userId: string,
157-
job: ImportJob,
157+
job: ImportJob
158158
): Promise<{ success: boolean; imported?: number; message?: string }> {
159159
try {
160160
handleLog(`[wakapi] Starting WakAPI sequential import for user ${userId}`);
161161

162162
const datesWithData = Array.from(heartbeatsByDate.keys());
163163
handleLog(
164-
`[wakapi] Processing ${datesWithData.length} days of data sequentially`,
164+
`[wakapi] Processing ${datesWithData.length} days of data sequentially`
165165
);
166166

167167
updateJob(job, {
@@ -170,14 +170,12 @@ export async function handleWakApiSequentialImport(
170170
total: datesWithData.length,
171171
});
172172

173-
let totalHeartbeats = 0;
174173
for (let i = 0; i < datesWithData.length; i++) {
175174
const dateStr = datesWithData[i];
176175
const heartbeats = heartbeatsByDate.get(dateStr);
177176

178177
if (heartbeats && heartbeats.length > 0) {
179178
await processHeartbeatsByDate(userId, heartbeats);
180-
totalHeartbeats += heartbeats.length;
181179
}
182180

183181
updateJob(job, {
@@ -193,13 +191,13 @@ export async function handleWakApiSequentialImport(
193191
});
194192

195193
handleLog(
196-
`[wakapi] Successfully imported ${totalHeartbeats} heartbeats from WakAPI`,
194+
`[wakapi] Successfully imported ${datesWithData.length} days from WakAPI`
197195
);
198196

199197
return {
200198
success: true,
201-
imported: totalHeartbeats,
202-
message: `Successfully imported ${totalHeartbeats} heartbeats`,
199+
imported: datesWithData.length,
200+
message: `Successfully imported ${datesWithData.length} days`,
203201
};
204202
} catch (error: any) {
205203
const errorMessage = error instanceof Error ? error.message : String(error);
@@ -209,21 +207,21 @@ export async function handleWakApiSequentialImport(
209207
});
210208

211209
handleLog(
212-
`[wakapi] WakAPI import failed for user ${userId}: ${errorMessage}`,
210+
`[wakapi] WakAPI import failed for user ${userId}: ${errorMessage}`
213211
);
214212

215213
throw handleApiError(
216214
69,
217215
`WakAPI import failed for user ${userId}: ${errorMessage}`,
218-
"Failed to import data from WakAPI. Please check your API key and try again.",
216+
"Failed to import data from WakAPI. Please check your API key and try again."
219217
);
220218
}
221219
}
222220

223221
export async function handleWakApiDateChunk(
224222
dates: string[],
225223
userId: string,
226-
heartbeatsByDate: Record<string, any[]>,
224+
heartbeatsByDate: Record<string, any[]>
227225
): Promise<{ success: boolean; processed: number }> {
228226
try {
229227
let totalProcessed = 0;
@@ -242,7 +240,7 @@ export async function handleWakApiDateChunk(
242240
};
243241
} catch (error) {
244242
handleLog(
245-
`[wakapi] Failed to process date chunk for user ${userId}: ${error}`,
243+
`[wakapi] Failed to process date chunk for user ${userId}: ${error}`
246244
);
247245
throw error;
248246
}
@@ -252,7 +250,7 @@ export async function handleWakApiImport(
252250
apiKey: string,
253251
instanceUrl: string,
254252
userId: string,
255-
existingJob?: ImportJob,
253+
existingJob?: ImportJob
256254
): Promise<{ success: boolean; imported?: number; message?: string }> {
257255
const job: ImportJob = existingJob || {
258256
id: randomUUID(),
@@ -279,7 +277,7 @@ export async function handleWakApiImport(
279277
const heartbeatsByDate = await prepareWakApiData(
280278
apiKey,
281279
instanceUrl,
282-
userId,
280+
userId
283281
);
284282

285283
return await handleWakApiSequentialImport(heartbeatsByDate, userId, job);
@@ -291,13 +289,13 @@ export async function handleWakApiImport(
291289
});
292290

293291
handleLog(
294-
`[wakapi] WakAPI import failed for user ${userId}: ${errorMessage}`,
292+
`[wakapi] WakAPI import failed for user ${userId}: ${errorMessage}`
295293
);
296294

297295
throw handleApiError(
298296
69,
299297
`WakAPI import failed for user ${userId}: ${errorMessage}`,
300-
"Failed to import data from WakAPI. Please check your API key and try again.",
298+
"Failed to import data from WakAPI. Please check your API key and try again."
301299
);
302300
}
303301
}

server/utils/wakatime.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ async function createDataDumpRequest(apiKey: string): Promise<void> {
8888
}
8989
}
9090

91-
async function pollForDataDump(apiKey: string): Promise<WakatimeDataDump> {
91+
async function pollForDataDump(job: ImportJob | undefined, apiKey: string): Promise<WakatimeDataDump> {
9292
const url = `${Endpoints.WakatimeApiUrl}${Endpoints.WakatimeApiDataDumpUrl}`;
9393
const maxAttempts = 180;
9494
let attempts = 0;
@@ -112,6 +112,14 @@ async function pollForDataDump(apiKey: string): Promise<WakatimeDataDump> {
112112
return heartbeatsDump;
113113
}
114114

115+
if (job) {
116+
updateJob(job, {
117+
status: ImportStatus.WaitingForDataDump,
118+
current: heartbeatsDump.percent_complete,
119+
total: 100
120+
})
121+
}
122+
115123
await new Promise((resolve) => setTimeout(resolve, 10000));
116124
attempts++;
117125
}
@@ -246,7 +254,7 @@ export async function prepareWakatimeApiData(
246254
status: ImportStatus.WaitingForDataDump,
247255
});
248256
}
249-
const dataDump = await pollForDataDump(apiKey);
257+
const dataDump = await pollForDataDump(job, apiKey);
250258

251259
if (job) {
252260
updateJob(job, {
@@ -303,15 +311,13 @@ export async function handleWakatimeImport(
303311
total: daysWithHeartbeats.length,
304312
});
305313

306-
let totalHeartbeats = 0;
307314
for (let i = 0; i < daysWithHeartbeats.length; i++) {
308315
const day = daysWithHeartbeats[i];
309316
const processedHeartbeats = day.heartbeats.map((h: any) =>
310317
mapHeartbeat(h, userAgents, userId, exportData.user.last_plugin),
311318
);
312319

313320
await processHeartbeatsByDate(userId, processedHeartbeats);
314-
totalHeartbeats += processedHeartbeats.length;
315321

316322
updateJob(job, {
317323
status: ImportStatus.ProcessingHeartbeats,
@@ -322,17 +328,17 @@ export async function handleWakatimeImport(
322328

323329
updateJob(job, {
324330
status: ImportStatus.Completed,
325-
importedCount: totalHeartbeats,
331+
importedCount: daysWithHeartbeats.length,
326332
});
327333

328334
handleLog(
329-
`[wakatime] Successfully imported ${totalHeartbeats} heartbeats from WakaTime API`,
335+
`[wakatime] Successfully imported ${daysWithHeartbeats.length} days from WakaTime API`,
330336
);
331337

332338
return {
333339
success: true,
334-
imported: totalHeartbeats,
335-
message: `Successfully imported ${totalHeartbeats} heartbeats`,
340+
imported: daysWithHeartbeats.length,
341+
message: `Successfully imported ${daysWithHeartbeats.length} days`,
336342
};
337343
} catch (error: any) {
338344
const errorMessage = error instanceof Error ? error.message : String(error);
@@ -401,7 +407,6 @@ export async function handleWakatimeFileImport(
401407
});
402408

403409
const userAgents = new Map<string, WakatimeUserAgent>();
404-
let totalHeartbeats = 0;
405410

406411
for (let i = 0; i < daysWithHeartbeats.length; i++) {
407412
const day = daysWithHeartbeats[i];
@@ -410,7 +415,6 @@ export async function handleWakatimeFileImport(
410415
);
411416

412417
await processHeartbeatsByDate(userId, processedHeartbeats);
413-
totalHeartbeats += processedHeartbeats.length;
414418

415419
updateJob(job, {
416420
status: ImportStatus.ProcessingHeartbeats,
@@ -425,13 +429,13 @@ export async function handleWakatimeFileImport(
425429
});
426430

427431
handleLog(
428-
`[wakatime] Successfully imported ${totalHeartbeats} heartbeats from WakaTime file`,
432+
`[wakatime] Successfully imported ${daysWithHeartbeats.length} days from WakaTime file`,
429433
);
430434

431435
return {
432436
success: true,
433-
imported: totalHeartbeats,
434-
message: `Successfully imported ${totalHeartbeats} heartbeats`,
437+
imported: daysWithHeartbeats.length,
438+
message: `Successfully imported ${daysWithHeartbeats.length} days`,
435439
};
436440
}
437441
} catch (error: any) {

0 commit comments

Comments
 (0)