Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: restructure multimodal module #650

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/mobility-core/mobility-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ library
Kernel.External.Maps.Interface.Google
Kernel.External.Maps.Interface.MMI
Kernel.External.Maps.Interface.NextBillion
Kernel.External.Maps.Interface.OpenTripPlanner
Kernel.External.Maps.Interface.OSRM
Kernel.External.Maps.Interface.Types
Kernel.External.Maps.MMI.AutoSuggest
Expand All @@ -104,13 +103,17 @@ library
Kernel.External.Maps.NextBillion.Config
Kernel.External.Maps.NextBillion.Route
Kernel.External.Maps.NextBillion.Types
Kernel.External.Maps.OpenTripPlanner.Config
Kernel.External.Maps.OpenTripPlanner.Types
Kernel.External.Maps.OSRM.Config
Kernel.External.Maps.OSRM.RoadsClient
Kernel.External.Maps.Types
Kernel.External.Maps.Utils
Kernel.External.MultiModal.Common.Polyline
Kernel.External.MultiModal
Kernel.External.MultiModal.Interface
Kernel.External.MultiModal.Interface.Google
Kernel.External.MultiModal.Interface.OpenTripPlanner
Kernel.External.MultiModal.Interface.Types
Kernel.External.MultiModal.OpenTripPlanner.Config
Kernel.External.MultiModal.OpenTripPlanner.Types
Kernel.External.MultiModal.Types
Kernel.External.MultiModal.Utils
Kernel.External.Notification
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ data TransitDirectionsReq = TransitDirectionsReq
arrivalTime :: Maybe String, -- yyyy-mm-ddThh:mm:ssZ
departureTime :: Maybe String
}
-----------remove null fields-----------
deriving (Generic, ToJSON, FromJSON, ToSchema)

data AdvancedDirectionsReq = AdvancedDirectionsReq
Expand Down Expand Up @@ -351,7 +350,7 @@ instance ToJSON VehicleV2 where
newtype Polyline = Polyline
{ encodedPolyline :: PolyLinePoints
}
deriving (Generic, ToJSON, FromJSON, ToSchema)
deriving (Generic, ToJSON, FromJSON, ToSchema, Show)

newtype EncodedPointObject = EncodedPointObject {points :: PolyLinePoints}
deriving stock (Generic)
Expand Down
15 changes: 0 additions & 15 deletions lib/mobility-core/src/Kernel/External/Maps/Interface.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module Kernel.External.Maps.Interface
getDistances,
getRoutesProvided,
getRoutes,
getTransitRoutes,
snapToRoadProvided,
snapToRoad,
snapToRoadWithFallback,
Expand All @@ -39,12 +38,10 @@ import qualified Kernel.External.Maps.Interface.Google as Google
import qualified Kernel.External.Maps.Interface.MMI as MMI
import qualified Kernel.External.Maps.Interface.NextBillion as NextBillion
import qualified Kernel.External.Maps.Interface.OSRM as OSRM
import qualified Kernel.External.Maps.Interface.OpenTripPlanner as OTP
import Kernel.External.Maps.Interface.Types as Reexport
import Kernel.External.Maps.MMI.Config as Reexport
import Kernel.External.Maps.OSRM.Config as Reexport
import Kernel.External.Maps.Types as Reexport
import Kernel.External.MultiModal.Types
import Kernel.Prelude
import Kernel.Storage.Hedis as Redis
import Kernel.Tools.Metrics.CoreMetrics (CoreMetrics)
Expand Down Expand Up @@ -128,18 +125,6 @@ getRoutes isAvoidToll serviceConfig req = case serviceConfig of
MMIConfig cfg -> MMI.getRoutes cfg req
NextBillionConfig cfg -> NextBillion.getRoutes cfg req

