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,70 @@
|
||||
// Package summary builds daily summaries and evaluates goals against them.
|
||||
package summary
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"ccp/internal/store"
|
||||
)
|
||||
|
||||
type GoalResult struct {
|
||||
store.Goal
|
||||
ActualS int64 `json:"actual_s"`
|
||||
Met bool `json:"met"`
|
||||
}
|
||||
|
||||
type Daily struct {
|
||||
Date string `json:"date"`
|
||||
From time.Time `json:"from"`
|
||||
To time.Time `json:"to"`
|
||||
Totals []store.Total `json:"totals"`
|
||||
ByKind map[string]int64 `json:"by_kind"`
|
||||
Goals []GoalResult `json:"goals"`
|
||||
}
|
||||
|
||||
// DayBounds returns the start and end of the calendar day containing t in loc.
|
||||
// It uses AddDate rather than 24h so DST transitions are handled.
|
||||
func DayBounds(t time.Time, loc *time.Location) (time.Time, time.Time) {
|
||||
t = t.In(loc)
|
||||
start := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
|
||||
return start, start.AddDate(0, 0, 1)
|
||||
}
|
||||
|
||||
// Build assembles a daily summary from per-label totals and goals.
|
||||
func Build(date string, from, to time.Time, totals []store.Total, goals []store.Goal) Daily {
|
||||
d := Daily{
|
||||
Date: date,
|
||||
From: from,
|
||||
To: to,
|
||||
Totals: totals,
|
||||
ByKind: map[string]int64{},
|
||||
Goals: Evaluate(totals, goals),
|
||||
}
|
||||
if d.Totals == nil {
|
||||
d.Totals = []store.Total{}
|
||||
}
|
||||
for _, t := range totals {
|
||||
d.ByKind[t.Kind] += t.Seconds
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// Evaluate checks each goal against the totals. A goal with an empty label
|
||||
// counts every label of its kind.
|
||||
func Evaluate(totals []store.Total, goals []store.Goal) []GoalResult {
|
||||
out := make([]GoalResult, 0, len(goals))
|
||||
for _, g := range goals {
|
||||
var actual int64
|
||||
for _, t := range totals {
|
||||
if t.Kind == g.Kind && (g.Label == "" || t.Label == g.Label) {
|
||||
actual += t.Seconds
|
||||
}
|
||||
}
|
||||
met := actual >= g.TargetS
|
||||
if g.Op == "<=" {
|
||||
met = actual <= g.TargetS
|
||||
}
|
||||
out = append(out, GoalResult{Goal: g, ActualS: actual, Met: met})
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package summary
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"ccp/internal/store"
|
||||
)
|
||||
|
||||
func TestEvaluate(t *testing.T) {
|
||||
totals := []store.Total{
|
||||
{Kind: "tab_focus", Label: "youtube.com", Seconds: 5400},
|
||||
{Kind: "tab_focus", Label: "github.com", Seconds: 1800},
|
||||
{Kind: "walk", Label: "Outdoor Walk", Seconds: 1200},
|
||||
}
|
||||
goals := []store.Goal{
|
||||
{ID: 1, Kind: "tab_focus", Label: "youtube.com", Op: "<=", TargetS: 3600},
|
||||
{ID: 2, Kind: "walk", Op: ">=", TargetS: 1800},
|
||||
{ID: 3, Kind: "tab_focus", Op: ">=", TargetS: 7200}, // empty label = all labels
|
||||
}
|
||||
got := Evaluate(totals, goals)
|
||||
want := []struct {
|
||||
actual int64
|
||||
met bool
|
||||
}{{5400, false}, {1200, false}, {7200, true}}
|
||||
for i, w := range want {
|
||||
if got[i].ActualS != w.actual || got[i].Met != w.met {
|
||||
t.Errorf("goal %d: got actual=%d met=%v, want %+v", goals[i].ID, got[i].ActualS, got[i].Met, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDayBoundsDST(t *testing.T) {
|
||||
loc, err := time.LoadLocation("America/New_York")
|
||||
if err != nil {
|
||||
t.Skip("no tzdata:", err)
|
||||
}
|
||||
// 2026-03-08 is the spring-forward day: only 23 hours long.
|
||||
from, to := DayBounds(time.Date(2026, 3, 8, 15, 0, 0, 0, loc), loc)
|
||||
if to.Sub(from) != 23*time.Hour || from.Hour() != 0 {
|
||||
t.Fatalf("got %v - %v", from, to)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user