mirror of
https://github.com/AmanTahiliani/box-box.git
synced 2026-08-08 04:06:18 -04:00
Add local navigation API
This commit is contained in:
57
internal/query/datasets.go
Normal file
57
internal/query/datasets.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package query
|
||||
|
||||
import "github.com/AmanTahiliani/box-box/internal/store"
|
||||
|
||||
func emptyDatasetMap() map[string]DatasetInfo {
|
||||
return map[string]DatasetInfo{
|
||||
"meeting": missingDataset(),
|
||||
"session": missingDataset(),
|
||||
"drivers": missingDataset(),
|
||||
"results": missingDataset(),
|
||||
"starting_grid": missingDataset(),
|
||||
"stints": missingDataset(),
|
||||
"pit_stops": missingDataset(),
|
||||
"positions": missingDataset(),
|
||||
"race_control": missingDataset(),
|
||||
"weather": missingDataset(),
|
||||
"laps": missingDataset(),
|
||||
}
|
||||
}
|
||||
|
||||
func datasetsFromCounts(meetingAvailable, sessionAvailable bool, counts store.SessionDatasetCounts) map[string]DatasetInfo {
|
||||
ds := emptyDatasetMap()
|
||||
if meetingAvailable {
|
||||
ds["meeting"] = availableLocal(1)
|
||||
}
|
||||
if sessionAvailable {
|
||||
ds["session"] = availableLocal(1)
|
||||
}
|
||||
if counts.Drivers > 0 {
|
||||
ds["drivers"] = availableLocal(counts.Drivers)
|
||||
}
|
||||
if counts.Results > 0 {
|
||||
ds["results"] = availableLocal(counts.Results)
|
||||
}
|
||||
if counts.StartingGrid > 0 {
|
||||
ds["starting_grid"] = availableLocal(counts.StartingGrid)
|
||||
}
|
||||
if counts.Stints > 0 {
|
||||
ds["stints"] = availableLocal(counts.Stints)
|
||||
}
|
||||
if counts.PitStops > 0 {
|
||||
ds["pit_stops"] = availableLocal(counts.PitStops)
|
||||
}
|
||||
if counts.Positions > 0 {
|
||||
ds["positions"] = availableLocal(counts.Positions)
|
||||
}
|
||||
if counts.RaceControl > 0 {
|
||||
ds["race_control"] = availableLocal(counts.RaceControl)
|
||||
}
|
||||
if counts.Weather > 0 {
|
||||
ds["weather"] = availableLocal(counts.Weather)
|
||||
}
|
||||
if counts.Laps > 0 {
|
||||
ds["laps"] = availableLocal(counts.Laps)
|
||||
}
|
||||
return ds
|
||||
}
|
||||
133
internal/query/navigation.go
Normal file
133
internal/query/navigation.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/AmanTahiliani/box-box/internal/models"
|
||||
)
|
||||
|
||||
// ErrMeetingNotFound is returned when a meeting is not in the local store.
|
||||
var ErrMeetingNotFound = errors.New("meeting not found")
|
||||
|
||||
// WeekendSession is one session within a meeting with dataset coverage.
|
||||
type WeekendSession struct {
|
||||
Session models.Session `json:"session"`
|
||||
Source string `json:"source"`
|
||||
Datasets map[string]DatasetInfo `json:"datasets"`
|
||||
}
|
||||
|
||||
// Weekend is the local-first read model for one race weekend.
|
||||
type Weekend struct {
|
||||
Source string `json:"source"`
|
||||
MeetingKey int `json:"meeting_key"`
|
||||
Meeting models.Meeting `json:"meeting"`
|
||||
Sessions []WeekendSession `json:"sessions"`
|
||||
DefaultSessionKey int `json:"default_session_key,omitempty"`
|
||||
}
|
||||
|
||||
// ListSeasons returns years with ingested meetings, newest first.
|
||||
func (s *Service) ListSeasons() ([]int, error) {
|
||||
return s.store.ListYears()
|
||||
}
|
||||
|
||||
// GetWeekend loads meeting metadata, sessions, and per-session dataset coverage.
|
||||
func (s *Service) GetWeekend(meetingKey int) (Weekend, error) {
|
||||
meeting, err := s.store.GetMeeting(meetingKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Weekend{}, ErrMeetingNotFound
|
||||
}
|
||||
return Weekend{}, err
|
||||
}
|
||||
|
||||
sessions, err := s.store.ListSessionsByMeeting(meetingKey)
|
||||
if err != nil {
|
||||
return Weekend{}, err
|
||||
}
|
||||
|
||||
out := Weekend{
|
||||
Source: ResponseSourceNone,
|
||||
MeetingKey: meetingKey,
|
||||
Meeting: meetingToModel(meeting),
|
||||
Sessions: []WeekendSession{},
|
||||
}
|
||||
|
||||
if len(sessions) == 0 {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
out.Sessions = make([]WeekendSession, 0, len(sessions))
|
||||
for _, sess := range sessions {
|
||||
counts, err := s.store.CountSessionDatasets(sess.SessionKey)
|
||||
if err != nil {
|
||||
return Weekend{}, err
|
||||
}
|
||||
datasets := datasetsFromCounts(true, true, counts)
|
||||
out.Sessions = append(out.Sessions, WeekendSession{
|
||||
Session: sessionToModel(sess),
|
||||
Source: responseSource(datasets),
|
||||
Datasets: datasets,
|
||||
})
|
||||
}
|
||||
|
||||
out.Source = weekendSource(out.Sessions)
|
||||
out.DefaultSessionKey = pickDefaultSession(out.Sessions)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func weekendSource(sessions []WeekendSession) string {
|
||||
if len(sessions) == 0 {
|
||||
return ResponseSourceNone
|
||||
}
|
||||
hasLocal := false
|
||||
allMissing := true
|
||||
for _, sess := range sessions {
|
||||
if sess.Source == ResponseSourceNone {
|
||||
continue
|
||||
}
|
||||
allMissing = false
|
||||
if sess.Source == ResponseSourceLocal || sess.Source == ResponseSourcePartial {
|
||||
hasLocal = true
|
||||
}
|
||||
}
|
||||
if allMissing {
|
||||
return ResponseSourceNone
|
||||
}
|
||||
for _, sess := range sessions {
|
||||
if sess.Source == ResponseSourcePartial || sess.Source == ResponseSourceNone {
|
||||
return ResponseSourcePartial
|
||||
}
|
||||
}
|
||||
if hasLocal {
|
||||
return ResponseSourceLocal
|
||||
}
|
||||
return ResponseSourceNone
|
||||
}
|
||||
|
||||
func pickDefaultSession(sessions []WeekendSession) int {
|
||||
if len(sessions) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
bestIdx := 0
|
||||
bestScore := datasetScore(sessions[0].Datasets)
|
||||
for i := 1; i < len(sessions); i++ {
|
||||
score := datasetScore(sessions[i].Datasets)
|
||||
if score >= bestScore {
|
||||
bestScore = score
|
||||
bestIdx = i
|
||||
}
|
||||
}
|
||||
return sessions[bestIdx].Session.SessionKey
|
||||
}
|
||||
|
||||
func datasetScore(datasets map[string]DatasetInfo) int {
|
||||
score := 0
|
||||
for _, info := range datasets {
|
||||
if info.Status == DatasetStatusAvailable {
|
||||
score++
|
||||
}
|
||||
}
|
||||
return score
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package query
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -238,3 +239,77 @@ func TestListDriversRequiresSession(t *testing.T) {
|
||||
t.Fatalf("ListDrivers() error = %v, want sql.ErrNoRows", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListSeasonsEmpty(t *testing.T) {
|
||||
svc := openTestService(t)
|
||||
|
||||
years, err := svc.ListSeasons()
|
||||
if err != nil {
|
||||
t.Fatalf("ListSeasons() error = %v", err)
|
||||
}
|
||||
if len(years) != 0 {
|
||||
t.Fatalf("ListSeasons() = %v, want empty", years)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListSeasonsWithData(t *testing.T) {
|
||||
svc := openTestService(t)
|
||||
seedRaceHubData(t, svc.store)
|
||||
|
||||
years, err := svc.ListSeasons()
|
||||
if err != nil {
|
||||
t.Fatalf("ListSeasons() error = %v", err)
|
||||
}
|
||||
if len(years) != 1 || years[0] != 2025 {
|
||||
t.Fatalf("ListSeasons() = %v, want [2025]", years)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetWeekendMissingMeeting(t *testing.T) {
|
||||
svc := openTestService(t)
|
||||
|
||||
_, err := svc.GetWeekend(1229)
|
||||
if err == nil {
|
||||
t.Fatal("GetWeekend() error = nil, want ErrMeetingNotFound")
|
||||
}
|
||||
if !errors.Is(err, ErrMeetingNotFound) {
|
||||
t.Fatalf("GetWeekend() error = %v, want ErrMeetingNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetWeekendWithSessions(t *testing.T) {
|
||||
svc := openTestService(t)
|
||||
seedRaceHubData(t, svc.store)
|
||||
|
||||
if err := svc.store.UpsertSession(store.Session{
|
||||
SessionKey: 9000,
|
||||
MeetingKey: 1229,
|
||||
SessionName: "Core Only",
|
||||
SessionType: "Race",
|
||||
DateStart: "2025-05-24T13:00:00+00:00",
|
||||
}); err != nil {
|
||||
t.Fatalf("UpsertSession() error = %v", err)
|
||||
}
|
||||
if err := svc.store.UpsertSessionDriver(store.SessionDriver{
|
||||
SessionKey: 9000, DriverNumber: 1, MeetingKey: 1229,
|
||||
}); err != nil {
|
||||
t.Fatalf("UpsertSessionDriver() error = %v", err)
|
||||
}
|
||||
|
||||
weekend, err := svc.GetWeekend(1229)
|
||||
if err != nil {
|
||||
t.Fatalf("GetWeekend() error = %v", err)
|
||||
}
|
||||
if weekend.Meeting.MeetingName != "Monaco" {
|
||||
t.Fatalf("MeetingName = %q, want Monaco", weekend.Meeting.MeetingName)
|
||||
}
|
||||
if len(weekend.Sessions) != 2 {
|
||||
t.Fatalf("Sessions len = %d, want 2", len(weekend.Sessions))
|
||||
}
|
||||
if weekend.DefaultSessionKey != 9472 {
|
||||
t.Fatalf("DefaultSessionKey = %d, want 9472 (full data session)", weekend.DefaultSessionKey)
|
||||
}
|
||||
if weekend.Sessions[0].Datasets["drivers"].Status != DatasetStatusAvailable {
|
||||
t.Fatalf("first session drivers = %+v, want available", weekend.Sessions[0].Datasets["drivers"])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user