getTransitRoutes ::
( EncFlow m r,
CoreMetrics m,
Log m
) =>
TransitServiceConfig ->
GetTransitRoutesReq ->
m (Maybe MultiModalResponse)
getTransitRoutes serviceConfig req = case serviceConfig of
GoogleTransit cfg -> Google.getTransitRoutes cfg req
OTPTransit cfg -> OTP.getTransitRoutes cfg req

snapToRoadProvided :: MapsService -> Bool
snapToRoadProvided = \case
Google -> True
Expand Down
29 changes: 0 additions & 29 deletions lib/mobility-core/src/Kernel/External/Maps/Interface/Google.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ module Kernel.External.Maps.Interface.Google
( module Reexport,
getDistances,
getRoutes,
getTransitRoutes,
snapToRoad,
autoComplete,
getPlaceDetails,
Expand All @@ -39,8 +38,6 @@ import qualified Kernel.External.Maps.Google.RoadsClient as GoogleRoads
import Kernel.External.Maps.HasCoordinates as Reexport (HasCoordinates (..))
import Kernel.External.Maps.Interface.Types as Types
import Kernel.External.Maps.Types as Reexport
import qualified Kernel.External.MultiModal.Types as MultiModalTypes
import Kernel.External.MultiModal.Utils
import Kernel.Prelude
import Kernel.Tools.Metrics.CoreMetrics (CoreMetrics)
import Kernel.Types.Common hiding (id)
Expand Down Expand Up @@ -186,32 +183,6 @@ getRoutes isAvoidToll cfg req = do
BICYCLE -> GoogleMaps.BICYCLE
MOTORCYCLE -> GoogleMaps.TWO_WHEELER

getTransitRoutes ::
( EncFlow m r,
CoreMetrics m,
Log m
) =>
GoogleCfg ->
GetTransitRoutesReq ->
m (Maybe MultiModalTypes.MultiModalResponse)
getTransitRoutes cfg req = do
key <- decrypt cfg.googleKey
let googleMapsUrl = cfg.googleRouteConfig.url
computeAlternativeRoutes = cfg.googleRouteConfig.computeAlternativeRoutes
routePreference = cfg.googleRouteConfig.routePreference
origin = req.origin
destination = req.destination
travelMode = req.mode
arrivalTime = req.arrivalTime
departureTime = req.departureTime
transitPreferences = req.transitPreferences
result <- try @_ @SomeException $ GoogleMaps.transitDirectionsAPI googleMapsUrl key origin destination travelMode computeAlternativeRoutes routePreference transitPreferences arrivalTime departureTime
case result of
Right gRes -> do
pure $ Just $ convertGoogleToGeneric gRes
Left _ -> do
pure Nothing

mkRoute' ::
(MonadFlow m) =>
GetRoutesReqProxy ->
Expand Down
18 changes: 0 additions & 18 deletions lib/mobility-core/src/Kernel/External/Maps/Interface/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ import qualified Debug.Trace as T
import Deriving.Aeson
import EulerHS.Prelude
import qualified Kernel.External.Maps.Google.Config as Google
import qualified Kernel.External.Maps.Google.MapsClient.Types as GT
import qualified Kernel.External.Maps.MMI.Config as MMI
import qualified Kernel.External.Maps.NextBillion.Config as NextBillion
import qualified Kernel.External.Maps.OSRM.Config as OSRM
import qualified Kernel.External.Maps.OpenTripPlanner.Config as OTP
import qualified Kernel.External.Maps.OpenTripPlanner.Types as OTPTypes
import Kernel.External.Maps.Types
import Kernel.External.Types (Language)
import Kernel.Types.Common
Expand All @@ -62,10 +59,6 @@ data MapsServiceConfig = GoogleConfig Google.GoogleCfg | OSRMConfig OSRM.OSRMCfg
deriving stock (Show, Eq, Generic)
deriving (FromJSON, ToJSON) via CustomJSON '[SumTaggedObject "tag" "content"] MapsServiceConfig

data TransitServiceConfig = GoogleTransit Google.GoogleCfg | OTPTransit OTP.OTPCfg
deriving stock (Show, Eq, Generic)
deriving (FromJSON, ToJSON) via CustomJSON '[SumTaggedObject "tag" "content"] TransitServiceConfig

data TravelMode = CAR | MOTORCYCLE | BICYCLE | FOOT
deriving (Show, Eq, Generic, ToJSON, FromJSON, ToSchema)

Expand Down Expand Up @@ -106,17 +99,6 @@ data GetRoutesReq = GetRoutesReq
}
deriving (Generic, ToJSON, FromJSON, Show, ToSchema)

