Skip to content

Conversation

@ebigunso
Copy link
Owner

Summary

This PR delivers the foundational backend and UI pieces for Sprint 3. It introduces DST-safe duration semantics, a SQL view for daily sleep with pre-computed duration, on-demand Trends JSON endpoints, and a minimal SSR Trends page wired with Chart.js.

Key changes

  • Duration semantics (DST-safe, wake-date)

    • Server-side duration computation using chrono-tz in Rust with wake-date semantics; supports cross-midnight sessions and DST gaps/overlaps.
    • Persisted as sleep_metrics.duration_min for consistent downstream use.
    • Removed the previous wake-after-bed restriction and 2–14h duration guard; any positive duration is accepted (per product guidance).
    • Files: sleep-api/src/time.rs, sleep-api/src/handlers.rs (compute & persist).
  • DB migrations & view

    • 0002_add_duration_min.sql: adds sleep_metrics.duration_min; partial unique index for a daily exercise sentinel.
    • 0003_views.sql: v_daily_sleep view exposing wake_date, bed_time, wake_time, latency_min, awakenings, quality, duration_min.
  • Trends endpoints (on-demand JSON)

    • GET /api/trends/sleep-bars?from=&to=
      • Returns [{date, bed_time, wake_time, quality?, duration_min?}] based on v_daily_sleep.
    • GET /api/trends/summary?from=&to=&bucket=day|week
      • Aggregates: duration (avg/min/max), quality (avg), latency (median in Rust). Week bucket uses ISO (YYYY-Www).
    • Implementation: sleep-api/src/trends.rs
  • SSR Trends page

    • GET /trends renders an Askama template with date range + bucket controls and Chart.js charts for Sleep Bars (duration proxy), Duration avg, Quality avg, Latency median.
    • Files: sleep-api/src/views.rs, sleep-api/templates/trends.html, route in src/app.rs.
  • OpenAPI

    • openapi.yaml updated with trends endpoints and response shapes.
  • Dependency updates & routing

    • axum 0.8.4, sqlx 0.8.6, askama 0.14 (+ askama_web), tower-http 0.6, axum-extra 0.10.
    • Migrated route patterns to Axum 0.8 syntax (/{id}).
  • Tests & quality

    • All existing tests pass.
    • Added tests/trends_bars.rs: validates /api/trends/sleep-bars contract.
    • Clippy cleanup: removed unused DomainError variant and unused cookie-key util; build is clean.

How to run locally

Notes & next steps

  • Auth + session + CSRF (single-user) not included in this PR; will secure /ui/* and mutating routes next.
  • Dashboard + HTMX modals (Quick Log, Create/Edit/Delete) + Day view are incoming in subsequent PR(s), along with mobile/a11y polish and security headers.

This sets the groundwork for the Sprint 3 UI flows and trends exploration while keeping time semantics correct across DST.

Copilot AI review requested due to automatic review settings August 10, 2025 19:45
@ebigunso ebigunso self-assigned this Aug 10, 2025
@ebigunso ebigunso changed the title Sprint 3 — Foundations: Trends API, DST-safe durations, SSR Trends page ✨ Foundations: Trends API, DST-safe durations, SSR Trends page Aug 10, 2025

This comment was marked as outdated.

@ebigunso ebigunso requested a review from Copilot August 10, 2025 20:14

This comment was marked as outdated.

- summary: guard empty buckets and simplify median calc (no redundant is_empty branch)
- app: log Askama template rendering errors with context
- ui: preserve nulls for missing durations in Chart.js mapping
Tests: cargo test passes
@ebigunso ebigunso requested a review from Copilot August 10, 2025 20:34

This comment was marked as outdated.

- app: log Askama errors but return generic user-facing message
- time: extract MAX_DST_GAP_MINUTES constant (replaces magic number 3*60)
- trends: compute median via select_nth_unstable (O(n) avg), keep empty-bucket guard
Tests: cargo test passes
@ebigunso ebigunso requested a review from Copilot August 10, 2025 20:45

This comment was marked as outdated.

- trends: fix median correctness by sorting once and indexing (even/odd)
- ui: remove unused HTMX include from Trends page
Tests: cargo test passes
@ebigunso ebigunso requested a review from Copilot August 10, 2025 20:53

This comment was marked as outdated.

…on via helpers; time: add tracing warn on DST fallback; tests pass
@ebigunso ebigunso requested a review from Copilot August 10, 2025 21:08

This comment was marked as outdated.

…k; trends: compute median latency in O(n) using select_nth_unstable instead of full sort
@ebigunso ebigunso requested a review from Copilot August 10, 2025 21:49

This comment was marked as outdated.

@ebigunso ebigunso requested a review from Copilot August 10, 2025 22:02

This comment was marked as outdated.

@ebigunso ebigunso requested a review from Copilot August 10, 2025 22:15

This comment was marked as outdated.

@ebigunso ebigunso requested a review from Copilot August 10, 2025 22:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements foundational backend and UI components for Sprint 3, introducing DST-safe duration calculations, Trends API endpoints, and a server-side rendered Trends page with Chart.js visualizations.

  • Adds DST-safe duration computation using chrono-tz with wake-date semantics
  • Implements Trends API endpoints for sleep data visualization and aggregated metrics
  • Creates SSR Trends page with Chart.js charts for sleep analysis

Reviewed Changes

Copilot reviewed 17 out of 18 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
sleep-api/src/time.rs DST-safe duration computation with timezone handling
sleep-api/src/trends.rs Trends API endpoints for sleep bars and summary data
sleep-api/templates/trends.html SSR template with Chart.js charts and date controls
sleep-api/src/views.rs Askama template struct for trends page
migrations/0002_add_duration_min.sql Database migration adding duration_min column
migrations/0003_views.sql SQL view for daily sleep data
sleep-api/tests/trends_bars.rs Integration test for sleep-bars endpoint
sleep-api/src/handlers.rs Updated to compute and persist duration
sleep-api/src/repository.rs Modified to accept and store duration_min
sleep-api/src/app.rs Added trends routes and template rendering
sleep-api/Cargo.toml Updated dependencies for new functionality

@ebigunso ebigunso merged commit a2a44d4 into main Aug 10, 2025
2 checks passed
@ebigunso ebigunso deleted the feature/2025-08-11/web-ui branch August 10, 2025 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants