Skip to content

TD-0012: Health service

Status: Implemented (server) · Python 3.13 · 15 November 2026

Related: the loryth-health service docs (server/services/loryth-health/docs/index.md).

Service and storage

loryth-health follows API → transactional services → aggregates/repositories → PostgreSQL in the health database. It has no worker and no RabbitMQ connection. The gateway routes /api/v1/health/* to loryth-health-api:8000 through a variable upstream, so the gateway starts without it. MCP reaches it through LORYTH_HEALTH_URL.

Two tables carry user_id, BIGSERIAL IDs, public UUIDs and audit timestamps:

  • health_event: every record, whatever its type (health_event_type: headache, medication, water, coffee, sleep, stress). It stores started_at, nullable ended_at, JSONB details, source (app or chat), revision and a nullable related_event_id with ON DELETE SET NULL. Check constraints repeat the shape rules that do not depend on the clock. A partial unique index on user_id WHERE type = 'headache' AND ended_at IS NULL allows one open headache per user.
  • health_settings: one row per user (water goal, glass size, tracker switches), created on the first PUT /settings. Reads return defaults until then.

A new type adds a details model and an enum value; day views, history and statistics work for every type.

Event rules

Details are Pydantic models with schema_version 1 that reject unknown keys. The create body is a union discriminated by type. PATCH validates details against the stored type. Stored JSON is always parsed with the model for its type, because several models accept the same keys.

  • Point types (medication, water, coffee, stress) have no ended_at.
  • Sleep needs an ended_at after started_at and at most 24 hours later.
  • started_at and ended_at may be at most 5 minutes in the future, and ended_at >= started_at.
  • Only medication may link to an event, and only to the same user's headache.
  • A headache keeps its intensity updates sorted by time. An update sent without at takes the headache's start (on create or when details are replaced) or the current time (intensity_update).

Writes take a per-user advisory lock. The service rejects a second open headache (on create or when a PATCH reopens one) with 409 open_headache_exists, and the partial index backs that check up. Each PATCH compares expected_revision and returns 409 revision_conflict with the current event in detail.event; a successful PATCH increments revision. Deleting a headache unlinks its medication and increments each dose's revision.

Days and statistics

All local-day arithmetic lives in domain/days/local_days.py and converts local midnights in the requested IANA timezone to UTC. Sleep is placed on the local day it ended; other events on the day they started. These modules are pure and unit tested:

  • days/day_totals.py: water against the goal, coffee count and last time, the latest sleep, the latest stress record and the headache count.
  • headaches/around.py: the 24 hours before a headache started, the latest sleep that ended in that time, the mean sleep of the 30 days before, and the medication linked to it.
  • headaches/stats.py: headache days with their peak and medication (a dose taken that day or linked to that day's headache), and range statistics. Lengths use finished headaches. median_peak rounds half up. days_in_range stops at the user's local today.
  • headaches/patterns.py: headache days against other days with records in the 60 days ending at the range end, capped at today. It returns nothing below 8 headache days. Each metric averages only the days that recorded its type.

Validation

make lint and make test in server/services/loryth-health cover each type's shape rules, open-headache uniqueness in the service and the database, revision conflicts, day views in Europe/Athens, the 24-hour context, statistics, the 8-day patterns threshold, recent medication, settings and two-user isolation. docker/tests cover Compose wiring, the gateway without a Health container and the packaged migrations.