Skip to content

Commit 411e09e

Browse files
committed
fix: improve heartbeat storing logic
1 parent 83d4df1 commit 411e09e

3 files changed

Lines changed: 24 additions & 12 deletions

File tree

rustytime/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rustytime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "rustytime-server"
33
description = "🕒 blazingly fast time tracking for developers"
4-
version = "0.23.2"
4+
version = "0.23.3"
55
edition = "2024"
66
authors = ["ImShyMike"]
77
readme = "../README.md"

rustytime/src/handlers/api/user.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,21 +289,33 @@ async fn store_heartbeats_in_db_internal(
289289
let mut inserted_total = 0usize;
290290

291291
for chunk in new_heartbeats.chunks(HEARTBEAT_INSERT_BATCH_SIZE) {
292-
let chunk_len = chunk.len();
293-
let returned: Vec<Heartbeat> =
294-
instrumented::load("Heartbeat::batch_insert", || {
292+
if include_responses {
293+
let chunk_len = chunk.len();
294+
let returned: Vec<Heartbeat> =
295+
instrumented::load("Heartbeat::batch_insert", || {
296+
diesel::insert_into(heartbeats::table)
297+
.values(chunk)
298+
.on_conflict((heartbeats::user_id, heartbeats::time))
299+
.do_update()
300+
.set(heartbeats::time.eq(excluded(heartbeats::time)))
301+
.returning(heartbeats::all_columns)
302+
.load::<Heartbeat>(conn)
303+
})?;
304+
305+
inserted_total += returned.len().min(chunk_len);
306+
for hb in returned {
307+
inserted_map.insert((hb.user_id, hb.time), hb);
308+
}
309+
} else {
310+
let count = instrumented::first("Heartbeat::batch_insert_count", || {
295311
diesel::insert_into(heartbeats::table)
296312
.values(chunk)
297313
.on_conflict((heartbeats::user_id, heartbeats::time))
298-
.do_update()
299-
.set(heartbeats::time.eq(excluded(heartbeats::time)))
300-
.returning(heartbeats::all_columns)
301-
.load::<Heartbeat>(conn)
314+
.do_nothing()
315+
.execute(conn)
302316
})?;
303317

304-
inserted_total += returned.len().min(chunk_len);
305-
for hb in returned {
306-
inserted_map.insert((hb.user_id, hb.time), hb);
318+
inserted_total += count;
307319
}
308320
}
309321

0 commit comments

Comments
 (0)