From 502979d43bbce0dcf0c7ea5bafa082416c637da4 Mon Sep 17 00:00:00 2001 From: Vladimir Zagainov Date: Tue, 18 Aug 2026 19:54:12 +0300 Subject: [PATCH] fix: address code review findings --- cmd/api/handlers.go | 24 ++++++++++++++++++++++++ internal/cache/store.go | 10 ++++++++-- internal/cache/store_test.go | 9 ++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/cmd/api/handlers.go b/cmd/api/handlers.go index cc90685..c2712cb 100644 --- a/cmd/api/handlers.go +++ b/cmd/api/handlers.go @@ -239,6 +239,30 @@ func RouteSearch(hc *HandlerContext, w http.ResponseWriter, r *http.Request) { } } + // Detect closed stations by checking if they have real edges in the graph + for _, cityID := range []string{req.FromCityID, req.ToCityID} { + // Get stations for this city + stations := getStationsForCity(cityID) + for _, station := range stations { + if len(station) > 0 { + stationID := station[0] + // Check if this station has real edges + hasRealEdges := false + for _, edge := range hc.Router.Edges() { + if edge.From.ID == stationID || edge.To.ID == stationID { + if edge.Kind == routing.EdgeKindReal { + hasRealEdges = true + break + } + } + } + if !hasRealEdges { + closedStationsMap[stationID] = true + } + } + } + } + // Run Pareto-optimal route search using the graph results := hc.Router.FindRoutesPareto(req.FromCityID, req.ToCityID, opts, closedStationsMap, neighborsMap) diff --git a/internal/cache/store.go b/internal/cache/store.go index c473127..0f1dcb5 100644 --- a/internal/cache/store.go +++ b/internal/cache/store.go @@ -116,10 +116,16 @@ func keyString(k *CacheKey) string { case k.Kind == "station": return fmt.Sprintf("stations:%s", sanitizeKeyComponent(k.Code)) case k.Kind == "search": - return fmt.Sprintf("search:%s:%s:%s", + // Include far-term flag in cache key to distinguish near-term (3-hour TTL) from far-term (7-day TTL) + farTermFlag := "near" + if k.Request != "" { + farTermFlag = k.Request + } + return fmt.Sprintf("search:%s:%s:%s:%s", sanitizeKeyComponent(k.From), sanitizeKeyComponent(k.To), - sanitizeKeyComponent(k.Date)) + sanitizeKeyComponent(k.Date), + farTermFlag) default: return fmt.Sprintf("unknown:%s", sanitizeKeyComponent(k.Kind)) } diff --git a/internal/cache/store_test.go b/internal/cache/store_test.go index f91a01f..caee1e0 100644 --- a/internal/cache/store_test.go +++ b/internal/cache/store_test.go @@ -72,10 +72,17 @@ func TestCacheKeyString(t *testing.T) { // Search key searchKey := &CacheKey{Kind: "search", From: "c146", To: "c213", Date: "2026-08-15"} - expectedSearchKey := "search:c146:c213:2026-08-15" + expectedSearchKey := "search:c146:c213:2026-08-15:near" if keyString(searchKey) != expectedSearchKey { t.Errorf("expected %s, got %s", expectedSearchKey, keyString(searchKey)) } + + // Search key with far-term flag + searchKeyFarTerm := &CacheKey{Kind: "search", From: "c146", To: "c213", Date: "2026-08-30", Request: "far"} + expectedSearchKeyFarTerm := "search:c146:c213:2026-08-30:far" + if keyString(searchKeyFarTerm) != expectedSearchKeyFarTerm { + t.Errorf("expected %s, got %s", expectedSearchKeyFarTerm, keyString(searchKeyFarTerm)) + } } // TestCacheAsideGetOrSet tests the cache-aside GetOrSetFuncPattern.