| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | | use super::coverage::{chart, compact, Point, Series}; |
| 6 | | use super::{filters, lookup, Error, Page, RepoShell, Shell}; |
| 7 | | use crate::auth::Viewer; |
| 8 | | use crate::junit::{self, Case, Flaky, Report}; |
| 9 | | use crate::Shared; |
| 10 | | use askama::Template; |
| 11 | | use axum::extract::{Path, Query, State}; |
| 12 | | use serde::Deserialize; |
| 13 | |
|
| 14 | |
|
| 15 | | const HISTORY: i64 = 200; |
| 16 | |
|
| 17 | | const RECENT: usize = 10; |
| 18 | |
|
| 19 | | const FLAKY_RUNS: usize = 30; |
| 20 | |
|
| 21 | | const LISTED: i64 = 15; |
| 22 | |
|
| 23 | | #[derive(Deserialize, Default)] |
| 24 | | pub struct TestsQuery { |
| 25 | | |
| 26 | | bookmark: Option<String>, |
| 27 | | |
| 28 | | report: Option<i64>, |
| 29 | | } |
| 30 | |
|
| 31 | |
|
| 32 | | pub struct RecentRow { |
| 33 | | pub report: Report, |
| 34 | | |
| 35 | | pub delta: Option<i64>, |
| 36 | | } |
| 37 | |
|
| 38 | |
|
| 39 | | pub struct Changed { |
| 40 | | pub title: String, |
| 41 | | pub status: String, |
| 42 | | } |
| 43 | |
|
| 44 | | #[derive(Template)] |
| 45 | | #[template(path = "tests.html")] |
| 46 | | pub struct TestsPage { |
| 47 | | shell: Shell, |
| 48 | | repo: RepoShell, |
| 49 | | bookmark: String, |
| 50 | | bookmarks: Vec<String>, |
| 51 | | report: Option<Report>, |
| 52 | | previous: Option<Report>, |
| 53 | | |
| 54 | | broke: Vec<Changed>, |
| 55 | | |
| 56 | | fixed: Vec<Changed>, |
| 57 | | |
| 58 | | added: usize, |
| 59 | | failures: Vec<Case>, |
| 60 | | slowest: Vec<Case>, |
| 61 | | flaky: Vec<Flaky>, |
| 62 | | flaky_runs: usize, |
| 63 | | rate_chart: String, |
| 64 | | count_chart: String, |
| 65 | | runs: usize, |
| 66 | | first_ms: Option<i64>, |
| 67 | | recent: Vec<RecentRow>, |
| 68 | | } |
| 69 | |
|
| 70 | |
|
| 71 | 48× | pub fn pass_rate(r: &Report) -> Option<f64> { |
| 72 | 48× | let ran = r.tests - r.skipped; |
| 73 | 48× | (ran > 0).then(|| r.passed as f64 * 100.0 / ran as f64) |
| 74 | 48× | } |
| 75 | |
|
| 76 | | impl TestsPage { |
| 77 | 15× | pub fn rate(&self, r: &Report) -> String { |
| 78 | 15× | match pass_rate(r) { |
| 79 | 15× | Some(p) if p >= 99.95 => "100%".into(), |
| 80 | 5× | Some(p) => format!("{p:.1}%"), |
| 81 | 0× | None => "–".into(), |
| 82 | | } |
| 83 | 15× | } |
| 84 | |
|
| 85 | | |
| 86 | 15× | pub fn class_of(&self, r: &Report) -> &'static str { |
| 87 | 15× | match pass_rate(r) { |
| 88 | 0× | None => "cov-none", |
| 89 | 15× | Some(_) if r.broken() == 0 => "cov-good", |
| 90 | 5× | Some(p) if p >= 95.0 => "cov-low", |
| 91 | 5× | Some(_) => "cov-poor", |
| 92 | | } |
| 93 | 15× | } |
| 94 | |
|
| 95 | 24× | pub fn time(&self, ms: &i64) -> String { |
| 96 | 24× | crate::flow::human_ms(*ms) |
| 97 | 24× | } |
| 98 | |
|
| 99 | 6× | pub fn signed(&self, d: &i64) -> String { |
| 100 | 6× | if *d == 0 { |
| 101 | 2× | "±0".into() |
| 102 | | } else { |
| 103 | 4× | format!("{d:+}") |
| 104 | | } |
| 105 | 6× | } |
| 106 | |
|
| 107 | 3× | pub fn count_delta(&self) -> Option<i64> { |
| 108 | 3× | Some(self.report.as_ref()?.tests - self.previous.as_ref()?.tests) |
| 109 | 3× | } |
| 110 | |
|
| 111 | 3× | pub fn time_delta(&self) -> Option<String> { |
| 112 | 3× | let (r, p) = (self.report.as_ref()?, self.previous.as_ref()?); |
| 113 | 3× | if p.time_ms == 0 { |
| 114 | 0× | return None; |
| 115 | 3× | } |
| 116 | 3× | let d = (r.time_ms - p.time_ms) as f64 * 100.0 / p.time_ms as f64; |
| 117 | 3× | (d.abs() >= 1.0).then(|| format!("{d:+.0}%")) |
| 118 | 3× | } |
| 119 | | } |
| 120 | |
|
| 121 | |
|
| 122 | 3× | pub async fn overview( |
| 123 | 3× | State(app): State<Shared>, |
| 124 | 3× | viewer: Viewer, |
| 125 | 3× | Path((owner, name)): Path<(String, String)>, |
| 126 | 3× | Query(q): Query<TestsQuery>, |
| 127 | 3× | ) -> Result<Page<TestsPage>, Error> { |
| 128 | 3× | let shell = Shell::new(&app, &format!("/{owner}/{name}/tests"), &viewer); |
| 129 | 3× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 130 | 3× | let e = |e| Error::from_db(shell.clone(), e); |
| 131 | 3× | let picked = match q.report { |
| 132 | 1× | Some(id) => Some(junit::report(&app.db, row.id, id).await.map_err(e)?.ok_or_else(|| Error::not_found(shell.clone()))?), |
| 133 | 2× | None => None, |
| 134 | | }; |
| 135 | 3× | let bookmark = picked.as_ref().map(|r| r.bookmark.clone()).or(q.bookmark.clone()).unwrap_or_else(|| row.default_bookmark.clone()); |
| 136 | 3× | let bookmarks = junit::bookmarks(&app.db, row.id).await.map_err(e)?; |
| 137 | 3× | let history = junit::on_bookmark(&app.db, row.id, &bookmark, HISTORY).await.map_err(e)?; |
| 138 | 3× | let report = picked.or_else(|| history.first().cloned()); |
| 139 | | |
| 140 | 3× | let previous = report.as_ref().and_then(|r| { |
| 141 | 7× | history.iter().find(|h| h.job == r.job && (h.created_ms < r.created_ms || (h.created_ms == r.created_ms && h.id < r.id))).cloned() |
| 142 | 3× | }); |
| 143 | 3× | let (mut broke, mut fixed, mut added) = (vec![], vec![], 0); |
| 144 | 3× | let (mut failures, mut slowest) = (vec![], vec![]); |
| 145 | 3× | if let Some(r) = &report { |
| 146 | 3× | failures = junit::failures(&app.db, r.id, 50).await.map_err(e)?; |
| 147 | 3× | slowest = junit::slowest(&app.db, r.id, LISTED).await.map_err(e)?; |
| 148 | 3× | if let Some(p) = &previous { |
| 149 | 3× | let now = junit::statuses(&app.db, r.id).await.map_err(e)?; |
| 150 | 3× | let before = junit::statuses(&app.db, p.id).await.map_err(e)?; |
| 151 | 22× | let bad = |s: &str| s == "failed" || s == "error"; |
| 152 | 3× | let title = |(c, n): &(String, String)| if c.is_empty() { n.clone() } else { format!("{c}::{n}") }; |
| 153 | 12× | for (k, s) in &now { |
| 154 | 12× | match before.get(k) { |
| 155 | 1× | None => added += 1, |
| 156 | 11× | Some(b) if bad(s) && !bad(b) => broke.push(Changed { title: title(k), status: s.clone() }), |
| 157 | 10× | Some(b) if s == "passed" && bad(b) => fixed.push(Changed { title: title(k), status: s.clone() }), |
| 158 | 8× | _ => {} |
| 159 | | } |
| 160 | | } |
| 161 | 3× | broke.sort_by(|a, b| a.title.cmp(&b.title)); |
| 162 | 3× | fixed.sort_by(|a, b| a.title.cmp(&b.title)); |
| 163 | 3× | broke.truncate(LISTED as usize); |
| 164 | 3× | fixed.truncate(LISTED as usize); |
| 165 | 0× | } |
| 166 | 0× | } |
| 167 | 3× | let recent_ids: Vec<i64> = history.iter().take(FLAKY_RUNS).map(|r| r.id).collect(); |
| 168 | 3× | let flaky = junit::flaky(&app.db, &recent_ids, LISTED).await.map_err(e)?; |
| 169 | 3× | let repo = RepoShell::new(&app, &row, "tests").with_role(role); |
| 170 | | |
| 171 | 9× | let oldest_first: Vec<&Report> = history.iter().rev().filter(|r| r.tests > 0).collect(); |
| 172 | 3× | let points: Vec<Point> = oldest_first |
| 173 | 3× | .iter() |
| 174 | 3× | .map(|r| Point { |
| 175 | 9× | run: r.run_number, |
| 176 | 9× | href: format!("{}/tests?report={}", repo.url, r.id), |
| 177 | 9× | tip: format!("Run #{} · {} · {}", r.run_number, r.summary(), crate::flow::human_ms(r.time_ms)), |
| 178 | 9× | }) |
| 179 | 3× | .collect(); |
| 180 | 9× | let rates: Vec<f64> = oldest_first.iter().map(|r| pass_rate(r).unwrap_or(0.0)).collect(); |
| 181 | 3× | let lo = (rates.iter().copied().fold(100.0, f64::min) - 2.0).floor().max(0.0); |
| 182 | 3× | let rate_chart = chart( |
| 183 | 3× | &format!("Tests passing on {bookmark} over the last {} runs", oldest_first.len()), |
| 184 | 3× | &points, |
| 185 | 3× | &[Series { values: rates, class: "cov", floor: None }], |
| 186 | 3× | lo, |
| 187 | | 100.0, |
| 188 | 15× | &|v| format!("{v:.0}%"), |
| 189 | | ); |
| 190 | 9× | let passed: Vec<f64> = oldest_first.iter().map(|r| r.passed as f64).collect(); |
| 191 | 9× | let top = oldest_first.iter().map(|r| r.tests - r.skipped).max().unwrap_or(0) as f64; |
| 192 | 3× | let count_chart = chart( |
| 193 | 3× | &format!("Tests passing and failing on {bookmark}"), |
| 194 | 3× | &points, |
| 195 | | &[ |
| 196 | 3× | Series { values: passed.clone(), class: "covered", floor: None }, |
| 197 | 9× | Series { values: oldest_first.iter().map(|r| (r.tests - r.skipped) as f64).collect(), class: "relevant", floor: Some(passed) }, |
| 198 | | ], |
| 199 | | 0.0, |
| 200 | 3× | (top * 1.08).max(1.0), |
| 201 | 3× | &compact, |
| 202 | | ); |
| 203 | 3× | let recent = history |
| 204 | 3× | .iter() |
| 205 | 3× | .enumerate() |
| 206 | 3× | .take(RECENT) |
| 207 | 9× | .map(|(i, r)| RecentRow { report: r.clone(), delta: history.get(i + 1).filter(|p| p.job == r.job).map(|p| r.tests - p.tests) }) |
| 208 | 3× | .collect(); |
| 209 | 3× | let first_ms = junit::first_ms(&app.db, row.id).await.map_err(e)?; |
| 210 | 3× | Ok(Page(TestsPage { |
| 211 | 3× | shell, |
| 212 | 3× | repo, |
| 213 | 3× | bookmark, |
| 214 | 3× | bookmarks, |
| 215 | 3× | report, |
| 216 | 3× | previous, |
| 217 | 3× | broke, |
| 218 | 3× | fixed, |
| 219 | 3× | added, |
| 220 | 3× | failures, |
| 221 | 3× | slowest, |
| 222 | 3× | flaky, |
| 223 | 3× | flaky_runs: recent_ids.len(), |
| 224 | 3× | rate_chart, |
| 225 | 3× | count_chart, |
| 226 | 3× | runs: oldest_first.len(), |
| 227 | 3× | first_ms, |
| 228 | 3× | recent, |
| 229 | 3× | })) |
| 230 | 3× | } |
| 231 | |
|
| 232 | | #[derive(Deserialize)] |
| 233 | | pub struct CaseQuery { |
| 234 | | #[serde(default)] |
| 235 | | classname: String, |
| 236 | | name: String, |
| 237 | | bookmark: Option<String>, |
| 238 | | } |
| 239 | |
|
| 240 | | #[derive(Template)] |
| 241 | | #[template(path = "test_case.html")] |
| 242 | | pub struct TestCasePage { |
| 243 | | shell: Shell, |
| 244 | | repo: RepoShell, |
| 245 | | bookmark: String, |
| 246 | | title: String, |
| 247 | | runs: Vec<junit::Run>, |
| 248 | | passed: usize, |
| 249 | | failed: usize, |
| 250 | | time_chart: String, |
| 251 | | } |
| 252 | |
|
| 253 | | impl TestCasePage { |
| 254 | 3× | pub fn time(&self, ms: &i64) -> String { |
| 255 | 3× | crate::flow::human_ms(*ms) |
| 256 | 3× | } |
| 257 | | } |
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | 2× | pub async fn case( |
| 262 | 2× | State(app): State<Shared>, |
| 263 | 2× | viewer: Viewer, |
| 264 | 2× | Path((owner, name)): Path<(String, String)>, |
| 265 | 2× | Query(q): Query<CaseQuery>, |
| 266 | 2× | ) -> Result<Page<TestCasePage>, Error> { |
| 267 | 2× | let shell = Shell::new(&app, &format!("/{owner}/{name}/tests"), &viewer); |
| 268 | 2× | let (_, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 269 | 2× | let bookmark = q.bookmark.unwrap_or_else(|| row.default_bookmark.clone()); |
| 270 | 2× | let runs = |
| 271 | 2× | junit::history(&app.db, row.id, &bookmark, &q.classname, &q.name, HISTORY).await.map_err(|e| Error::from_db(shell.clone(), e))?; |
| 272 | 2× | if runs.is_empty() { |
| 273 | 1× | return Err(Error::not_found(shell)); |
| 274 | 1× | } |
| 275 | 1× | let repo = RepoShell::new(&app, &row, "tests").with_role(role); |
| 276 | 3× | let passed = runs.iter().filter(|r| r.status == "passed").count(); |
| 277 | 3× | let failed = runs.iter().filter(|r| r.status == "failed" || r.status == "error").count(); |
| 278 | 1× | let oldest: Vec<&junit::Run> = runs.iter().rev().collect(); |
| 279 | 1× | let points: Vec<Point> = oldest |
| 280 | 1× | .iter() |
| 281 | 1× | .map(|r| Point { |
| 282 | 3× | run: r.run_number, |
| 283 | 3× | href: format!("{}/tests?report={}", repo.url, r.report_id), |
| 284 | 3× | tip: format!("Run #{} · {} · {}", r.run_number, r.status, crate::flow::human_ms(r.time_ms)), |
| 285 | 3× | }) |
| 286 | 1× | .collect(); |
| 287 | 1× | let top = oldest.iter().map(|r| r.time_ms).max().unwrap_or(0) as f64; |
| 288 | 1× | let time_chart = chart( |
| 289 | 1× | "Time per run", |
| 290 | 1× | &points, |
| 291 | 3× | &[Series { values: oldest.iter().map(|r| r.time_ms as f64).collect(), class: "cov", floor: None }], |
| 292 | | 0.0, |
| 293 | 1× | (top * 1.1).max(1.0), |
| 294 | 5× | &|v| crate::flow::human_ms(v as i64), |
| 295 | | ); |
| 296 | 1× | let title = if q.classname.is_empty() { q.name.clone() } else { format!("{}::{}", q.classname, q.name) }; |
| 297 | 1× | Ok(Page(TestCasePage { shell, repo, bookmark, title, runs, passed, failed, time_chart })) |
| 298 | 2× | } |