regular routes journeys, persistent KV to store return states, ...
This commit is contained in:
68
servers/grpc/server/management.go
Normal file
68
servers/grpc/server/management.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/servers/grpc/proto"
|
||||
"github.com/paulmach/orb/geojson"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func (s *CarpoolServiceServerImpl) CreateRegularRoutes(ctx context.Context, req *proto.CreateRegularRoutesRequest) (*proto.CreateRegularRoutesResponse, error) {
|
||||
log.Debug().Msg("grpc CarpoolService - CreateTrips")
|
||||
routes := []*geojson.FeatureCollection{}
|
||||
|
||||
for _, r := range req.Routes {
|
||||
route, err := geojson.UnmarshalFeatureCollection([]byte(r.Serialized))
|
||||
if err == nil {
|
||||
routes = append(routes, route)
|
||||
}
|
||||
}
|
||||
|
||||
if len(routes) == 0 {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "no route to create")
|
||||
}
|
||||
|
||||
if err := s.Handler.CreateRegularRoutes(routes); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "could not create routes in CreateRegularRoutes : %s", err.Error())
|
||||
}
|
||||
|
||||
return &proto.CreateRegularRoutesResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *CarpoolServiceServerImpl) DeleteRegularRoutes(context.Context, *proto.DeleteRegularRoutesRequest) (*proto.DeleteRegularRoutesResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method DeleteTrips not implemented")
|
||||
}
|
||||
|
||||
func (s *CarpoolServiceServerImpl) GetUserPlanning(ctx context.Context, req *proto.GetUserPlanningRequest) (*proto.GetUserPlanningResponse, error) {
|
||||
log.Debug().Msg("grpc CarpoolService - GetRegularUserRoutes")
|
||||
|
||||
planned_schedules, err := s.Handler.GetUserPlanning(req.UserId, req.MinDepartureDate.AsTime(), req.MaxDepartureDate.AsTime())
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "could not get user planning : %s", err.Error())
|
||||
}
|
||||
|
||||
results := map[string]*proto.CarpoolRoutesCollection{}
|
||||
|
||||
for k, scheds := range planned_schedules {
|
||||
|
||||
results[k] = &proto.CarpoolRoutesCollection{
|
||||
Collection: []*proto.CarpoolFeatureCollection{},
|
||||
}
|
||||
|
||||
for _, s := range scheds {
|
||||
s.Route.ExtraMembers["departure_date"] = s.DepartureDate
|
||||
s.Route.ExtraMembers["id"] = s.ID
|
||||
fcraw, _ := s.Route.FeatureCollection().MarshalJSON()
|
||||
results[k].Collection = append(results[k].Collection, &proto.CarpoolFeatureCollection{
|
||||
Serialized: string(fcraw),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return &proto.GetUserPlanningResponse{
|
||||
RoutesByDates: results,
|
||||
}, nil
|
||||
}
|
||||
167
servers/grpc/server/search.go
Normal file
167
servers/grpc/server/search.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/servers/grpc/proto"
|
||||
"github.com/paulmach/orb"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
func (s *CarpoolServiceServerImpl) DriverJourneys(ctx context.Context, req *proto.DriverJourneysRequest) (*proto.DriverJourneysResponse, error) {
|
||||
log.Debug().
|
||||
Str("departure date", req.DepartureDate.String()).
|
||||
Msg("grpc server - DriverJourneys")
|
||||
|
||||
departure := orb.Point{req.DepartureLng, req.DepartureLat}
|
||||
arrival := orb.Point{req.ArrivalLng, req.ArrivalLat}
|
||||
|
||||
td := 900 * time.Second
|
||||
if req.TimeDelta != nil {
|
||||
td = time.Duration(*req.TimeDelta) * time.Second
|
||||
}
|
||||
|
||||
departureDate := req.DepartureDate.AsTime()
|
||||
|
||||
minDate := departureDate.Add(-td)
|
||||
maxDate := departureDate.Add(td)
|
||||
|
||||
log.Debug().
|
||||
Str("departure_date", departureDate.Format(time.RFC3339)).
|
||||
Str("mindate", minDate.Format(time.RFC3339)).
|
||||
Str("maxdate", maxDate.Format(time.RFC3339)).
|
||||
Int64("td", int64(td)).
|
||||
Msg("DriverJourneys show dates")
|
||||
|
||||
journeys, err := s.Handler.GetDriverJourneys(departure, arrival, req.DepartureRadius, req.ArrivalRadius, minDate, maxDate, req.Count)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("error finding driver journeys")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results := []*proto.CarpoolServiceDriverJourney{}
|
||||
|
||||
for _, j := range journeys {
|
||||
properties := j.Route.ExtraMembers["properties"].(map[string]any)
|
||||
usermap := properties["user"].(map[string]any)
|
||||
journeyId := j.ID
|
||||
driverDepartureLat := j.Route.Features[0].Point().Lat()
|
||||
driverDepartureLng := j.Route.Features[0].Point().Lon()
|
||||
driverArrivalLat := j.Route.Features[1].Point().Lat()
|
||||
driverArrivalLng := j.Route.Features[1].Point().Lon()
|
||||
duration := time.Duration(0)
|
||||
var distance *int64
|
||||
if len(j.Itinerary.Legs) > 2 {
|
||||
duration = j.Itinerary.Legs[1].Duration
|
||||
dist := int64(j.Itinerary.Legs[1].Distance)
|
||||
distance = &dist
|
||||
}
|
||||
|
||||
results = append(results, &proto.CarpoolServiceDriverJourney{
|
||||
Driver: &proto.CarpoolServiceUser{
|
||||
Id: usermap["id"].(string),
|
||||
Operator: usermap["operator"].(string),
|
||||
Alias: usermap["alias"].(string),
|
||||
},
|
||||
Operator: "ridygo.fr",
|
||||
PassengerPickupLat: req.DepartureLat,
|
||||
PassengerPickupLng: req.DepartureLng,
|
||||
PassengerDropLat: req.ArrivalLat,
|
||||
PassengerDropLng: req.ArrivalLng,
|
||||
DriverDepartureLat: &driverDepartureLat,
|
||||
DriverDepartureLng: &driverDepartureLng,
|
||||
DriverArrivalLat: &driverArrivalLat,
|
||||
DriverArrivalLng: &driverArrivalLng,
|
||||
Duration: int64(duration),
|
||||
Distance: distance,
|
||||
Id: journeyId,
|
||||
PassengerPickupDate: timestamppb.New(j.DepartureDate.Add(j.Itinerary.Legs[0].Duration)),
|
||||
DriverDepartureDate: timestamppb.New(j.DepartureDate),
|
||||
Type: proto.CarpoolServiceJourneyType_PLANNED,
|
||||
})
|
||||
}
|
||||
|
||||
return &proto.DriverJourneysResponse{
|
||||
DriverJourneys: results,
|
||||
}, nil
|
||||
}
|
||||
func (s *CarpoolServiceServerImpl) PassengerJourneys(ctx context.Context, req *proto.PassengerJourneysRequest) (*proto.PassengerJourneysResponse, error) {
|
||||
log.Debug().
|
||||
Str("departure date", req.DepartureDate.String()).
|
||||
Msg("grpc server - PassengerJourneys")
|
||||
|
||||
departure := orb.Point{req.DepartureLng, req.DepartureLat}
|
||||
arrival := orb.Point{req.ArrivalLng, req.ArrivalLat}
|
||||
|
||||
td := 900 * time.Second
|
||||
if req.TimeDelta != nil {
|
||||
td = time.Duration(*req.TimeDelta) * time.Second
|
||||
}
|
||||
|
||||
minDate := req.DepartureDate.AsTime().Add(-td)
|
||||
maxDate := req.DepartureDate.AsTime().Add(td)
|
||||
|
||||
journeys, err := s.Handler.GetPassengerJourneys(departure, arrival, req.DepartureRadius, req.ArrivalRadius, minDate, maxDate, req.Count)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results := []*proto.CarpoolServicePassengerJourney{}
|
||||
|
||||
for _, j := range journeys {
|
||||
properties := j.Route.ExtraMembers["properties"].(map[string]any)
|
||||
usermap := properties["user"].(map[string]any)
|
||||
journeyId := j.ID
|
||||
passengerDepartureLat := j.Route.Features[0].Point().Lat()
|
||||
passengerDepartureLng := j.Route.Features[0].Point().Lon()
|
||||
passengerArrivalLat := j.Route.Features[1].Point().Lat()
|
||||
passengerArrivalLng := j.Route.Features[1].Point().Lon()
|
||||
passengerDepartureDate := timestamppb.New(j.DepartureDate)
|
||||
driverDepartureDate := timestamppb.New(j.DepartureDate.Add(-j.Itinerary.Legs[0].Duration))
|
||||
duration := time.Duration(0)
|
||||
var distance *int64
|
||||
log.Debug().Any("itinerary", j.Itinerary).Msg("debug itinerary")
|
||||
if len(j.Itinerary.Legs) > 2 {
|
||||
duration = j.Itinerary.Legs[1].Duration / time.Second
|
||||
dist := int64(j.Itinerary.Legs[1].Distance)
|
||||
distance = &dist
|
||||
}
|
||||
|
||||
results = append(results, &proto.CarpoolServicePassengerJourney{
|
||||
Passenger: &proto.CarpoolServiceUser{
|
||||
Id: usermap["id"].(string),
|
||||
Operator: usermap["operator"].(string),
|
||||
Alias: usermap["alias"].(string),
|
||||
},
|
||||
Operator: "ridygo.fr",
|
||||
PassengerPickupLat: passengerDepartureLat,
|
||||
PassengerPickupLng: passengerDepartureLng,
|
||||
PassengerDropLat: passengerArrivalLat,
|
||||
PassengerDropLng: passengerArrivalLng,
|
||||
DriverDepartureLat: &req.DepartureLat,
|
||||
DriverDepartureLng: &req.DepartureLng,
|
||||
DriverArrivalLat: &req.ArrivalLat,
|
||||
DriverArrivalLng: &req.ArrivalLng,
|
||||
Duration: int64(duration),
|
||||
Distance: distance,
|
||||
Id: journeyId,
|
||||
PassengerPickupDate: passengerDepartureDate,
|
||||
DriverDepartureDate: driverDepartureDate,
|
||||
Type: proto.CarpoolServiceJourneyType_PLANNED,
|
||||
})
|
||||
}
|
||||
|
||||
return &proto.PassengerJourneysResponse{
|
||||
PassengerJourneys: results,
|
||||
}, nil
|
||||
}
|
||||
func (s *CarpoolServiceServerImpl) DriverRegularTrips(context.Context, *proto.DriverRegularTripsRequest) (*proto.DriverRegularTripsResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method DriverRegularTrips not implemented")
|
||||
}
|
||||
func (s *CarpoolServiceServerImpl) PassengerRegularTrips(context.Context, *proto.PassengerRegularTripsRequest) (*proto.PassengerRegularTripsResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method PassengerRegularTrips not implemented")
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/handler"
|
||||
"git.coopgo.io/coopgo-platform/carpool-service/servers/grpc/proto"
|
||||
"github.com/paulmach/orb/geojson"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/viper"
|
||||
"google.golang.org/grpc"
|
||||
@@ -28,74 +27,6 @@ func NewCarpoolServiceServer(handler *handler.CarpoolServiceHandler) *CarpoolSer
|
||||
}
|
||||
}
|
||||
|
||||
func (s *CarpoolServiceServerImpl) CreateRegularRoutes(ctx context.Context, req *proto.CreateRegularRoutesRequest) (*proto.CreateRegularRoutesResponse, error) {
|
||||
log.Debug().Msg("grpc CarpoolService - CreateTrips")
|
||||
routes := []*geojson.FeatureCollection{}
|
||||
|
||||
for _, r := range req.Routes {
|
||||
route, err := geojson.UnmarshalFeatureCollection([]byte(r.Serialized))
|
||||
if err == nil {
|
||||
routes = append(routes, route)
|
||||
}
|
||||
}
|
||||
|
||||
if len(routes) == 0 {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "no route to create")
|
||||
}
|
||||
|
||||
if err := s.Handler.CreateRegularRoutes(routes); err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "could not create routes in CreateRegularRoutes : %s", err.Error())
|
||||
}
|
||||
|
||||
return &proto.CreateRegularRoutesResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *CarpoolServiceServerImpl) DeleteRegularRoutes(context.Context, *proto.DeleteRegularRoutesRequest) (*proto.DeleteRegularRoutesResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method DeleteTrips not implemented")
|
||||
}
|
||||
|
||||
func (s *CarpoolServiceServerImpl) GetUserPlanning(ctx context.Context, req *proto.GetUserPlanningRequest) (*proto.GetUserPlanningResponse, error) {
|
||||
log.Debug().Msg("grpc CarpoolService - GetRegularUserRoutes")
|
||||
|
||||
planned_schedules, err := s.Handler.GetUserPlanning(req.UserId, req.MinDepartureDate.AsTime(), req.MaxDepartureDate.AsTime())
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "could not get user planning : %s", err.Error())
|
||||
}
|
||||
|
||||
results := map[string]*proto.CarpoolRoutesCollection{}
|
||||
|
||||
for k, scheds := range planned_schedules {
|
||||
|
||||
results[k] = &proto.CarpoolRoutesCollection{
|
||||
Collection: []*proto.CarpoolFeatureCollection{},
|
||||
}
|
||||
|
||||
for _, s := range scheds {
|
||||
s.Route.ExtraMembers["departure_date"] = s.DepartureDate
|
||||
fcraw, _ := s.Route.FeatureCollection().MarshalJSON()
|
||||
results[k].Collection = append(results[k].Collection, &proto.CarpoolFeatureCollection{
|
||||
Serialized: string(fcraw),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return &proto.GetUserPlanningResponse{
|
||||
RoutesByDates: results,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *CarpoolServiceServerImpl) DriverJourneys(context.Context, *proto.DriverJourneysRequest) (*proto.DriverJourneysResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method DriverJourneys not implemented")
|
||||
}
|
||||
func (s *CarpoolServiceServerImpl) PassengerJourneys(context.Context, *proto.PassengerJourneysRequest) (*proto.PassengerJourneysResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method PassengerJourneys not implemented")
|
||||
}
|
||||
func (s *CarpoolServiceServerImpl) DriverRegularTrips(context.Context, *proto.DriverRegularTripsRequest) (*proto.DriverRegularTripsResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method DriverRegularTrips not implemented")
|
||||
}
|
||||
func (s *CarpoolServiceServerImpl) PassengerRegularTrips(context.Context, *proto.PassengerRegularTripsRequest) (*proto.PassengerRegularTripsResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method PassengerRegularTrips not implemented")
|
||||
}
|
||||
func (s *CarpoolServiceServerImpl) CreateBooking(context.Context, *proto.CreateBookingRequest) (*proto.CreateBookingResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method CreateBooking not implemented")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user