From 1f7ef933ba11f1bb8a0cba6d68b0e4125983a7df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 24 Oct 2026 02:38:08 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20contribution=20calendar?= =?UTF-8?q?=20counts=20days=20and=20today=20in=20the=20viewer's=20timezone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Events are grouped by minute and placed on their local day, so the calendar no longer ends at yesterday for zones ahead of UTC between midnight and their offset. --- crates/forge-core/src/store/contributions.rs | 53 ++++++++++++++------ crates/forge-core/src/tz.rs | 12 ++++- crates/forge-web/src/pages/dashboard.rs | 32 ++++++++---- 3 files changed, 71 insertions(+), 26 deletions(-) diff --git a/crates/forge-core/src/store/contributions.rs b/crates/forge-core/src/store/contributions.rs index 5b8bcdf..1540c5b 100644 --- a/crates/forge-core/src/store/contributions.rs +++ b/crates/forge-core/src/store/contributions.rs @@ -1,32 +1,39 @@ //! Daily contribution counts for one user, for the dashboard heatmap. +use std::collections::BTreeMap; + +use jiff::Timestamp; use turso::Value; use super::push_recent::{ZERO_OID_SHA1, ZERO_OID_SHA256}; use super::{Store, col_int, col_text, fetch_all}; use crate::error::Result; +use crate::tz::local_date; impl Store { - /// Contributions per UTC day by `user_id` at or after `since` (an RFC 3339 stamp), as + /// Contributions per day in `tz` by `user_id` at or after `since` (an RFC 3339 stamp), as /// `(YYYY-MM-DD, count)` sorted by day ascending; days with no activity are left out. + /// Events are grouped by minute and each minute is placed on its local day, so zones with + /// half-hour or quarter-hour offsets and DST changes land on the right day. /// A contribution is a push (ref deletions are left out), an issue or pull request opened /// (pull requests are `issues` rows with `kind = 'pull'`), or a comment. pub async fn contribution_counts( &self, user_id: &str, since: &str, + tz: &str, ) -> Result> { // Parameters: ?1 user, ?2 since, ?3 and ?4 zero oids. - let sql = "SELECT day, COUNT(*) FROM ( \ - SELECT substr(created_at, 1, 10) AS day FROM push_events \ + let sql = "SELECT minute, COUNT(*) FROM ( \ + SELECT substr(created_at, 1, 16) AS minute FROM push_events \ WHERE actor_id = ?1 AND created_at >= ?2 AND new_oid NOT IN (?3, ?4) \ UNION ALL \ - SELECT substr(created_at, 1, 10) FROM issues \ + SELECT substr(created_at, 1, 16) FROM issues \ WHERE author_id = ?1 AND created_at >= ?2 AND kind IN ('issue', 'pull') \ UNION ALL \ - SELECT substr(created_at, 1, 10) FROM comments \ + SELECT substr(created_at, 1, 16) FROM comments \ WHERE author_id = ?1 AND created_at >= ?2 \ - ) GROUP BY day ORDER BY day"; + ) GROUP BY minute"; let params = vec![ Value::Text(user_id.to_string()), Value::Text(since.to_string()), @@ -39,10 +46,18 @@ impl Store { Ok((col_text(row, 0)?, col_int(row, 1)?)) }) .await?; - Ok(rows - .into_iter() - .map(|(day, count)| (day, u32::try_from(count).unwrap_or(u32::MAX))) - .collect()) + // `minute` is `YYYY-MM-DDTHH:MM` in UTC; the stamp is completed with seconds to parse. + let mut days: BTreeMap = BTreeMap::new(); + for (minute, count) in rows { + let Ok(ts) = format!("{minute}:00Z").parse::() else { + continue; + }; + let day = local_date(ts, tz).to_string(); + let count = u32::try_from(count).unwrap_or(u32::MAX); + let total = days.entry(day).or_default(); + *total = total.saturating_add(count); + } + Ok(days.into_iter().collect()) } } @@ -135,23 +150,31 @@ mod tests { .unwrap(); let since = "2026-03-01T00:00:00.000000000Z"; - let counts = store.contribution_counts(&tahinli.id, since).await.unwrap(); + let counts = store + .contribution_counts(&tahinli.id, since, "UTC") + .await + .unwrap(); assert_eq!( counts, vec![("2026-03-01".to_string(), 2), ("2026-03-02".to_string(), 3)] ); - // `since` filters out the first day. + // `since` filters out the first day. In Kiritimati (+14) the 10:00 UTC events fall on + // the next local day. let later = store - .contribution_counts(&tahinli.id, "2026-03-02T00:00:00.000000000Z") + .contribution_counts( + &tahinli.id, + "2026-03-02T00:00:00.000000000Z", + "Pacific/Kiritimati", + ) .await .unwrap(); - assert_eq!(later, vec![("2026-03-02".to_string(), 3)]); + assert_eq!(later, vec![("2026-03-03".to_string(), 3)]); // A user with nothing in range gets an empty list. assert!( store - .contribution_counts(&bob.id, "2026-03-03T00:00:00.000000000Z") + .contribution_counts(&bob.id, "2026-03-03T00:00:00.000000000Z", "UTC") .await .unwrap() .is_empty() diff --git a/crates/forge-core/src/tz.rs b/crates/forge-core/src/tz.rs index fe4b905..6fc2101 100644 --- a/crates/forge-core/src/tz.rs +++ b/crates/forge-core/src/tz.rs @@ -1,6 +1,6 @@ //! IANA timezone helpers backed by jiff's bundled tzdb, so results do not depend on the host. -use jiff::{Timestamp, tz::TimeZone}; +use jiff::{Timestamp, civil, tz::TimeZone}; use crate::error::{Error, Result}; @@ -9,6 +9,16 @@ fn zone_or_utc(tz: &str) -> TimeZone { TimeZone::get(tz).unwrap_or(TimeZone::UTC) } +/// The civil date and clock time `ts` falls on in `tz`, DST-aware. Unknown zones fall back to UTC. +pub fn local_datetime(ts: Timestamp, tz: &str) -> civil::DateTime { + ts.to_zoned(zone_or_utc(tz)).datetime() +} + +/// The civil date `ts` falls on in `tz`. Unknown zones fall back to UTC. +pub fn local_date(ts: Timestamp, tz: &str) -> civil::Date { + local_datetime(ts, tz).date() +} + /// Accepts only names known to the bundled tzdb, such as "Europe/Istanbul" or "UTC". pub fn validate_timezone(name: &str) -> Result<()> { TimeZone::get(name) diff --git a/crates/forge-web/src/pages/dashboard.rs b/crates/forge-web/src/pages/dashboard.rs index cc5ea73..2d8ae06 100644 --- a/crates/forge-web/src/pages/dashboard.rs +++ b/crates/forge-web/src/pages/dashboard.rs @@ -8,7 +8,7 @@ use std::time::Duration; use forge_core::store::RepoSort; use forge_core::store::repo_listing::RepoScope; use forge_core::{Notification, Repo, Visibility}; -use time::OffsetDateTime; +use jiff::Timestamp; use topcoat::Result; use topcoat::context::{Cx, app_context}; use topcoat::router::response::Response; @@ -29,6 +29,12 @@ use crate::views::{SIDE_REPOS, display_name, now_unix, relative_time, shell}; /// How far back the calendar reads contributions: 53 weeks plus the days of the current week. const CALENDAR_DAYS: u64 = 371; +/// The `since` stamp for the calendar. One extra day covers the viewer's local day, which can +/// start before the UTC day that `CALENDAR_DAYS` reaches back to. +fn calendar_since() -> String { + forge_core::time::stamp_ago(Duration::from_secs((CALENDAR_DAYS + 1) * 86_400)) +} + /// Unread inbox rows in the "Needs your attention" column. const ATTENTION_ROWS: u32 = 5; @@ -145,8 +151,12 @@ async fn dashboard_fingerprint(state: &AppState, user: &forge_core::User) -> Res repo.name, repo.visibility, repo.description )); } - let since = forge_core::time::stamp_ago(Duration::from_secs(CALENDAR_DAYS * 86_400)); - for (day, count) in state.store.contribution_counts(&user.id, &since).await? { + let since = calendar_since(); + for (day, count) in state + .store + .contribution_counts(&user.id, &since, &user.timezone) + .await? + { lines.push(format!("day {day} {count}")); } for note in state @@ -169,14 +179,16 @@ async fn load_dashboard( lang: Lang, ) -> Result { let repos = state.store.list_repos_by_owner(&user.id).await?; - let now = OffsetDateTime::now_utc(); + let local_now = forge_core::tz::local_datetime(Timestamp::now(), &user.timezone); let today = ( - i64::from(now.year()), - i64::from(u8::from(now.month())), - i64::from(now.day()), + i64::from(local_now.year()), + i64::from(local_now.month()), + i64::from(local_now.day()), ); - let since = forge_core::time::stamp_ago(Duration::from_secs(CALENDAR_DAYS * 86_400)); - let counts = state.store.contribution_counts(&user.id, &since).await?; + let counts = state + .store + .contribution_counts(&user.id, &calendar_since(), &user.timezone) + .await?; let calendar = heatmap::build(&counts, today); let now_secs = now_unix(); let attention = state @@ -188,7 +200,7 @@ async fn load_dashboard( .collect(); Ok(Dashboard { name: display_name(lang, &user.username), - hour: now.hour(), + hour: u8::try_from(local_now.hour()).unwrap_or_default(), repos, calendar, attention,