It already matched every http and https page. Local files are labelled "file". The version bump makes installed copies update. Co-Authored-By: Claude Opus 5.5 <[email protected]>
11 KiB
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".
[hypr-agent] [browser ext] [webcam agent] [Health Auto Export]
└────────── 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.
Status
| Collector | Status |
|---|---|
Hyprland (cmd/hypr-agent) |
✅ built |
Apple Health (Health Auto Export → /health/hae) |
✅ endpoint built; configure the app |
Browser (Violentmonkey userscript, /tracker.user.js) |
✅ built |
Webcam presence (/presence/events) |
endpoint ready, agent not yet written |
AI analysis (reports table) |
planned |
Running the server
make up # builds the image and starts tracker + a dev Postgres on :8080 / :5432 (AUTH_MODE=dev: no login)
curl localhost:8080/readyz
In production, run only the image and point it at your own database:
docker build -f deploy/Dockerfile -t activity-tracker .
docker run -d -p 8080:8080 \
-e DATABASE_URL='postgres://user:pass@db-host:5432/tracker' \
-e TRACKER_TZ='Europe/London' \
activity-tracker
| Env var | Default | |
|---|---|---|
DATABASE_URL |
(required) | PostgreSQL connection string |
LISTEN_ADDR |
:8080 |
|
AUTO_MIGRATE |
true |
Apply migrations on startup. Set it to false to run tracker migrate yourself |
TRACKER_TZ |
UTC |
Timezone that defines "a day" in summaries |
AUTH_MODE |
forward |
forward gates the dashboard on the main site's admin login (see Dashboard and auth). dev admits everyone and requires DEV_MODE=true |
AUTH_VALIDATE_URL |
http://backend:8080/auth/validate-admin |
The site backend's admin check |
Put the service behind your reverse proxy (Caddy, Traefik and so on) for TLS, because API keys are sent as bearer tokens.
Deploying to stppi
Pushing to main on Gitea (adamf/ccp) runs .gitea/workflows/ci.yaml on the Pi's runner. The workflow runs the tests, pushes adam-french.co.uk/adamf/activity-tracker:<sha> and :latest to Gitea's container registry, and recreates the activity-tracker service in the web_server stack. The service is served at https://www.adam-french.co.uk/tracker/, so set collectors to TRACKER_URL=https://www.adam-french.co.uk/tracker. Use the www host because the apex redirect would turn uploaded POSTs into GETs. To hold a version, set ACTIVITY_TRACKER_IMAGE_TAG to a sha in web_server's .env.
The repo needs the REGISTRY_USER and REGISTRY_TOKEN Actions secrets, the same ones job-tracker uses. Create its database role once on the Pi:
docker exec db psql -U "$POSTGRES_USER" -d postgres \
-c "CREATE ROLE tracker LOGIN PASSWORD '…'" -c "CREATE DATABASE tracker OWNER tracker"
Then run the admin commands with docker exec activity-tracker tracker apikey create ….
Admin commands
The admin commands are built into the same binary. Inside the container, run them with docker exec <container> tracker ….
tracker apikey create --name laptop-hypr --kind hyprland # prints the key once
tracker apikey list
tracker apikey revoke --name laptop-hypr
tracker goal add --kind tab_focus --label youtube.com --op '<=' --target 1h
tracker goal add --kind walk --op '>=' --target 30m # empty label = any walk
tracker goal list
tracker goal rm --id 2
Dashboard and auth
The dashboard (/) and the setup page (/setup) have no login of their own. As in job-tracker, the tracker borrows the main site's session. It forwards the browser's cookies to the site backend's GET /auth/validate-admin and treats a 2xx as admin (internal/auth). A visitor who isn't signed in is sent to the site's /admin/login?redirect=/tracker/, which returns them afterwards. A "yes" is cached for 30 seconds per cookie, so signing out takes up to 30 seconds to reach the tracker. If the backend refreshes an expired token, its new cookies are passed back to the browser.
Signed in, the dashboard shows every device's data without asking for a key. The setup page has step-by-step instructions for each collector, with your server URL filled in. It also lists devices and creates and revokes their API keys, so you don't need docker exec for keys. Each collector's section has a Generate key button that names the device (for example firefox, or hyprland-2 if that's taken), copies the key to the clipboard and fills it into that collector's steps.
Collectors never use the site login. Each one sends its own bearer key.
API
| Auth | Meaning |
|---|---|
| key | Authorization: Bearer <key> from tracker apikey create or the setup page |
| admin | the main site's admin session cookies |
| Method | Path | Auth | Accepted kinds |
|---|---|---|---|
| POST | /api/v1/desktop/events |
key | app_focus, idle, locked |
| 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 |
| 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 |
| DELETE | /api/v1/devices/{name} |
admin | revoke a device's key |
| GET | /, /setup |
admin | dashboard and setup pages |
| GET | /tracker.user.js, /style.css, /healthz, /readyz |
none | the browser userscript holds no secrets |
The event batch format is shared by all */events endpoints. A batch holds at most 1000 events.
{"events": [
{"id": "3f2a…", "kind": "app_focus", "label": "kitty",
"started_at": "2026-10-14T09:00:00Z", "ended_at": "2026-10-14T09:42:10Z",
"attrs": {"title": "nvim"}}
]}
The response is {"received": 1, "inserted": 1}. If inserted is lower than received, the missing events were duplicates.
Hyprland agent
The agent follows activewindow events on Hyprland's event socket and records one interval for each window class and title. Focus that lasts under 2s is dropped. hypridle hooks mark idle and locked time. Pending events are kept in $XDG_STATE_HOME/hypr-agent/buffer.jsonl and uploaded every minute, so nothing is lost while the server is unreachable.
tracker apikey create --name laptop-hypr --kind hyprland # on the server
make install-agent # builds into ~/.local/bin and installs the user unit
mkdir -p ~/.config/hypr-agent
cat > ~/.config/hypr-agent/env <<EOF
TRACKER_URL=https://tracker.example.com
TRACKER_API_KEY=trk_...
# HYPR_AGENT_TITLES=false # don't send window titles at all
# HYPR_AGENT_REDACT=(?i)bank|private # replace matching titles with [redacted]
EOF
chmod 600 ~/.config/hypr-agent/env
systemctl --user enable --now hypr-agent
To record idle and locked time, add these to ~/.config/hypr/hypridle.conf:
general {
lock_cmd = hypr-agent lock; pidof hyprlock || hyprlock
unlock_cmd = hypr-agent unlock
}
listener {
timeout = 300
on-timeout = hypr-agent idle
on-resume = hypr-agent resume
}
Browser
The browser collector is a Violentmonkey userscript served by the tracker itself. It runs in every tab and records a tab_focus interval, labelled with the hostname (www. stripped, so goals use youtube.com), only while that tab is the selected tab of the focused browser window. Focus that lasts under 2s is dropped. The interval ends after 5 minutes without mouse or keyboard input, unless a video or audio element is playing. Time spent asleep or suspended is never counted.
The setup page has these steps with your URL filled in.
- Create a key with kind
browseron the setup page, or withtracker apikey create --name laptop-firefox --kind browser. Use one key per browser. - Install Violentmonkey and open
https://www.adam-french.co.uk/tracker/tracker.user.js. Violentmonkey offers to install the script and later checks that URL for updates. - From the Violentmonkey toolbar menu, choose Set server and API key. Enter
https://www.adam-french.co.uk/trackerand the key. - Other menu commands toggle full URLs (
attrs.url, which also splits intervals per page) and page titles (attrs.title), set a regexp of hosts to ignore, change the idle timeout, and show the queue and last upload result.
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
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
Walking and hiking workouts are stored with the kind walk, running workouts as run, and any other workout as workout.
Development
make test # unit tests
make up && make test-integration # also runs the API tests against the compose Postgres (truncates its tables!)
make run # run the server locally against the compose Postgres
Layout: cmd/tracker (server and admin CLI), cmd/hypr-agent, internal/{api,auth,ingest,store,summary,migrations,config,web} (web/tracker.user.js is the browser collector), internal/agent/hypr, deploy/.
Roadmap
- Webcam presence agent. Detection will run on the device and only intervals will be uploaded; frames never leave the machine.
- Nudges: push notifications through ntfy when a goal is at risk during the day.
- Location: an OwnTracks or Home Assistant geofence will provide reliable "time outside" intervals.
- 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.