@@ -4,9 +4,10 @@ import (
44 "context"
55 "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
66 "github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1"
7- "github.com/kevinmichaelchen/api-dispatch/internal/service/distance"
87 "github.com/kevinmichaelchen/api-dispatch/internal/service/money"
98 "github.com/kevinmichaelchen/api-dispatch/internal/service/ranking"
9+ "github.com/kevinmichaelchen/api-dispatch/pkg/maps"
10+ "github.com/kevinmichaelchen/api-dispatch/pkg/maps/distance"
1011 "go.uber.org/zap"
1112 "google.golang.org/protobuf/types/known/durationpb"
1213 "google.golang.org/protobuf/types/known/timestamppb"
@@ -23,15 +24,11 @@ func (s *Service) GetNearestDrivers(
2324 ctx context.Context ,
2425 req * v1beta1.GetNearestDriversRequest ,
2526) (* v1beta1.GetNearestDriversResponse , error ) {
26- logger := ctxzap .Extract (ctx )
27-
2827 err := validate (req , req )
2928 if err != nil {
3029 return nil , err
3130 }
3231
33- trafficAware := s .distanceSvc != nil
34-
3532 // Query database
3633 nearby , err := s .dataStore .GetNearbyDriverLocations (ctx , req .GetPickupLocation ())
3734 if err != nil {
@@ -55,32 +52,17 @@ func (s *Service) GetNearestDrivers(
5552 results = results [:maxResults ]
5653 }
5754
58- // In the event we have no Google Maps client and are operating in a
59- // degraded state, k-ring-sorting is still pretty good.
55+ // The initial sort will be based on H3 resolutions and k-rings
6056 results = ranking .SortResultsByKRing (results )
6157
62- // Enrich results with distance/duration info from Google Maps API
58+ // Enrich results (e.g., with distance/duration info, among other things)
6359 var driverLocations []* v1beta1.LatLng
6460 for _ , result := range results {
6561 driverLocations = append (driverLocations , result .GetLocation ())
6662 }
67- var pickupAddress string
68- if trafficAware {
69- out , err := s .distanceSvc .BetweenPoints (ctx , distance.BetweenPointsInput {
70- PickupLocations : []* v1beta1.LatLng {req .GetPickupLocation ()},
71- DriverLocations : driverLocations ,
72- })
73- if err != nil {
74- return nil , err
75- }
76- for i , info := range out .Info {
77- logger .Info ("received distance matrix info" , zap .Any ("info" , info ))
78- results [i ].Duration = durationpb .New (info .Duration )
79- results [i ].DistanceMeters = float64 (info .DistanceMeters )
80- // the driver is always the origin
81- results [i ].Address = info .OriginAddress
82- pickupAddress = info .DestinationAddress
83- }
63+ matrixOut , err := s .enrichNearbyDrivers (ctx , results , driverLocations , req .GetPickupLocation ())
64+ if err != nil {
65+ return nil , err
8466 }
8567
8668 // Final ranking/sorting pass
@@ -94,7 +76,7 @@ func (s *Service) GetNearestDrivers(
9476
9577 return & v1beta1.GetNearestDriversResponse {
9678 Results : results ,
97- PickupAddress : pickupAddress ,
79+ PickupAddress : matrixOut . DestinationAddresses [ 0 ] ,
9880 }, nil
9981}
10082
@@ -108,8 +90,6 @@ func (s *Service) GetNearestTrips(
10890 return nil , err
10991 }
11092
111- trafficAware := s .distanceSvc != nil
112-
11393 // Query database
11494 nearby , err := s .dataStore .GetNearbyTrips (ctx , req .GetDriverLocation ())
11595 if err != nil {
@@ -134,34 +114,19 @@ func (s *Service) GetNearestTrips(
134114 results = results [:maxResults ]
135115 }
136116
137- // In the event we have no Google Maps client and are operating in a
138- // degraded state, k-ring-sorting is still pretty good.
117+ // The initial sort will be based on H3 resolutions and k-rings
139118 results = ranking .SortResultsByKRing (results )
140119
141- // Enrich results with distance/duration info from Google Maps API
142- var locations []* v1beta1.LatLng
120+ // Enrich results (e.g., with distance/duration info, among other things)
121+ var pickupLocations []* v1beta1.LatLng
143122 for _ , result := range results {
144- locations = append (locations , result .GetLocation ())
123+ pickupLocations = append (pickupLocations , result .GetLocation ())
145124 }
146- if trafficAware {
147- out , err := s .distanceSvc .BetweenPoints (ctx , distance.BetweenPointsInput {
148- PickupLocations : locations ,
149- DriverLocations : []* v1beta1.LatLng {req .GetDriverLocation ()},
150- })
151- if err != nil {
152- return nil , err
153- }
154- for i , info := range out .Info {
155- results [i ].Duration = durationpb .New (info .Duration )
156- results [i ].DistanceMeters = float64 (info .DistanceMeters )
157- // the driver is always the origin, the pickup is the destination
158- results [i ].Address = info .DestinationAddress
159- }
125+ _ , err = s .enrichNearbyTrips (ctx , results , req .GetDriverLocation (), pickupLocations )
126+ if err != nil {
127+ return nil , err
160128 }
161129
162- // Enrich results
163- enrichTripsWithFakeData (results )
164-
165130 // Final ranking/sorting pass
166131 results = ranking .RankTrips (results )
167132
@@ -176,13 +141,69 @@ func (s *Service) GetNearestTrips(
176141 }, nil
177142}
178143
179- func enrichTripsWithFakeData (in []* v1beta1.SearchResult ) {
180- for idx := range in {
181- e := in [idx ]
144+ func (s * Service ) enrichNearbyDrivers (
145+ ctx context.Context ,
146+ results []* v1beta1.SearchResult ,
147+ driverLocations []* v1beta1.LatLng ,
148+ pickupLocation * v1beta1.LatLng ,
149+ ) (* distance.MatrixResponse , error ) {
150+ logger := ctxzap .Extract (ctx )
151+
152+ out , err := s .distanceSvc .BetweenPoints (ctx , distance.BetweenPointsInput {
153+ // the driver location(s) is/are always the origin(s)
154+ Origins : toLatLngs (driverLocations ),
155+ Destinations : toLatLngs ([]* v1beta1.LatLng {pickupLocation }),
156+ })
157+ if err != nil {
158+ return nil , err
159+ }
160+
161+ for i , row := range out .Rows {
162+ for _ , elem := range row .Elements {
163+ logger .Info ("Got Distance Matrix element" , zap .Any ("elem" , elem ))
164+ results [i ].Duration = durationpb .New (elem .Duration )
165+ results [i ].DistanceMeters = float64 (elem .Distance )
166+ results [i ].Address = out .OriginAddresses [i ]
167+ }
168+ }
169+
170+ return out , nil
171+ }
172+
173+ func (s * Service ) enrichNearbyTrips (
174+ ctx context.Context ,
175+ results []* v1beta1.SearchResult ,
176+ driverLocation * v1beta1.LatLng ,
177+ pickupLocations []* v1beta1.LatLng ,
178+ ) (* distance.MatrixResponse , error ) {
179+ logger := ctxzap .Extract (ctx )
180+
181+ out , err := s .distanceSvc .BetweenPoints (ctx , distance.BetweenPointsInput {
182+ // the driver location(s) is/are always the origin(s)
183+ Origins : toLatLngs ([]* v1beta1.LatLng {driverLocation }),
184+ Destinations : toLatLngs (pickupLocations ),
185+ })
186+ if err != nil {
187+ return nil , err
188+ }
189+
190+ for idx := range results {
191+ e := results [idx ]
182192 t := e .GetTrip ()
183193 t .ScheduledFor = timestamppb .New (randomTime ())
184194 t .ExpectedPayment = randomMoney ()
185195 }
196+
197+ for _ , row := range out .Rows {
198+ for i , elem := range row .Elements {
199+ logger .Info ("Got Distance Matrix element" , zap .Any ("elem" , elem ))
200+ results [i ].Duration = durationpb .New (elem .Duration )
201+ results [i ].DistanceMeters = float64 (elem .Distance )
202+ results [i ].Address = out .DestinationAddresses [i ]
203+ }
204+ }
205+
206+ return out , nil
186207}
187208
188209func randomTime () time.Time {
@@ -197,3 +218,14 @@ func randomMoney() *v1beta1.Money {
197218 f := float64 (randomUnits ) + (float64 (randomCents ) / float64 (100 ))
198219 return money .ConvertFloatToMoney (f )
199220}
221+
222+ func toLatLngs (in []* v1beta1.LatLng ) []maps.LatLng {
223+ var out []maps.LatLng
224+ for _ , e := range in {
225+ out = append (out , maps.LatLng {
226+ Lat : e .GetLatitude (),
227+ Lng : e .GetLongitude (),
228+ })
229+ }
230+ return out
231+ }
0 commit comments