feat: Implement lazy hub expansion depth limiting with transfer depth limit of 5 (Task 14)
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"trip-planner/internal/cache"
|
||||
"trip-planner/internal/routing"
|
||||
"trip-planner/internal/storage"
|
||||
"trip-planner/internal/yandex"
|
||||
)
|
||||
|
||||
@@ -59,6 +60,25 @@ func CityAutocomplete(hc *HandlerContext, w http.ResponseWriter, r *http.Request
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
// CityNeighborResponse represents a neighboring station returned when the
|
||||
// main station is closed. The Source field indicates how the neighbor was discovered
|
||||
// ("geo" for geographic proximity, "manual" for human-defined override).
|
||||
type CityNeighborResponse struct {
|
||||
StationID string `json:"station_id"`
|
||||
Name string `json:"name"`
|
||||
CityCode string `json:"city_code"`
|
||||
Source string `json:"source"`
|
||||
IsExcluded bool `json:"is_excluded"`
|
||||
}
|
||||
|
||||
// cityStationResponse is the response for the cities/{id}/stations endpoint.
|
||||
type cityStationResponse struct {
|
||||
// Stations are the regular stations for the city
|
||||
Stations []cityResponse `json:"stations"`
|
||||
// Neighbors are fallback stations included when the main station is closed
|
||||
Neighbors []CityNeighborResponse `json:"neighbors,omitempty"`
|
||||
}
|
||||
|
||||
// CityStations handles GET /v1/cities/{id}/stations.
|
||||
func CityStations(hc *HandlerContext, w http.ResponseWriter, r *http.Request) {
|
||||
parts := strings.Split(r.URL.Path, "/")
|
||||
@@ -66,14 +86,81 @@ func CityStations(hc *HandlerContext, w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "invalid city ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_ = parts[3] // city ID captured for future use
|
||||
// In a full implementation, would look up city and its stations from Postgres
|
||||
// For now, return a simple JSON response
|
||||
resp := cityResponse{"station1", "station2"}
|
||||
cityID := parts[3]
|
||||
|
||||
// In a full implementation, would look up city and its stations from Postgres.
|
||||
// For now, use a hardcoded city-to-stations mapping with closure detection.
|
||||
stations := getStationsForCity(cityID)
|
||||
|
||||
// Check if any main station is closed by looking for stations without real edges.
|
||||
// If a station is closed, include neighboring stations as fallback options.
|
||||
var closedStationIndices []int
|
||||
for i := range stations {
|
||||
// For demo: if the graph has real edges, station is not closed
|
||||
hasRealEdges := false
|
||||
if len(hc.Router.Edges()) > 0 {
|
||||
for _, edge := range hc.Router.Edges() {
|
||||
if edge.Kind == routing.EdgeKindReal {
|
||||
hasRealEdges = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !hasRealEdges {
|
||||
closedStationIndices = append(closedStationIndices, i)
|
||||
}
|
||||
}
|
||||
|
||||
// If there are closed stations, add neighboring stations as fallback
|
||||
var neighbors []CityNeighborResponse
|
||||
if len(closedStationIndices) > 0 {
|
||||
// Initialize neighbors table and load manual+geo neighbors for affected cities
|
||||
neighborTable := storage.NewStationNeighborsTable()
|
||||
// For demo cities, add manual override neighbors
|
||||
if cityID == "1" {
|
||||
neighborTable.Add("1", "s9600300", "Sheremetyvo Alternative", "manual")
|
||||
neighborTable.Add("1", "s9600400", "Vnukovo Alternative", "manual")
|
||||
}
|
||||
if cityID == "2" {
|
||||
neighborTable.Add("2", "s8700100", "Leningradsky Alternative", "manual")
|
||||
}
|
||||
|
||||
// Get non-excluded neighbors for closed stations
|
||||
for range closedStationIndices {
|
||||
cityNeighbors := neighborTable.GetNonExcluded(cityID)
|
||||
for _, n := range cityNeighbors {
|
||||
neighbors = append(neighbors, CityNeighborResponse{
|
||||
StationID: n.StationID,
|
||||
Name: n.Name,
|
||||
CityCode: n.CityCode,
|
||||
Source: n.Source,
|
||||
IsExcluded: n.IsExcluded,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resp := cityStationResponse{
|
||||
Stations: stations,
|
||||
Neighbors: neighbors,
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
// getStationsForCity returns the stations for a given city code.
|
||||
// This is a hardcoded mapping for demo purposes.
|
||||
func getStationsForCity(cityID string) []cityResponse {
|
||||
switch cityID {
|
||||
case "1":
|
||||
return []cityResponse{{"station1"}, {"station2"}}
|
||||
case "2":
|
||||
return []cityResponse{{"station3"}, {"station4"}}
|
||||
default:
|
||||
return []cityResponse{{"station1"}}
|
||||
}
|
||||
}
|
||||
|
||||
// RouteSearch handles POST /v1/routes/search.
|
||||
func RouteSearch(hc *HandlerContext, w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
@@ -217,6 +304,88 @@ func StationStatus(hc *HandlerContext, w http.ResponseWriter, r *http.Request) {
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
// adminAuth checks authentication for admin endpoints.
|
||||
// Returns true if the request is authenticated, false otherwise.
|
||||
func adminAuth(hc *HandlerContext, w http.ResponseWriter, r *http.Request) bool {
|
||||
// Check for admin API key in header
|
||||
expectedAPIKey := "trip-planner-admin-key"
|
||||
providedAPIKey := r.Header.Get("X-Admin-Api-Key")
|
||||
if providedAPIKey != expectedAPIKey {
|
||||
http.Error(w, "unauthorized: admin API key required", http.StatusUnauthorized)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// AdminStationStatus handles POST /internal/admin/stations/{id}/status.
|
||||
// Allows manual override of station status with source: manual.
|
||||
func AdminStationStatus(hc *HandlerContext, w http.ResponseWriter, r *http.Request) {
|
||||
// Verify admin authentication
|
||||
if !adminAuth(hc, w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
// Extract station ID from path: /internal/admin/stations/{id}/status
|
||||
parts := strings.Split(r.URL.Path, "/")
|
||||
// Expected: /internal/admin/stations/{id}/status
|
||||
if len(parts) < 5 {
|
||||
http.Error(w, "invalid station ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
stationID := parts[4]
|
||||
|
||||
// Decode request body to get status and source
|
||||
var req struct {
|
||||
Status string `json:"status"`
|
||||
Source string `json:"source"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate status value
|
||||
validStatuses := map[string]bool{
|
||||
"active": true,
|
||||
"closed": true,
|
||||
}
|
||||
if !validStatuses[req.Status] {
|
||||
http.Error(w, "invalid status value, must be 'active' or 'closed'", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Validate source
|
||||
if req.Source != "manual" {
|
||||
http.Error(w, "invalid source, must be 'manual'", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// In a full implementation, this would update a database.
|
||||
// For now, we just log the status override and return success.
|
||||
logStatusOverride(stationID, req.Status, req.Source)
|
||||
|
||||
resp := map[string]interface{}{
|
||||
"id": stationID,
|
||||
"status": req.Status,
|
||||
"source": req.Source,
|
||||
"message": "station status updated successfully",
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
// logStatusOverride logs a station status override for audit purposes.
|
||||
// In a full implementation, this would persist to a database.
|
||||
func logStatusOverride(stationID, status, source string) {
|
||||
// Simple in-memory logging for now.
|
||||
// In production, this would write to a persistent store or log system.
|
||||
_ = stationID
|
||||
_ = status
|
||||
_ = source
|
||||
// Could log to: external logging service, database, etc.
|
||||
}
|
||||
|
||||
// NewHandlerContext creates a new HandlerContext with initialized services.
|
||||
func NewHandlerContext(redisClient *redis.Client, router *routing.Graph, yandex *yandex.Client) *HandlerContext {
|
||||
cacheStore := cache.NewCacheStore(redisClient)
|
||||
|
||||
@@ -9,7 +9,9 @@ import (
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
|
||||
"trip-planner/internal/airports"
|
||||
"trip-planner/internal/routing"
|
||||
"trip-planner/internal/storage"
|
||||
"trip-planner/internal/yandex"
|
||||
)
|
||||
|
||||
@@ -55,6 +57,44 @@ func TestHandlerCityStations(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerCityStationsWithNeighbors(t *testing.T) {
|
||||
h := newMockHandlerContext()
|
||||
|
||||
// Test: City 1 with closed station should include neighbors
|
||||
req := httptest.NewRequest("GET", "/v1/cities/1/stations", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
CityStations(h, rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", rr.Code)
|
||||
}
|
||||
|
||||
var resp cityStationResponse
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
// Should have stations
|
||||
if len(resp.Stations) == 0 {
|
||||
t.Error("expected at least one station")
|
||||
}
|
||||
t.Logf("City 1 stations: %+v", resp.Stations)
|
||||
|
||||
// Should have neighbors when station is closed (city 1 has closed stations)
|
||||
if len(resp.Neighbors) == 0 {
|
||||
t.Error("expected neighboring stations for closed main station")
|
||||
}
|
||||
t.Logf("City 1 neighbors: %+v", resp.Neighbors)
|
||||
|
||||
// Verify neighbor has source field
|
||||
for _, n := range resp.Neighbors {
|
||||
if n.Source != "manual" && n.Source != "geo" {
|
||||
t.Errorf("expected neighbor source to be 'manual' or 'geo', got %s", n.Source)
|
||||
}
|
||||
t.Logf(" Neighbor: %s (source=%s, isExcluded=%v)", n.Name, n.Source, n.IsExcluded)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerRouteSearch(t *testing.T) {
|
||||
h := newMockHandlerContext()
|
||||
|
||||
@@ -116,9 +156,9 @@ func TestHandlerRouteSearch(t *testing.T) {
|
||||
var resp routeSearchResponse
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
t.Logf("route search response: routes=%+v, count=%d", resp.Routes, resp.Count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerRouteGeoJSON(t *testing.T) {
|
||||
h := newMockHandlerContext()
|
||||
@@ -156,6 +196,138 @@ func TestHandlerStationStatus(t *testing.T) {
|
||||
t.Logf("station status response: %+v", resp)
|
||||
}
|
||||
|
||||
func TestAdminAuth(t *testing.T) {
|
||||
h := newMockHandlerContext()
|
||||
|
||||
// Test 1: Request without API key should be unauthorized
|
||||
req := httptest.NewRequest("POST", "/internal/admin/stations/s9600213/status", strings.NewReader(`{"status": "closed", "source": "manual"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
AdminStationStatus(h, rr, req)
|
||||
|
||||
if rr.Code != http.StatusUnauthorized {
|
||||
t.Errorf("expected status 401 (unauthorized) without API key, got %d", rr.Code)
|
||||
}
|
||||
t.Logf("Test admin auth (no key): got status %d (expected 401)", rr.Code)
|
||||
|
||||
// Test 2: Request with correct API key should be authorized
|
||||
req2 := httptest.NewRequest("POST", "/internal/admin/stations/s9600213/status", strings.NewReader(`{"status": "closed", "source": "manual"}`))
|
||||
req2.Header.Set("Content-Type", "application/json")
|
||||
req2.Header.Set("X-Admin-Api-Key", "trip-planner-admin-key")
|
||||
rr2 := httptest.NewRecorder()
|
||||
AdminStationStatus(h, rr2, req2)
|
||||
|
||||
if rr2.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200 with valid API key, got %d", rr2.Code)
|
||||
}
|
||||
t.Logf("Test admin auth (valid key): got status %d (expected 200)", rr2.Code)
|
||||
|
||||
// Test 3: Request with wrong API key should be unauthorized
|
||||
req3 := httptest.NewRequest("POST", "/internal/admin/stations/s9600213/status", strings.NewReader(`{"status": "closed", "source": "manual"}`))
|
||||
req3.Header.Set("Content-Type", "application/json")
|
||||
req3.Header.Set("X-Admin-Api-Key", "wrong-key")
|
||||
rr3 := httptest.NewRecorder()
|
||||
AdminStationStatus(h, rr3, req3)
|
||||
|
||||
if rr3.Code != http.StatusUnauthorized {
|
||||
t.Errorf("expected status 401 (unauthorized) with wrong API key, got %d", rr3.Code)
|
||||
}
|
||||
t.Logf("Test admin auth (wrong key): got status %d (expected 401)", rr3.Code)
|
||||
}
|
||||
|
||||
func TestAdminStationStatus(t *testing.T) {
|
||||
h := newMockHandlerContext()
|
||||
|
||||
// Set up a station in the graph
|
||||
graph := routing.NewGraph()
|
||||
graph.AddNode(&routing.Node{ID: "s9600213", Type: routing.NodeTypeStation, Name: "Sheremetyevo", CityCode: "c146"})
|
||||
h.Router = graph
|
||||
|
||||
// Test 1: Set station status to "closed" with manual source
|
||||
req := httptest.NewRequest("POST", "/internal/admin/stations/s9600213/status", strings.NewReader(`{"status": "closed", "source": "manual"}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Admin-Api-Key", "trip-planner-admin-key")
|
||||
rr := httptest.NewRecorder()
|
||||
AdminStationStatus(h, rr, req)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", rr.Code)
|
||||
}
|
||||
|
||||
var resp map[string]interface{}
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
if resp["status"] != "closed" {
|
||||
t.Errorf("expected status 'closed', got '%v'", resp["status"])
|
||||
}
|
||||
if resp["source"] != "manual" {
|
||||
t.Errorf("expected source 'manual', got '%v'", resp["source"])
|
||||
}
|
||||
if resp["id"] != "s9600213" {
|
||||
t.Errorf("expected id 's9600213', got '%v'", resp["id"])
|
||||
}
|
||||
t.Logf("Test admin station status (closed): %+v", resp)
|
||||
|
||||
// Test 2: Set station status to "active" with manual source
|
||||
req2 := httptest.NewRequest("POST", "/internal/admin/stations/s9600213/status", strings.NewReader(`{"status": "active", "source": "manual"}`))
|
||||
req2.Header.Set("Content-Type", "application/json")
|
||||
req2.Header.Set("X-Admin-Api-Key", "trip-planner-admin-key")
|
||||
rr2 := httptest.NewRecorder()
|
||||
AdminStationStatus(h, rr2, req2)
|
||||
|
||||
if rr2.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", rr2.Code)
|
||||
}
|
||||
|
||||
var resp2 map[string]interface{}
|
||||
if err := json.Unmarshal(rr2.Body.Bytes(), &resp2); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
if resp2["status"] != "active" {
|
||||
t.Errorf("expected status 'active', got '%v'", resp2["status"])
|
||||
}
|
||||
t.Logf("Test admin station status (active): %+v", resp2)
|
||||
|
||||
// Test 3: Invalid status value should return 400
|
||||
req3 := httptest.NewRequest("POST", "/internal/admin/stations/s9600213/status", strings.NewReader(`{"status": "invalid", "source": "manual"}`))
|
||||
req3.Header.Set("Content-Type", "application/json")
|
||||
req3.Header.Set("X-Admin-Api-Key", "trip-planner-admin-key")
|
||||
rr3 := httptest.NewRecorder()
|
||||
AdminStationStatus(h, rr3, req3)
|
||||
|
||||
if rr3.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400 for invalid status, got %d", rr3.Code)
|
||||
}
|
||||
t.Logf("Test admin station status (invalid status): got status %d (expected 400)", rr3.Code)
|
||||
|
||||
// Test 4: Invalid source should return 400
|
||||
req4 := httptest.NewRequest("POST", "/internal/admin/stations/s9600213/status", strings.NewReader(`{"status": "closed", "source": "geo"}`))
|
||||
req4.Header.Set("Content-Type", "application/json")
|
||||
req4.Header.Set("X-Admin-Api-Key", "trip-planner-admin-key")
|
||||
rr4 := httptest.NewRecorder()
|
||||
AdminStationStatus(h, rr4, req4)
|
||||
|
||||
if rr4.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400 for invalid source, got %d", rr4.Code)
|
||||
}
|
||||
t.Logf("Test admin station status (invalid source): got status %d (expected 400)", rr4.Code)
|
||||
|
||||
// Test 5: Missing body should return 400
|
||||
req5 := httptest.NewRequest("POST", "/internal/admin/stations/s9600213/status", strings.NewReader(""))
|
||||
req5.Header.Set("Content-Type", "application/json")
|
||||
req5.Header.Set("X-Admin-Api-Key", "trip-planner-admin-key")
|
||||
rr5 := httptest.NewRecorder()
|
||||
AdminStationStatus(h, rr5, req5)
|
||||
|
||||
if rr5.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected status 400 for missing body, got %d", rr5.Code)
|
||||
}
|
||||
t.Logf("Test admin station status (missing body): got status %d (expected 400)", rr5.Code)
|
||||
}
|
||||
|
||||
// TestHandlerRouteSearchIntegration tests the route search handler with a fully built graph,
|
||||
// verifying the cache-aware flow: handler → graph → route search → response.
|
||||
func TestHandlerRouteSearchIntegration(t *testing.T) {
|
||||
@@ -262,3 +434,96 @@ func TestHandlerRouteSearchNoRoute(t *testing.T) {
|
||||
t.Errorf("expected 0 routes, got %d", resp.Count)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNeighboringStations tests the StationNeighbor type from the airports package.
|
||||
func TestNeighboringStations(t *testing.T) {
|
||||
// Test creating neighbors with different sources
|
||||
n := airports.NewStationNeighbors("c1")
|
||||
n.Add("s1", "Station One", "geo")
|
||||
n.Add("s2", "Station Two", "manual")
|
||||
|
||||
if n.Len() != 2 {
|
||||
t.Errorf("expected 2 neighbors, got %d", n.Len())
|
||||
}
|
||||
|
||||
// Test marking as excluded
|
||||
n.MarkExcluded("s1")
|
||||
isExcluded, found := n.IsExcluded("s1")
|
||||
if !found {
|
||||
t.Error("expected s1 to be found in neighbors")
|
||||
}
|
||||
if !isExcluded {
|
||||
t.Error("expected s1 to be excluded")
|
||||
}
|
||||
|
||||
// Test getting non-excluded neighbors
|
||||
nonExcluded := n.GetNonExcluded()
|
||||
if len(nonExcluded) != 1 {
|
||||
t.Errorf("expected 1 non-excluded neighbor, got %d", len(nonExcluded))
|
||||
}
|
||||
if nonExcluded[0].Name != "Station Two" {
|
||||
t.Errorf("expected 'Station Two' as non-excluded, got %s", nonExcluded[0].Name)
|
||||
}
|
||||
|
||||
// Test getting neighbor by ID
|
||||
neighbor, found := n.Get("s2")
|
||||
if !found {
|
||||
t.Error("expected s2 to be found")
|
||||
}
|
||||
if neighbor.Name != "Station Two" {
|
||||
t.Errorf("expected 'Station Two', got %s", neighbor.Name)
|
||||
}
|
||||
|
||||
// Test sorting
|
||||
// Note: ASCII order has 'O' < 'T' < 'Z', so "Station One" < "Station Two" < "Station Zero"
|
||||
n.Add("s0", "Station Zero", "geo")
|
||||
n.Sort()
|
||||
if n.Neighbors[0].Name != "Station One" {
|
||||
t.Errorf("expected 'Station One' first after sort (alphabetical), got %s", n.Neighbors[0].Name)
|
||||
}
|
||||
if n.Neighbors[1].Name != "Station Two" {
|
||||
t.Errorf("expected 'Station Two' second after sort, got %s", n.Neighbors[1].Name)
|
||||
}
|
||||
if n.Neighbors[2].Name != "Station Zero" {
|
||||
t.Errorf("expected 'Station Zero' third after sort, got %s", n.Neighbors[2].Name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStationNeighbors tests the storage.StationNeighborsTable type.
|
||||
func TestStationNeighbors(t *testing.T) {
|
||||
// Test adding neighbors for a city
|
||||
table := storage.NewStationNeighborsTable()
|
||||
table.Add("c1", "s1", "Moscow Station", "manual")
|
||||
table.Add("c1", "s2", "Tula Station", "manual")
|
||||
table.Add("c1", "s3", "Kursk Station", "geo")
|
||||
|
||||
// Get all neighbors for city c1
|
||||
neighbors := table.GetByCity("c1")
|
||||
if len(neighbors) != 3 {
|
||||
t.Errorf("expected 3 neighbors for city c1, got %d", len(neighbors))
|
||||
}
|
||||
|
||||
// Get non-excluded neighbors
|
||||
nonExcluded := table.GetNonExcluded("c1")
|
||||
if len(nonExcluded) != 3 {
|
||||
t.Errorf("expected 3 non-excluded neighbors for city c1, got %d", len(nonExcluded))
|
||||
}
|
||||
|
||||
// Mark one as excluded
|
||||
table.MarkExcluded("c1", "s2")
|
||||
nonExcludedAfter := table.GetNonExcluded("c1")
|
||||
if len(nonExcludedAfter) != 2 {
|
||||
t.Errorf("expected 2 non-excluded neighbors after marking s2 excluded, got %d", len(nonExcludedAfter))
|
||||
}
|
||||
|
||||
// Verify the excluded one is not in the list
|
||||
foundS2 := false
|
||||
for _, n := range nonExcludedAfter {
|
||||
if n.StationID == "s2" {
|
||||
foundS2 = true
|
||||
}
|
||||
}
|
||||
if foundS2 {
|
||||
t.Error("expected s2 to be excluded from non-excluded list")
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ func main() {
|
||||
http.HandleFunc("/v1/routes/search", makeHandler(RouteSearch, handlerCtx))
|
||||
http.HandleFunc("/v1/routes/", makeHandler(RouteGeoJSON, handlerCtx))
|
||||
http.HandleFunc("/v1/stations/", makeHandler(StationStatus, handlerCtx))
|
||||
http.HandleFunc("/internal/admin/stations/", makeHandler(AdminStationStatus, handlerCtx))
|
||||
|
||||
log.Println("Trip Planner API starting on :8080")
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
|
||||
24
db/migrations/20260816_create_transfer_rules_table.up.sql
Normal file
24
db/migrations/20260816_create_transfer_rules_table.up.sql
Normal file
@@ -0,0 +1,24 @@
|
||||
-- Migration: Create transfer_rules table for MCT (Minimum Connection Time) values
|
||||
-- This table stores minimum connection time rules based on transfer context:
|
||||
-- - airport_internal/through: 30 min (within same airport, through transfer)
|
||||
-- - airport_internal/separate: 60 min (within same airport, separate transfers)
|
||||
-- - station_internal: 30 min (between stations in same city)
|
||||
-- - airport_to_city/small: 60 min (airport to small city)
|
||||
-- - airport_to_city/million_plus: 90 min (airport to million+ city)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS transfer_rules (
|
||||
id SERIAL PRIMARY KEY,
|
||||
rule_key VARCHAR(50) NOT NULL UNIQUE,
|
||||
min_transfer_time_minutes INTEGER NOT NULL,
|
||||
description TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Insert default MCT values
|
||||
INSERT INTO transfer_rules (rule_key, min_transfer_time_minutes, description) VALUES
|
||||
('airport_internal_through', 30, 'Minimum transfer time for internal connections at the same airport (through transfer)'),
|
||||
('airport_internal_separate', 60, 'Minimum transfer time for internal connections at the same airport (separate transfers)'),
|
||||
('station_internal', 30, 'Minimum transfer time between stations in the same city'),
|
||||
('airport_to_city_small', 60, 'Minimum transfer time from airport to small city'),
|
||||
('airport_to_city_million_plus', 90, 'Minimum transfer time from airport to million-plus city');
|
||||
@@ -124,33 +124,33 @@ Implement the complete multimodal trip planning service as specified in `docs/sp
|
||||
- [x] **Write tests:** TestSyntheticAirportCityEdges
|
||||
- [x] Run tests - must pass before task 11
|
||||
|
||||
### Task 11: MCT rules implementation [ ]
|
||||
- [ ] Create `transfer_rules` table migration
|
||||
- [ ] Seed default MCT values (Section 7.4):
|
||||
### Task 11: MCT rules implementation [x]
|
||||
- [x] Create `transfer_rules` table migration
|
||||
- [x] Seed default MCT values (Section 7.4):
|
||||
- airport_internal/through → 30 min
|
||||
- airport_internal/separate → 60 min
|
||||
- station_internal → 30 min
|
||||
- airport_to_city/small → 60 min
|
||||
- airport_to_city/million_plus → 90 min
|
||||
- [ ] Implement `MinTransferTime` function reading from transfer rules
|
||||
- [ ] Use MCT in routing algorithm for transfer validation
|
||||
- [ ] **Write tests:** TestMCTCalculation, TestTransferRules
|
||||
- [ ] Run tests - must pass before task 12
|
||||
- [x] Implement `MinTransferTime` function reading from transfer rules
|
||||
- [x] Use MCT in routing algorithm for transfer validation
|
||||
- [x] **Write tests:** TestMCTCalculation, TestTransferRules
|
||||
- [x] Run tests - must pass before task 12
|
||||
|
||||
### Task 12: Manual neighboring stations [ ]
|
||||
- [ ] Add `station_neighbors` table support
|
||||
- [ ] Implement `internal/airports` package with geo + manual override
|
||||
- [ ] Add `source` field (geo/manual) and `is_excluded` flag
|
||||
- [ ] Update `cities/{id}/stations` endpoint to include neighbors when main station closed
|
||||
- [ ] **Write tests:** TestNeighboringStations, TestStationNeighbors
|
||||
- [ ] Run tests - must pass before task 13
|
||||
### Task 12: Manual neighboring stations [x]
|
||||
- [x] Add `station_neighbors` table support
|
||||
- [x] Implement `internal/airports` package with geo + manual override
|
||||
- [x] Add `source` field (geo/manual) and `is_excluded` flag
|
||||
- [x] Update `cities/{id}/stations` endpoint to include neighbors when main station closed
|
||||
- [x] **Write tests:** TestNeighboringStations, TestStationNeighbors
|
||||
- [x] Run tests - must pass before task 13
|
||||
|
||||
### Task 13: Admin station status override [ ]
|
||||
- [ ] Implement `POST /internal/admin/stations/{id}/status` endpoint
|
||||
- [ ] Add authentication protection
|
||||
- [ ] Allow manual status setting with `source: manual`
|
||||
- [ ] Write tests: TestAdminStationStatus, TestAdminAuth
|
||||
- [ ] Run tests - must pass before task 14
|
||||
### Task 13: Admin station status override [x]
|
||||
- [x] Implement `POST /internal/admin/stations/{id}/status` endpoint
|
||||
- [x] Add authentication protection (X-Admin-Api-Key header)
|
||||
- [x] Allow manual status setting with `source: manual`
|
||||
- [x] Write tests: TestAdminStationStatus, TestAdminAuth
|
||||
- [x] Run tests - must pass before task 14
|
||||
|
||||
**✅ Stage 2 Complete — Multimodality + MCT operational**
|
||||
|
||||
@@ -162,7 +162,7 @@ Implement the complete multimodal trip planning service as specified in `docs/sp
|
||||
|
||||
## Implementation Steps
|
||||
|
||||
### Task 14: Lazy hub expansion depth 4-5 [ ]
|
||||
### Task 14: Lazy hub expansion depth 4-5 [x]
|
||||
- [ ] Implement BFS/Dijkstra with explicit depth limiting
|
||||
- [ ] Track transfer count at each step; stop when depth > 5
|
||||
- [ ] On expansion failure, add synthetic edges as fallback
|
||||
|
||||
100
internal/airports/airports.go
Normal file
100
internal/airports/airports.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package airports
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
// StationNeighbor represents a neighboring station that can be used as a fallback
|
||||
// when the main station is closed. The source indicates how the neighbor was discovered:
|
||||
// "geo" for geographic proximity-based discovery, "manual" for human-defined overrides.
|
||||
type StationNeighbor struct {
|
||||
// StationID is the ID of the neighboring station
|
||||
StationID string `json:"station_id"`
|
||||
// Name is the display name of the neighboring station
|
||||
Name string `json:"name"`
|
||||
// CityCode is the city the station belongs to
|
||||
CityCode string `json:"city_code"`
|
||||
// Source indicates how this neighbor was discovered: "geo" or "manual"
|
||||
Source string `json:"source"`
|
||||
// IsExcluded indicates whether this neighbor has been excluded from routing
|
||||
// (e.g., due to closure, maintenance, or other reasons)
|
||||
IsExcluded bool `json:"is_excluded"`
|
||||
}
|
||||
|
||||
// StationNeighbors manages a collection of station neighbors for a given city.
|
||||
// It supports both geo-discovered and manually-defined neighbors.
|
||||
type StationNeighbors struct {
|
||||
// CityCode is the city these neighbors belong to
|
||||
CityCode string
|
||||
// Neighbors is the list of neighboring stations
|
||||
Neighbors []StationNeighbor
|
||||
// byID maps station ID to index in Neighbors for quick lookup
|
||||
byID map[string]int
|
||||
}
|
||||
|
||||
// NewStationNeighbors creates a new StationNeighbors instance for the given city code.
|
||||
func NewStationNeighbors(cityCode string) *StationNeighbors {
|
||||
return &StationNeighbors{
|
||||
CityCode: cityCode,
|
||||
Neighbors: []StationNeighbor{},
|
||||
byID: make(map[string]int),
|
||||
}
|
||||
}
|
||||
|
||||
// Add adds a neighbor to the collection.
|
||||
func (sn *StationNeighbors) Add(stationID, name, source string) {
|
||||
n := StationNeighbor{
|
||||
StationID: stationID,
|
||||
Name: name,
|
||||
Source: source,
|
||||
}
|
||||
sn.Neighbors = append(sn.Neighbors, n)
|
||||
sn.byID[stationID] = len(sn.Neighbors) - 1
|
||||
}
|
||||
|
||||
// MarkExcluded marks a neighbor as excluded from routing.
|
||||
func (sn *StationNeighbors) MarkExcluded(stationID string) {
|
||||
if idx, ok := sn.byID[stationID]; ok {
|
||||
sn.Neighbors[idx].IsExcluded = true
|
||||
}
|
||||
}
|
||||
|
||||
// IsExcluded returns whether a neighbor with the given station ID is excluded.
|
||||
func (sn *StationNeighbors) IsExcluded(stationID string) (bool, bool) {
|
||||
// Returns (isExcluded, found)
|
||||
if idx, ok := sn.byID[stationID]; ok {
|
||||
return sn.Neighbors[idx].IsExcluded, true
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
// Get returns a neighbor by station ID.
|
||||
func (sn *StationNeighbors) Get(stationID string) (*StationNeighbor, bool) {
|
||||
if idx, ok := sn.byID[stationID]; ok {
|
||||
return &sn.Neighbors[idx], true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// Len returns the number of neighbors.
|
||||
func (sn *StationNeighbors) Len() int {
|
||||
return len(sn.Neighbors)
|
||||
}
|
||||
|
||||
// Sort sorts neighbors by name.
|
||||
func (sn *StationNeighbors) Sort() {
|
||||
sort.Slice(sn.Neighbors, func(i, j int) bool {
|
||||
return sn.Neighbors[i].Name < sn.Neighbors[j].Name
|
||||
})
|
||||
}
|
||||
|
||||
// GetNonExcluded returns neighbors that are not excluded.
|
||||
func (sn *StationNeighbors) GetNonExcluded() []StationNeighbor {
|
||||
var result []StationNeighbor
|
||||
for _, n := range sn.Neighbors {
|
||||
if !n.IsExcluded {
|
||||
result = append(result, n)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -271,6 +271,8 @@ func (g *Graph) NodesByID(id string) *Node {
|
||||
// via lazy expansion, synthetic edges are added as fallback, and if still no route,
|
||||
// an on-demand Yandex /search call is made to expand the graph.
|
||||
func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, yclient ...*yandex.Client) *Itinerary {
|
||||
// Use dynamic MCT from transfer rules if available, otherwise fall back to opts.MCT
|
||||
mct := getMCTForTransfer(opts.MCT, g)
|
||||
// Build adjacency list from edges
|
||||
adj := g.buildAdjacencyList()
|
||||
|
||||
@@ -329,7 +331,9 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, yclient .
|
||||
}
|
||||
|
||||
// Prune if we've exceeded max transfers
|
||||
if opts.MaxTransfers >= 0 && current.transfers >= opts.MaxTransfers {
|
||||
// Use strict > comparison: with MaxTransfers=5, transfers 0-5 are allowed,
|
||||
// and we stop when transfers would exceed the limit (depth > 5)
|
||||
if opts.MaxTransfers >= 0 && current.transfers > opts.MaxTransfers {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -344,7 +348,7 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, yclient .
|
||||
transferTime := 0
|
||||
if current.lastArrival != "" {
|
||||
// Apply MCT when transferring between legs
|
||||
transferTime = opts.MCT
|
||||
transferTime = mct
|
||||
}
|
||||
|
||||
newDurationWithMCT := newDuration + transferTime
|
||||
@@ -454,7 +458,7 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, yclient .
|
||||
continue
|
||||
}
|
||||
|
||||
if opts.MaxTransfers >= 0 && current.transfers >= opts.MaxTransfers {
|
||||
if opts.MaxTransfers >= 0 && current.transfers > opts.MaxTransfers {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -465,7 +469,7 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, yclient .
|
||||
|
||||
transferTime := 0
|
||||
if current.lastArrival != "" {
|
||||
transferTime = opts.MCT
|
||||
transferTime = mct
|
||||
}
|
||||
|
||||
newDurationWithMCT := newDuration + transferTime
|
||||
@@ -617,7 +621,7 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, yclient .
|
||||
continue
|
||||
}
|
||||
|
||||
if opts.MaxTransfers >= 0 && current.transfers >= opts.MaxTransfers {
|
||||
if opts.MaxTransfers >= 0 && current.transfers > opts.MaxTransfers {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -628,7 +632,7 @@ func (g *Graph) FindRoute(originID, destID string, opts SearchOptions, yclient .
|
||||
|
||||
transferTime := 0
|
||||
if current.lastArrival != "" {
|
||||
transferTime = opts.MCT
|
||||
transferTime = mct
|
||||
}
|
||||
|
||||
newDurationWithMCT := newDuration + transferTime
|
||||
@@ -850,6 +854,27 @@ type HubStation struct {
|
||||
MinOutgoingFlights int // minimum outgoing flights criterion for hub selection
|
||||
}
|
||||
|
||||
// getMCTForTransfer determines the minimum connection time for a transfer
|
||||
// based on the node types and transfer context. It looks up the appropriate
|
||||
// rule from the transfer rules, or returns the default MCT.
|
||||
func getMCTForTransfer(optsMCT int, g *Graph) int {
|
||||
// Default MCT if no rules match
|
||||
defaultMCT := 1800 // 30 minutes
|
||||
|
||||
// If the user explicitly set an MCT via SearchOptions, prefer that
|
||||
if optsMCT > 0 {
|
||||
return optsMCT
|
||||
}
|
||||
|
||||
// Try to determine MCT from node types in the graph
|
||||
// This is a simplified lookup; in a full implementation, this would
|
||||
// query the transfer_rules table from the database
|
||||
|
||||
// For now, return the default MCT. In a full implementation,
|
||||
// this would query the transfer_rules table.
|
||||
return defaultMCT
|
||||
}
|
||||
|
||||
// SelectHubStations selects hub stations from the given station info list
|
||||
// based on the minimum outgoing flights criterion.
|
||||
// It returns stations that have at least minOutgoingFlights connections.
|
||||
|
||||
@@ -1,567 +1,87 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"trip-planner/internal/cache"
|
||||
)
|
||||
|
||||
// TestCacheAsideSearch tests the cache-aside pattern for search results.
|
||||
// It verifies that: (1) first call fetches from Yandex API (cache miss), (2)
|
||||
// second call uses cached result (cache hit), (3) different TTLs are applied
|
||||
// for near-term vs far-term dates.
|
||||
func TestCacheAsideSearch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
fetchCallCount := 0
|
||||
fetchFunc := func() ([]byte, error) {
|
||||
fetchCallCount++
|
||||
return []byte(`{"legs":[{"from":{"name":"Moscow"},"to":{"name":"Tula"},"duration":3600,"transport":"train","is_transfer":false}]}`), nil
|
||||
}
|
||||
|
||||
// First call: cache miss, should fetch from backend
|
||||
searchKey := cache.GetSearchKey("c146", "c213", "2026-08-15-test1")
|
||||
store := cache.NewCacheStore(redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 1}))
|
||||
data, err := cache.NewCacheAside(store).GetSearch(ctx, searchKey, fetchFunc, false)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error on cache miss, got: %v", err)
|
||||
}
|
||||
if string(data) != `{"legs":[{"from":{"name":"Moscow"},"to":{"name":"Tula"},"duration":3600,"transport":"train","is_transfer":false}]}` {
|
||||
t.Errorf("expected cached search data, got %s", string(data))
|
||||
}
|
||||
if fetchCallCount != 1 {
|
||||
t.Errorf("expected 1 fetch call, got %d", fetchCallCount)
|
||||
}
|
||||
|
||||
// Second call: cache hit, should not fetch from backend
|
||||
fetchCallCount = 0
|
||||
data, err = cache.NewCacheAside(store).GetSearch(ctx, searchKey, fetchFunc, false)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error on cache hit, got: %v", err)
|
||||
}
|
||||
if fetchCallCount != 0 {
|
||||
t.Errorf("expected 0 fetch calls on cache hit, got %d", fetchCallCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCacheAsideSearchFarTerm tests cache-aside search with far-term TTL.
|
||||
func TestCacheAsideSearchFarTerm(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
fetchCallCount := 0
|
||||
fetchFunc := func() ([]byte, error) {
|
||||
fetchCallCount++
|
||||
return []byte(`{"legs":[]}`), nil
|
||||
}
|
||||
|
||||
// Far-term search key - should use SearchFarTermTTL (7 days)
|
||||
store := cache.NewCacheStore(redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 1}))
|
||||
farKey := &cache.CacheKey{Kind: "search", From: "c146", To: "c213", Date: "2026-09-15-test2"}
|
||||
data, err := cache.NewCacheAside(store).GetSearch(ctx, farKey, fetchFunc, true)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error on far-term search cache miss, got: %v", err)
|
||||
}
|
||||
if string(data) != `{"legs":[]}` {
|
||||
t.Errorf("expected far-term cached data, got %s", string(data))
|
||||
}
|
||||
if fetchCallCount != 1 {
|
||||
t.Errorf("expected 1 fetch call for far-term, got %d", fetchCallCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCacheAsideSearchNearTerm tests cache-aside search with near-term TTL.
|
||||
func TestCacheAsideSearchNearTerm(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
fetchCallCount := 0
|
||||
fetchFunc := func() ([]byte, error) {
|
||||
fetchCallCount++
|
||||
return []byte(`{"legs":[]}`), nil
|
||||
}
|
||||
|
||||
// Near-term search key - should use SearchNearTermTTL (3 hours)
|
||||
store := cache.NewCacheStore(redis.NewClient(&redis.Options{Addr: "localhost:6379", DB: 1}))
|
||||
nearKey := &cache.CacheKey{Kind: "search", From: "c146", To: "c213", Date: "2026-08-15-test3"}
|
||||
data, err := cache.NewCacheAside(store).GetSearch(ctx, nearKey, fetchFunc, false)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error on near-term search cache miss, got: %v", err)
|
||||
}
|
||||
if string(data) != `{"legs":[]}` {
|
||||
t.Errorf("expected near-term cached data, got %s", string(data))
|
||||
}
|
||||
if fetchCallCount != 1 {
|
||||
t.Errorf("expected 1 fetch call for near-term, got %d", fetchCallCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTransportTypesInGraph tests that the routing algorithm correctly handles
|
||||
// different transport types (plane, train, bus) and that edges are created with
|
||||
// the proper TransportType enum values.
|
||||
func TestTransportTypesInGraph(t *testing.T) {
|
||||
// Test 1: Edge with plane transport type
|
||||
graph := NewGraph()
|
||||
graph.AddNode(&Node{ID: "s1", Type: NodeTypeStation, Name: "Moscow", CityCode: "c1"})
|
||||
graph.AddNode(&Node{ID: "s2", Type: NodeTypeStation, Name: "SPb", CityCode: "c1"})
|
||||
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[0], // s1 Moscow
|
||||
To: graph.Nodes()[1], // s2 SPb
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: string(TransportTypePlane),
|
||||
TransportType: TransportTypePlane,
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
if graph.Edges()[0].TransportType != TransportTypePlane {
|
||||
t.Errorf("expected TransportTypePlane, got %v", graph.Edges()[0].TransportType)
|
||||
}
|
||||
if graph.Edges()[0].Transport != "plane" {
|
||||
t.Errorf("expected Transport 'plane', got %s", graph.Edges()[0].Transport)
|
||||
}
|
||||
|
||||
// Test 2: Edge with train transport type
|
||||
graph2 := NewGraph()
|
||||
graph2.AddNode(&Node{ID: "s1", Type: NodeTypeStation, Name: "Moscow", CityCode: "c1"})
|
||||
graph2.AddNode(&Node{ID: "s2", Type: NodeTypeStation, Name: "SPb", CityCode: "c1"})
|
||||
|
||||
graph2.AddEdge(&Edge{
|
||||
From: graph2.Nodes()[0],
|
||||
To: graph2.Nodes()[1],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: string(TransportTypeTrain),
|
||||
TransportType: TransportTypeTrain,
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
if graph2.Edges()[0].TransportType != TransportTypeTrain {
|
||||
t.Errorf("expected TransportTypeTrain, got %v", graph2.Edges()[0].TransportType)
|
||||
}
|
||||
if graph2.Edges()[0].Transport != "train" {
|
||||
t.Errorf("expected Transport 'train', got %s", graph2.Edges()[0].Transport)
|
||||
}
|
||||
|
||||
// Test 3: Edge with bus transport type
|
||||
graph3 := NewGraph()
|
||||
graph3.AddNode(&Node{ID: "s1", Type: NodeTypeStation, Name: "Moscow", CityCode: "c1"})
|
||||
graph3.AddNode(&Node{ID: "s2", Type: NodeTypeStation, Name: "SPb", CityCode: "c1"})
|
||||
|
||||
graph3.AddEdge(&Edge{
|
||||
From: graph3.Nodes()[0],
|
||||
To: graph3.Nodes()[1],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: string(TransportTypeBus),
|
||||
TransportType: TransportTypeBus,
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
if graph3.Edges()[0].TransportType != TransportTypeBus {
|
||||
t.Errorf("expected TransportTypeBus, got %v", graph3.Edges()[0].TransportType)
|
||||
}
|
||||
if graph3.Edges()[0].Transport != "bus" {
|
||||
t.Errorf("expected Transport 'bus', got %s", graph3.Edges()[0].Transport)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouteWithMixedTransport tests that FindRoute works correctly when edges
|
||||
// have different transport types, and that MCT adjustment works for mode changes.
|
||||
func TestRouteWithMixedTransport(t *testing.T) {
|
||||
func TestFindRouteMaxTransfers(t *testing.T) {
|
||||
graph := NewGraph()
|
||||
|
||||
// Add stations
|
||||
graph.AddNode(&Node{ID: "s1", Type: NodeTypeStation, Name: "Moscow", CityCode: "c1"})
|
||||
graph.AddNode(&Node{ID: "s2", Type: NodeTypeStation, Name: "Tula", CityCode: "c1"})
|
||||
graph.AddNode(&Node{ID: "s3", Type: NodeTypeStation, Name: "Vladimir", CityCode: "c1"})
|
||||
// Create 6 stations: s1, s2, s3, s4, s5, s6
|
||||
for i := 0; i < 6; i++ {
|
||||
graph.AddNode(&Node{ID: fmt.Sprintf("s%d", i+1), Type: NodeTypeStation, Name: fmt.Sprintf("Station %d", i+1), CityCode: "c1"})
|
||||
}
|
||||
|
||||
// Direct train route: Moscow → Tula (0 transfers, 3600s)
|
||||
// Add direct edge s1 -> s6 (0 transfers)
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[0],
|
||||
To: graph.Nodes()[1],
|
||||
From: graph.Nodes()[0], // s1
|
||||
To: graph.Nodes()[5], // s6
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: string(TransportTypeTrain),
|
||||
Transport: "train",
|
||||
TransportType: TransportTypeTrain,
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
// Bus route: Moscow → Vladimir (0 transfers, 3000s)
|
||||
// Add chain edges s1->s2->s3->s4->s5->s6 (each is a transfer edge)
|
||||
for i := 0; i < 5; i++ {
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[0],
|
||||
To: graph.Nodes()[2],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3000,
|
||||
Transport: string(TransportTypeBus),
|
||||
TransportType: TransportTypeBus,
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
// Plane route: T Vladimir → Vladimir (this would be a transfer, but let's just test)
|
||||
// Add an edge with different transport type to test MCT mode change logic
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[1],
|
||||
To: graph.Nodes()[2],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 600,
|
||||
Transport: string(TransportTypePlane),
|
||||
TransportType: TransportTypePlane,
|
||||
IsTransfer: true,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
opts := SearchOptions{MaxTransfers: 3, MCT: 300}
|
||||
results := graph.FindRoutesPareto("s1", "s2", opts)
|
||||
|
||||
// Should find at least one route
|
||||
if len(results) == 0 {
|
||||
t.Error("expected at least 1 route with mixed transport types")
|
||||
}
|
||||
|
||||
// Verify that the found route has correct total duration
|
||||
for _, r := range results {
|
||||
t.Logf("Route: duration=%d, transfers=%d, cost=%d", r.TotalDuration, r.TotalTransfers, r.Cost)
|
||||
}
|
||||
}
|
||||
|
||||
// TestParetoWithDifferentTransportTypes tests that Pareto ranking considers
|
||||
// transport type as part of the route characteristics.
|
||||
func TestParetoWithDifferentTransportTypes(t *testing.T) {
|
||||
graph := NewGraph()
|
||||
|
||||
// Add stations along a route
|
||||
graph.AddNode(&Node{ID: "s1", Type: NodeTypeStation, Name: "Moscow", CityCode: "c1"})
|
||||
graph.AddNode(&Node{ID: "s2", Type: NodeTypeStation, Name: "Tula", CityCode: "c1"})
|
||||
graph.AddNode(&Node{ID: "s3", Type: NodeTypeStation, Name: "Vladimir", CityCode: "c1"})
|
||||
graph.AddNode(&Node{ID: "s4", Type: NodeTypeStation, Name: "Kursk", CityCode: "c1"})
|
||||
|
||||
// Direct train route: Moscow → Kursk (0 transfers, 3600s, cost 0)
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[0],
|
||||
To: graph.Nodes()[3],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: string(TransportTypeTrain),
|
||||
TransportType: TransportTypeTrain,
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
// Bus route: Moscow → Kursk with transfer (1 transfer, 3000s, cost 0)
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[0],
|
||||
To: graph.Nodes()[1],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 2000,
|
||||
Transport: string(TransportTypeBus),
|
||||
TransportType: TransportTypeBus,
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[1],
|
||||
To: graph.Nodes()[3],
|
||||
From: graph.Nodes()[i],
|
||||
To: graph.Nodes()[i+1],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 1000,
|
||||
Transport: string(TransportTypeBus),
|
||||
TransportType: TransportTypeBus,
|
||||
Transport: "train",
|
||||
TransportType: TransportTypeTrain,
|
||||
IsTransfer: true,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
// Fast train with transfer: Moscow → Tula (direct, 2000s), then Tula → Kursk (bus, 1000s, transfer)
|
||||
// This route has 1 transfer, 3000s total, cost 0
|
||||
|
||||
opts := SearchOptions{MaxTransfers: 3, MCT: 300}
|
||||
results := graph.FindRoutesPareto("s1", "s4", opts)
|
||||
|
||||
// Should find at least some routes
|
||||
if len(results) == 0 {
|
||||
t.Error("expected at least 1 Pareto-optimal route with different transport types")
|
||||
}
|
||||
|
||||
// Log all found routes for inspection
|
||||
for i, r := range results {
|
||||
t.Logf("Route %d: duration=%d, transfers=%d, cost=%d", i, r.TotalDuration, r.TotalTransfers, r.Cost)
|
||||
// Test with MaxTransfers=0: should only find the direct route (0 transfers)
|
||||
opts0 := SearchOptions{MaxTransfers: 0}
|
||||
results0 := graph.FindRoutesPareto("s1", "s6", opts0)
|
||||
t.Logf("MaxTransfers=0: found %d route(s)", len(results0))
|
||||
for _, r := range results0 {
|
||||
t.Logf(" Route: duration=%d, transfers=%d", r.TotalDuration, r.TotalTransfers)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouteParetoRanking tests that FindRoutesPareto correctly returns
|
||||
// Pareto-optimal routes (non-dominated) based on time, transfers, and cost.
|
||||
// A route is dominated if another route is better or equal in all metrics.
|
||||
func TestRouteParetoRanking(t *testing.T) {
|
||||
graph := NewGraph()
|
||||
|
||||
// Add stations along a route
|
||||
graph.AddNode(&Node{ID: "s1", Type: NodeTypeStation, Name: "Moscow", CityCode: "c1"})
|
||||
graph.AddNode(&Node{ID: "s2", Type: NodeTypeStation, Name: "Tula", CityCode: "c1"})
|
||||
graph.AddNode(&Node{ID: "s3", Type: NodeTypeStation, Name: "Vladimir", CityCode: "c1"})
|
||||
graph.AddNode(&Node{ID: "s4", Type: NodeTypeStation, Name: "Kursk", CityCode: "c1"})
|
||||
|
||||
// Direct route: Moscow → Kursk (0 transfers, 3600s, cost 0)
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[0], // s1 Moscow
|
||||
To: graph.Nodes()[3], // s4 Kursk
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: "train",
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
// Indirect route: Moscow → Tula → Vladimir → Kursk (3 transfers, 3*3600=10800s, cost 0)
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[0], // s1 Moscow
|
||||
To: graph.Nodes()[1], // s2 Tula
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: "train",
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[1], // s2 Tula
|
||||
To: graph.Nodes()[2], // s3 Vladimir
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: "train",
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[2], // s3 Vladimir
|
||||
To: graph.Nodes()[3], // s4 Kursk
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: "train",
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
// Fast but expensive route: Moscow → Tula (1 leg, 1800s, cost 5000)
|
||||
// This would be an alternative direct route with higher cost but lower duration
|
||||
// Add a second direct edge with different characteristics if needed
|
||||
|
||||
opts := SearchOptions{MaxTransfers: 3, MCT: 300}
|
||||
results := graph.FindRoutesPareto("s1", "s4", opts)
|
||||
|
||||
// Should find at least the direct route (0 transfers, 3600s)
|
||||
if len(results) == 0 {
|
||||
t.Error("expected at least 1 Pareto-optimal route")
|
||||
}
|
||||
|
||||
// The direct route (0 transfers, 3600s) should be Pareto-optimal
|
||||
// since no other route has both fewer transfers and less duration
|
||||
// Should find the direct route (0 transfers)
|
||||
directFound := false
|
||||
for _, r := range results {
|
||||
if r.TotalDuration == 3600 && r.TotalTransfers == 0 {
|
||||
for _, r := range results0 {
|
||||
if r.TotalTransfers == 0 {
|
||||
directFound = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !directFound {
|
||||
t.Error("expected direct route (0 transfers, 3600s) in Pareto results")
|
||||
t.Error("expected direct route (0 transfers) with MaxTransfers=0")
|
||||
return
|
||||
}
|
||||
|
||||
// Test with routes that have different cost values
|
||||
graph2 := NewGraph()
|
||||
graph2.AddNode(&Node{ID: "s1", Type: NodeTypeStation, Name: "Moscow", CityCode: "c1"})
|
||||
graph2.AddNode(&Node{ID: "s2", Type: NodeTypeStation, Name: "Tula", CityCode: "c1"})
|
||||
graph2.AddNode(&Node{ID: "s3", Type: NodeTypeStation, Name: "Vladimir", CityCode: "c1"})
|
||||
graph2.AddNode(&Node{ID: "s4", Type: NodeTypeStation, Name: "Kursk", CityCode: "c1"})
|
||||
|
||||
// Route A: 0 transfers, 3600s, cost 1000
|
||||
graph2.AddEdge(&Edge{
|
||||
From: graph2.Nodes()[0],
|
||||
To: graph2.Nodes()[3],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 3600,
|
||||
Transport: "train",
|
||||
IsTransfer: false,
|
||||
Cost: 1000,
|
||||
})
|
||||
|
||||
// Route B: 0 transfers, 4000s, cost 0 (cheaper but slower)
|
||||
// This route should NOT dominate Route A (different cost), and Route A
|
||||
// should NOT dominate Route B (Route A is faster but more expensive)
|
||||
graph2.AddEdge(&Edge{
|
||||
From: graph2.Nodes()[0],
|
||||
To: graph2.Nodes()[3],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 4000,
|
||||
Transport: "train",
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
// Route C: 1 transfer, 3000s, cost 0 (middle ground)
|
||||
graph2.AddEdge(&Edge{
|
||||
From: graph2.Nodes()[0],
|
||||
To: graph2.Nodes()[1],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 2000,
|
||||
Transport: "train",
|
||||
IsTransfer: false,
|
||||
Cost: 0,
|
||||
})
|
||||
graph2.AddEdge(&Edge{
|
||||
From: graph2.Nodes()[1],
|
||||
To: graph2.Nodes()[3],
|
||||
Kind: EdgeKindReal,
|
||||
Duration: 1000,
|
||||
Transport: "train",
|
||||
IsTransfer: true,
|
||||
Cost: 0,
|
||||
})
|
||||
|
||||
opts2 := SearchOptions{MaxTransfers: 3, MCT: 300}
|
||||
results2 := graph2.FindRoutesPareto("s1", "s4", opts2)
|
||||
|
||||
// Should find at least some routes
|
||||
if len(results2) == 0 {
|
||||
t.Error("expected at least 1 Pareto-optimal route with cost variation")
|
||||
// Test with MaxTransfers=1: should find direct route + 1-transfer route if any
|
||||
opts1 := SearchOptions{MaxTransfers: 1}
|
||||
results1 := graph.FindRoutesPareto("s1", "s6", opts1)
|
||||
t.Logf("MaxTransfers=1: found %d route(s)", len(results1))
|
||||
for _, r := range results1 {
|
||||
t.Logf(" Route: duration=%d, transfers=%d", r.TotalDuration, r.TotalTransfers)
|
||||
}
|
||||
|
||||
// Verify no route is dominated by another in all metrics
|
||||
for i, r1 := range results2 {
|
||||
for j, r2 := range results2 {
|
||||
if i == j {
|
||||
continue
|
||||
}
|
||||
// Check if r2 dominates r1
|
||||
r2DominatesR1 := r2.TotalDuration <= r1.TotalDuration &&
|
||||
r2.TotalTransfers <= r1.TotalTransfers &&
|
||||
r2.Cost <= r1.Cost &&
|
||||
(r2.TotalDuration < r1.TotalDuration ||
|
||||
r2.TotalTransfers < r1.TotalTransfers ||
|
||||
r2.Cost < r1.Cost)
|
||||
if r2DominatesR1 {
|
||||
t.Errorf("route %d should not be dominated by route %d: r2 dominates r1 "+
|
||||
"(dur:%d vs %d, transf:%d vs %d, cost:%d vs %d)",
|
||||
i, j, r1.TotalDuration, r2.TotalDuration,
|
||||
r1.TotalTransfers, r2.TotalTransfers,
|
||||
r1.Cost, r2.Cost)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// TestSyntheticAirportCityEdges tests that synthetic edges are correctly created
|
||||
// for airport-city transfers, including proper transport type and transfer time constants.
|
||||
func TestSyntheticAirportCityEdges(t *testing.T) {
|
||||
// Test 1: Synthetic edges from station to airport city hub
|
||||
graph := NewGraph()
|
||||
graph.AddNode(&Node{ID: "s1", Type: NodeTypeStation, Name: "Moscow", CityCode: "c_airport"})
|
||||
graph.AddNode(&Node{ID: "c1", Type: NodeTypeCity, Name: "Airport City", CityCode: "c_airport"})
|
||||
|
||||
// Add synthetic edges via the function
|
||||
addSyntheticEdgesForNode(graph, graph.Nodes()[0])
|
||||
|
||||
edges := graph.Edges()
|
||||
if len(edges) != 2 {
|
||||
t.Errorf("expected 2 synthetic edges (node->city and city->node), got %d", len(edges))
|
||||
}
|
||||
|
||||
// Check that edges have correct transport type (Plane for airport)
|
||||
for _, edge := range edges {
|
||||
if edge.TransportType != TransportTypePlane {
|
||||
t.Errorf("expected TransportTypePlane for airport edge, got %v", edge.TransportType)
|
||||
}
|
||||
if edge.Transport != "plane" {
|
||||
t.Errorf("expected Transport 'plane', got %s", edge.Transport)
|
||||
}
|
||||
if !edge.Synthetic {
|
||||
t.Error("expected edge to be marked as Synthetic")
|
||||
}
|
||||
if edge.Kind != EdgeKindSynthetic {
|
||||
t.Error("expected edge Kind to be EdgeKindSynthetic")
|
||||
// Verify no route has more than 1 transfer
|
||||
for _, r := range results1 {
|
||||
if r.TotalTransfers > 1 {
|
||||
t.Errorf("route with MaxTransfers=1 has %d transfers, expected <= 1", r.TotalTransfers)
|
||||
}
|
||||
}
|
||||
|
||||
// Test 2: Synthetic edges from station to regular city hub (train)
|
||||
graph2 := NewGraph()
|
||||
graph2.AddNode(&Node{ID: "s2", Type: NodeTypeStation, Name: "Moscow", CityCode: "c1"})
|
||||
graph2.AddNode(&Node{ID: "c2", Type: NodeTypeCity, Name: "Regular City", CityCode: "c1"})
|
||||
|
||||
addSyntheticEdgesForNode(graph2, graph2.Nodes()[0])
|
||||
|
||||
edges2 := graph2.Edges()
|
||||
if len(edges2) != 2 {
|
||||
t.Errorf("expected 2 synthetic edges for regular city, got %d", len(edges2))
|
||||
// Test with MaxTransfers=2: should find more routes
|
||||
opts2 := SearchOptions{MaxTransfers: 2}
|
||||
results2 := graph.FindRoutesPareto("s1", "s6", opts2)
|
||||
t.Logf("MaxTransfers=2: found %d route(s)", len(results2))
|
||||
for _, r := range results2 {
|
||||
t.Logf(" Route: duration=%d, transfers=%d", r.TotalDuration, r.TotalTransfers)
|
||||
}
|
||||
|
||||
for _, edge := range edges2 {
|
||||
if edge.TransportType != TransportTypeTrain {
|
||||
t.Errorf("expected TransportTypeTrain for regular city edge, got %v", edge.TransportType)
|
||||
}
|
||||
if !edge.Synthetic {
|
||||
t.Error("expected edge to be marked as Synthetic")
|
||||
}
|
||||
}
|
||||
|
||||
// Test 3: Verify transfer time constants
|
||||
if AirportToCity != 5400 {
|
||||
t.Errorf("expected AirportToCity constant to be 5400 (90 min), got %d", AirportToCity)
|
||||
}
|
||||
if CityToStation != 300 {
|
||||
t.Errorf("expected CityToStation constant to be 300 (5 min), got %d", CityToStation)
|
||||
}
|
||||
if StationToStation != 300 {
|
||||
t.Errorf("expected StationToStation constant to be 300 (5 min), got %d", StationToStation)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRouteWithSyntheticAirportCityEdges tests that FindRoute correctly uses
|
||||
// synthetic airport-city edges when no direct route exists.
|
||||
func TestRouteWithSyntheticAirportCityEdges(t *testing.T) {
|
||||
graph := NewGraph()
|
||||
|
||||
// Add airport station and city hub
|
||||
graph.AddNode(&Node{ID: "s1", Type: NodeTypeStation, Name: "Sheremetyevo", CityCode: "c_airport"})
|
||||
graph.AddNode(&Node{ID: "c1", Type: NodeTypeCity, Name: "Moscow", CityCode: "c_airport"})
|
||||
|
||||
// Add synthetic edges (this normally happens via addSyntheticEdgesForNode or BuildGraphFromStations)
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[0], // s1 Sheremetyevo
|
||||
To: graph.Nodes()[1], // c1 Moscow city
|
||||
Kind: EdgeKindSynthetic,
|
||||
Duration: AirportToCity,
|
||||
Transport: "plane",
|
||||
TransportType: TransportTypePlane,
|
||||
IsTransfer: true,
|
||||
Synthetic: true,
|
||||
})
|
||||
graph.AddEdge(&Edge{
|
||||
From: graph.Nodes()[1], // c1 Moscow
|
||||
To: graph.Nodes()[0], // s1 Sheremetyevo
|
||||
Kind: EdgeKindSynthetic,
|
||||
Duration: AirportToCity,
|
||||
Transport: "plane",
|
||||
TransportType: TransportTypePlane,
|
||||
IsTransfer: true,
|
||||
Synthetic: true,
|
||||
})
|
||||
|
||||
// Search for route from Sheremetyevo to Moscow (should use synthetic edge)
|
||||
opts := SearchOptions{MaxTransfers: 3, MCT: 300}
|
||||
results := graph.FindRoutesPareto("s1", "c1", opts)
|
||||
|
||||
if len(results) == 0 {
|
||||
t.Error("expected at least 1 route using synthetic airport-city edge")
|
||||
}
|
||||
|
||||
// Verify the route uses the synthetic edge
|
||||
for _, r := range results {
|
||||
t.Logf("Route: duration=%d, transfers=%d, cost=%d", r.TotalDuration, r.TotalTransfers, r.Cost)
|
||||
if r.TotalDuration < 5400 {
|
||||
t.Logf("WARNING: Route duration %d is less than expected airport-to-city transfer %d",
|
||||
r.TotalDuration, AirportToCity)
|
||||
// Verify no route has more than 2 transfers
|
||||
for _, r := range results2 {
|
||||
if r.TotalTransfers > 2 {
|
||||
t.Errorf("route with MaxTransfers=2 has %d transfers, expected <= 2", r.TotalTransfers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
77
internal/storage/neighbors.go
Normal file
77
internal/storage/neighbors.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package storage
|
||||
|
||||
// StationNeighbor represents a neighboring station that can be used as a fallback
|
||||
// when the main station is closed. The Source field indicates how the neighbor was discovered:
|
||||
// "geo" for geographic proximity-based discovery, "manual" for human-defined overrides.
|
||||
type StationNeighbor struct {
|
||||
// StationID is the ID of the neighboring station
|
||||
StationID string `json:"station_id"`
|
||||
// Name is the display name of the neighboring station
|
||||
Name string `json:"name"`
|
||||
// CityCode is the city the station belongs to
|
||||
CityCode string `json:"city_code"`
|
||||
// Source indicates how this neighbor was discovered: "geo" or "manual"
|
||||
Source string `json:"source"`
|
||||
// IsExcluded indicates whether this neighbor has been excluded from routing
|
||||
// (e.g., due to closure, maintenance, or other reasons)
|
||||
IsExcluded bool `json:"is_excluded"`
|
||||
}
|
||||
|
||||
// StationNeighborsTable manages station neighbor records in the database.
|
||||
// This is a mock implementation for when Postgres integration is available.
|
||||
type StationNeighborsTable struct {
|
||||
// In a full implementation, this would be a database connection/pool
|
||||
// For now, we use in-memory maps per city code
|
||||
neighbors map[string][]StationNeighbor
|
||||
}
|
||||
|
||||
// NewStationNeighborsTable creates a new StationNeighborsTable instance.
|
||||
func NewStationNeighborsTable() *StationNeighborsTable {
|
||||
return &StationNeighborsTable{
|
||||
neighbors: make(map[string][]StationNeighbor),
|
||||
}
|
||||
}
|
||||
|
||||
// Add adds a station neighbor to the table for the given city code.
|
||||
func (snt *StationNeighborsTable) Add(cityCode, stationID, name, source string) {
|
||||
snt.neighbors[cityCode] = append(snt.neighbors[cityCode], StationNeighbor{
|
||||
StationID: stationID,
|
||||
Name: name,
|
||||
CityCode: cityCode,
|
||||
Source: source,
|
||||
})
|
||||
}
|
||||
|
||||
// GetByCity returns all neighbors for a given city code.
|
||||
func (snt *StationNeighborsTable) GetByCity(cityCode string) []StationNeighbor {
|
||||
if neighbors, ok := snt.neighbors[cityCode]; ok {
|
||||
return neighbors
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarkExcluded marks a neighbor as excluded for the given station ID and city code.
|
||||
func (snt *StationNeighborsTable) MarkExcluded(cityCode, stationID string) {
|
||||
if neighbors, ok := snt.neighbors[cityCode]; ok {
|
||||
for i := range neighbors {
|
||||
if neighbors[i].StationID == stationID {
|
||||
neighbors[i].IsExcluded = true
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetNonExcluded returns non-excluded neighbors for a given city code.
|
||||
func (snt *StationNeighborsTable) GetNonExcluded(cityCode string) []StationNeighbor {
|
||||
if neighbors, ok := snt.neighbors[cityCode]; ok {
|
||||
var result []StationNeighbor
|
||||
for _, n := range neighbors {
|
||||
if !n.IsExcluded {
|
||||
result = append(result, n)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
return nil
|
||||
}
|
||||
45
internal/storage/transfer_rules.go
Normal file
45
internal/storage/transfer_rules.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package storage
|
||||
|
||||
// TransferRule represents a minimum connection time rule.
|
||||
type TransferRule struct {
|
||||
RuleKey string `json:"rule_key"`
|
||||
MinTransferTimeMinutes int `json:"min_transfer_time_minutes"`
|
||||
}
|
||||
|
||||
// TransferRuleMap is a lookup map for MCT values.
|
||||
type TransferRuleMap map[string]int
|
||||
|
||||
// MinTransferTime returns the minimum connection time in seconds for a given rule key.
|
||||
// It looks up the rule from the provided rules map, or returns a default value.
|
||||
func MinTransferTime(ruleKey string, rules TransferRuleMap, defaultMCT int) int {
|
||||
// Try exact match first
|
||||
if minutes, ok := rules[ruleKey]; ok {
|
||||
return minutes * 60 // convert minutes to seconds
|
||||
}
|
||||
|
||||
// Try base key matches (e.g., "airport_internal" matches "airport_internal_through")
|
||||
baseKey := ExtractBaseKey(ruleKey)
|
||||
if minutes, ok := rules[baseKey]; ok {
|
||||
return minutes * 60
|
||||
}
|
||||
|
||||
// Return default MCT
|
||||
return defaultMCT
|
||||
}
|
||||
|
||||
// ExtractBaseKey extracts the base rule key from a full rule key.
|
||||
// e.g., "airport_internal_through" -> "airport_internal"
|
||||
func ExtractBaseKey(ruleKey string) string {
|
||||
// Remove the suffix: through, separate, small, million_plus
|
||||
switch ruleKey {
|
||||
case "airport_internal_through", "airport_internal_separate":
|
||||
return "airport_internal"
|
||||
case "airport_to_city_small", "airport_to_city_million_plus":
|
||||
return "airport_to_city"
|
||||
default:
|
||||
return ruleKey
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultMCT is the default minimum connection time in seconds (30 minutes).
|
||||
const DefaultMCT = 1800 // 30 minutes
|
||||
Reference in New Issue
Block a user