Fix break tracking (out→in gap), add startup log, clean up /start message

This commit is contained in:
2026-06-23 22:00:25 +03:30
parent 7da6f4cb49
commit 32446a99d1
3 changed files with 15 additions and 20 deletions

View File

@@ -14,16 +14,12 @@ const (
StateOnBreak
)
// DailyTotals holds aggregated times for a single day.
type DailyTotals struct {
TotalSeconds int64
BreakSeconds int64
PairCount int
}
// ComputeDailyTotals pairs events with break inference.
// Multiout sequences: first out ends work, second out starts break,
// next in ends break, next out ends work, etc.
func ComputeDailyTotals(events []db.Event) DailyTotals {
if len(events) == 0 {
return DailyTotals{}
@@ -40,34 +36,31 @@ func ComputeDailyTotals(events []db.Event) DailyTotals {
state := StateIdle
for _, e := range sorted {
switch {
case e.EventType == "in":
switch e.EventType {
case "in":
switch state {
case StateIdle, StateOnBreak:
if state == StateOnBreak {
breakTotal += e.OccurredAt - breakStart
breakStart = 0
}
case StateIdle:
workStart = e.OccurredAt
state = StateWorking
case StateOnBreak:
breakTotal += e.OccurredAt - breakStart
breakStart = 0
workStart = e.OccurredAt
state = StateWorking
// Working → ignore duplicate in (shouldn't happen)
case StateWorking:
// already working, ignore
}
case e.EventType == "out":
case "out":
switch state {
case StateWorking:
// End of work period
workTotal += e.OccurredAt - workStart
workStart = 0
pairs++
state = StateIdle
breakStart = e.OccurredAt
state = StateOnBreak
case StateIdle:
// Second consecutive out → break start
breakStart = e.OccurredAt
state = StateOnBreak
case StateOnBreak:
// Duplicate break start ignore
breakStart = e.OccurredAt
}
}
}