debug(cdi): logs de diagnóstico em ApplyYield e fetchCDIRates

This commit is contained in:
2026-05-29 00:28:09 -03:00
parent 4de1af5687
commit 3c13c85081
+16 -3
View File
@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@@ -95,16 +96,20 @@ func (s *CDIYieldService) ApplyYield(ctx context.Context, a *model.Account) erro
startDate = t.AddDate(0, 0, 1).Format("2006-01-02") startDate = t.AddDate(0, 0, 1).Format("2006-01-02")
} }
log.Printf("[cdi] account=%d lastYieldDate=%v startDate=%s yesterday=%s", a.ID, a.LastYieldDate, startDate, yesterday)
if startDate > yesterday { if startDate > yesterday {
// nothing to calculate yet log.Printf("[cdi] account=%d early return (already up to date)", a.ID)
return nil return nil
} }
log.Printf("[cdi] account=%d calling BCB for [%s, %s]", a.ID, startDate, yesterday)
rates, err := s.fetchCDIRates(startDate, yesterday) rates, err := s.fetchCDIRates(startDate, yesterday)
if err != nil { if err != nil {
// BCB API unreachable — skip silently; will retry next load log.Printf("[cdi] account=%d BCB error: %v", a.ID, err)
return nil return nil
} }
log.Printf("[cdi] account=%d BCB returned %d rates", a.ID, len(rates))
if len(rates) == 0 { if len(rates) == 0 {
return nil return nil
} }
@@ -146,10 +151,16 @@ func (s *CDIYieldService) ApplyYield(ctx context.Context, a *model.Account) erro
AccountID: &a.ID, AccountID: &a.ID,
} }
if _, err := s.txRepo.Create(ctx, tx); err != nil { if _, err := s.txRepo.Create(ctx, tx); err != nil {
log.Printf("[cdi] account=%d txRepo.Create error: %v", a.ID, err)
return err return err
} }
return s.accountRepo.UpdateLastYieldDate(ctx, a.ID, yesterday) if err := s.accountRepo.UpdateLastYieldDate(ctx, a.ID, yesterday); err != nil {
log.Printf("[cdi] account=%d UpdateLastYieldDate error: %v", a.ID, err)
return err
}
log.Printf("[cdi] account=%d yield applied OK, lastYieldDate → %s", a.ID, yesterday)
return nil
} }
type bcbEntry struct { type bcbEntry struct {
@@ -162,8 +173,10 @@ func (s *CDIYieldService) fetchCDIRates(startDate, endDate string) ([]bcbEntry,
if s.cache.covers(startDate, endDate) { if s.cache.covers(startDate, endDate) {
entries := s.cache.get(startDate, endDate) entries := s.cache.get(startDate, endDate)
s.cache.mu.RUnlock() s.cache.mu.RUnlock()
log.Printf("[cdi] cache HIT [%s, %s] → %d entries", startDate, endDate, len(entries))
return entries, nil return entries, nil
} }
log.Printf("[cdi] cache MISS [%s, %s] (minDate=%s maxDate=%s valid=%v)", startDate, endDate, s.cache.minDate, s.cache.maxDate, s.cache.valid())
s.cache.mu.RUnlock() s.cache.mu.RUnlock()
s.cache.mu.Lock() s.cache.mu.Lock()