Fix break tracking (out→in gap), add startup log, clean up /start message
This commit is contained in:
@@ -37,6 +37,8 @@ func main() {
|
||||
|
||||
handler := bot.NewHandler(botAPI, dbStore, allowedUsers)
|
||||
|
||||
log.Printf("Bot started. Allowed users: %d", len(allowedUsers))
|
||||
|
||||
webhookURL := os.Getenv("BOT_WEBHOOK_URL")
|
||||
if webhookURL != "" {
|
||||
startWebhook(botAPI, handler, webhookURL)
|
||||
|
||||
@@ -82,7 +82,7 @@ func (h *Handler) handleStart(msg *tgbotapi.Message) {
|
||||
h.Bot.Send(reply)
|
||||
|
||||
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
|
||||
h.Bot.Send(quick)
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
// Multi‑out 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user