| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#![allow(clippy::needless_pass_by_value)] |
| 120 |
|
| 121 |
use std::time::{Duration, SystemTime}; |
| 122 |
|
| 123 |
use chrono::TimeZone as _; |
| 124 |
use goingson_core::{ |
| 125 |
Task, TaskFilterQuery, TaskId, TaskStatus, TimeReport, TimeReportProject, TimeSession, |
| 126 |
TimeSessionMode, TimeSummaryPanel, |
| 127 |
}; |
| 128 |
use makeover_layout::Tone; |
| 129 |
use quasi_declare::declare; |
| 130 |
use quasi_router::screen::{Consult, Figure, Meter, Tag}; |
| 131 |
use quasi_router::{Action, Chrome, Node, Response, Role, RouteError, Router}; |
| 132 |
|
| 133 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 134 |
|
| 135 |
#[cfg(test)] |
| 136 |
mod tests; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
pub const PANEL: &str = "timer-panel"; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
pub const BODY: &str = "timer"; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
#[must_use] |
| 166 |
pub fn chrome(chrome: Chrome) -> Chrome { |
| 167 |
chrome.presenting(PANEL, Role::Activity, Node::Region(band())) |
| 168 |
} |
| 169 |
|
| 170 |
declare! { |
| 171 |
|
| 172 |
shape band() -> Slot; |
| 173 |
|
| 174 |
region BODY as Band { |
| 175 |
fed_by Action::get("/timer/panel"); |
| 176 |
live; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
fn started(session: &TimeSession) -> Option<SystemTime> { |
| 186 |
instant(session.started_at) |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
fn ending(session: &TimeSession) -> Option<SystemTime> { |
| 191 |
instant(session.ends_at?) |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
fn instant(at: chrono::DateTime<chrono::Utc>) -> Option<SystemTime> { |
| 199 |
let seconds = u64::try_from(at.timestamp()).ok()?; |
| 200 |
Some(SystemTime::UNIX_EPOCH + Duration::from_secs(seconds)) |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
fn spans(minutes: i32) -> String { |
| 210 |
let minutes = minutes.max(0); |
| 211 |
match (minutes / 60, minutes % 60) { |
| 212 |
(0, rest) => format!("{rest}m"), |
| 213 |
(hours, 0) => format!("{hours}h"), |
| 214 |
(hours, rest) => format!("{hours}h {rest}m"), |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
struct Running { |
| 220 |
session: TimeSession, |
| 221 |
|
| 222 |
description: String, |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
fn running(state: &AppState) -> Result<Option<Running>, RouteError> { |
| 227 |
Ok(state |
| 228 |
.tasks |
| 229 |
.get_active_timer(DESKTOP_USER_ID) |
| 230 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 231 |
.map(|(session, description)| Running { |
| 232 |
session, |
| 233 |
description, |
| 234 |
})) |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
fn is_running(running: Option<&Running>) -> bool { |
| 239 |
running.is_some() |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
fn mode_label(running: Option<&Running>) -> &'static str { |
| 249 |
running.map_or("", |running| running.session.mode.label()) |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
fn description(running: Option<&Running>) -> &str { |
| 254 |
running.map_or("", |running| running.description.as_str()) |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
fn counting_down(running: Option<&Running>) -> Option<SystemTime> { |
| 264 |
let running = running?; |
| 265 |
match running.session.mode { |
| 266 |
TimeSessionMode::Focus => ending(&running.session), |
| 267 |
TimeSessionMode::Track => None, |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
fn started_at(running: Option<&Running>) -> Option<SystemTime> { |
| 273 |
started(&running?.session) |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
fn counting_up(running: Option<&Running>) -> Option<SystemTime> { |
| 287 |
counting_down(running) |
| 288 |
.is_none() |
| 289 |
.then(|| started_at(running))? |
| 290 |
} |
| 291 |
|
| 292 |
declare! { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
shape contents(running: Option<&Running>) -> Slot; |
| 303 |
|
| 304 |
region BODY as Band { |
| 305 |
fed_by Action::get("/timer/panel"); |
| 306 |
live; |
| 307 |
|
| 308 |
text mode_label(running) when is_running(running); |
| 309 |
text description(running) when is_running(running); |
| 310 |
|
| 311 |
for at in counting_down(running).into_iter() { |
| 312 |
until at; |
| 313 |
} |
| 314 |
for at in counting_up(running).into_iter() { |
| 315 |
since at; |
| 316 |
} |
| 317 |
|
| 318 |
act "Stop" to post "/timer/stop" when is_running(running) { |
| 319 |
tone Success; |
| 320 |
} |
| 321 |
|
| 322 |
act "Discard" to post "/timer/discard" when is_running(running) { |
| 323 |
tone Danger; |
| 324 |
confirm "Discard the time this timer has tracked?"; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
fn panel(state: &AppState) -> Result<Response, RouteError> { |
| 331 |
Ok(Response::fragment( |
| 332 |
BODY, |
| 333 |
Node::Region(contents(running(state)?.as_ref())), |
| 334 |
)) |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
fn showing(state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> { |
| 339 |
panel(state) |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
fn asked_for(request: &quasi_router::Request) -> Result<TaskId, RouteError> { |
| 344 |
let raw = request |
| 345 |
.payload |
| 346 |
.get("task") |
| 347 |
.or_else(|| request.carried.get("task")) |
| 348 |
.ok_or_else(|| RouteError::not_found("no task id"))?; |
| 349 |
Ok(TaskId::from( |
| 350 |
uuid::Uuid::parse_str(raw).map_err(|_| RouteError::not_found("not a task id"))?, |
| 351 |
)) |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
fn start(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 361 |
let id = asked_for(&request)?; |
| 362 |
match state.tasks.start_timer(id, DESKTOP_USER_ID) { |
| 363 |
Ok(_) => Ok(panel(state)?.toast(Tone::Success, "Timer started.")), |
| 364 |
Err(error) => Ok(panel(state)?.toast(Tone::Warning, error.to_string())), |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
fn stop(state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> { |
| 375 |
let running = state |
| 376 |
.tasks |
| 377 |
.get_active_timer(DESKTOP_USER_ID) |
| 378 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
let Some((session, _)) = running else { |
| 384 |
return panel(state); |
| 385 |
}; |
| 386 |
|
| 387 |
let stopped = state |
| 388 |
.tasks |
| 389 |
.stop_timer(session.task_id, DESKTOP_USER_ID) |
| 390 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 391 |
|
| 392 |
let recorded = stopped |
| 393 |
.and_then(|session| session.duration_minutes) |
| 394 |
.unwrap_or(0); |
| 395 |
Ok(panel(state)?.toast(Tone::Success, format!("Tracked {}", spans(recorded)))) |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
fn discard(state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> { |
| 400 |
let running = state |
| 401 |
.tasks |
| 402 |
.get_active_timer(DESKTOP_USER_ID) |
| 403 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 404 |
|
| 405 |
let Some((session, _)) = running else { |
| 406 |
return panel(state); |
| 407 |
}; |
| 408 |
|
| 409 |
state |
| 410 |
.tasks |
| 411 |
.discard_timer(session.task_id, DESKTOP_USER_ID) |
| 412 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 413 |
|
| 414 |
Ok(panel(state)?.toast(Tone::Info, "Timer discarded.")) |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
const SESSION: &str = "timer-session"; |
| 421 |
|
| 422 |
|
| 423 |
const CHOICES: &str = "timer-choices"; |
| 424 |
|
| 425 |
|
| 426 |
const REPORT: &str = "timer-report"; |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
pub const SUMMARY: &str = "time-summary"; |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
const WORK: i64 = 25; |
| 438 |
const BREAK: i64 = 5; |
| 439 |
const DAYS: i64 = 7; |
| 440 |
|
| 441 |
|
| 442 |
const WINDOWS: [i64; 3] = [7, 30, 90]; |
| 443 |
|
| 444 |
|
| 445 |
const OFFERED: i64 = 200; |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
#[derive(Clone, Copy)] |
| 453 |
struct View { |
| 454 |
|
| 455 |
work: i64, |
| 456 |
|
| 457 |
rest: i64, |
| 458 |
|
| 459 |
days: i64, |
| 460 |
} |
| 461 |
|
| 462 |
impl Default for View { |
| 463 |
fn default() -> Self { |
| 464 |
Self { |
| 465 |
work: WORK, |
| 466 |
rest: BREAK, |
| 467 |
days: DAYS, |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
impl View { |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
fn of(request: &quasi_router::Request) -> Self { |
| 479 |
let read = |name: &str, fallback: i64, low: i64, high: i64| { |
| 480 |
request |
| 481 |
.payload |
| 482 |
.get(name) |
| 483 |
.or_else(|| request.carried.get(name)) |
| 484 |
.and_then(|raw| raw.parse::<i64>().ok()) |
| 485 |
.unwrap_or(fallback) |
| 486 |
.clamp(low, high) |
| 487 |
}; |
| 488 |
Self { |
| 489 |
work: read("work", WORK, 1, 240), |
| 490 |
rest: read("break", BREAK, 1, 60), |
| 491 |
days: read("days", DAYS, 1, 365), |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
fn carry(self, action: Action) -> Action { |
| 497 |
action |
| 498 |
.carrying("work", self.work.to_string()) |
| 499 |
.carrying("break", self.rest.to_string()) |
| 500 |
.carrying("days", self.days.to_string()) |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
fn url(self) -> String { |
| 509 |
format!( |
| 510 |
"/timer?work={}&break={}&days={}", |
| 511 |
self.work, self.rest, self.days |
| 512 |
) |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
declare! { |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
shape modes(view: View) -> Slot; |
| 532 |
|
| 533 |
region "timer-modes" as Group { |
| 534 |
section "Track"; |
| 535 |
text "An open-ended stopwatch. Runs until you stop it, and records the time \ |
| 536 |
against the task."; |
| 537 |
|
| 538 |
section "Focus"; |
| 539 |
text "A countdown of {view.work} minutes, then a {view.rest} minute break. \ |
| 540 |
Records the same time."; |
| 541 |
|
| 542 |
field Number "work" "Minutes of work" { |
| 543 |
within "1" "240"; |
| 544 |
value view.work.to_string(); |
| 545 |
consulting Consult::new( |
| 546 |
Action::get("/timer") |
| 547 |
.carrying("break", view.rest.to_string()) |
| 548 |
.carrying("days", view.days.to_string()) |
| 549 |
); |
| 550 |
} |
| 551 |
|
| 552 |
field Number "break" "Minutes of break" { |
| 553 |
within "1" "60"; |
| 554 |
value view.rest.to_string(); |
| 555 |
consulting Consult::new( |
| 556 |
Action::get("/timer") |
| 557 |
.carrying("work", view.work.to_string()) |
| 558 |
.carrying("days", view.days.to_string()) |
| 559 |
); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
declare! { |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
shape session(running: Option<&Running>, view: View) -> Slot; |
| 573 |
|
| 574 |
region SESSION as Band { |
| 575 |
empty "Nothing is being tracked." unless is_running(running); |
| 576 |
|
| 577 |
text description(running) when is_running(running); |
| 578 |
|
| 579 |
for at in started_at(running).into_iter() { |
| 580 |
since at; |
| 581 |
} |
| 582 |
|
| 583 |
act "Stop" to doing view.carry(Action::post("/timer/view/stop")) |
| 584 |
when is_running(running) { |
| 585 |
tone Success; |
| 586 |
} |
| 587 |
|
| 588 |
act "Discard" to doing view.carry(Action::post("/timer/view/discard")) |
| 589 |
when is_running(running) { |
| 590 |
tone Danger; |
| 591 |
confirm "Discard the time this timer has tracked?"; |
| 592 |
} |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
fn offered(state: &AppState, running: Option<TaskId>) -> Result<Vec<Task>, RouteError> { |
| 602 |
let mut out = Vec::new(); |
| 603 |
for status in [TaskStatus::Started, TaskStatus::Pending] { |
| 604 |
let (tasks, _) = state |
| 605 |
.tasks |
| 606 |
.list_filtered( |
| 607 |
DESKTOP_USER_ID, |
| 608 |
TaskFilterQuery { |
| 609 |
status: Some(status), |
| 610 |
project_id: None, |
| 611 |
milestone_id: None, |
| 612 |
priority: None, |
| 613 |
show_snoozed: false, |
| 614 |
waiting_only: false, |
| 615 |
offset: Some(0), |
| 616 |
limit: Some(OFFERED), |
| 617 |
sort_column: None, |
| 618 |
sort_direction: None, |
| 619 |
}, |
| 620 |
) |
| 621 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 622 |
out.extend(tasks.into_iter().filter(|task| Some(task.id) != running)); |
| 623 |
} |
| 624 |
Ok(out) |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
struct Offered { |
| 629 |
tasks: Vec<Task>, |
| 630 |
|
| 631 |
|
| 632 |
busy: bool, |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
fn offering(state: &AppState, running: Option<&Running>) -> Result<Offered, RouteError> { |
| 637 |
let held = running.map(|running| running.session.task_id); |
| 638 |
Ok(Offered { |
| 639 |
tasks: offered(state, held)?, |
| 640 |
busy: held.is_some(), |
| 641 |
}) |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
fn task_meta(task: &Task) -> String { |
| 651 |
let project = task.project_name.clone(); |
| 652 |
let estimate = task |
| 653 |
.estimated_minutes |
| 654 |
.map(|minutes| format!("{} est", spans(minutes))); |
| 655 |
let tracked = |
| 656 |
(task.actual_minutes > 0).then(|| format!("{} tracked", spans(task.actual_minutes))); |
| 657 |
let said = [project, estimate, tracked] |
| 658 |
.into_iter() |
| 659 |
.flatten() |
| 660 |
.collect::<Vec<_>>() |
| 661 |
.join(" · "); |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
if said.is_empty() { |
| 666 |
task.project_name_or_dash().to_owned() |
| 667 |
} else { |
| 668 |
said |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
fn today() -> String { |
| 674 |
chrono::Local::now().date_naive().to_string() |
| 675 |
} |
| 676 |
|
| 677 |
declare! { |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
shape row_for(task: &Task, offered: &Offered, view: View) -> Row; |
| 702 |
|
| 703 |
row &task.title { |
| 704 |
meta task_meta(task); |
| 705 |
|
| 706 |
for marker in super::Availability::of(task).marker().into_iter() { |
| 707 |
token marker; |
| 708 |
} |
| 709 |
|
| 710 |
act "Track" |
| 711 |
to doing view.carry(Action::post("/timer/view/track")) |
| 712 |
.with("task", task.id.to_string()) { |
| 713 |
tone Success; |
| 714 |
disabled when offered.busy; |
| 715 |
} |
| 716 |
|
| 717 |
act "Focus {view.work}m" |
| 718 |
to doing view.carry(Action::post("/timer/view/focus")) |
| 719 |
.with("task", task.id.to_string()) { |
| 720 |
disabled when offered.busy; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
act "Log" |
| 726 |
to doing view.carry(Action::post("/timer/view/log")) |
| 727 |
.with("task", task.id.to_string()) { |
| 728 |
field Number "minutes" "Minutes" { |
| 729 |
within "1" "1440"; |
| 730 |
value "30"; |
| 731 |
required; |
| 732 |
} |
| 733 |
field Date "date" "Date" { |
| 734 |
value today(); |
| 735 |
} |
| 736 |
} |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
declare! { |
| 741 |
|
| 742 |
shape choices(offered: &Offered, view: View) -> Slot; |
| 743 |
|
| 744 |
region CHOICES as Pane { |
| 745 |
empty "No pending or started tasks to track." when offered.tasks.is_empty(); |
| 746 |
|
| 747 |
list { |
| 748 |
for task in offered.tasks.iter() { |
| 749 |
include row_for(task, offered, view); |
| 750 |
} |
| 751 |
} unless offered.tasks.is_empty(); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
fn over_window(report: &Report, window: i64) -> bool { |
| 757 |
window == report.view.days |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
fn windowed_view(view: View, window: i64) -> View { |
| 762 |
View { |
| 763 |
days: window, |
| 764 |
..view |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
struct Report { |
| 770 |
read: TimeReport, |
| 771 |
view: View, |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
total: u32, |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
fn reported(state: &AppState, view: View) -> Result<Report, RouteError> { |
| 782 |
let report = crate::commands::time_report(state, Some(view.days)) |
| 783 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 784 |
Ok(Report { |
| 785 |
total: counted(report.tracked_minutes), |
| 786 |
read: report, |
| 787 |
view, |
| 788 |
}) |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
fn counted(minutes: i32) -> u32 { |
| 793 |
u32::try_from(minutes.max(0)).unwrap_or(u32::MAX) |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
fn window_total(report: &Report) -> String { |
| 798 |
format!( |
| 799 |
"{} tracked in the last {} days. Estimates are lifetime totals over tasks that \ |
| 800 |
carry one.", |
| 801 |
spans(report.read.tracked_minutes), |
| 802 |
report.view.days |
| 803 |
) |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
fn has_accuracy(project: &TimeReportProject) -> bool { |
| 812 |
project.estimate_accuracy_percent.is_some() |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
fn project_meta(project: &TimeReportProject) -> String { |
| 822 |
let against = has_accuracy(project).then(|| against_estimate(project)); |
| 823 |
[Some(spans(project.tracked_minutes)), against] |
| 824 |
.into_iter() |
| 825 |
.flatten() |
| 826 |
.collect::<Vec<_>>() |
| 827 |
.join(" · ") |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
fn against_estimate(project: &TimeReportProject) -> String { |
| 832 |
format!( |
| 833 |
"{} est / {} actual", |
| 834 |
spans(project.estimated_minutes), |
| 835 |
spans(project.actual_minutes) |
| 836 |
) |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
fn accuracy(project: &TimeReportProject) -> String { |
| 841 |
format!("{}%", project.estimate_accuracy_percent.unwrap_or(0)) |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
fn overran(project: &TimeReportProject) -> Tone { |
| 846 |
if project |
| 847 |
.estimate_accuracy_percent |
| 848 |
.is_some_and(|percent| percent > 100) |
| 849 |
{ |
| 850 |
Tone::Danger |
| 851 |
} else { |
| 852 |
Tone::Success |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
declare! { |
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
shape report(report: &Report) -> Slot; |
| 863 |
|
| 864 |
region REPORT as Pane { |
| 865 |
fed_by report.view.carry(Action::get("/timer/report")); |
| 866 |
|
| 867 |
section "Where the time went"; |
| 868 |
|
| 869 |
for window in WINDOWS { |
| 870 |
chip "{window}d" |
| 871 |
to doing windowed_view(report.view, window).carry(Action::get("/timer/report")) { |
| 872 |
latched over_window(report, window); |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
empty "Nothing tracked or estimated yet." when report.read.projects.is_empty(); |
| 877 |
|
| 878 |
text window_total(report) unless report.read.projects.is_empty(); |
| 879 |
|
| 880 |
list { |
| 881 |
for project in report.read.projects.iter() { |
| 882 |
row &project.name { |
| 883 |
meta project_meta(project); |
| 884 |
meter Meter::new(counted(project.tracked_minutes), report.total) |
| 885 |
.label("minutes"); |
| 886 |
|
| 887 |
token Tag::badge(accuracy(project)).tone(overran(project)) |
| 888 |
when has_accuracy(project); |
| 889 |
token Tag::badge("no estimates") unless has_accuracy(project); |
| 890 |
} |
| 891 |
} |
| 892 |
} unless report.read.projects.is_empty(); |
| 893 |
} |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
fn week(panel: &TimeSummaryPanel) -> u32 { |
| 898 |
counted( |
| 899 |
panel |
| 900 |
.projects |
| 901 |
.iter() |
| 902 |
.map(|project| project.total_minutes) |
| 903 |
.sum(), |
| 904 |
) |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
fn today_total(panel: &TimeSummaryPanel) -> String { |
| 909 |
spans(panel.today_minutes) |
| 910 |
} |
| 911 |
|
| 912 |
declare! { |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
shape summary_body(panel: &TimeSummaryPanel) -> Slot; |
| 921 |
|
| 922 |
region "time-summary-body" as Group { |
| 923 |
stats [] { |
| 924 |
figure Figure::new(today_total(panel), "today"); |
| 925 |
} |
| 926 |
|
| 927 |
section "This week" unless panel.projects.is_empty(); |
| 928 |
|
| 929 |
list { |
| 930 |
for project in panel.projects.iter() { |
| 931 |
row &project.name { |
| 932 |
meta spans(project.total_minutes); |
| 933 |
meter Meter::new(counted(project.total_minutes), week(panel)) |
| 934 |
.label("minutes"); |
| 935 |
} |
| 936 |
} |
| 937 |
} unless panel.projects.is_empty(); |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
declare! { |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
pub(super) shape summary_panel(panel: &TimeSummaryPanel) -> Slot; |
| 953 |
|
| 954 |
region SUMMARY as Group { |
| 955 |
fed_by Action::get("/timer/summary"); |
| 956 |
showing_at_most_one Some(0); |
| 957 |
|
| 958 |
framed "Time tracked" include summary_body(panel); |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
pub(super) fn tracked(state: &AppState) -> Result<TimeSummaryPanel, RouteError> { |
| 964 |
crate::commands::time_summary_panel(state) |
| 965 |
.map_err(|error| RouteError::internal(error.to_string())) |
| 966 |
} |
| 967 |
|
| 968 |
declare! { |
| 969 |
|
| 970 |
shape screen( |
| 971 |
running: Option<&Running>, |
| 972 |
offered: &Offered, |
| 973 |
window: &Report, |
| 974 |
view: View |
| 975 |
) -> Screen; |
| 976 |
|
| 977 |
screen list_detail "Timer" false { |
| 978 |
at_place super::shell::TIMER; |
| 979 |
|
| 980 |
region "timer-band" as Band { |
| 981 |
page "Timer"; |
| 982 |
} |
| 983 |
|
| 984 |
include session(running, view); |
| 985 |
include modes(view); |
| 986 |
include choices(offered, view); |
| 987 |
include report(window); |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
fn read(state: &AppState, view: View) -> Result<(Option<Running>, Offered, Report), RouteError> { |
| 993 |
let running = running(state)?; |
| 994 |
let offered = offering(state, running.as_ref())?; |
| 995 |
let report = reported(state, view)?; |
| 996 |
Ok((running, offered, report)) |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
fn view(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1001 |
let view = View::of(&request); |
| 1002 |
let (running, offered, window) = read(state, view)?; |
| 1003 |
Ok(screen(running.as_ref(), &offered, &window, view).into()) |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
fn answer(state: &AppState, view: View) -> Result<Response, RouteError> { |
| 1012 |
let (running, offered, window) = read(state, view)?; |
| 1013 |
Ok( |
| 1014 |
Response::fragment(SESSION, Node::Region(session(running.as_ref(), view))) |
| 1015 |
.also(CHOICES, Node::Region(choices(&offered, view))) |
| 1016 |
.also(REPORT, Node::Region(report(&window))), |
| 1017 |
) |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
fn windowed(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1026 |
let view = View::of(&request); |
| 1027 |
Ok(Response::fragment(REPORT, Node::Region(report(&reported(state, view)?))).at(view.url())) |
| 1028 |
} |
| 1029 |
|
| 1030 |
|
| 1031 |
fn summarised(state: &AppState, _request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1032 |
Ok(Response::fragment( |
| 1033 |
SUMMARY, |
| 1034 |
Node::Region(summary_panel(&tracked(state)?)), |
| 1035 |
)) |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
fn track(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1043 |
let id = asked_for(&request)?; |
| 1044 |
let view = View::of(&request); |
| 1045 |
match state.tasks.start_timer(id, DESKTOP_USER_ID) { |
| 1046 |
Ok(_) => Ok(answer(state, view)?.toast(Tone::Success, "Timer started.")), |
| 1047 |
Err(error) => Ok(answer(state, view)?.toast(Tone::Warning, error.to_string())), |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
fn focus(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1066 |
let id = asked_for(&request)?; |
| 1067 |
let view = View::of(&request); |
| 1068 |
let ends_at = chrono::Utc::now() + chrono::Duration::minutes(view.work); |
| 1069 |
match state |
| 1070 |
.tasks |
| 1071 |
.start_focus_session(id, DESKTOP_USER_ID, ends_at) |
| 1072 |
{ |
| 1073 |
Ok(_) => Ok(answer(state, view)?.toast( |
| 1074 |
Tone::Success, |
| 1075 |
format!("Focus session started, {} minutes.", view.work), |
| 1076 |
)), |
| 1077 |
Err(error) => Ok(answer(state, view)?.toast(Tone::Warning, error.to_string())), |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
fn halt(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1083 |
let view = View::of(&request); |
| 1084 |
let running = state |
| 1085 |
.tasks |
| 1086 |
.get_active_timer(DESKTOP_USER_ID) |
| 1087 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1088 |
|
| 1089 |
let Some((session, _)) = running else { |
| 1090 |
return answer(state, view); |
| 1091 |
}; |
| 1092 |
|
| 1093 |
let stopped = state |
| 1094 |
.tasks |
| 1095 |
.stop_timer(session.task_id, DESKTOP_USER_ID) |
| 1096 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1097 |
|
| 1098 |
let recorded = stopped |
| 1099 |
.and_then(|session| session.duration_minutes) |
| 1100 |
.unwrap_or(0); |
| 1101 |
Ok(answer(state, view)?.toast(Tone::Success, format!("Tracked {}", spans(recorded)))) |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
fn drop_it(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1106 |
let view = View::of(&request); |
| 1107 |
let running = state |
| 1108 |
.tasks |
| 1109 |
.get_active_timer(DESKTOP_USER_ID) |
| 1110 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1111 |
|
| 1112 |
let Some((session, _)) = running else { |
| 1113 |
return answer(state, view); |
| 1114 |
}; |
| 1115 |
|
| 1116 |
state |
| 1117 |
.tasks |
| 1118 |
.discard_timer(session.task_id, DESKTOP_USER_ID) |
| 1119 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1120 |
|
| 1121 |
Ok(answer(state, view)?.toast(Tone::Info, "Timer discarded.")) |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
fn log(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1131 |
let id = asked_for(&request)?; |
| 1132 |
let view = View::of(&request); |
| 1133 |
|
| 1134 |
let minutes = request |
| 1135 |
.payload |
| 1136 |
.get("minutes") |
| 1137 |
.and_then(|raw| raw.parse::<i32>().ok()) |
| 1138 |
.ok_or_else(|| RouteError::not_found("no duration"))?; |
| 1139 |
let minutes = goingson_core::PositiveMinutes::try_new(minutes) |
| 1140 |
.map_err(|error| RouteError::not_found(error.to_string()))?; |
| 1141 |
|
| 1142 |
let day = match request.payload.get("date") { |
| 1143 |
Some(raw) => chrono::NaiveDate::parse_from_str(raw, "%Y-%m-%d") |
| 1144 |
.map_err(|_| RouteError::not_found("not a date"))?, |
| 1145 |
None => chrono::Local::now().date_naive(), |
| 1146 |
}; |
| 1147 |
let at = chrono::Utc.from_utc_datetime( |
| 1148 |
&day.and_hms_opt(12, 0, 0) |
| 1149 |
.ok_or_else(|| RouteError::internal("noon is always valid"))?, |
| 1150 |
); |
| 1151 |
|
| 1152 |
let session = state |
| 1153 |
.tasks |
| 1154 |
.log_manual_time(id, DESKTOP_USER_ID, minutes, at) |
| 1155 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1156 |
|
| 1157 |
Ok(answer(state, view)?.toast( |
| 1158 |
Tone::Success, |
| 1159 |
format!("Logged {}", spans(session.duration_minutes.unwrap_or(0))), |
| 1160 |
)) |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
pub fn routes(router: Router<AppState>) -> Router<AppState> { |
| 1165 |
router |
| 1166 |
.get("/timer", view) |
| 1167 |
.get("/timer/panel", showing) |
| 1168 |
.get("/timer/report", windowed) |
| 1169 |
.get("/timer/summary", summarised) |
| 1170 |
.post("/timer/start", start) |
| 1171 |
.post("/timer/stop", stop) |
| 1172 |
.post("/timer/discard", discard) |
| 1173 |
.post("/timer/view/track", track) |
| 1174 |
.post("/timer/view/focus", focus) |
| 1175 |
.post("/timer/view/stop", halt) |
| 1176 |
.post("/timer/view/discard", drop_it) |
| 1177 |
.post("/timer/view/log", log) |
| 1178 |
} |
| 1179 |
|