| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#![allow(clippy::needless_pass_by_value)] |
| 38 |
|
| 39 |
use std::collections::{HashMap, HashSet}; |
| 40 |
|
| 41 |
use chrono::{DateTime, Utc}; |
| 42 |
use goingson_core::{Problem, ProblemBand, ProblemFilter, ProblemId, ProblemStatus, ProjectId}; |
| 43 |
use quasi_declare::declare; |
| 44 |
use quasi_router::layout::Tone; |
| 45 |
use quasi_router::screen::Tag; |
| 46 |
use quasi_router::{Action, Response, RouteError, Router}; |
| 47 |
|
| 48 |
use crate::commands::{PromoteProblemInput, promote}; |
| 49 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 50 |
|
| 51 |
#[cfg(test)] |
| 52 |
mod tests; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
const STATUSES: [Option<ProblemStatus>; 5] = [ |
| 57 |
Some(ProblemStatus::Open), |
| 58 |
Some(ProblemStatus::Promoted), |
| 59 |
Some(ProblemStatus::Dismissed), |
| 60 |
Some(ProblemStatus::Resolved), |
| 61 |
None, |
| 62 |
]; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
const fn band_tone(band: ProblemBand) -> makeover_layout::Tone { |
| 70 |
match band { |
| 71 |
ProblemBand::Critical => makeover_layout::Tone::Danger, |
| 72 |
ProblemBand::High => makeover_layout::Tone::Warning, |
| 73 |
ProblemBand::Medium => makeover_layout::Tone::Info, |
| 74 |
ProblemBand::Low => makeover_layout::Tone::Neutral, |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
const fn status_tone(status: ProblemStatus) -> makeover_layout::Tone { |
| 80 |
match status { |
| 81 |
ProblemStatus::Promoted => makeover_layout::Tone::Success, |
| 82 |
ProblemStatus::Resolved => makeover_layout::Tone::Info, |
| 83 |
ProblemStatus::Open | ProblemStatus::Dismissed => makeover_layout::Tone::Neutral, |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
fn text<'a>(params: &'a quasi_router::Params, name: &str) -> Option<&'a str> { |
| 90 |
params.get(name).map(str::trim).filter(|v| !v.is_empty()) |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
fn status_filter(request: &quasi_router::Request) -> Result<Option<ProblemStatus>, RouteError> { |
| 100 |
match text(&request.carried, "status") { |
| 101 |
None => Ok(Some(ProblemStatus::Open)), |
| 102 |
Some(word) if word.eq_ignore_ascii_case("all") => Ok(None), |
| 103 |
Some(word) => word |
| 104 |
.parse() |
| 105 |
.map(Some) |
| 106 |
.map_err(|_| RouteError::not_found("not a triage state")), |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
fn status_word(status: Option<ProblemStatus>) -> &'static str { |
| 112 |
status.as_ref().map_or("all", ProblemStatus::as_str) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
fn filtered(mut action: Action, status: Option<ProblemStatus>, source: Option<&str>) -> Action { |
| 118 |
action = action.carrying("status", status_word(status)); |
| 119 |
if let Some(source) = source { |
| 120 |
action = action.carrying("source", source); |
| 121 |
} |
| 122 |
action |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
fn list_action(status: Option<ProblemStatus>, source: Option<&str>) -> Action { |
| 127 |
filtered(Action::get("/problems/list"), status, source) |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
fn load(state: &AppState, id: ProblemId) -> Result<Problem, RouteError> { |
| 132 |
state |
| 133 |
.problems |
| 134 |
.get_by_id(id, DESKTOP_USER_ID) |
| 135 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 136 |
.ok_or_else(|| RouteError::not_found("no such problem")) |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
fn problem_id(request: &quasi_router::Request) -> Result<ProblemId, RouteError> { |
| 141 |
let raw = request |
| 142 |
.captures |
| 143 |
.get("id") |
| 144 |
.ok_or_else(|| RouteError::not_found("no id"))?; |
| 145 |
uuid::Uuid::parse_str(raw) |
| 146 |
.map(ProblemId::from) |
| 147 |
.map_err(|_| RouteError::not_found("not an id")) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
fn sources(state: &AppState) -> Result<Vec<String>, RouteError> { |
| 159 |
let all = state |
| 160 |
.problems |
| 161 |
.list(DESKTOP_USER_ID, &ProblemFilter::default()) |
| 162 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 163 |
|
| 164 |
let mut sources: Vec<String> = all.into_iter().map(|problem| problem.source).collect(); |
| 165 |
sources.sort_unstable(); |
| 166 |
sources.dedup(); |
| 167 |
Ok(sources) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
struct Listed { |
| 173 |
problem: Problem, |
| 174 |
|
| 175 |
project: Option<String>, |
| 176 |
|
| 177 |
stale: bool, |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
struct Listing { |
| 186 |
rows: Vec<Listed>, |
| 187 |
status: Option<ProblemStatus>, |
| 188 |
source: Option<String>, |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
fn read(state: &AppState, request: &quasi_router::Request) -> Result<Listing, RouteError> { |
| 197 |
let status = status_filter(request)?; |
| 198 |
let source = text(&request.carried, "source").map(str::to_owned); |
| 199 |
|
| 200 |
let problems = state |
| 201 |
.problems |
| 202 |
.list( |
| 203 |
DESKTOP_USER_ID, |
| 204 |
&ProblemFilter { |
| 205 |
source: source.clone(), |
| 206 |
status, |
| 207 |
project_id: None, |
| 208 |
}, |
| 209 |
) |
| 210 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 211 |
|
| 212 |
if problems.is_empty() { |
| 213 |
return Ok(Listing { |
| 214 |
rows: Vec::new(), |
| 215 |
status, |
| 216 |
source, |
| 217 |
}); |
| 218 |
} |
| 219 |
|
| 220 |
let projects = state |
| 221 |
.projects |
| 222 |
.list_all(DESKTOP_USER_ID) |
| 223 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 224 |
let name_of = |id: Option<ProjectId>| { |
| 225 |
id.and_then(|id| { |
| 226 |
projects |
| 227 |
.iter() |
| 228 |
.find(|project| project.id == id) |
| 229 |
.map(|project| project.name.clone()) |
| 230 |
}) |
| 231 |
}; |
| 232 |
|
| 233 |
let mut last_pulls: HashMap<String, Option<DateTime<Utc>>> = HashMap::new(); |
| 234 |
for name in problems |
| 235 |
.iter() |
| 236 |
.map(|problem| problem.source.clone()) |
| 237 |
.collect::<HashSet<_>>() |
| 238 |
{ |
| 239 |
let at = state |
| 240 |
.problems |
| 241 |
.last_pulled_at(DESKTOP_USER_ID, &name) |
| 242 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 243 |
last_pulls.insert(name, at); |
| 244 |
} |
| 245 |
|
| 246 |
let rows = problems |
| 247 |
.into_iter() |
| 248 |
.map(|problem| Listed { |
| 249 |
project: name_of(problem.project_id), |
| 250 |
stale: last_pulls |
| 251 |
.get(&problem.source) |
| 252 |
.copied() |
| 253 |
.flatten() |
| 254 |
.is_some_and(|at| problem.is_stale(at)), |
| 255 |
problem, |
| 256 |
}) |
| 257 |
.collect(); |
| 258 |
|
| 259 |
Ok(Listing { |
| 260 |
rows, |
| 261 |
status, |
| 262 |
source, |
| 263 |
}) |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
fn has_body(listed: &Listed) -> bool { |
| 268 |
!listed.problem.body.trim().is_empty() |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
fn painhours(listed: &Listed) -> String { |
| 273 |
listed.problem.painhours().to_string() |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
fn project_name(listed: &Listed) -> Option<&str> { |
| 278 |
listed.project.as_deref() |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
fn score_line(listed: &Listed) -> String { |
| 289 |
format!( |
| 290 |
"pain {} x scale {}, aged {} · {}", |
| 291 |
listed.problem.pain, |
| 292 |
listed.problem.scale, |
| 293 |
listed.problem.age(), |
| 294 |
listed.problem.source_ref, |
| 295 |
) |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
fn is_open(listed: &Listed) -> bool { |
| 300 |
listed.problem.status == ProblemStatus::Open |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
fn promoted_task(listed: &Listed) -> Option<goingson_core::TaskId> { |
| 309 |
(listed.problem.status == ProblemStatus::Promoted) |
| 310 |
.then_some(listed.problem.promoted_task_id) |
| 311 |
.flatten() |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
fn promote_action(listing: &Listing, listed: &Listed) -> Action { |
| 316 |
filtered( |
| 317 |
Action::post(format!("/problems/{}/promote", listed.problem.id)), |
| 318 |
listing.status, |
| 319 |
listing.source.as_deref(), |
| 320 |
) |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
fn triage_action(listing: &Listing, listed: &Listed, to: &str) -> Action { |
| 329 |
filtered( |
| 330 |
Action::post(format!("/problems/{}/status", listed.problem.id)), |
| 331 |
listing.status, |
| 332 |
listing.source.as_deref(), |
| 333 |
) |
| 334 |
.with("status", to) |
| 335 |
} |
| 336 |
|
| 337 |
declare! { |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
shape row_for(listing: &Listing, listed: &Listed) -> Row; |
| 368 |
|
| 369 |
row &listed.problem.title { |
| 370 |
secondary listed.problem.body.clone() when has_body(listed); |
| 371 |
|
| 372 |
token Tag::badge(painhours(listed)).tone(band_tone(listed.problem.band())); |
| 373 |
token Tag::chip( |
| 374 |
&listed.problem.source, |
| 375 |
list_action(listing.status, Some(&listed.problem.source)) |
| 376 |
); |
| 377 |
for project in project_name(listed).into_iter() { |
| 378 |
token Tag::badge(project).tone(Tone::Info); |
| 379 |
} |
| 380 |
token Tag::badge(listed.problem.status.as_str()) |
| 381 |
.tone(status_tone(listed.problem.status)) |
| 382 |
when listed.problem.status.is_settled(); |
| 383 |
token Tag::badge("Dormant").tone(Tone::Neutral) when listed.problem.is_dormant(); |
| 384 |
token Tag::badge("Stale").tone(Tone::Warning) when listed.stale; |
| 385 |
|
| 386 |
for tag in listed.problem.tags.iter() { |
| 387 |
token Tag::badge(tag); |
| 388 |
} |
| 389 |
|
| 390 |
meta score_line(listed); |
| 391 |
|
| 392 |
act "Promote" to doing promote_action(listing, listed) when is_open(listed); |
| 393 |
act "Dismiss" to doing triage_action(listing, listed, "Dismissed") when is_open(listed); |
| 394 |
|
| 395 |
for task in promoted_task(listed).into_iter() { |
| 396 |
act "Open task" to get "/tasks/{task}"; |
| 397 |
} |
| 398 |
|
| 399 |
act "Reopen" to doing triage_action(listing, listed, "Open") unless is_open(listed); |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
fn nothing_here(listing: &Listing) -> &'static str { |
| 405 |
match (listing.status, listing.source.as_deref()) { |
| 406 |
(Some(ProblemStatus::Open), None) => { |
| 407 |
"Nothing waiting for triage. Problems arrive from wam and from audit runs; \ |
| 408 |
they are candidates, and promoting one makes it a task." |
| 409 |
} |
| 410 |
(Some(_), _) | (None, Some(_)) => "No problems match that filter.", |
| 411 |
(None, None) => "No problems yet.", |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
declare! { |
| 416 |
|
| 417 |
shape ranked(listing: &Listing) -> Node; |
| 418 |
|
| 419 |
given listing.rows.is_empty() { |
| 420 |
true -> empty nothing_here(listing); |
| 421 |
otherwise -> list { |
| 422 |
for listed in listing.rows.iter() { |
| 423 |
include row_for(listing, listed); |
| 424 |
} |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
fn status_latched(listing: &Listing, offered: Option<ProblemStatus>) -> bool { |
| 431 |
listing.status == offered |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
fn source_latched(listing: &Listing, offered: &str) -> bool { |
| 436 |
listing.source.as_deref() == Some(offered) |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
fn cleared<'a>(listing: &Listing, offered: &'a str) -> Option<&'a str> { |
| 445 |
(!source_latched(listing, offered)).then_some(offered) |
| 446 |
} |
| 447 |
|
| 448 |
declare! { |
| 449 |
|
| 450 |
shape screen(listing: &Listing, offered: &[String]) -> Screen; |
| 451 |
|
| 452 |
screen list_detail "Problems" false { |
| 453 |
at_place super::shell::PROBLEMS; |
| 454 |
|
| 455 |
region "problems-band" as Band { |
| 456 |
page "Problems"; |
| 457 |
|
| 458 |
for state in STATUSES { |
| 459 |
chip status_word(state) |
| 460 |
to doing list_action(state, listing.source.as_deref()) { |
| 461 |
latched status_latched(listing, state); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
for source in offered.iter() { |
| 466 |
chip source to doing list_action(listing.status, cleared(listing, source)) { |
| 467 |
latched source_latched(listing, source); |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
region "problems-list" as Pane { |
| 473 |
include ranked(listing); |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
fn index(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 480 |
let listing = read(state, &request)?; |
| 481 |
Ok(screen(&listing, &sources(state)?).into()) |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
fn list(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 486 |
Ok(Response::fragment( |
| 487 |
"problems-list", |
| 488 |
ranked(&read(state, &request)?), |
| 489 |
)) |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
fn triaged( |
| 498 |
state: &AppState, |
| 499 |
request: &quasi_router::Request, |
| 500 |
message: &str, |
| 501 |
) -> Result<Response, RouteError> { |
| 502 |
Ok( |
| 503 |
Response::fragment("problems-list", ranked(&read(state, request)?)) |
| 504 |
.toast(Tone::Success, message), |
| 505 |
) |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
fn promote_one(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 515 |
let id = problem_id(&request)?; |
| 516 |
let outcome = promote( |
| 517 |
state, |
| 518 |
id, |
| 519 |
&PromoteProblemInput { |
| 520 |
description: None, |
| 521 |
priority: None, |
| 522 |
}, |
| 523 |
) |
| 524 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 525 |
|
| 526 |
triaged( |
| 527 |
state, |
| 528 |
&request, |
| 529 |
if outcome.created { |
| 530 |
"Promoted to a task." |
| 531 |
} else { |
| 532 |
"Already promoted; the task it made is on the row." |
| 533 |
}, |
| 534 |
) |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
fn set_status(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 543 |
let id = problem_id(&request)?; |
| 544 |
let target: ProblemStatus = request |
| 545 |
.payload |
| 546 |
.get("status") |
| 547 |
.ok_or_else(|| RouteError::not_found("no status"))? |
| 548 |
.parse() |
| 549 |
.map_err(|_| RouteError::not_found("not a triage state"))?; |
| 550 |
|
| 551 |
|
| 552 |
load(state, id)?; |
| 553 |
state |
| 554 |
.problems |
| 555 |
.set_status(id, DESKTOP_USER_ID, target) |
| 556 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 557 |
.ok_or_else(|| RouteError::not_found("no such problem"))?; |
| 558 |
|
| 559 |
triaged( |
| 560 |
state, |
| 561 |
&request, |
| 562 |
match target { |
| 563 |
ProblemStatus::Open => "Back in triage.", |
| 564 |
ProblemStatus::Dismissed => "Dismissed. It stays down through the next pull.", |
| 565 |
ProblemStatus::Promoted => "Marked promoted.", |
| 566 |
ProblemStatus::Resolved => "Marked resolved.", |
| 567 |
}, |
| 568 |
) |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
#[must_use] |
| 573 |
pub fn routes(router: Router<AppState>) -> Router<AppState> { |
| 574 |
router |
| 575 |
.get("/problems", index) |
| 576 |
.get("/problems/list", list) |
| 577 |
.post("/problems/{id}/promote", promote_one) |
| 578 |
.post("/problems/{id}/status", set_status) |
| 579 |
} |
| 580 |
|