Skip to content

Commit 7ca12f9

Browse files
committed
fix(cache): tweak admin page caching + revert count estimate
1 parent c50b579 commit 7ca12f9

File tree

5 files changed

+30
-55
lines changed

5 files changed

+30
-55
lines changed

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.15.3"
4+
version = "0.15.4"
55
edition = "2024"
66
authors = ["ImShyMike"]
77
readme = "../README.md"

rustytime/src/handlers/page/admin.rs

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -71,48 +71,34 @@ pub async fn admin_dashboard(
7171
let offset = query.offset.max(0);
7272

7373
let cached = app_state.cache.admin.get(&());
74-
let (total_users, total_heartbeats, heartbeats_last_hour, heartbeats_last_24h, daily_activity) =
75-
if let Some(cached) = cached {
76-
(
77-
cached.total_users,
78-
cached.total_heartbeats,
79-
cached.heartbeats_last_hour,
80-
cached.heartbeats_last_24h,
81-
cached.daily_activity,
82-
)
83-
} else {
84-
let mut conn = get_db_conn!(app_state);
85-
86-
let raw_daily_activity = db_query!(
87-
Heartbeat::get_daily_activity_last_week(&mut conn),
88-
"Failed to fetch daily activity"
89-
);
90-
let total_users = db_query!(User::count_total_users(&mut conn, false));
91-
let total_heartbeats = db_query!(Heartbeat::count_total_heartbeats_estimate(&mut conn));
92-
let heartbeats_last_hour = db_query!(Heartbeat::count_heartbeats_last_hour(&mut conn));
93-
let heartbeats_last_24h = db_query!(Heartbeat::count_heartbeats_last_24h(&mut conn));
94-
95-
app_state.cache.admin.insert(
96-
(),
97-
CachedAdminStats {
98-
total_users,
99-
total_heartbeats,
100-
heartbeats_last_hour,
101-
heartbeats_last_24h,
102-
daily_activity: raw_daily_activity.clone(),
103-
},
104-
);
105-
106-
(
107-
total_users,
74+
let (total_heartbeats, daily_activity) = if let Some(cached) = cached {
75+
(cached.total_heartbeats, cached.daily_activity)
76+
} else {
77+
let mut conn = get_db_conn!(app_state);
78+
79+
let raw_daily_activity = db_query!(
80+
Heartbeat::get_daily_activity_last_week(&mut conn),
81+
"Failed to fetch daily activity"
82+
);
83+
let total_heartbeats = db_query!(Heartbeat::count_total_heartbeats(&mut conn));
84+
85+
app_state.cache.admin.insert(
86+
(),
87+
CachedAdminStats {
10888
total_heartbeats,
109-
heartbeats_last_hour,
110-
heartbeats_last_24h,
111-
raw_daily_activity,
112-
)
113-
};
89+
daily_activity: raw_daily_activity.clone(),
90+
},
91+
);
92+
93+
(total_heartbeats, raw_daily_activity)
94+
};
11495

11596
let mut conn = get_db_conn!(app_state);
97+
98+
let total_users = db_query!(User::count_total_users(&mut conn, false));
99+
let heartbeats_last_hour = db_query!(Heartbeat::count_heartbeats_last_hour(&mut conn));
100+
let heartbeats_last_24h = db_query!(Heartbeat::count_heartbeats_last_24h(&mut conn));
101+
116102
let paginated_users = db_query!(
117103
User::list_users_paginated(&mut conn, limit, offset),
118104
"Failed to fetch users"

rustytime/src/models/heartbeat.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ use crate::schema::heartbeats::{self};
1212
use crate::utils::http::parse_user_agent;
1313
use crate::utils::time::{TimeFormat, human_readable_duration};
1414

15-
#[derive(QueryableByName)]
16-
struct ApproximateRowCount {
17-
#[diesel(sql_type = BigInt)]
18-
count: i64,
19-
}
20-
2115
diesel::define_sql_function! {
2216
/// Calculate user duration with filters
2317
#[allow(clippy::too_many_arguments)]
@@ -762,10 +756,8 @@ impl From<Heartbeat> for BulkResponseItem {
762756
}
763757

764758
impl Heartbeat {
765-
pub fn count_total_heartbeats_estimate(conn: &mut PgConnection) -> QueryResult<i64> {
766-
diesel::sql_query("SELECT approximate_row_count('heartbeats') AS count")
767-
.get_result::<ApproximateRowCount>(conn)
768-
.map(|r| r.count)
759+
pub fn count_total_heartbeats(conn: &mut PgConnection) -> QueryResult<i64> {
760+
heartbeats::table.count().get_result(conn)
769761
}
770762

771763
pub fn count_heartbeats_last_24h(conn: &mut PgConnection) -> QueryResult<i64> {

rustytime/src/utils/cache.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ pub struct CachedLeaderboard {
3333

3434
#[derive(Clone)]
3535
pub struct CachedAdminStats {
36-
pub total_users: i64,
3736
pub total_heartbeats: i64,
38-
pub heartbeats_last_hour: i64,
39-
pub heartbeats_last_24h: i64,
4037
pub daily_activity: Vec<DailyActivity>,
4138
}
4239

@@ -65,7 +62,7 @@ impl AppCache {
6562
admin: Arc::new(
6663
Cache::builder()
6764
.max_capacity(1)
68-
.time_to_live(Duration::from_secs(30)) // 30 second TTL
65+
.time_to_live(Duration::from_secs(300)) // 5 minute TTL
6966
.build(),
7067
),
7168
}

0 commit comments

Comments
 (0)