Needs you can dismiss your failed runs: they leave the page and the profile corner's dot until a run fails again
Browse files at this commited4cd43be12c260acb5f1e5e26ba993552f8fcf2Parent: 2ba0d23- feat
- Needs you can dismiss your failed runs: they leave the page and the profile corner's dot until a run fails again
- the corner and the page now count failed runs with one shared query
- fix
- a Needs you section links its full list only when it has more rows than the overview shows, instead of opening the same rows again
6 changed files +104 -41
- crates/forge-core/migrations/0074_failed_runs_dismissed.sql +3 -0
- crates/forge-core/src/store/migrate.rs +4 -0
- crates/forge-core/src/store/needs.rs +29 -10
- crates/forge-core/src/store/shell_activity.rs +5 -18
- crates/forge-web-base/src/i18n/needs.rs +4 -0
- crates/forge-web/src/pages/needs_you.rs +59 -13
crates/forge-core/migrations/0074_failed_runs_dismissed.sqladded+3 -0
| @@ -0,0 +1,3 @@ | ||
| 1 | +-- When the user last dismissed their failed runs. Runs that finished before it are no longer listed | |
| 2 | +-- as needing them. Applied by store/migrate.rs. | |
| 3 | +ALTER TABLE users ADD COLUMN failed_runs_dismissed_at TEXT; |
crates/forge-core/src/store/migrate.rsmodified+4 -0
| @@ -125,6 +125,10 @@ pub const MIGRATIONS: &[(u32, &str)] = &[ | ||
| 125 | 125 | 73, |
| 126 | 126 | include_str!("../../migrations/0073_sage_ink_default.sql"), |
| 127 | 127 | ), |
| 128 | + ( | |
| 129 | + 74, | |
| 130 | + include_str!("../../migrations/0074_failed_runs_dismissed.sql"), | |
| 131 | + ), | |
| 128 | 132 | ]; |
| 129 | 133 | |
| 130 | 134 | /// The highest migration version this binary knows about. |
crates/forge-core/src/store/needs.rsmodified+29 -10
| @@ -9,13 +9,13 @@ use turso::Value; | ||
| 9 | 9 | |
| 10 | 10 | use super::notifications::{readable_params, readable_repo}; |
| 11 | 11 | use super::shell_activity::WaitingItem; |
| 12 | -use super::{Store, col_int, col_text, fetch_all}; | |
| 12 | +use super::{Store, col_int, col_text, db_err, fetch_all}; | |
| 13 | 13 | use crate::error::Result; |
| 14 | 14 | use crate::notifications::Reason; |
| 15 | -use crate::time::stamp_ago; | |
| 15 | +use crate::time::{now_stamp, stamp_ago}; | |
| 16 | 16 | |
| 17 | -/// How far back a failing run counts. The same window as the profile corner's failing count. | |
| 18 | -const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60); | |
| 17 | +/// How far back a failing run counts, here and in the profile corner's failing count. | |
| 18 | +pub(super) const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60); | |
| 19 | 19 | |
| 20 | 20 | /// A ref whose newest run by the user failed within `FAILING_WINDOW`. |
| 21 | 21 | #[derive(Debug, Clone)] |
| @@ -72,16 +72,17 @@ pub struct AssignedIssue { | ||
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /// The failing runs query without its page, newest first. |
| 75 | -fn failing_runs_sql(readable: &str) -> String { | |
| 75 | +pub(super) fn failing_runs_sql(readable: &str) -> String { | |
| 76 | 76 | format!( |
| 77 | 77 | "SELECT owner, name, number, git_ref, created_at FROM ( \ |
| 78 | - SELECT ci_runs.state, ci_runs.created_at, ci_runs.number, ci_runs.git_ref, \ | |
| 78 | + SELECT ci_runs.state, ci_runs.created_at, ci_runs.finished_at, ci_runs.number, ci_runs.git_ref, \ | |
| 79 | 79 | o.username AS owner, r.name AS name, ROW_NUMBER() OVER ( \ |
| 80 | 80 | PARTITION BY ci_runs.repo_id, ci_runs.git_ref ORDER BY ci_runs.number DESC) AS rn \ |
| 81 | 81 | FROM ci_runs JOIN repos r ON r.id = ci_runs.repo_id \ |
| 82 | 82 | JOIN users o ON o.id = r.owner_id \ |
| 83 | 83 | WHERE ci_runs.actor_id = ? AND {readable} \ |
| 84 | 84 | ) WHERE rn = 1 AND state IN ('failure', 'error') AND created_at >= ? \ |
| 85 | + AND COALESCE(finished_at, created_at) > COALESCE((SELECT failed_runs_dismissed_at FROM users WHERE id = ?), '') \ | |
| 85 | 86 | ORDER BY created_at DESC, number DESC" |
| 86 | 87 | ) |
| 87 | 88 | } |
| @@ -139,6 +140,14 @@ fn viewer_params(user_id: &str) -> Vec<Value> { | ||
| 139 | 140 | params |
| 140 | 141 | } |
| 141 | 142 | |
| 143 | +/// The parameters of `failing_runs_sql`, in order. | |
| 144 | +pub(super) fn failing_runs_params(user_id: &str) -> Vec<Value> { | |
| 145 | + let mut params = viewer_params(user_id); | |
| 146 | + params.push(Value::Text(stamp_ago(FAILING_WINDOW))); | |
| 147 | + params.push(Value::Text(user_id.to_string())); | |
| 148 | + params | |
| 149 | +} | |
| 150 | + | |
| 142 | 151 | /// `base` with its page: `LIMIT` and `OFFSET` are the last two placeholders. |
| 143 | 152 | fn paged(base: &str) -> String { |
| 144 | 153 | format!("{base} LIMIT ? OFFSET ?") |
| @@ -169,8 +178,7 @@ impl Store { | ||
| 169 | 178 | limit: i64, |
| 170 | 179 | offset: i64, |
| 171 | 180 | ) -> Result<Vec<FailingRun>> { |
| 172 | - let mut params = viewer_params(actor_id); | |
| 173 | - params.push(Value::Text(stamp_ago(FAILING_WINDOW))); | |
| 181 | + let mut params = failing_runs_params(actor_id); | |
| 174 | 182 | params.push(Value::Integer(limit)); |
| 175 | 183 | params.push(Value::Integer(offset)); |
| 176 | 184 | let conn = self.read_conn().await; |
| @@ -191,10 +199,21 @@ impl Store { | ||
| 191 | 199 | .await |
| 192 | 200 | } |
| 193 | 201 | |
| 202 | + /// Hides the user's failed runs that have finished so far. A run that fails later shows again. | |
| 203 | + pub async fn dismiss_failed_runs(&self, user_id: &str) -> Result<()> { | |
| 204 | + let conn = self.inner.conn.lock().await; | |
| 205 | + conn.execute( | |
| 206 | + "UPDATE users SET failed_runs_dismissed_at = ?2 WHERE id = ?1", | |
| 207 | + (user_id.to_string(), now_stamp()), | |
| 208 | + ) | |
| 209 | + .await | |
| 210 | + .map_err(db_err)?; | |
| 211 | + Ok(()) | |
| 212 | + } | |
| 213 | + | |
| 194 | 214 | /// How many failing runs `failing_runs` lists in all. |
| 195 | 215 | pub async fn failing_runs_total(&self, actor_id: &str) -> Result<u64> { |
| 196 | - let mut params = viewer_params(actor_id); | |
| 197 | - params.push(Value::Text(stamp_ago(FAILING_WINDOW))); | |
| 216 | + let params = failing_runs_params(actor_id); | |
| 198 | 217 | self.count_rows(&failing_runs_sql(&readable_repo()), params) |
| 199 | 218 | .await |
| 200 | 219 | } |
crates/forge-core/src/store/shell_activity.rsmodified+5 -18
| @@ -5,6 +5,7 @@ use std::time::Duration; | ||
| 5 | 5 | |
| 6 | 6 | use turso::{Row, Value}; |
| 7 | 7 | |
| 8 | +use super::needs::{failing_runs_params, failing_runs_sql}; | |
| 8 | 9 | use super::notifications::{readable_params, readable_repo}; |
| 9 | 10 | use super::{Store, col_int, col_opt_text, col_text, fetch_all}; |
| 10 | 11 | use crate::ci::RunState; |
| @@ -16,9 +17,6 @@ use crate::time::stamp_ago; | ||
| 16 | 17 | /// counts and list, bounded when runs pile up in the queue with no runner online. |
| 17 | 18 | const MAX_ACTOR_RUNS: i64 = 100; |
| 18 | 19 | |
| 19 | -/// How far back a failing run still counts toward `ActorRuns::failing`. | |
| 20 | -const FAILING_WINDOW: Duration = Duration::from_secs(7 * 24 * 60 * 60); | |
| 21 | - | |
| 22 | 20 | /// Unread inbox rows of the two kinds the corner shows. |
| 23 | 21 | #[derive(Debug, Clone, Copy, Default)] |
| 24 | 22 | pub struct InboxKinds { |
| @@ -223,25 +221,14 @@ impl Store { | ||
| 223 | 221 | ) |
| 224 | 222 | .await?; |
| 225 | 223 | |
| 226 | - // Newest run per (repo, ref) among this actor's runs; the ones that failed are counted, and the | |
| 227 | - // newest of them is kept for its link. | |
| 228 | - let mut params = vec![Value::Text(actor_id.to_string())]; | |
| 229 | - params.extend(readable_params(actor_id)); | |
| 230 | - params.push(Value::Text(stamp_ago(FAILING_WINDOW))); | |
| 224 | + // The same failing runs `/needs-you` lists: counted, and the newest kept for its link. | |
| 231 | 225 | let failing = fetch_all( |
| 232 | 226 | &conn, |
| 233 | 227 | &format!( |
| 234 | - "SELECT COUNT(*) OVER (), owner, name, number FROM ( \ | |
| 235 | - SELECT ci_runs.state, ci_runs.created_at, ci_runs.number, o.username AS owner, \ | |
| 236 | - r.name AS name, ROW_NUMBER() OVER ( \ | |
| 237 | - PARTITION BY ci_runs.repo_id, ci_runs.git_ref ORDER BY ci_runs.number DESC) AS rn \ | |
| 238 | - FROM ci_runs JOIN repos r ON r.id = ci_runs.repo_id \ | |
| 239 | - JOIN users o ON o.id = r.owner_id \ | |
| 240 | - WHERE ci_runs.actor_id = ? AND {readable} \ | |
| 241 | - ) WHERE rn = 1 AND state IN ('failure', 'error') AND created_at >= ? \ | |
| 242 | - ORDER BY created_at DESC LIMIT 1" | |
| 228 | + "SELECT COUNT(*) OVER (), owner, name, number FROM ({}) LIMIT 1", | |
| 229 | + failing_runs_sql(&readable) | |
| 243 | 230 | ), |
| 244 | - params, | |
| 231 | + failing_runs_params(actor_id), | |
| 245 | 232 | |row| { |
| 246 | 233 | Ok(( |
| 247 | 234 | col_int(row, 0)?, |
crates/forge-web-base/src/i18n/needs.rsmodified+4 -0
| @@ -14,6 +14,7 @@ pub enum K { | ||
| 14 | 14 | Approved, |
| 15 | 15 | AllReviewRequests, |
| 16 | 16 | AllFailedRuns, |
| 17 | + DismissFailedRuns, | |
| 17 | 18 | AllYourPulls, |
| 18 | 19 | AllAssignedIssues, |
| 19 | 20 | AllMentions, |
| @@ -34,6 +35,7 @@ pub const ALL: &[K] = &[ | ||
| 34 | 35 | K::Approved, |
| 35 | 36 | K::AllReviewRequests, |
| 36 | 37 | K::AllFailedRuns, |
| 38 | + K::DismissFailedRuns, | |
| 37 | 39 | K::AllYourPulls, |
| 38 | 40 | K::AllAssignedIssues, |
| 39 | 41 | K::AllMentions, |
| @@ -65,6 +67,8 @@ pub fn t(lang: Lang, key: K) -> &'static str { | ||
| 65 | 67 | (AllReviewRequests, Tr) => "Tüm inceleme istekleri", |
| 66 | 68 | (AllFailedRuns, En) => "All failed runs", |
| 67 | 69 | (AllFailedRuns, Tr) => "Tüm başarısız çalıştırmalar", |
| 70 | + (DismissFailedRuns, En) => "Dismiss", | |
| 71 | + (DismissFailedRuns, Tr) => "Kapat", | |
| 68 | 72 | (AllYourPulls, En) => "All your pull requests", |
| 69 | 73 | (AllYourPulls, Tr) => "Tüm pull request'leriniz", |
| 70 | 74 | (AllAssignedIssues, En) => "All assigned issues", |
crates/forge-web/src/pages/needs_you.rsmodified+59 -13
| @@ -8,20 +8,23 @@ | ||
| 8 | 8 | use forge_core::User; |
| 9 | 9 | use forge_core::authz::{Action, authorize}; |
| 10 | 10 | use forge_core::store::needs::Verdict; |
| 11 | +use serde::Deserialize; | |
| 11 | 12 | use topcoat::Result; |
| 12 | 13 | use topcoat::context::{Cx, context}; |
| 14 | +use topcoat::router::content::Form; | |
| 15 | +use topcoat::router::error::{SeeOther, see_other}; | |
| 13 | 16 | use topcoat::router::response::Response; |
| 14 | 17 | use topcoat::router::{Body, StatusCode, page, query_params, route}; |
| 15 | 18 | use topcoat::view::{View, component, view}; |
| 16 | 19 | |
| 17 | -use crate::auth::{current_user, require_user}; | |
| 20 | +use crate::auth::{current_user, require_csrf, require_user}; | |
| 18 | 21 | use crate::i18n::authoring::{self as ga}; |
| 19 | 22 | use crate::i18n::needs::{self as ni, K}; |
| 20 | 23 | use crate::i18n::{self, Lang}; |
| 21 | 24 | use crate::live::{Topic, stream_wakes}; |
| 22 | 25 | use crate::pages::ci_runs::run_href; |
| 23 | 26 | use crate::state::AppState; |
| 24 | -use crate::views::parts::head; | |
| 27 | +use crate::views::parts::{head, post_act}; | |
| 25 | 28 | use crate::views::repo_list::{PageLink, pager, pager_links}; |
| 26 | 29 | use crate::views::{rel_time, repo_href, shell}; |
| 27 | 30 | |
| @@ -220,34 +223,45 @@ impl NeedPager { | ||
| 220 | 223 | pub(crate) struct NeedSection { |
| 221 | 224 | heading: &'static str, |
| 222 | 225 | more: Option<(&'static str, &'static str)>, |
| 226 | + /// The failed runs section can be dismissed. | |
| 227 | + dismiss: bool, | |
| 223 | 228 | rows: Vec<NeedRow>, |
| 224 | 229 | pager: NeedPager, |
| 225 | 230 | } |
| 226 | 231 | |
| 227 | 232 | /// Adds an overview section for `rows`, unless there are none. |
| 228 | -fn push_section(sections: &mut Vec<NeedSection>, lang: Lang, section: Section, rows: Vec<NeedRow>) { | |
| 233 | +fn push_section( | |
| 234 | + sections: &mut Vec<NeedSection>, | |
| 235 | + lang: Lang, | |
| 236 | + section: Section, | |
| 237 | + mut rows: Vec<NeedRow>, | |
| 238 | +) { | |
| 229 | 239 | if rows.is_empty() { |
| 230 | 240 | return; |
| 231 | 241 | } |
| 242 | + let more = rows.len() > SECTION_ROWS; | |
| 243 | + rows.truncate(SECTION_ROWS); | |
| 232 | 244 | sections.push(NeedSection { |
| 233 | 245 | heading: ni::t(lang, section.heading()), |
| 234 | - more: Some((section.href(), ni::t(lang, section.all_key()))), | |
| 246 | + more: more.then(|| (section.href(), ni::t(lang, section.all_key()))), | |
| 247 | + dismiss: matches!(section, Section::Runs), | |
| 235 | 248 | rows, |
| 236 | 249 | pager: NeedPager::single(), |
| 237 | 250 | }); |
| 238 | 251 | } |
| 239 | 252 | |
| 240 | -/// The sections that have rows, in page order, each capped at `SECTION_ROWS`. | |
| 253 | +/// The sections that have rows, in page order, each capped at `SECTION_ROWS`. One row more is read, | |
| 254 | +/// so a section links its full list only when it has rows the overview leaves out. | |
| 241 | 255 | async fn load_sections(state: &AppState, user: &User, lang: Lang) -> Result<Vec<NeedSection>> { |
| 242 | 256 | let mut sections = Vec::new(); |
| 243 | - let limit = SECTION_ROWS as i64; | |
| 257 | + let limit = SECTION_ROWS as i64 + 1; | |
| 244 | 258 | |
| 245 | 259 | let runs = Section::Runs.rows(state, user, lang, limit, 0).await?; |
| 246 | 260 | push_section(&mut sections, lang, Section::Runs, runs); |
| 247 | 261 | |
| 248 | 262 | let mut reviews = Vec::new(); |
| 249 | 263 | for item in state.store.review_requests_for_user(&user.id).await? { |
| 250 | - if reviews.len() >= SECTION_ROWS { | |
| 264 | + if reviews.len() > SECTION_ROWS { | |
| 251 | 265 | break; |
| 252 | 266 | } |
| 253 | 267 | // A repo the viewer can no longer read is not listed. |
| @@ -286,9 +300,12 @@ async fn load_sections(state: &AppState, user: &User, lang: Lang) -> Result<Vec< | ||
| 286 | 300 | }); |
| 287 | 301 | } |
| 288 | 302 | if !reviews.is_empty() { |
| 303 | + let more = reviews.len() > SECTION_ROWS; | |
| 304 | + reviews.truncate(SECTION_ROWS); | |
| 289 | 305 | sections.push(NeedSection { |
| 290 | 306 | heading: ni::t(lang, K::ReviewsRequested), |
| 291 | - more: Some(("/review-requests", ni::t(lang, K::AllReviewRequests))), | |
| 307 | + more: more.then(|| ("/review-requests", ni::t(lang, K::AllReviewRequests))), | |
| 308 | + dismiss: false, | |
| 292 | 309 | rows: reviews, |
| 293 | 310 | pager: NeedPager::single(), |
| 294 | 311 | }); |
| @@ -331,6 +348,7 @@ async fn load_full( | ||
| 331 | 348 | Ok(NeedSection { |
| 332 | 349 | heading: ni::t(lang, section.heading()), |
| 333 | 350 | more: None, |
| 351 | + dismiss: matches!(section, Section::Runs), | |
| 334 | 352 | rows, |
| 335 | 353 | pager: NeedPager { |
| 336 | 354 | page, |
| @@ -344,10 +362,16 @@ async fn load_full( | ||
| 344 | 362 | |
| 345 | 363 | /// One section of the page: its head, its rows as a compact list, its full-list link and its pager. |
| 346 | 364 | #[component] |
| 347 | -pub(crate) async fn need_section(cx: &Cx, lang: Lang, section: NeedSection) -> Result<impl View> { | |
| 365 | +pub(crate) async fn need_section( | |
| 366 | + cx: &Cx, | |
| 367 | + lang: Lang, | |
| 368 | + csrf: String, | |
| 369 | + section: NeedSection, | |
| 370 | +) -> Result<impl View> { | |
| 348 | 371 | let NeedSection { |
| 349 | 372 | heading, |
| 350 | 373 | more, |
| 374 | + dismiss, | |
| 351 | 375 | rows, |
| 352 | 376 | pager: |
| 353 | 377 | NeedPager { |
| @@ -371,9 +395,14 @@ pub(crate) async fn need_section(cx: &Cx, lang: Lang, section: NeedSection) -> R | ||
| 371 | 395 | <span class="val">rel_time(lang: lang, stamp: row.stamp.as_str())</span> |
| 372 | 396 | </a> |
| 373 | 397 | } |
| 374 | - if let Some((href, text)) = more { | |
| 398 | + if more.is_some() || dismiss { | |
| 375 | 399 | <div class="acts"> |
| 376 | - <a class="act" href=(href)>(text)</a> | |
| 400 | + if let Some((href, text)) = more { | |
| 401 | + <a class="act" href=(href)>(text)</a> | |
| 402 | + } | |
| 403 | + if dismiss { | |
| 404 | + post_act(label: ni::t(lang, K::DismissFailedRuns), action: "/needs-you/runs/dismiss", csrf: csrf.as_str()) | |
| 405 | + } | |
| 377 | 406 | </div> |
| 378 | 407 | } |
| 379 | 408 | pager(page: page, pages: pages, links: links, prev: prev, next: next, lang: lang) |
| @@ -410,6 +439,7 @@ async fn needs_you(cx: &Cx) -> Result<impl View> { | ||
| 410 | 439 | let back = full.as_ref().map(|_| ni::t(lang, K::BackToNeedsYou)); |
| 411 | 440 | let heading = ni::t(lang, K::NeedsYou); |
| 412 | 441 | let unread = state.store.unread_count(&user.id).await?; |
| 442 | + let csrf = viewer.1.csrf_token.clone(); | |
| 413 | 443 | Ok(view! { |
| 414 | 444 | shell(title: heading, viewer: Some(&viewer), |
| 415 | 445 | head(title: heading, live: Some(live.as_str()), |
| @@ -424,10 +454,10 @@ async fn needs_you(cx: &Cx) -> Result<impl View> { | ||
| 424 | 454 | <p class="state">(ni::t(lang, K::AllClear))</p> |
| 425 | 455 | } |
| 426 | 456 | for section in sections.into_iter() { |
| 427 | - need_section(lang: lang, section: section) | |
| 457 | + need_section(lang: lang, csrf: csrf.clone(), section: section) | |
| 428 | 458 | } |
| 429 | 459 | if let Some(section) = full { |
| 430 | - need_section(lang: lang, section: section) | |
| 460 | + need_section(lang: lang, csrf: csrf.clone(), section: section) | |
| 431 | 461 | } |
| 432 | 462 | </div> |
| 433 | 463 | </div> |
| @@ -435,6 +465,22 @@ async fn needs_you(cx: &Cx) -> Result<impl View> { | ||
| 435 | 465 | }) |
| 436 | 466 | } |
| 437 | 467 | |
| 468 | +#[derive(Deserialize)] | |
| 469 | +struct CsrfForm { | |
| 470 | + #[serde(default)] | |
| 471 | + csrf: String, | |
| 472 | +} | |
| 473 | + | |
| 474 | +/// Hides the viewer's failed runs so far, from this page and from the profile corner's dot. | |
| 475 | +#[route(POST "/needs-you/runs/dismiss")] | |
| 476 | +async fn dismiss_failed_runs(cx: &Cx, Form(form): Form<CsrfForm>) -> Result<SeeOther> { | |
| 477 | + let (user, _) = require_csrf(cx, &form.csrf).await?; | |
| 478 | + let state = context::<AppState>(cx); | |
| 479 | + state.store.dismiss_failed_runs(&user.id).await?; | |
| 480 | + state.live.bump(&Topic::Inbox(user.id)); | |
| 481 | + Ok(see_other("/needs-you")) | |
| 482 | +} | |
| 483 | + | |
| 438 | 484 | /// The page's stream: a wake on the viewer's review-request or inbox topic. Anonymous visitors get |
| 439 | 485 | /// a bare 401. The query of a section view is ignored here, so the same stream serves both. |
| 440 | 486 | #[route(GET "/needs-you/events")] |