fix: address code review findings

This commit is contained in:
2026-08-18 19:54:12 +03:00
parent 6e65e6161c
commit 502979d43b
3 changed files with 40 additions and 3 deletions

View File

@@ -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 // Run Pareto-optimal route search using the graph
results := hc.Router.FindRoutesPareto(req.FromCityID, req.ToCityID, opts, closedStationsMap, neighborsMap) results := hc.Router.FindRoutesPareto(req.FromCityID, req.ToCityID, opts, closedStationsMap, neighborsMap)

View File

@@ -116,10 +116,16 @@ func keyString(k *CacheKey) string {
case k.Kind == "station": case k.Kind == "station":
return fmt.Sprintf("stations:%s", sanitizeKeyComponent(k.Code)) return fmt.Sprintf("stations:%s", sanitizeKeyComponent(k.Code))
case k.Kind == "search": 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.From),
sanitizeKeyComponent(k.To), sanitizeKeyComponent(k.To),
sanitizeKeyComponent(k.Date)) sanitizeKeyComponent(k.Date),
farTermFlag)
default: default:
return fmt.Sprintf("unknown:%s", sanitizeKeyComponent(k.Kind)) return fmt.Sprintf("unknown:%s", sanitizeKeyComponent(k.Kind))
} }

View File

@@ -72,10 +72,17 @@ func TestCacheKeyString(t *testing.T) {
// Search key // Search key
searchKey := &CacheKey{Kind: "search", From: "c146", To: "c213", Date: "2026-08-15"} 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 { if keyString(searchKey) != expectedSearchKey {
t.Errorf("expected %s, got %s", expectedSearchKey, keyString(searchKey)) 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. // TestCacheAsideGetOrSet tests the cache-aside GetOrSetFuncPattern.