Skip to content

Commit

Permalink
fix: add max default to numItineraries and standardise distance
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayanth-Parthsarathy committed Oct 11, 2024
1 parent 0f34c9f commit f747f72
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ getTransitRoutes cfg req = do
let dateTime = req.departureTime <&> formatUtcDateTime
let planClient = fromString (showBaseUrl cfg.baseUrl)
let transportModes' = req.transportModes
let numItineraries' = Just 7
resp <-
liftIO $
planClient
Expand All @@ -50,7 +51,8 @@ getTransitRoutes cfg req = do
to = destination,
date = fst <$> dateTime,
time = snd <$> dateTime,
transportModes = transportModes'
transportModes = transportModes',
numItineraries = numItineraries'
}
>>= single
case resp of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import qualified Kernel.External.Maps.Google.Config as Google
import qualified Kernel.External.Maps.Google.MapsClient.Types as GT
import qualified Kernel.External.MultiModal.OpenTripPlanner.Config as OTP
import qualified Kernel.External.MultiModal.OpenTripPlanner.Types as OTPTypes
import qualified Kernel.Types.Distance as Distance

newtype MultiModalResponse = MultiModalResponse {routes :: [MultiModalRoute]}
deriving (Show, Generic)

data MultiModalRoute = MultiModalRoute
{ distance :: Double,
{ distance :: Distance.Distance,
duration :: Int,
legs :: [MultiModalLeg]
}
Expand All @@ -38,7 +39,7 @@ data MultiModalAgency = MultiModalAgency
deriving (Show, Generic)

data MultiModalLeg = MultiModalLeg
{ distance :: Double,
{ distance :: Distance.Distance,
duration :: Double,
polyline :: GT.Polyline,
mode :: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ instance ToJSON TransportMode where
instance RequestType OTPPlan where
type RequestArgs OTPPlan = OTPPlanArgs
__name _ = "OTPPlan"
__query _ = "query OTPPlan (\n $from: InputCoordinates!,\n $to: InputCoordinates!,\n $date: String,\n $time:String,\n $transportModes: [TransportMode]\n){\n plan(\n from: $from,\n to: $to,\n date: $date,\n time: $time,\n transportModes: $transportModes\n ) {\n itineraries {\n duration\n legs {\n pickupType\n distance\n mode\n duration\n startTime\n endTime\n from {\n name\n lat\n lon\n departureTime\n arrivalTime\n stop {\n code\n }\n }\n to {\n name\n lat\n lon\n departureTime\n arrivalTime\n stop {\n code\n }\n }\n route {\n gtfsId\n longName\n shortName\n agency {\n gtfsId\n name\n }\n }\n legGeometry {\n points\n }\n fareProducts {\n id\n }\n }\n }\n }\n}\n"
__query _ = "query OTPPlan (\n $from: InputCoordinates!,\n $to: InputCoordinates!,\n $date: String,\n $time:String,\n $transportModes: [TransportMode],\n $numItineraries: Int\n){\n plan(\n from: $from,\n to: $to,\n date: $date,\n time: $time,\n transportModes: $transportModes,\n numItineraries : $numItineraries\n ) {\n itineraries {\n duration\n legs {\n pickupType\n distance\n mode\n duration\n startTime\n endTime\n from {\n name\n lat\n lon\n departureTime\n arrivalTime\n stop {\n code\n }\n }\n to {\n name\n lat\n lon\n departureTime\n arrivalTime\n stop {\n code\n }\n }\n route {\n gtfsId\n longName\n shortName\n agency {\n gtfsId\n name\n }\n }\n legGeometry {\n points\n }\n fareProducts {\n id\n }\n }\n }\n }\n}\n"
__type _ = OPERATION_QUERY

newtype OTPPlan = OTPPlan
Expand Down Expand Up @@ -179,16 +179,18 @@ data OTPPlanArgs = OTPPlanArgs
to :: InputCoordinates,
date :: Maybe String,
time :: Maybe String,
transportModes :: Maybe [Maybe TransportMode]
transportModes :: Maybe [Maybe TransportMode],
numItineraries :: Maybe Int
}
deriving (Generic, Show, Eq)

instance ToJSON OTPPlanArgs where
toJSON (OTPPlanArgs oTPPlanArgsFrom oTPPlanArgsTo oTPPlanArgsDate oTPPlanArgsTime oTPPlanArgsTransportModes) =
toJSON (OTPPlanArgs oTPPlanArgsFrom oTPPlanArgsTo oTPPlanArgsDate oTPPlanArgsTime oTPPlanArgsTransportModes oTPPlanArgsNumItineraries) =
omitNulls
[ "from" .= oTPPlanArgsFrom,
"to" .= oTPPlanArgsTo,
"date" .= oTPPlanArgsDate,
"time" .= oTPPlanArgsTime,
"transportModes" .= oTPPlanArgsTransportModes
"transportModes" .= oTPPlanArgsTransportModes,
"numItineraries" .= oTPPlanArgsNumItineraries
]
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ query OTPPlan (
$to: InputCoordinates!,
$date: String,
$time:String,
$transportModes: [TransportMode]
$transportModes: [TransportMode],
$numItineraries: Int
){
plan(
from: $from,
to: $to,
date: $date,
time: $time,
transportModes: $transportModes
transportModes: $transportModes,
numItineraries : $numItineraries
) {
itineraries {
duration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Place {
lon: Float!
departureTime: Float!
arrivalTime: Float!
stop : Stop
}

type Route {
Expand All @@ -65,12 +66,17 @@ type Agency {
gtfsId: String!
}

type Stop {
code : String
}

type Query {
plan(
from: InputCoordinates!
to: InputCoordinates!
transportModes: [TransportMode]
date: String
time: String
numItineraries: Int
): Plan!
}
50 changes: 43 additions & 7 deletions lib/mobility-core/src/Kernel/External/MultiModal/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Kernel.External.Maps.Google.PolyLinePoints (oneCoordEnc, stringToCoords)
import Kernel.External.MultiModal.Interface.Types
import qualified Kernel.External.MultiModal.OpenTripPlanner.Types as OTP
import Kernel.Prelude
import qualified Kernel.Types.Distance as Distance
import Kernel.Utils.Time (millisecondsToUTC, parseISO8601UTC)

extractDuration :: T.Text -> Int
Expand Down Expand Up @@ -55,11 +56,18 @@ convertGoogleToGeneric gResponse =
accumulateRoutes :: GT.RouteV2 -> [MultiModalRoute] -> [MultiModalRoute]
accumulateRoutes gRoute genericRoutes =
let routeDuration = extractDuration gRoute.duration
routeDistance = gRoute.distanceMeters
routeDistance =
Distance.Distance
{ value =
Distance.HighPrecDistance
{ getHighPrecDistance = toRational gRoute.distanceMeters
},
unit = Distance.Meter
}
gLegs = gRoute.legs
routeLegs = adjustWalkingLegs $ mergeWalkingLegs $ foldr accumulateLegs [] gLegs
in MultiModalRoute
{ distance = fromIntegral routeDistance,
{ distance = routeDistance,
duration = routeDuration,
legs = routeLegs
} :
Expand All @@ -70,8 +78,15 @@ convertGoogleToGeneric gResponse =
in genericLegs ++ foldr accumulateSteps [] gSteps
accumulateSteps :: GT.StepV2 -> [MultiModalLeg] -> [MultiModalLeg]
accumulateSteps gStep genericLegs =
let stepDistance = fromIntegral gStep.distanceMeters
stepDuration = extractDuration gStep.staticDuration
let stepDuration = extractDuration gStep.staticDuration
stepDistance =
Distance.Distance
{ value =
Distance.HighPrecDistance
{ getHighPrecDistance = toRational gStep.distanceMeters
},
unit = Distance.Meter
}
stepPolyline = GT.encodedPolyline gStep.polyline
(stepTravelMode, genericAgency, fromStopDetails', toStopDetails', fromArrivalTime', fromDepartureTime', toArrivalTime', toDepartureTime') = case gStep.travelMode of
GT.TRANSIT ->
Expand Down Expand Up @@ -156,7 +171,14 @@ convertGoogleToGeneric gResponse =
leg2End = leg2.endLocation
encodedPolylineText = encode [leg1Start.latLng, leg2Start.latLng, leg2End.latLng]
in MultiModalLeg
{ distance = leg1.distance + leg2.distance,
{ distance =
Distance.Distance
{ value =
Distance.HighPrecDistance
{ getHighPrecDistance = fromRational leg1.distance.value.getHighPrecDistance + fromRational leg2.distance.value.getHighPrecDistance
},
unit = leg1.distance.unit
},
duration = leg1.duration + leg2.duration,
polyline = GT.Polyline {encodedPolyline = encodedPolylineText},
mode = "WALK",
Expand Down Expand Up @@ -202,7 +224,14 @@ convertOTPToGeneric otpResponse =
route =
MultiModalRoute
{ duration = round duration,
distance = distance,
distance =
Distance.Distance
{ value =
Distance.HighPrecDistance
{ getHighPrecDistance = toRational distance
},
unit = Distance.Meter
},
legs = legs
}
in route : genericRoutes
Expand Down Expand Up @@ -258,7 +287,14 @@ convertOTPToGeneric otpResponse =
}
leg =
MultiModalLeg
{ distance = distance,
{ distance =
Distance.Distance
{ value =
Distance.HighPrecDistance
{ getHighPrecDistance = toRational distance
},
unit = Distance.Meter
},
duration = duration,
polyline =
GT.Polyline
Expand Down

0 comments on commit f747f72

Please sign in to comment.