Files
ccp/internal/ingest/ingest.go
T
adamfandClaude Opus 5.5 67638cdef0 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]>
2026-10-14 22:06:25 +01:00

120 lines
3.3 KiB
Go

// Package ingest defines the wire format collectors upload and normalises it
// into activities ready for storage.
package ingest
import (
"fmt"
"strings"
"time"
)
const (
MaxBatch = 1000
maxDuration = 7 * 24 * time.Hour
maxFutureSkew = 5 * time.Minute
maxLabelLength = 512
maxIDLength = 128
)
// Event is a single interval as sent by a collector. ID is generated by the
// collector and makes re-uploads idempotent.
type Event struct {
ID string `json:"id"`
Kind string `json:"kind"`
Label string `json:"label"`
StartedAt time.Time `json:"started_at"`
EndedAt time.Time `json:"ended_at"`
Attrs map[string]any `json:"attrs,omitempty"`
}
type Batch struct {
Events []Event `json:"events"`
}
// Activity is a normalised interval ready to be stored.
type Activity struct {
Source string
Kind string
Label string
StartedAt time.Time
EndedAt time.Time
Attrs map[string]any
ExternalID string
}
// Spec describes which kinds an endpoint accepts and the source its
// activities are stored under.
type Spec struct {
Source string
Kinds map[string]bool // nil accepts any kind
}
var (
Desktop = Spec{Source: "hyprland", Kinds: set("app_focus", "idle", "locked")}
Browser = Spec{Source: "browser", Kinds: set("tab_focus", "idle")}
Presence = Spec{Source: "presence", Kinds: set("at_desk", "in_bed", "away")}
Manual = Spec{Source: "manual"}
)
func set(kinds ...string) map[string]bool {
m := make(map[string]bool, len(kinds))
for _, k := range kinds {
m[k] = true
}
return m
}
// Normalize validates every event in the batch and converts it to activities.
// The whole batch is rejected if any event is invalid.
func (s Spec) Normalize(b Batch, now time.Time) ([]Activity, error) {
if len(b.Events) == 0 {
return nil, fmt.Errorf("batch has no events")
}
if len(b.Events) > MaxBatch {
return nil, fmt.Errorf("batch has %d events, max is %d", len(b.Events), MaxBatch)
}
out := make([]Activity, 0, len(b.Events))
for i, e := range b.Events {
if err := s.validate(e, now); err != nil {
return nil, fmt.Errorf("event %d: %w", i, err)
}
attrs := e.Attrs
if attrs == nil {
attrs = map[string]any{}
}
out = append(out, Activity{
Source: s.Source,
Kind: e.Kind,
Label: strings.TrimSpace(e.Label),
StartedAt: e.StartedAt.UTC(),
EndedAt: e.EndedAt.UTC(),
Attrs: attrs,
ExternalID: e.ID,
})
}
return out, nil
}
func (s Spec) validate(e Event, now time.Time) error {
switch {
case e.ID == "" || len(e.ID) > maxIDLength:
return fmt.Errorf("id must be 1-%d characters", maxIDLength)
case e.Kind == "":
return fmt.Errorf("kind is required")
case s.Kinds != nil && !s.Kinds[e.Kind]:
return fmt.Errorf("kind %q not accepted by this endpoint", e.Kind)
case len(e.Label) > maxLabelLength:
return fmt.Errorf("label longer than %d characters", maxLabelLength)
case e.StartedAt.IsZero() || e.EndedAt.IsZero():
return fmt.Errorf("started_at and ended_at are required")
case e.EndedAt.Before(e.StartedAt):
return fmt.Errorf("ended_at is before started_at")
case e.EndedAt.Sub(e.StartedAt) > maxDuration:
return fmt.Errorf("interval longer than %s", maxDuration)
case e.EndedAt.After(now.Add(maxFutureSkew)):
return fmt.Errorf("ended_at is in the future")
}
return nil
}