fix(#76): report request-scoped data freshness

This commit is contained in:
2026-07-12 20:26:21 -04:00
parent 1a9e08c0ab
commit dafb9b6dc5
18 changed files with 562 additions and 91 deletions

View File

@@ -6,6 +6,7 @@ import (
"strconv"
"time"
"github.com/AmanTahiliani/box-box/internal/api"
"github.com/AmanTahiliani/box-box/internal/models"
)
@@ -78,6 +79,7 @@ func (s *Server) handleDriverSummary(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("source") == "" {
mode = sourceAuto
}
client := s.client.Scoped()
if mode == sourceLocal || mode == sourceAuto {
resp, sessionKey, ok, lerr := s.localDriverSummary(year, driverNumber)
@@ -87,7 +89,15 @@ func (s *Server) handleDriverSummary(w http.ResponseWriter, r *http.Request) {
}
if ok {
if mode != sourceLocal {
s.tryEnrichDriverSummary(&resp, sessionKey)
tryEnrichDriverSummary(client, &resp, sessionKey)
}
switch resp.Enrichment {
case "full":
markMixedResponse(w, client, false)
case "limited":
markDataResponse(w, "local", "limited")
default:
markLocalResponse(w, false)
}
writeJSON(w, resp)
return
@@ -98,15 +108,16 @@ func (s *Server) handleDriverSummary(w http.ResponseWriter, r *http.Request) {
}
}
resp, err := s.openF1DriverSummary(year, driverNumber)
resp, err := openF1DriverSummary(client, year, driverNumber)
if err != nil {
writeError(w, err, http.StatusInternalServerError, s.client.LastResponseWasStale())
writeError(w, err, http.StatusInternalServerError, client.LastResponseWasStale())
return
}
if resp == nil {
http.Error(w, fmt.Sprintf("driver %d not found in %d championship", driverNumber, year), http.StatusNotFound)
return
}
markOpenF1Response(w, client)
writeJSON(w, resp)
}
@@ -152,8 +163,8 @@ func (s *Server) localDriverSummary(year, driverNumber int) (driverSummaryRespon
return resp, sessionKey, true, nil
}
func (s *Server) openF1DriverSummary(year, driverNumber int) (*driverSummaryResponse, error) {
champ, err := s.client.GetDriverChampionshipForYear(year)
func openF1DriverSummary(client *api.OpenF1Client, year, driverNumber int) (*driverSummaryResponse, error) {
champ, err := client.GetDriverChampionshipForYear(year)
if err != nil {
return nil, err
}
@@ -170,14 +181,14 @@ func (s *Server) openF1DriverSummary(year, driverNumber int) (*driverSummaryResp
driverInfo := map[int]models.Driver{}
sessionKey := champ[0].SessionKey
if ds, derr := s.client.GetDriversForSession(sessionKey); derr == nil {
if ds, derr := client.GetDriversForSession(sessionKey); derr == nil {
driverInfo = buildDriverMapFirst(ds)
}
if d, ok := s.championshipDriverInfo(entry.SessionKey, driverNumber, driverInfo); ok {
if d, ok := championshipDriverInfo(client, entry.SessionKey, driverNumber, driverInfo); ok {
driverInfo[driverNumber] = d
}
races, _, err := s.fetchSeasonRaces(year)
races, _, err := fetchSeasonRaces(client, year)
if err != nil {
return nil, err
}
@@ -194,8 +205,8 @@ func (s *Server) openF1DriverSummary(year, driverNumber int) (*driverSummaryResp
// tryEnrichDriverSummary optionally fills headshot / polished identity from
// OpenF1. It never blocks longer than driverEnrichmentTimeout — on timeout or
// failure the local profile remains intact with enrichment=limited.
func (s *Server) tryEnrichDriverSummary(resp *driverSummaryResponse, sessionKey int) {
if resp == nil || s.client == nil || sessionKey <= 0 {
func tryEnrichDriverSummary(client *api.OpenF1Client, resp *driverSummaryResponse, sessionKey int) {
if resp == nil || client == nil || sessionKey <= 0 {
if resp != nil && resp.Enrichment == "none" {
// No session to enrich from — leave as none (local identity only).
}
@@ -209,7 +220,7 @@ func (s *Server) tryEnrichDriverSummary(resp *driverSummaryResponse, sessionKey
done := make(chan enrichResult, 1)
go func() {
d, ok := s.championshipDriverInfo(sessionKey, resp.DriverNumber, nil)
d, ok := championshipDriverInfo(client, sessionKey, resp.DriverNumber, nil)
done <- enrichResult{driver: d, ok: ok}
}()