The mail ledger crosses over
Browse files at this commit923fdba9458c39e75413eb3df57e7e67818e8b35Parent: 02916e3- feat
- the mail ledger crosses over
1 changed file +411 -1
- crates/izlek-topcoat/src/logs.rs +411 -1
crates/izlek-topcoat/src/logs.rsmodified+411 -1
| @@ -1 +1,411 @@ | |||
| 1 | -//! Lands in the page-port fan-out. | 1 | +//! The admin's Logs screen: what mail is owed, what the rules decided, and |
| 2 | +//! what happened across the workspace. Read-only — nothing here is written | ||
| 3 | +//! back, so unlike Mail rules there is no action to guard beyond the read | ||
| 4 | +//! itself. | ||
| 5 | +//! | ||
| 6 | +//! Ported from `izlek-web/src/logs.rs`. That version reads through a Leptos | ||
| 7 | +//! server fn behind a `Resource`; here the page is rendered server-side on | ||
| 8 | +//! every request, so `snapshot` — the shared read — backs both the page and | ||
| 9 | +//! the JSON route `tests/http.rs` polls. | ||
| 10 | + | ||
| 11 | +use serde::{Deserialize, Serialize}; | ||
| 12 | +use topcoat::Result; | ||
| 13 | +use topcoat::context::Cx; | ||
| 14 | +use topcoat::router::content::Json; | ||
| 15 | +use topcoat::router::{page, route}; | ||
| 16 | +use topcoat::view::view; | ||
| 17 | + | ||
| 18 | +use crate::server::{Refusal, accounts, require_admin}; | ||
| 19 | + | ||
| 20 | +/// The person the current browser is signed in as. Mirrors | ||
| 21 | +/// `crate::auth::Me` (not yet ported); the two will merge once auth lands. | ||
| 22 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
| 23 | +struct Me { | ||
| 24 | + id: String, | ||
| 25 | + display_name: String, | ||
| 26 | + email: String, | ||
| 27 | + role: izlek_core::Role, | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +/// One send still owed or refused, as the queue panel reads it. | ||
| 31 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
| 32 | +struct QueueLine { | ||
| 33 | + recipient: String, | ||
| 34 | + subject: String, | ||
| 35 | + /// "pending" or "failed". | ||
| 36 | + state: String, | ||
| 37 | + attempts: u32, | ||
| 38 | + last_error: Option<String>, | ||
| 39 | + next_attempt: Option<String>, | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +/// One rule's verdict inside a decision block. | ||
| 43 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
| 44 | +struct RuleVerdict { | ||
| 45 | + rule: String, | ||
| 46 | + outcome: String, | ||
| 47 | + detail: String, | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +/// Every rule's verdict on one event, so the task and moment are said once | ||
| 51 | +/// rather than repeated per rule. | ||
| 52 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
| 53 | +struct DecisionGroup { | ||
| 54 | + event_id: String, | ||
| 55 | + task: String, | ||
| 56 | + /// What the event did — "moved to Review" — when the event and its | ||
| 57 | + /// destination column are both still there; absent rather than guessed | ||
| 58 | + /// at otherwise. | ||
| 59 | + happened: Option<String>, | ||
| 60 | + at: String, | ||
| 61 | + verdicts: Vec<RuleVerdict>, | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +/// One line of the workspace activity feed. | ||
| 65 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
| 66 | +struct ActivityRow { | ||
| 67 | + at: String, | ||
| 68 | + actor: String, | ||
| 69 | + sentence: String, | ||
| 70 | + title: String, | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +/// The screen in one answer. | ||
| 74 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
| 75 | +struct LogsSnapshot { | ||
| 76 | + me: Me, | ||
| 77 | + queue: Vec<QueueLine>, | ||
| 78 | + decisions: Vec<DecisionGroup>, | ||
| 79 | + activity: Vec<ActivityRow>, | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +/// How many mail is owed, what the rules decided, and what happened, all | ||
| 83 | +/// capped so the screen stays a read a person can finish. | ||
| 84 | +const LIMIT: u32 = 50; | ||
| 85 | + | ||
| 86 | +/// The word the decisions panel prints for an outcome. Terse: the panel is | ||
| 87 | +/// read, not explained. | ||
| 88 | +fn outcome_word(outcome: izlek_core::store::MailOutcome) -> &'static str { | ||
| 89 | + use izlek_core::store::MailOutcome; | ||
| 90 | + match outcome { | ||
| 91 | + MailOutcome::Owed => "queued", | ||
| 92 | + MailOutcome::AlreadyOwed => "already queued", | ||
| 93 | + MailOutcome::NoRecipients => "nobody to mail", | ||
| 94 | + MailOutcome::NotMatched => "did not match", | ||
| 95 | + MailOutcome::Disabled => "rule off", | ||
| 96 | + MailOutcome::TaskGone => "task gone", | ||
| 97 | + } | ||
| 98 | +} | ||
| 99 | + | ||
| 100 | +/// The sentence after a task's name, mirroring `ActivityEntry::sentence` — | ||
| 101 | +/// the per-task strip on the detail screen — since the workspace feed carries | ||
| 102 | +/// the same kind and detail but no `Person` to build an `ActivityEntry` from. | ||
| 103 | +fn activity_sentence(kind: &izlek_core::detail::ActivityKind, detail: &str) -> String { | ||
| 104 | + use izlek_core::detail::ActivityKind; | ||
| 105 | + let detail = detail.trim(); | ||
| 106 | + match kind { | ||
| 107 | + ActivityKind::Created => "created this task".to_string(), | ||
| 108 | + ActivityKind::Retitled => "renamed this task".to_string(), | ||
| 109 | + ActivityKind::Described => "edited the description".to_string(), | ||
| 110 | + ActivityKind::DeadlineSet => format!("set deadline {detail}"), | ||
| 111 | + ActivityKind::DeadlineCleared => "removed the deadline".to_string(), | ||
| 112 | + ActivityKind::Assigned => format!("assigned {detail}"), | ||
| 113 | + ActivityKind::Unassigned => format!("unassigned {detail}"), | ||
| 114 | + ActivityKind::Linked => format!("linked {detail}"), | ||
| 115 | + ActivityKind::Unlinked => format!("unlinked {detail}"), | ||
| 116 | + ActivityKind::Moved => format!("moved {detail}"), | ||
| 117 | + ActivityKind::Unblocked => format!("unblocked this task — {detail}"), | ||
| 118 | + ActivityKind::Deleted => "deleted this task".to_string(), | ||
| 119 | + ActivityKind::Commented => "commented".to_string(), | ||
| 120 | + ActivityKind::Other(_) => detail.to_string(), | ||
| 121 | + } | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +/// What an event did, in the decisions panel's own words — "moved to | ||
| 125 | +/// Review" — or nothing when the event or its destination column is gone: | ||
| 126 | +/// the panel names what it can still read, never a guess. | ||
| 127 | +async fn event_happened( | ||
| 128 | + store: &std::sync::Arc<dyn izlek_core::store::Store>, | ||
| 129 | + event_id: &str, | ||
| 130 | +) -> std::result::Result<Option<String>, izlek_core::store::StoreError> { | ||
| 131 | + use izlek_core::store::Event; | ||
| 132 | + | ||
| 133 | + let Some(event) = store.event(event_id).await? else { | ||
| 134 | + return Ok(None); | ||
| 135 | + }; | ||
| 136 | + match event { | ||
| 137 | + Event::Moved(transition) => { | ||
| 138 | + let Some(board_id) = store.board_of_task(&transition.task_id).await? else { | ||
| 139 | + return Ok(None); | ||
| 140 | + }; | ||
| 141 | + let columns = store.columns_for_board(&board_id).await?; | ||
| 142 | + Ok(columns | ||
| 143 | + .into_iter() | ||
| 144 | + .find(|column| column.id == transition.to_column) | ||
| 145 | + .map(|column| format!("moved to {}", column.name))) | ||
| 146 | + } | ||
| 147 | + Event::Freed(_) => Ok(Some("unblocked".to_string())), | ||
| 148 | + // Not a wired path yet — S4's work — so this only needs to be | ||
| 149 | + // exhaustive, not right. | ||
| 150 | + Event::Happened(activity) => Ok(Some(activity.kind.as_str().to_string())), | ||
| 151 | + } | ||
| 152 | +} | ||
| 153 | + | ||
| 154 | +/// The queue, the decisions, and the activity, admin only. Shared by the | ||
| 155 | +/// `/logs` page and the `/api/current_logs` route so both answer the same | ||
| 156 | +/// read the same way. | ||
| 157 | +async fn snapshot(cx: &Cx) -> Result<std::result::Result<LogsSnapshot, Refusal>> { | ||
| 158 | + use izlek_core::store::SendState; | ||
| 159 | + | ||
| 160 | + let user = match require_admin(cx).await { | ||
| 161 | + Ok(user) => user, | ||
| 162 | + Err(refusal) => return Ok(Err(refusal)), | ||
| 163 | + }; | ||
| 164 | + let store = accounts(cx).store().clone(); | ||
| 165 | + | ||
| 166 | + let sends = store.mail_queue(LIMIT).await?; | ||
| 167 | + let mut queue = Vec::with_capacity(sends.len()); | ||
| 168 | + for send in sends { | ||
| 169 | + let subject = match &send.subject { | ||
| 170 | + Some(subject) => subject.clone(), | ||
| 171 | + None => match &send.rule_id { | ||
| 172 | + Some(id) => store | ||
| 173 | + .mail_rule(id) | ||
| 174 | + .await? | ||
| 175 | + .map(|rule| rule.subject) | ||
| 176 | + .unwrap_or_else(|| "a rule that is gone".to_string()), | ||
| 177 | + None => "a rule that is gone".to_string(), | ||
| 178 | + }, | ||
| 179 | + }; | ||
| 180 | + queue.push(QueueLine { | ||
| 181 | + recipient: send.recipient, | ||
| 182 | + subject, | ||
| 183 | + state: match send.state { | ||
| 184 | + SendState::Pending => "pending".to_string(), | ||
| 185 | + // A send the engine never attempted is only held — usually | ||
| 186 | + // for want of a sender — not failed at anything. | ||
| 187 | + SendState::Failed if send.attempts == 0 => "held".to_string(), | ||
| 188 | + SendState::Failed => "failed".to_string(), | ||
| 189 | + SendState::Sent => "sent".to_string(), | ||
| 190 | + SendState::Abandoned => "abandoned".to_string(), | ||
| 191 | + }, | ||
| 192 | + attempts: send.attempts, | ||
| 193 | + last_error: send.last_error, | ||
| 194 | + next_attempt: send.next_attempt_at.map(izlek_core::detail::moment_label), | ||
| 195 | + }); | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + let raw_decisions = store.recent_mail_decisions(LIMIT).await?; | ||
| 199 | + let mut decisions: Vec<DecisionGroup> = Vec::new(); | ||
| 200 | + for decision in raw_decisions { | ||
| 201 | + let rule = store | ||
| 202 | + .mail_rule(&decision.rule_id) | ||
| 203 | + .await? | ||
| 204 | + .map(|rule| rule.subject) | ||
| 205 | + .unwrap_or_else(|| "a rule that is gone".to_string()); | ||
| 206 | + let verdict = RuleVerdict { | ||
| 207 | + rule, | ||
| 208 | + outcome: outcome_word(decision.outcome).to_string(), | ||
| 209 | + detail: decision.detail, | ||
| 210 | + }; | ||
| 211 | + if let Some(group) = decisions.iter_mut().find(|g| g.event_id == decision.event_id) { | ||
| 212 | + group.verdicts.push(verdict); | ||
| 213 | + continue; | ||
| 214 | + } | ||
| 215 | + let task = store | ||
| 216 | + .task(&decision.task_id) | ||
| 217 | + .await? | ||
| 218 | + .map(|facts| format!("{} {}", facts.row.task_key, facts.row.title)) | ||
| 219 | + .unwrap_or_else(|| "a task that is gone".to_string()); | ||
| 220 | + let happened = event_happened(&store, &decision.event_id).await?; | ||
| 221 | + decisions.push(DecisionGroup { | ||
| 222 | + event_id: decision.event_id, | ||
| 223 | + task, | ||
| 224 | + happened, | ||
| 225 | + at: izlek_core::detail::moment_label(decision.at), | ||
| 226 | + verdicts: vec![verdict], | ||
| 227 | + }); | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + let activity = store | ||
| 231 | + .recent_activity(LIMIT) | ||
| 232 | + .await? | ||
| 233 | + .into_iter() | ||
| 234 | + .map(|line| ActivityRow { | ||
| 235 | + at: izlek_core::detail::moment_label(line.at), | ||
| 236 | + actor: line.actor_name.unwrap_or_else(|| "The system".to_string()), | ||
| 237 | + sentence: activity_sentence(&line.kind, &line.detail), | ||
| 238 | + title: line.title, | ||
| 239 | + }) | ||
| 240 | + .collect(); | ||
| 241 | + | ||
| 242 | + Ok(Ok(LogsSnapshot { | ||
| 243 | + me: Me { | ||
| 244 | + id: user.id, | ||
| 245 | + display_name: user.display_name, | ||
| 246 | + email: user.email, | ||
| 247 | + role: user.role, | ||
| 248 | + }, | ||
| 249 | + queue, | ||
| 250 | + decisions, | ||
| 251 | + activity, | ||
| 252 | + })) | ||
| 253 | +} | ||
| 254 | + | ||
| 255 | +/// The same read the page renders, as JSON — polled by `tests/http.rs`. | ||
| 256 | +#[route(POST "/api/current_logs")] | ||
| 257 | +async fn current_logs(cx: &Cx) -> Result<Json<std::result::Result<LogsSnapshot, Refusal>>> { | ||
| 258 | + Ok(Json(snapshot(cx).await?)) | ||
| 259 | +} | ||
| 260 | + | ||
| 261 | +#[page("/logs")] | ||
| 262 | +async fn logs_page(cx: &Cx) -> Result { | ||
| 263 | + match snapshot(cx).await { | ||
| 264 | + Ok(Ok(snapshot)) => logs_screen(cx, snapshot).await, | ||
| 265 | + Ok(Err(refusal)) => view! { | ||
| 266 | + cx => | ||
| 267 | + <main class="scaffold-note"> | ||
| 268 | + <p>(refusal.message())</p> | ||
| 269 | + <p><a href="/">"Back to the board"</a></p> | ||
| 270 | + </main> | ||
| 271 | + }, | ||
| 272 | + Err(_) => view! { | ||
| 273 | + cx => | ||
| 274 | + <main class="scaffold-note"> | ||
| 275 | + <p>"Something went wrong."</p> | ||
| 276 | + </main> | ||
| 277 | + }, | ||
| 278 | + } | ||
| 279 | +} | ||
| 280 | + | ||
| 281 | +async fn logs_screen(cx: &Cx, snapshot: LogsSnapshot) -> Result { | ||
| 282 | + let me = snapshot.me; | ||
| 283 | + let queue = snapshot.queue; | ||
| 284 | + let decisions = snapshot.decisions; | ||
| 285 | + let activity = snapshot.activity; | ||
| 286 | + let queue_empty = queue.is_empty(); | ||
| 287 | + let decisions_empty = decisions.is_empty(); | ||
| 288 | + let activity_empty = activity.is_empty(); | ||
| 289 | + | ||
| 290 | + view! { | ||
| 291 | + cx => | ||
| 292 | + <header class="topbar"> | ||
| 293 | + <div class="wordmark"> | ||
| 294 | + <span class="wordmark-text">"izlek"</span> | ||
| 295 | + <span class="wordmark-dot"></span> | ||
| 296 | + </div> | ||
| 297 | + <div class="topbar-divider"></div> | ||
| 298 | + <span class="board-name">"Logs"</span> | ||
| 299 | + <div class="spacer"></div> | ||
| 300 | + <span class="topbar-who" title=(me.email)>(me.display_name)</span> | ||
| 301 | + </header> | ||
| 302 | + | ||
| 303 | + <div class="settings-shell"> | ||
| 304 | + <nav class="sidenav"> | ||
| 305 | + <a class="sidenav-item" href="/">"Board"</a> | ||
| 306 | + <a class="sidenav-item" href="/rules">"Mail rules"</a> | ||
| 307 | + <a class="sidenav-item sidenav-item-on" href="/logs">"Logs"</a> | ||
| 308 | + <a class="sidenav-item" href="/settings">"Settings"</a> | ||
| 309 | + </nav> | ||
| 310 | + | ||
| 311 | + <main class="settings-stage"> | ||
| 312 | + <div class="settings-head"> | ||
| 313 | + <h1 class="settings-title">"Logs"</h1> | ||
| 314 | + <span class="chip chip-admin">"Admin only"</span> | ||
| 315 | + </div> | ||
| 316 | + | ||
| 317 | + <section class="panel"> | ||
| 318 | + <div class="panel-head"> | ||
| 319 | + <h2 class="panel-title">"Mail queue"</h2> | ||
| 320 | + </div> | ||
| 321 | + <div class="panel-body"> | ||
| 322 | + <div class="rule-list"> | ||
| 323 | + for line in queue { | ||
| 324 | + let state_note = if line.state == "failed" || line.state == "held" { | ||
| 325 | + line.last_error.unwrap_or_else(|| line.state.clone()) | ||
| 326 | + } else { | ||
| 327 | + line.state.clone() | ||
| 328 | + }; | ||
| 329 | + let next_attempt = line.next_attempt.unwrap_or_else(|| "no retry".to_string()); | ||
| 330 | + <div class="rule-row"> | ||
| 331 | + <div class="rule-sentence"> | ||
| 332 | + <span class="rule-term">(line.recipient)</span> | ||
| 333 | + <span>(line.subject)</span> | ||
| 334 | + <span class="rule-term">(state_note)</span> | ||
| 335 | + <span>(format!("attempt {}", line.attempts))</span> | ||
| 336 | + </div> | ||
| 337 | + <span class="rule-stamp">(next_attempt)</span> | ||
| 338 | + </div> | ||
| 339 | + } | ||
| 340 | + if queue_empty { | ||
| 341 | + <p class="rules-quiet">"Nothing owed."</p> | ||
| 342 | + } | ||
| 343 | + </div> | ||
| 344 | + </div> | ||
| 345 | + </section> | ||
| 346 | + | ||
| 347 | + <section class="panel"> | ||
| 348 | + <div class="panel-head"> | ||
| 349 | + <h2 class="panel-title">"Mail decisions"</h2> | ||
| 350 | + </div> | ||
| 351 | + <div class="panel-body"> | ||
| 352 | + <div class="rule-list"> | ||
| 353 | + for group in decisions { | ||
| 354 | + let header = match group.happened { | ||
| 355 | + Some(happened) => format!("{} · {}", group.task, happened), | ||
| 356 | + None => group.task, | ||
| 357 | + }; | ||
| 358 | + <div class="decision-block"> | ||
| 359 | + <div class="decision-head"> | ||
| 360 | + <span class="decision-title">(header)</span> | ||
| 361 | + <span class="rule-stamp">(group.at)</span> | ||
| 362 | + </div> | ||
| 363 | + <div class="decision-verdicts"> | ||
| 364 | + for verdict in group.verdicts { | ||
| 365 | + let chip_class = if verdict.outcome == "queued" { | ||
| 366 | + "rule-term rule-term-queued" | ||
| 367 | + } else { | ||
| 368 | + "rule-term" | ||
| 369 | + }; | ||
| 370 | + <div class="decision-verdict"> | ||
| 371 | + <span class="rule-term">(verdict.rule)</span> | ||
| 372 | + <span class=(chip_class)>(verdict.outcome)</span> | ||
| 373 | + <span>(verdict.detail)</span> | ||
| 374 | + </div> | ||
| 375 | + } | ||
| 376 | + </div> | ||
| 377 | + </div> | ||
| 378 | + } | ||
| 379 | + if decisions_empty { | ||
| 380 | + <p class="rules-quiet">"No decisions yet."</p> | ||
| 381 | + } | ||
| 382 | + </div> | ||
| 383 | + </div> | ||
| 384 | + </section> | ||
| 385 | + | ||
| 386 | + <section class="panel"> | ||
| 387 | + <div class="panel-head"> | ||
| 388 | + <h2 class="panel-title">"Activity"</h2> | ||
| 389 | + </div> | ||
| 390 | + <div class="panel-body"> | ||
| 391 | + <div class="rule-list"> | ||
| 392 | + for line in activity { | ||
| 393 | + <div class="rule-row"> | ||
| 394 | + <div class="rule-sentence"> | ||
| 395 | + <span class="rule-term">(line.actor)</span> | ||
| 396 | + <span>(line.sentence)</span> | ||
| 397 | + <span>(line.title)</span> | ||
| 398 | + </div> | ||
| 399 | + <span class="rule-stamp">(line.at)</span> | ||
| 400 | + </div> | ||
| 401 | + } | ||
| 402 | + if activity_empty { | ||
| 403 | + <p class="rules-quiet">"Nothing yet."</p> | ||
| 404 | + } | ||
| 405 | + </div> | ||
| 406 | + </div> | ||
| 407 | + </section> | ||
| 408 | + </main> | ||
| 409 | + </div> | ||
| 410 | + } | ||
| 411 | +} | ||