Add activity tracker server, Hyprland agent and Gitea image publishing
The Go API on PostgreSQL ingests desktop, browser, presence, manual and Health Auto Export events and serves activities and daily goal summaries. The Hyprland agent records window focus, idle and lock intervals. The CI workflow publishes adam-french.co.uk/adamf/activity-tracker to the Gitea registry and rolls the web_server stack onto it. The image probes itself through a new `tracker healthcheck` command, because distroless has no wget. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
package ingest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Health Auto Export (iOS) REST automation payload (JSON export format).
|
||||
// Only the fields we use are declared.
|
||||
type HAEPayload struct {
|
||||
Data struct {
|
||||
Metrics []HAEMetric `json:"metrics"`
|
||||
Workouts []HAEWorkout `json:"workouts"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type HAEMetric struct {
|
||||
Name string `json:"name"`
|
||||
Units string `json:"units"`
|
||||
Data []map[string]any `json:"data"`
|
||||
}
|
||||
|
||||
type HAEWorkout struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Start string `json:"start"`
|
||||
End string `json:"end"`
|
||||
Location string `json:"location"`
|
||||
Distance map[string]any `json:"distance"`
|
||||
Energy map[string]any `json:"activeEnergyBurned"`
|
||||
}
|
||||
|
||||
const (
|
||||
haeSource = "health"
|
||||
haeTimeLayout = "2006-01-02 15:04:05 -0700"
|
||||
)
|
||||
|
||||
// NormalizeHAE converts workouts and sleep into activities, and every other
|
||||
// quantity metric into samples. Entries that can't be parsed are skipped
|
||||
// rather than failing the whole export.
|
||||
func NormalizeHAE(p HAEPayload) ([]Activity, []Sample, error) {
|
||||
var acts []Activity
|
||||
var samples []Sample
|
||||
|
||||
for _, w := range p.Data.Workouts {
|
||||
start, err1 := parseHAETime(w.Start)
|
||||
end, err2 := parseHAETime(w.End)
|
||||
if err1 != nil || err2 != nil || end.Before(start) {
|
||||
continue
|
||||
}
|
||||
id := w.ID
|
||||
if id == "" {
|
||||
id = fmt.Sprintf("workout|%s|%s", w.Name, w.Start)
|
||||
}
|
||||
attrs := map[string]any{"name": w.Name}
|
||||
if w.Location != "" {
|
||||
attrs["location"] = w.Location
|
||||
}
|
||||
if w.Distance != nil {
|
||||
attrs["distance"] = w.Distance
|
||||
}
|
||||
if w.Energy != nil {
|
||||
attrs["active_energy"] = w.Energy
|
||||
}
|
||||
acts = append(acts, Activity{
|
||||
Source: haeSource,
|
||||
Kind: workoutKind(w.Name),
|
||||
Label: w.Name,
|
||||
StartedAt: start,
|
||||
EndedAt: end,
|
||||
Attrs: attrs,
|
||||
ExternalID: id,
|
||||
})
|
||||
}
|
||||
|
||||
for _, m := range p.Data.Metrics {
|
||||
if m.Name == "sleep_analysis" {
|
||||
acts = append(acts, sleepActivities(m)...)
|
||||
continue
|
||||
}
|
||||
for _, d := range m.Data {
|
||||
date, _ := d["date"].(string)
|
||||
qty, ok := d["qty"].(float64)
|
||||
ts, err := parseHAETime(date)
|
||||
if !ok || err != nil {
|
||||
continue
|
||||
}
|
||||
src, _ := d["source"].(string)
|
||||
samples = append(samples, Sample{
|
||||
Source: haeSource,
|
||||
Metric: m.Name,
|
||||
TS: ts,
|
||||
Value: qty,
|
||||
Unit: m.Units,
|
||||
ExternalID: fmt.Sprintf("%s|%s|%s", m.Name, date, src),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(acts) == 0 && len(samples) == 0 {
|
||||
return nil, nil, fmt.Errorf("payload contains no usable workouts or metrics")
|
||||
}
|
||||
return acts, samples, nil
|
||||
}
|
||||
|
||||
// sleepActivities handles the aggregated sleep_analysis format, which has
|
||||
// sleepStart/sleepEnd instead of a qty.
|
||||
func sleepActivities(m HAEMetric) []Activity {
|
||||
var out []Activity
|
||||
for _, d := range m.Data {
|
||||
s, _ := d["sleepStart"].(string)
|
||||
e, _ := d["sleepEnd"].(string)
|
||||
start, err1 := parseHAETime(s)
|
||||
end, err2 := parseHAETime(e)
|
||||
if err1 != nil || err2 != nil || end.Before(start) {
|
||||
continue
|
||||
}
|
||||
attrs := map[string]any{}
|
||||
for _, k := range []string{"asleep", "inBed", "core", "deep", "rem", "awake"} {
|
||||
if v, ok := d[k]; ok {
|
||||
attrs[k] = v
|
||||
}
|
||||
}
|
||||
out = append(out, Activity{
|
||||
Source: haeSource,
|
||||
Kind: "sleep",
|
||||
Label: "sleep",
|
||||
StartedAt: start,
|
||||
EndedAt: end,
|
||||
Attrs: attrs,
|
||||
ExternalID: "sleep|" + s,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func workoutKind(name string) string {
|
||||
n := strings.ToLower(name)
|
||||
switch {
|
||||
case strings.Contains(n, "walk"), strings.Contains(n, "hik"):
|
||||
return "walk"
|
||||
case strings.Contains(n, "run"):
|
||||
return "run"
|
||||
default:
|
||||
return "workout"
|
||||
}
|
||||
}
|
||||
|
||||
func parseHAETime(s string) (time.Time, error) {
|
||||
t, err := time.Parse(haeTimeLayout, s)
|
||||
if err != nil {
|
||||
return t, err
|
||||
}
|
||||
return t.UTC(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user