// 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 }