Replace Apple Health ingest with iPhone location geofences

POST /api/v1/location/{arrive,leave} records place intervals (home or a
named place, and away in between) from Shortcuts automations. A stale
"leave" for a place the device has already left is ignored. The Health
Auto Export endpoint and its normalizer are removed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-14 22:06:25 +01:00
co-authored by Claude Opus 5.5
parent 230840cabd
commit 67638cdef0
10 changed files with 269 additions and 238 deletions
+17 -15
View File
@@ -1,22 +1,22 @@
# Activity Tracker
A self-hosted service that records where your time goes: desktop apps, browser tabs, walks and workouts, and desk/bed presence. Each device runs a small collector that uploads timestamped intervals to a Go API backed by PostgreSQL. A daily summary checks your totals against goals such as "≤ 1h of YouTube" or "≥ 1h outside".
A self-hosted service that records where your time goes: desktop apps, browser tabs, time at home and away, and desk/bed presence. Each device runs a small collector that uploads timestamped intervals to a Go API backed by PostgreSQL. A daily summary checks your totals against goals such as "≤ 1h of YouTube" or "≥ 1h outside".
```
[hypr-agent] [browser ext] [webcam agent] [Health Auto Export]
[hypr-agent] [browser ext] [webcam agent] [iOS Shortcuts]
└────────── HTTPS + Bearer API key (batched JSON) ─────────┘
│
tracker (Go, container) ── PostgreSQL
```
Every source is stored as an interval in a single `activities` table, with columns `source, kind, label, started_at, ended_at, attrs`. Point metrics such as step counts go in `samples`. Collectors generate an `id` for each event, so re-uploading a batch never creates duplicates.
Every source is stored as an interval in a single `activities` table, with columns `source, kind, label, started_at, ended_at, attrs`. The `samples` table for point metrics (step counts and so on) has no collector at the moment. Collectors generate an `id` for each event, so re-uploading a batch never creates duplicates.
## Status
| Collector | Status |
|---|---|
| Hyprland (`cmd/hypr-agent`) | ✅ built |
| Apple Health (Health Auto Export → `/health/hae`) | ✅ endpoint built; configure the app |
| iPhone location (Shortcuts → `/location/{arrive,leave}`) | ✅ built; configure the automations |
| Browser (Violentmonkey userscript, `/tracker.user.js`) | ✅ built |
| Webcam presence (`/presence/events`) | endpoint ready, agent not yet written |
| AI analysis (`reports` table) | planned |
@@ -98,7 +98,7 @@ Collectors never use the site login. Each one sends its own bearer key.
| POST | `/api/v1/browser/events` | key | `tab_focus`, `idle` |
| POST | `/api/v1/presence/events` | key | `at_desk`, `in_bed`, `away` |
| POST | `/api/v1/manual/events` | key | any |
| POST | `/api/v1/health/hae` | key | Health Auto Export JSON. Workouts and sleep become activities; other metrics become samples |
| POST | `/api/v1/location/{arrive,leave}[/{place}]` | key | place from the path, `?place=`, or optional `{"place","at"}`; defaults to `home` and now. Builds `place` intervals (see [iPhone location](#iphone-location)) |
| GET | `/api/v1/activities?from=&to=&source=&kind=&limit=` | key or admin | RFC3339 times; defaults to the last 24h |
| GET | `/api/v1/summary/daily?date=YYYY-MM-DD` | key or admin | totals per label and kind, plus goal results |
| GET, POST | `/api/v1/devices` | admin | list devices; create one with `{"name","kind"}`, which returns its key once |
@@ -164,16 +164,19 @@ The setup page has these steps with your URL filled in.
Closed intervals are queued in the script's storage and uploaded every minute by one tab at a time. If a tab is closed or crashes mid-interval, another tab recovers its last checkpoint (at most 5s lost). It runs on every `http`/`https` page and on local `file://` pages, which are labelled `file` (in Chrome, enable **Allow access to file URLs** for Violentmonkey). It can't see browser pages such as `about:`, `chrome://` or the new tab page, extension stores, or the built-in PDF viewer, so time there isn't recorded. The Hyprland agent still counts it as browser `app_focus`.
## Apple Health
## iPhone location
In the Health Auto Export iOS app, create a **REST API** automation with these settings:
- URL: `https://tracker.example.com/api/v1/health/hae`
- Header: `Authorization: Bearer <key>`. Create the key with `--kind health`.
- Format: JSON
- Data: Workouts, plus metrics such as Step Count and Sleep Analysis
- Sync cadence: hourly
The free Shortcuts app reports when you arrive at and leave each place. Apple doesn't let you import automations, so you set them up on the phone. One shared shortcut holds the URL and key, so each automation is only two actions. The setup page shows the same steps with your key and place names filled in.
Walking and hiking workouts are stored with the kind `walk`, running workouts as `run`, and any other workout as `workout`.
1. Create a key with kind `location` on the setup page.
2. Once: make a shortcut named `Tracker` with one **Get Contents of URL** action. The URL is `https://www.adam-french.co.uk/tracker/api/v1/location/` followed by the **Shortcut Input** variable, the Method is `POST`, and the Header is `Authorization: Bearer <key>`.
3. For each place, make two personal automations (iOS 17 or later, **Run Immediately**). The first is **Arrive** at the address, with a **Text** action `arrive/<place>` followed by **Run Shortcut → Tracker**. The second is the same for **Leave**, with `leave/<place>`.
The endpoint is `POST /api/v1/location/{arrive,leave}/{place}`. `?place=` and a `{"place"}` body also work, and with no place given it means `home`.
Each device has one open `place` interval at a time, labelled with the place (lower-cased; `home` if none is given) or `away` between places. An arrive or leave closes the open interval and starts the next one. The open interval is stored with `ended_at = started_at` (marked `attrs.open`), and queries and summaries count it as running until now. Arriving where you already are does nothing, and a leave for a place you aren't in is ignored, so duplicate or late geofence triggers can't corrupt the timeline. A missed trigger stretches the previous interval until the next one arrives.
To track time outside: `tracker goal add --kind place --label away --op '>=' --target 1h`.
## Development
@@ -189,5 +192,4 @@ Layout: `cmd/tracker` (server and admin CLI), `cmd/hypr-agent`, `internal/{api,a
1. Webcam presence agent. Detection will run on the device and only intervals will be uploaded; frames never leave the machine.
2. Nudges: push notifications through ntfy when a goal is at risk during the day.
3. Location: an OwnTracks or Home Assistant geofence will provide reliable "time outside" intervals.
4. AI analysis: a nightly or weekly job will send the daily summaries (not raw rows) to the Claude API and store the result in `reports`.
3. AI analysis: a nightly or weekly job will send the daily summaries (not raw rows) to the Claude API and store the result in `reports`.