Skip to content

Commit

Permalink
backend/enhancement: Rectification
Browse files Browse the repository at this point in the history
  • Loading branch information
khuzema786 committed Sep 3, 2024
1 parent e7d0eb0 commit 110aadb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
57 changes: 34 additions & 23 deletions lib/mobility-core/src/Kernel/External/Maps/Interface.hs
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,35 @@ snapToRoadWithFallback mbMapServiceToRectifyDistantPointsFailure SnapToRoadHandl
callSnapToRoadWithFallback (preferredProvider : restProviders) = do
mapsConfig <- getProviderConfig preferredProvider
preCheckPassed <- runPreCheck preferredProvider req
case (preCheckPassed, mbMapServiceToRectifyDistantPointsFailure) of
(False, Nothing) -> do
logDebug $ "Pre check failed for provider " <> show preferredProvider <> ". Points: " <> show req.points
callSnapToRoadWithFallback restProviders
(False, Just mapServiceCfg) -> do
if preCheckPassed
then do
result <- try @_ @SomeException $ snapToRoad mapsConfig req
case result of
Left err -> do
logError $ "Snap-to-road failed with preCheckPassed with error : " <> show err <> " - provider : " <> show preferredProvider
(servicesUsed, snapResponse) <- callSnapToRoadWithRectification preferredProvider restProviders
return (preferredProvider : servicesUsed, snapResponse)
Right res -> do
confidencethreshold <- getConfidenceThreshold
postCheckPassed <- runPostCheck preferredProvider req res
when (not postCheckPassed) $ logError $ "Snap-to-road failed due to Post check - provider : " <> show preferredProvider
when (res.confidence < confidencethreshold) $ logError $ "Snap-to-road failed due to Confidence : " <> show res.confidence <> " - provider : " <> show preferredProvider
if res.confidence < confidencethreshold || not postCheckPassed
then do
(servicesUsed, snapResponse) <- callSnapToRoadWithRectification preferredProvider restProviders
return (preferredProvider : servicesUsed, snapResponse)
else return ([preferredProvider], Right res)
else do
(servicesUsed, snapResponse) <- callSnapToRoadWithRectification preferredProvider restProviders
return (preferredProvider : servicesUsed, snapResponse)

callSnapToRoadWithRectification preferredProvider restProviders = do
mapsConfig <- getProviderConfig preferredProvider
case mbMapServiceToRectifyDistantPointsFailure of
Just mapServiceCfg -> do
droppedPointsThreshold <- asks (.droppedPointsThreshold)
maxStraightLineRectificationThreshold <- asks (.maxStraightLineRectificationThreshold)
let starightDistancePoints = getEverySnippetWhichIsNot (< droppedPointsThreshold) req.points
let straightDistancePoints = getEverySnippetWhichIsNot (< droppedPointsThreshold) req.points
distanceRectified <-
mapM
( \(x1, x2, dist) -> do
Expand All @@ -209,15 +230,16 @@ snapToRoadWithFallback mbMapServiceToRectifyDistantPointsFailure SnapToRoadHandl
distanceRes <- getDistance mapServiceCfg (GetDistanceReq {origin = x1, destination = x2, travelMode = Just CAR, distanceUnit = req.distanceUnit} :: GetDistanceReq LatLong LatLong)
pure (x1, metersToHighPrecMeters distanceRes.distance)
)
starightDistancePoints
straightDistancePoints
logDebug $ "Rectified distances: " <> show distanceRectified
let (pointsOutOfThreshold, distance) = foldl' (\(accPoints, accDis) (x1, dis) -> (accPoints <> [x1], accDis + dis)) ([], 0) distanceRectified
let splitSnapToRoadCalls = filter (not . (<= 1) . length) $ splitWith pointsOutOfThreshold req.points
logDebug $ "Split snap-to-road calls: " <> show splitSnapToRoadCalls
pointsRes <- try @_ @SomeException $ mapM (\section -> snapToRoad mapsConfig (req {points = section})) splitSnapToRoadCalls
logDebug $ "Snap-to-road results: " <> show pointsRes
case pointsRes of
Left _ -> do
Left err -> do
logError $ "Snap-to-road failed with rectification with error : " <> show err <> " - provider : " <> show preferredProvider
(servicesUsed, snapResponse) <- callSnapToRoadWithFallback restProviders
return (preferredProvider : servicesUsed, snapResponse)
Right results -> do
Expand All @@ -230,21 +252,10 @@ snapToRoadWithFallback mbMapServiceToRectifyDistantPointsFailure SnapToRoadHandl
snappedPoints = snappedPoints
}
return ([preferredProvider, SelfTuned], Right snappedResp)
(True, _) -> do
result <- try @_ @SomeException $ snapToRoad mapsConfig req
case result of
Left _ -> do
(servicesUsed, snapResponse) <- callSnapToRoadWithFallback restProviders
return (preferredProvider : servicesUsed, snapResponse)
Right res -> do
confidencethreshold <- getConfidenceThreshold
postCheckPassed <- runPostCheck preferredProvider req res
when (not postCheckPassed) $ logError $ "Post check failed for provider " <> show preferredProvider
if res.confidence < confidencethreshold || not postCheckPassed
then do
(servicesUsed, snapResponse) <- callSnapToRoadWithFallback restProviders
return (preferredProvider : servicesUsed, snapResponse)
else return ([preferredProvider], Right res)
Nothing -> do
logError $ "Snap-to-road failed without rectification - provider : " <> show preferredProvider
(servicesUsed, snapResponse) <- callSnapToRoadWithFallback restProviders
return (preferredProvider : servicesUsed, snapResponse)

snapToRoad ::
( EncFlow m r,
Expand Down
1 change: 1 addition & 0 deletions lib/mobility-core/src/Kernel/Types/Beckn/City.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ data City
| Jaipur
| Siliguri
| Asansol
| Durgapur
| Gangtok
| Darjeeling
| Davanagere
Expand Down

0 comments on commit 110aadb

Please sign in to comment.