Files
trip-planner/internal/storage/transfer_rules.go
Vladimir Zagainov 6dcec6a7a5 fix: address code review findings
- Fix type mismatch in search_cache.go: SearchWithCache now returns *yandex.Response instead of *Itinerary
- Fix token bucket refill logic in yandex/client.go to properly accumulate tokens based on refillPerSec
- Fix hardcoded API key in main.go to load from YANDEX_API_KEY environment variable
- Fix missing error handling for JSON encoding in handlers.go RouteGeoJSON function
- Fix incorrect redis.Nil handling in cache/store.go CacheAside.Exists method
- Fix incorrect error return type in station_status.go updateStationStatus to return newStatus instead of empty string
2026-08-17 22:42:08 +03:00

46 lines
1.5 KiB
Go

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