data GetTransitRoutesReq = GetTransitRoutesReq
{ origin :: GT.WayPointV2,
destination :: GT.WayPointV2,
arrivalTime :: Maybe String,
departureTime :: Maybe String,
mode :: Maybe GT.ModeV2,
transitPreferences :: Maybe GT.TransitPreferencesV2,
transportModes :: Maybe [Maybe OTPTypes.TransportMode]
}
deriving (Generic, ToJSON, FromJSON, Show, ToSchema)

data GetRoutesReqProxy = GetRoutesReqProxy
{ waypoints :: [LatLong],
origin :: LatLong,
Expand Down
17 changes: 17 additions & 0 deletions lib/mobility-core/src/Kernel/External/MultiModal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{-
Copyright 2022-23, Juspay India Pvt Ltd

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License

as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is

distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS

FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero

General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
-}

module Kernel.External.MultiModal (module Reexport) where

import Kernel.External.MultiModal.Interface as Reexport

This file was deleted.

42 changes: 42 additions & 0 deletions lib/mobility-core/src/Kernel/External/MultiModal/Interface.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Kernel.External.MultiModal.Interface
( module Reexport,
getTransitRoutesProvided,
getTransitRoutes,
)
where

import Kernel.External.Maps.Google.Config as Reexport
import qualified Kernel.External.MultiModal.Interface.Google as Google
import qualified Kernel.External.MultiModal.Interface.OpenTripPlanner as OTP
import Kernel.External.MultiModal.Interface.Types as Reexport
import Kernel.External.MultiModal.OpenTripPlanner.Config as Reexport
import Kernel.External.MultiModal.Types
import Kernel.External.MultiModal.Utils as Reexport
import Kernel.Prelude
import Kernel.Tools.Metrics.CoreMetrics (CoreMetrics)
import Kernel.Types.Common hiding (id)

-- To be used in future when we add another provider
-- mkNotProvidedError :: Text -> MultiModalService -> Text
-- mkNotProvidedError functionName serviceName = "Function " <> functionName <> " is not provided by service " <> show serviceName
--
-- throwNotProvidedError :: (MonadFlow m) => Text -> MultiModalService -> m a
-- throwNotProvidedError =
-- (throwError . InternalError) ... mkNotProvidedError

getTransitRoutesProvided :: MultiModalService -> Bool
getTransitRoutesProvided = \case
GoogleTransit -> True
OTPTransit -> True

getTransitRoutes ::
( EncFlow m r,
CoreMetrics m,
Log m
) =>
MultiModalServiceConfig ->
GetTransitRoutesReq ->
m (Maybe MultiModalResponse)
getTransitRoutes serviceConfig req = case serviceConfig of
GoogleTransitConfig cfg -> Google.getTransitRoutes cfg req
OTPTransitConfig cfg -> OTP.getTransitRoutes cfg req
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Kernel.External.MultiModal.Interface.Google (getTransitRoutes) where

import Data.Time
import Kernel.External.Encryption
import Kernel.External.Maps.Google.Config
import qualified Kernel.External.Maps.Google.MapsClient as GoogleMaps
import Kernel.External.MultiModal.Interface.Types as MultiModalTypes
import Kernel.External.MultiModal.Utils
import Kernel.Prelude
import Kernel.Tools.Metrics.CoreMetrics (CoreMetrics)
import Kernel.Types.Common hiding (id)

formatUtcTime :: Maybe UTCTime -> Maybe String
formatUtcTime Nothing = Nothing
formatUtcTime (Just utcTime) = Just $ formatTime defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ" utcTime

getTransitRoutes ::
( EncFlow m r,
CoreMetrics m,
Log m
) =>
GoogleCfg ->
MultiModalTypes.GetTransitRoutesReq ->
m (Maybe MultiModalTypes.MultiModalResponse)
getTransitRoutes cfg req = do
key <- decrypt cfg.googleKey
let googleMapsUrl = cfg.googleRouteConfig.url
computeAlternativeRoutes = cfg.googleRouteConfig.computeAlternativeRoutes
routePreference = cfg.googleRouteConfig.routePreference
origin = req.origin
destination = req.destination
travelMode = req.mode
arrivalTime = formatUtcTime req.arrivalTime
departureTime = formatUtcTime req.departureTime
transitPreferences = req.transitPreferences
result <- try @_ @SomeException $ GoogleMaps.transitDirectionsAPI googleMapsUrl key origin destination travelMode computeAlternativeRoutes routePreference transitPreferences arrivalTime departureTime
case result of
Right gRes -> do
pure $ Just $ convertGoogleToGeneric gRes
Left _ -> do
pure Nothing
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
module Kernel.External.Maps.Interface.OpenTripPlanner (getTransitRoutes) where
module Kernel.External.MultiModal.Interface.OpenTripPlanner (getTransitRoutes) where

import Data.Morpheus.Client
( request,
single,
)
import Data.Time.Format (defaultTimeLocale, formatTime, parseTimeM)
import Data.Time.Format (defaultTimeLocale, formatTime)
import EulerHS.Prelude hiding (id, product)
import Kernel.External.Encryption
import qualified Kernel.External.Maps.Interface.Types as TP
import Kernel.External.Maps.OpenTripPlanner.Config
import Kernel.External.Maps.OpenTripPlanner.Types
import Kernel.External.MultiModal.Types
import qualified Kernel.External.MultiModal.Interface.Types as TP
import Kernel.External.MultiModal.OpenTripPlanner.Config
import Kernel.External.MultiModal.OpenTripPlanner.Types
import Kernel.External.MultiModal.Utils
import Kernel.Tools.Metrics.CoreMetrics (CoreMetrics)
import Kernel.Utils.Common hiding (id)

convertDateTime :: String -> Maybe (String, String)
convertDateTime input = do
time <- parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%SZ" input :: Maybe UTCTime
let dateStr = formatTime defaultTimeLocale "%Y-%m-%d" time
let timeStr = formatTime defaultTimeLocale "%H:%M" time
return (dateStr, timeStr)
formatUtcDateTime :: UTCTime -> (String, String)
formatUtcDateTime utcTime = (dateString, timeString)
where
dateString = formatTime defaultTimeLocale "%Y-%m-%d" utcTime
timeString = formatTime defaultTimeLocale "%H:%M" utcTime

getTransitRoutes ::
( EncFlow m r,
Expand All @@ -29,7 +26,7 @@ getTransitRoutes ::
) =>
OTPCfg ->
TP.GetTransitRoutesReq ->
m (Maybe MultiModalResponse)
m (Maybe TP.MultiModalResponse)
getTransitRoutes cfg req = do
let origin =
InputCoordinates
Expand All @@ -41,7 +38,7 @@ getTransitRoutes cfg req = do
{ lat = req.destination.location.latLng.latitude,
lon = req.destination.location.latLng.longitude
}
let dateTime = req.arrivalTime >>= convertDateTime
let dateTime = req.departureTime <&> formatUtcDateTime
let planClient = fromString cfg.baseUrl
let transportModes' = req.transportModes
resp <-
Expand Down
Loading