Contribution calendar counts days and today in the viewer's timezone
Browse files at this commit1f7ef933ba11f1bb8a0cba6d68b0e4125983a7dfParent: 7b115ab- fix
- contribution calendar counts days and today in the viewer's timezone
3 changed files +71 -26
- crates/forge-core/src/store/contributions.rs +38 -15
- crates/forge-core/src/tz.rs +11 -1
- crates/forge-web/src/pages/dashboard.rs +22 -10
crates/forge-core/src/store/contributions.rsmodified+38 -15
| @@ -1,32 +1,39 @@ | |||
| 1 | //! Daily contribution counts for one user, for the dashboard heatmap. | 1 | //! Daily contribution counts for one user, for the dashboard heatmap. |
| 2 | 2 | ||
| 3 | +use std::collections::BTreeMap; | ||
| 4 | + | ||
| 5 | +use jiff::Timestamp; | ||
| 3 | use turso::Value; | 6 | use turso::Value; |
| 4 | 7 | ||
| 5 | use super::push_recent::{ZERO_OID_SHA1, ZERO_OID_SHA256}; | 8 | use super::push_recent::{ZERO_OID_SHA1, ZERO_OID_SHA256}; |
| 6 | use super::{Store, col_int, col_text, fetch_all}; | 9 | use super::{Store, col_int, col_text, fetch_all}; |
| 7 | use crate::error::Result; | 10 | use crate::error::Result; |
| 11 | +use crate::tz::local_date; | ||
| 8 | 12 | ||
| 9 | impl Store { | 13 | impl Store { |
| 10 | - /// Contributions per UTC day by `user_id` at or after `since` (an RFC 3339 stamp), as | 14 | + /// Contributions per day in `tz` by `user_id` at or after `since` (an RFC 3339 stamp), as |
| 11 | /// `(YYYY-MM-DD, count)` sorted by day ascending; days with no activity are left out. | 15 | /// `(YYYY-MM-DD, count)` sorted by day ascending; days with no activity are left out. |
| 16 | + /// Events are grouped by minute and each minute is placed on its local day, so zones with | ||
| 17 | + /// half-hour or quarter-hour offsets and DST changes land on the right day. | ||
| 12 | /// A contribution is a push (ref deletions are left out), an issue or pull request opened | 18 | /// A contribution is a push (ref deletions are left out), an issue or pull request opened |
| 13 | /// (pull requests are `issues` rows with `kind = 'pull'`), or a comment. | 19 | /// (pull requests are `issues` rows with `kind = 'pull'`), or a comment. |
| 14 | pub async fn contribution_counts( | 20 | pub async fn contribution_counts( |
| 15 | &self, | 21 | &self, |
| 16 | user_id: &str, | 22 | user_id: &str, |
| 17 | since: &str, | 23 | since: &str, |
| 24 | + tz: &str, | ||
| 18 | ) -> Result<Vec<(String, u32)>> { | 25 | ) -> Result<Vec<(String, u32)>> { |
| 19 | // Parameters: ?1 user, ?2 since, ?3 and ?4 zero oids. | 26 | // Parameters: ?1 user, ?2 since, ?3 and ?4 zero oids. |
| 20 | - let sql = "SELECT day, COUNT(*) FROM ( \ | 27 | + let sql = "SELECT minute, COUNT(*) FROM ( \ |
| 21 | - SELECT substr(created_at, 1, 10) AS day FROM push_events \ | 28 | + SELECT substr(created_at, 1, 16) AS minute FROM push_events \ |
| 22 | WHERE actor_id = ?1 AND created_at >= ?2 AND new_oid NOT IN (?3, ?4) \ | 29 | WHERE actor_id = ?1 AND created_at >= ?2 AND new_oid NOT IN (?3, ?4) \ |
| 23 | UNION ALL \ | 30 | UNION ALL \ |
| 24 | - SELECT substr(created_at, 1, 10) FROM issues \ | 31 | + SELECT substr(created_at, 1, 16) FROM issues \ |
| 25 | WHERE author_id = ?1 AND created_at >= ?2 AND kind IN ('issue', 'pull') \ | 32 | WHERE author_id = ?1 AND created_at >= ?2 AND kind IN ('issue', 'pull') \ |
| 26 | UNION ALL \ | 33 | UNION ALL \ |
| 27 | - SELECT substr(created_at, 1, 10) FROM comments \ | 34 | + SELECT substr(created_at, 1, 16) FROM comments \ |
| 28 | WHERE author_id = ?1 AND created_at >= ?2 \ | 35 | WHERE author_id = ?1 AND created_at >= ?2 \ |
| 29 | - ) GROUP BY day ORDER BY day"; | 36 | + ) GROUP BY minute"; |
| 30 | let params = vec![ | 37 | let params = vec![ |
| 31 | Value::Text(user_id.to_string()), | 38 | Value::Text(user_id.to_string()), |
| 32 | Value::Text(since.to_string()), | 39 | Value::Text(since.to_string()), |
| @@ -39,10 +46,18 @@ impl Store { | |||
| 39 | Ok((col_text(row, 0)?, col_int(row, 1)?)) | 46 | Ok((col_text(row, 0)?, col_int(row, 1)?)) |
| 40 | }) | 47 | }) |
| 41 | .await?; | 48 | .await?; |
| 42 | - Ok(rows | 49 | + // `minute` is `YYYY-MM-DDTHH:MM` in UTC; the stamp is completed with seconds to parse. |
| 43 | - .into_iter() | 50 | + let mut days: BTreeMap<String, u32> = BTreeMap::new(); |
| 44 | - .map(|(day, count)| (day, u32::try_from(count).unwrap_or(u32::MAX))) | 51 | + for (minute, count) in rows { |
| 45 | - .collect()) | 52 | + let Ok(ts) = format!("{minute}:00Z").parse::<Timestamp>() else { |
| 53 | + continue; | ||
| 54 | + }; | ||
| 55 | + let day = local_date(ts, tz).to_string(); | ||
| 56 | + let count = u32::try_from(count).unwrap_or(u32::MAX); | ||
| 57 | + let total = days.entry(day).or_default(); | ||
| 58 | + *total = total.saturating_add(count); | ||
| 59 | + } | ||
| 60 | + Ok(days.into_iter().collect()) | ||
| 46 | } | 61 | } |
| 47 | } | 62 | } |
| 48 | 63 | ||
| @@ -135,23 +150,31 @@ mod tests { | |||
| 135 | .unwrap(); | 150 | .unwrap(); |
| 136 | 151 | ||
| 137 | let since = "2026-03-01T00:00:00.000000000Z"; | 152 | let since = "2026-03-01T00:00:00.000000000Z"; |
| 138 | - let counts = store.contribution_counts(&tahinli.id, since).await.unwrap(); | 153 | + let counts = store |
| 154 | + .contribution_counts(&tahinli.id, since, "UTC") | ||
| 155 | + .await | ||
| 156 | + .unwrap(); | ||
| 139 | assert_eq!( | 157 | assert_eq!( |
| 140 | counts, | 158 | counts, |
| 141 | vec![("2026-03-01".to_string(), 2), ("2026-03-02".to_string(), 3)] | 159 | vec![("2026-03-01".to_string(), 2), ("2026-03-02".to_string(), 3)] |
| 142 | ); | 160 | ); |
| 143 | 161 | ||
| 144 | - // `since` filters out the first day. | 162 | + // `since` filters out the first day. In Kiritimati (+14) the 10:00 UTC events fall on |
| 163 | + // the next local day. | ||
| 145 | let later = store | 164 | let later = store |
| 146 | - .contribution_counts(&tahinli.id, "2026-03-02T00:00:00.000000000Z") | 165 | + .contribution_counts( |
| 166 | + &tahinli.id, | ||
| 167 | + "2026-03-02T00:00:00.000000000Z", | ||
| 168 | + "Pacific/Kiritimati", | ||
| 169 | + ) | ||
| 147 | .await | 170 | .await |
| 148 | .unwrap(); | 171 | .unwrap(); |
| 149 | - assert_eq!(later, vec![("2026-03-02".to_string(), 3)]); | 172 | + assert_eq!(later, vec![("2026-03-03".to_string(), 3)]); |
| 150 | 173 | ||
| 151 | // A user with nothing in range gets an empty list. | 174 | // A user with nothing in range gets an empty list. |
| 152 | assert!( | 175 | assert!( |
| 153 | store | 176 | store |
| 154 | - .contribution_counts(&bob.id, "2026-03-03T00:00:00.000000000Z") | 177 | + .contribution_counts(&bob.id, "2026-03-03T00:00:00.000000000Z", "UTC") |
| 155 | .await | 178 | .await |
| 156 | .unwrap() | 179 | .unwrap() |
| 157 | .is_empty() | 180 | .is_empty() |
crates/forge-core/src/tz.rsmodified+11 -1
| @@ -1,6 +1,6 @@ | |||
| 1 | //! IANA timezone helpers backed by jiff's bundled tzdb, so results do not depend on the host. | 1 | //! IANA timezone helpers backed by jiff's bundled tzdb, so results do not depend on the host. |
| 2 | 2 | ||
| 3 | -use jiff::{Timestamp, tz::TimeZone}; | 3 | +use jiff::{Timestamp, civil, tz::TimeZone}; |
| 4 | 4 | ||
| 5 | use crate::error::{Error, Result}; | 5 | use crate::error::{Error, Result}; |
| 6 | 6 | ||
| @@ -9,6 +9,16 @@ fn zone_or_utc(tz: &str) -> TimeZone { | |||
| 9 | TimeZone::get(tz).unwrap_or(TimeZone::UTC) | 9 | TimeZone::get(tz).unwrap_or(TimeZone::UTC) |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | +/// The civil date and clock time `ts` falls on in `tz`, DST-aware. Unknown zones fall back to UTC. | ||
| 13 | +pub fn local_datetime(ts: Timestamp, tz: &str) -> civil::DateTime { | ||
| 14 | + ts.to_zoned(zone_or_utc(tz)).datetime() | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +/// The civil date `ts` falls on in `tz`. Unknown zones fall back to UTC. | ||
| 18 | +pub fn local_date(ts: Timestamp, tz: &str) -> civil::Date { | ||
| 19 | + local_datetime(ts, tz).date() | ||
| 20 | +} | ||
| 21 | + | ||
| 12 | /// Accepts only names known to the bundled tzdb, such as "Europe/Istanbul" or "UTC". | 22 | /// Accepts only names known to the bundled tzdb, such as "Europe/Istanbul" or "UTC". |
| 13 | pub fn validate_timezone(name: &str) -> Result<()> { | 23 | pub fn validate_timezone(name: &str) -> Result<()> { |
| 14 | TimeZone::get(name) | 24 | TimeZone::get(name) |
crates/forge-web/src/pages/dashboard.rsmodified+22 -10
| @@ -8,7 +8,7 @@ use std::time::Duration; | |||
| 8 | use forge_core::store::RepoSort; | 8 | use forge_core::store::RepoSort; |
| 9 | use forge_core::store::repo_listing::RepoScope; | 9 | use forge_core::store::repo_listing::RepoScope; |
| 10 | use forge_core::{Notification, Repo, Visibility}; | 10 | use forge_core::{Notification, Repo, Visibility}; |
| 11 | -use time::OffsetDateTime; | 11 | +use jiff::Timestamp; |
| 12 | use topcoat::Result; | 12 | use topcoat::Result; |
| 13 | use topcoat::context::{Cx, app_context}; | 13 | use topcoat::context::{Cx, app_context}; |
| 14 | use topcoat::router::response::Response; | 14 | use topcoat::router::response::Response; |
| @@ -29,6 +29,12 @@ use crate::views::{SIDE_REPOS, display_name, now_unix, relative_time, shell}; | |||
| 29 | /// How far back the calendar reads contributions: 53 weeks plus the days of the current week. | 29 | /// How far back the calendar reads contributions: 53 weeks plus the days of the current week. |
| 30 | const CALENDAR_DAYS: u64 = 371; | 30 | const CALENDAR_DAYS: u64 = 371; |
| 31 | 31 | ||
| 32 | +/// The `since` stamp for the calendar. One extra day covers the viewer's local day, which can | ||
| 33 | +/// start before the UTC day that `CALENDAR_DAYS` reaches back to. | ||
| 34 | +fn calendar_since() -> String { | ||
| 35 | + forge_core::time::stamp_ago(Duration::from_secs((CALENDAR_DAYS + 1) * 86_400)) | ||
| 36 | +} | ||
| 37 | + | ||
| 32 | /// Unread inbox rows in the "Needs your attention" column. | 38 | /// Unread inbox rows in the "Needs your attention" column. |
| 33 | const ATTENTION_ROWS: u32 = 5; | 39 | const ATTENTION_ROWS: u32 = 5; |
| 34 | 40 | ||
| @@ -145,8 +151,12 @@ async fn dashboard_fingerprint(state: &AppState, user: &forge_core::User) -> Res | |||
| 145 | repo.name, repo.visibility, repo.description | 151 | repo.name, repo.visibility, repo.description |
| 146 | )); | 152 | )); |
| 147 | } | 153 | } |
| 148 | - let since = forge_core::time::stamp_ago(Duration::from_secs(CALENDAR_DAYS * 86_400)); | 154 | + let since = calendar_since(); |
| 149 | - for (day, count) in state.store.contribution_counts(&user.id, &since).await? { | 155 | + for (day, count) in state |
| 156 | + .store | ||
| 157 | + .contribution_counts(&user.id, &since, &user.timezone) | ||
| 158 | + .await? | ||
| 159 | + { | ||
| 150 | lines.push(format!("day {day} {count}")); | 160 | lines.push(format!("day {day} {count}")); |
| 151 | } | 161 | } |
| 152 | for note in state | 162 | for note in state |
| @@ -169,14 +179,16 @@ async fn load_dashboard( | |||
| 169 | lang: Lang, | 179 | lang: Lang, |
| 170 | ) -> Result<Dashboard> { | 180 | ) -> Result<Dashboard> { |
| 171 | let repos = state.store.list_repos_by_owner(&user.id).await?; | 181 | let repos = state.store.list_repos_by_owner(&user.id).await?; |
| 172 | - let now = OffsetDateTime::now_utc(); | 182 | + let local_now = forge_core::tz::local_datetime(Timestamp::now(), &user.timezone); |
| 173 | let today = ( | 183 | let today = ( |
| 174 | - i64::from(now.year()), | 184 | + i64::from(local_now.year()), |
| 175 | - i64::from(u8::from(now.month())), | 185 | + i64::from(local_now.month()), |
| 176 | - i64::from(now.day()), | 186 | + i64::from(local_now.day()), |
| 177 | ); | 187 | ); |
| 178 | - let since = forge_core::time::stamp_ago(Duration::from_secs(CALENDAR_DAYS * 86_400)); | 188 | + let counts = state |
| 179 | - let counts = state.store.contribution_counts(&user.id, &since).await?; | 189 | + .store |
| 190 | + .contribution_counts(&user.id, &calendar_since(), &user.timezone) | ||
| 191 | + .await?; | ||
| 180 | let calendar = heatmap::build(&counts, today); | 192 | let calendar = heatmap::build(&counts, today); |
| 181 | let now_secs = now_unix(); | 193 | let now_secs = now_unix(); |
| 182 | let attention = state | 194 | let attention = state |
| @@ -188,7 +200,7 @@ async fn load_dashboard( | |||
| 188 | .collect(); | 200 | .collect(); |
| 189 | Ok(Dashboard { | 201 | Ok(Dashboard { |
| 190 | name: display_name(lang, &user.username), | 202 | name: display_name(lang, &user.username), |
| 191 | - hour: now.hour(), | 203 | + hour: u8::try_from(local_now.hour()).unwrap_or_default(), |
| 192 | repos, | 204 | repos, |
| 193 | calendar, | 205 | calendar, |
| 194 | attention, | 206 | attention, |