-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7d6a0a
commit f32e0df
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
Package osrm provides functions over the Open Source Routing Machine project. | ||
We use OSRM's Table service when we want to quickly determine the duration | ||
and/or distance between two points (without traffic awareness). | ||
This could be useful for saving money/requests to Google Maps API every time an | ||
anonymous end user sees an estimate quote for a potential trip. | ||
https://github.com/Project-OSRM/osrm-backend/blob/master/docs/http.md#table-service | ||
*/ | ||
package osrm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package osrm | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
osrm "github.com/gojuno/go.osrm" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" | ||
"github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1" | ||
geo "github.com/paulmach/go.geo" | ||
"go.uber.org/zap" | ||
"time" | ||
) | ||
|
||
var ( | ||
errFailedRequest = errors.New("failed OSRM request") | ||
) | ||
|
||
type CalculateOutput struct { | ||
Duration time.Duration | ||
DistanceMeters int | ||
} | ||
|
||
func Calculate(ctx context.Context, a, b *v1beta1.LatLng) (*CalculateOutput, error) { | ||
logger := ctxzap.Extract(ctx) | ||
|
||
client := osrm.NewFromURL("https://router.project-osrm.org") | ||
|
||
res, err := client.Table(ctx, osrm.TableRequest{ | ||
Profile: "car", | ||
Coordinates: osrm.NewGeometryFromPointSet(geo.PointSet{ | ||
{a.GetLongitude(), a.GetLatitude()}, | ||
{b.GetLongitude(), b.GetLatitude()}, | ||
}), | ||
Sources: []int{0}, | ||
Destinations: []int{1}, | ||
}) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed OSRM request: %w", err) | ||
} | ||
|
||
if res.Code != "Ok" { | ||
logger.Error("received non-ok OSRM response", | ||
zap.String("error.code", res.Code), | ||
zap.String("error.msg", res.Message), | ||
) | ||
return nil, errFailedRequest | ||
} | ||
|
||
return &CalculateOutput{ | ||
Duration: time.Duration(res.Durations[0][0]) * time.Second, | ||
DistanceMeters: 0, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package osrm | ||
|
||
import ( | ||
"context" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" | ||
"github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap/zaptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCalculate(t *testing.T) { | ||
a := &v1beta1.LatLng{ | ||
Latitude: 40.791680675548136, | ||
Longitude: -73.9650115649754, | ||
} | ||
b := &v1beta1.LatLng{ | ||
Latitude: 40.76866089218841, | ||
Longitude: -73.98145413365043, | ||
} | ||
|
||
ctx := ctxzap.ToContext(context.Background(), zaptest.NewLogger(t)) | ||
res, err := Calculate(ctx, a, b) | ||
require.NoError(t, err) | ||
require.Greater(t, res.Duration, time.Duration(0)) | ||
} |