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

@@ -37,6 +37,8 @@ func main() {
handler := bot.NewHandler(botAPI, dbStore, allowedUsers) handler := bot.NewHandler(botAPI, dbStore, allowedUsers)
log.Printf("Bot started. Allowed users: %d", len(allowedUsers))
webhookURL := os.Getenv("BOT_WEBHOOK_URL") webhookURL := os.Getenv("BOT_WEBHOOK_URL")
if webhookURL != "" { if webhookURL != "" {
startWebhook(botAPI, handler, webhookURL) startWebhook(botAPI, handler, webhookURL)

View File

@@ -82,7 +82,7 @@ func (h *Handler) handleStart(msg *tgbotapi.Message) {
h.Bot.Send(reply) h.Bot.Send(reply)
kb := quickKeyboard() kb := quickKeyboard()
quick := tgbotapi.NewMessage(msg.Chat.ID, "Use the buttons below to quickly clock in/out:") quick := tgbotapi.NewMessage(msg.Chat.ID, "")
quick.ReplyMarkup = kb quick.ReplyMarkup = kb
h.Bot.Send(quick) h.Bot.Send(quick)
} }

View File

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