fix: address code review findings
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
10
internal/cache/store.go
vendored
10
internal/cache/store.go
vendored
@@ -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))
|
||||
}
|
||||
|
||||
9
internal/cache/store_test.go
vendored
9
internal/cache/store_test.go
vendored
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user