Skip to content
This repository was archived by the owner on Jan 15, 2026. It is now read-only.

Commit aa40915

Browse files
refactoring (#17)
1 parent 87550e4 commit aa40915

35 files changed

Lines changed: 1617 additions & 878 deletions

.tokeignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ go.sum
99
internal/models
1010
*.json
1111
*.toml
12+
*_test.go

README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
1. [Introduction](#introduction)
88
1. [The problem](#the-problem)
99
1. [The solution](#the-solution)
10-
1. [Not considered yet](#not-considered-yet)
1110
1. [Project structure](#project-structure)
1211
1. [How does it work](#how-does-it-work)
1312
1. [Ingestion](#ingestion)
@@ -28,21 +27,18 @@ drivers? Conversely, how do we select the best trips for drivers?
2827
Combine [Google Maps](https://developers.google.com/maps/documentation/distance-matrix/distance-matrix)
2928
and [h3](https://h3geo.org/) (a hexagonal hierarchical geospatial indexing system).
3029

31-
### Not considered yet
32-
1. Driver eligibility for trip
33-
3430
## Project structure
3531

36-
| Directory | Description |
37-
|----------------------------------------------|-------------------------------------------|
38-
| [`./cmd`](./cmd) | CLI for making gRPC requests |
39-
| [`./idl`](./idl) | Protobufs (Interface Definition Language) |
40-
| [`./internal/app`](./internal/app) | App dependency injection / initialization |
41-
| [`./internal/distance`](./internal/distance) | Google Maps Distance Matrix logic |
42-
| [`./internal/idl`](./internal/idl) | Auto-generated protobufs |
43-
| [`./internal/models`](./internal/models) | Auto-generated ORM / models |
44-
| [`./internal/service`](./internal/service) | Service layer / Business logic |
45-
| [`./schema`](./schema) | SQL migration scripts |
32+
| Directory | Description |
33+
|-----------------------------------------------------|-------------------------------------------|
34+
| [`./cmd`](./cmd) | CLI for making gRPC requests |
35+
| [`./idl`](./idl/coop/drivers/dispatch/v1beta1) | Protobufs (Interface Definition Language) |
36+
| [`./internal/app`](./internal/app) | App dependency injection / initialization |
37+
| [`./internal/distance`](internal/service/distance) | Google Maps Distance Matrix logic |
38+
| [`./internal/idl`](./internal/idl) | Auto-generated protobufs |
39+
| [`./internal/models`](./internal/models) | Auto-generated ORM / models |
40+
| [`./internal/service`](./internal/service) | Service layer / Business logic |
41+
| [`./schema`](./schema) | SQL migration scripts |
4642

4743
## How does it work
4844

cmd/dispatch/ingest.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1"
7+
"github.com/kevinmichaelchen/api-dispatch/internal/service/money"
78
"github.com/spf13/cobra"
89
"google.golang.org/protobuf/types/known/timestamppb"
910
"io/ioutil"
@@ -126,7 +127,7 @@ func ingestTrips(cmd *cobra.Command, args []string) {
126127
Latitude: e.LatLng.Latitude,
127128
Longitude: e.LatLng.Longitude,
128129
},
129-
ExpectedPayment: e.ExpectedPayment,
130+
ExpectedPayment: money.ConvertFloatToMoney(e.ExpectedPayment),
130131
})
131132
}
132133

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/XSAM/otelsql v0.14.1
77
github.com/friendsofgo/errors v0.9.2
88
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
9+
github.com/google/go-cmp v0.5.8
910
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
1011
github.com/kat-co/vala v0.0.0-20170210184112-42e1d8b61f12
1112
github.com/lib/pq v1.10.5

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
198198
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
199199
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
200200
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
201-
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
202201
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
202+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
203+
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
203204
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
204205
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
205206
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=

idl/coop/drivers/dispatch/v1beta1/api.proto

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ syntax = "proto3";
22

33
package coop.drivers.dispatch.v1beta1;
44

5+
import "coop/drivers/dispatch/v1beta1/driver.proto";
56
import "coop/drivers/dispatch/v1beta1/latlng.proto";
7+
import "coop/drivers/dispatch/v1beta1/trip.proto";
68
import "google/protobuf/duration.proto";
7-
import "google/protobuf/timestamp.proto";
89

910
option go_package = "github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1";
1011

@@ -22,12 +23,6 @@ service DispatchService {
2223
rpc GetNearestTrips(GetNearestTripsRequest) returns (GetNearestTripsResponse) {}
2324
}
2425

25-
message DriverLocation {
26-
string driver_id = 1;
27-
google.protobuf.Timestamp most_recent_heartbeat = 2;
28-
LatLng current_location = 3;
29-
}
30-
3126
message UpdateDriverLocationsRequest {
3227
repeated DriverLocation locations = 1;
3328
}
@@ -38,16 +33,6 @@ message CreateTripsRequest {
3833
repeated Trip trips = 1;
3934
}
4035

41-
message Trip {
42-
string id = 1;
43-
// where
44-
LatLng pickup_location = 2;
45-
// when
46-
google.protobuf.Timestamp scheduled_for = 3;
47-
// how much $$$
48-
double expected_payment = 4;
49-
}
50-
5136
message CreateTripsResponse {}
5237

5338
message GetNearestDriversRequest {
@@ -91,4 +76,5 @@ message SearchResult {
9176
// The k-value of the lowest k-ring (most immediate neighborhood) in which the
9277
// match was found.
9378
int32 k_value = 8;
79+
double score = 9;
9480
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
syntax = "proto3";
2+
3+
package coop.drivers.dispatch.v1beta1;
4+
5+
import "coop/drivers/dispatch/v1beta1/latlng.proto";
6+
import "google/protobuf/timestamp.proto";
7+
8+
option go_package = "github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1";
9+
10+
message DriverLocation {
11+
string driver_id = 1;
12+
google.protobuf.Timestamp most_recent_heartbeat = 2;
13+
LatLng current_location = 3;
14+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
syntax = "proto3";
2+
3+
package coop.drivers.dispatch.v1beta1;
4+
5+
import "coop/drivers/dispatch/v1beta1/latlng.proto";
6+
import "google/protobuf/duration.proto";
7+
import "google/protobuf/timestamp.proto";
8+
9+
option go_package = "github.com/kevinmichaelchen/api-dispatch/internal/idl/coop/drivers/dispatch/v1beta1";
10+
11+
message Trip {
12+
string id = 1;
13+
// where
14+
LatLng pickup_location = 2;
15+
// when
16+
google.protobuf.Timestamp scheduled_for = 3;
17+
// how much $$$
18+
Money expected_payment = 4;
19+
}
20+
21+
// Represents an amount of money with its currency type.
22+
message Money {
23+
// The three-letter currency code defined in ISO 4217.
24+
string currency_code = 1;
25+
26+
// The whole units of the amount.
27+
// For example if `currencyCode` is `"USD"`, then 1 unit is one US dollar.
28+
int64 units = 2;
29+
30+
// Number of nano (10^-9) units of the amount.
31+
// The value must be between -999,999,999 and +999,999,999 inclusive.
32+
// If `units` is positive, `nanos` must be positive or zero.
33+
// If `units` is zero, `nanos` can be positive, zero, or negative.
34+
// If `units` is negative, `nanos` must be negative or zero.
35+
// For example $-1.75 is represented as `units`=-1 and `nanos`=-750,000,000.
36+
int32 nanos = 3;
37+
}

internal/app/service/service.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"database/sql"
55
"fmt"
66
"github.com/friendsofgo/errors"
7-
"github.com/kevinmichaelchen/api-dispatch/internal/distance"
87
"github.com/kevinmichaelchen/api-dispatch/internal/service"
8+
"github.com/kevinmichaelchen/api-dispatch/internal/service/db"
9+
"github.com/kevinmichaelchen/api-dispatch/internal/service/distance"
910
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
1011
"go.uber.org/fx"
1112
"go.uber.org/zap"
@@ -18,17 +19,22 @@ var Module = fx.Module("service",
1819
NewService,
1920
NewDistanceService,
2021
NewMapsClient,
22+
NewDataStore,
2123
),
2224
)
2325

24-
type ServiceParams struct {
26+
type Params struct {
2527
fx.In
26-
DB *sql.DB
28+
DataStore *db.Store
2729
DistanceService *distance.Service `optional:"true"`
2830
}
2931

30-
func NewService(p ServiceParams) *service.Service {
31-
return service.NewService(p.DB, p.DistanceService)
32+
func NewService(p Params) *service.Service {
33+
return service.NewService(p.DataStore, p.DistanceService)
34+
}
35+
36+
func NewDataStore(sqlDB *sql.DB) *db.Store {
37+
return db.NewStore(sqlDB)
3238
}
3339

3440
func NewMapsClient() (*maps.Client, error) {

0 commit comments

Comments
 (0)