2023-03-27 18:54:56 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2023-03-29 22:45:18 +00:00
|
|
|
"git.coopgo.io/coopgo-platform/carpool-service/geoutils"
|
2023-03-30 06:44:58 +00:00
|
|
|
"git.coopgo.io/coopgo-platform/carpool-service/internal"
|
2023-03-29 22:45:18 +00:00
|
|
|
"git.coopgo.io/coopgo-platform/routing-service/encoding/polylines"
|
2023-03-27 18:54:56 +00:00
|
|
|
"github.com/paulmach/orb"
|
2023-03-29 10:50:25 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2023-03-27 18:54:56 +00:00
|
|
|
)
|
|
|
|
|
2023-05-07 23:29:59 +00:00
|
|
|
// GetDriverJourneys searches for matching punctual planned driver journeys.
|
|
|
|
func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.PlannedRouteSchedule, error) {
|
2023-03-29 10:50:25 +00:00
|
|
|
|
|
|
|
log.Debug().
|
|
|
|
Any("departure", departure).
|
|
|
|
Any("arrival", arrival).
|
|
|
|
Any("dep radius", departureRadius).
|
|
|
|
Any("arr radius", arrivalRadius).
|
|
|
|
Str("mindate", minDate.Format(time.RFC3339)).
|
|
|
|
Str("maxdate", maxDate.Format(time.RFC3339)).
|
|
|
|
Any("count", count).
|
|
|
|
Msg("Carpool service handler - GetDriverJourneys")
|
|
|
|
|
|
|
|
if count == nil {
|
|
|
|
c := int64(10)
|
|
|
|
count = &c
|
|
|
|
}
|
|
|
|
|
2023-03-29 22:45:18 +00:00
|
|
|
drad := float64(1000)
|
|
|
|
if departureRadius != nil {
|
|
|
|
drad = *departureRadius * 1000
|
2023-03-29 10:50:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:45:18 +00:00
|
|
|
arad := float64(1000)
|
|
|
|
if arrivalRadius != nil {
|
|
|
|
arad = *arrivalRadius * 1000
|
2023-03-29 10:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tileset, err := h.Tiles.GetTiles("driver", minDate, departure, arrival)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Str("date", minDate.Format(time.RFC3339)).
|
|
|
|
Any("departure", departure).
|
|
|
|
Any("arrival", arrival).
|
|
|
|
Err(err).
|
|
|
|
Msg("could not retrieve tiles")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
candidate_routes := tileset.GetTiledRoutes()
|
|
|
|
|
2023-05-07 23:29:59 +00:00
|
|
|
journeys := []internal.PlannedRouteSchedule{}
|
2023-03-29 10:50:25 +00:00
|
|
|
|
|
|
|
counted := int64(0)
|
|
|
|
for _, r := range candidate_routes {
|
2023-03-29 22:45:18 +00:00
|
|
|
ls := r.Route.Features[2].Geometry.(orb.LineString)
|
2023-03-29 10:50:25 +00:00
|
|
|
|
2023-03-29 22:45:18 +00:00
|
|
|
distanceFromDeparture, indexDeparture := geoutils.DistanceFromLineString(departure, ls)
|
|
|
|
distanceFromArrival, indexArrival := geoutils.DistanceFromLineString(arrival, ls)
|
2023-03-29 10:50:25 +00:00
|
|
|
|
2023-04-05 18:57:27 +00:00
|
|
|
if indexArrival >= indexDeparture && distanceFromDeparture <= drad && distanceFromArrival <= arad {
|
2023-03-29 22:45:18 +00:00
|
|
|
//routePoints := []orb.Point{r.Route.Features[0].Point(), departure, arrival, r.Route.Features[1].Point()}
|
2023-04-05 18:57:27 +00:00
|
|
|
routePoints := []orb.Point{r.Route.Features[0].Point(), departure, arrival, r.Route.Features[1].Point()}
|
2023-03-29 22:45:18 +00:00
|
|
|
log.Debug().Any("route points", routePoints).Msg("calculate multipoint route")
|
|
|
|
itinerary, err := h.Routing.Route(routePoints)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error getting route with viapoints")
|
|
|
|
continue
|
|
|
|
}
|
2023-05-07 23:29:59 +00:00
|
|
|
journeys = append(journeys, internal.PlannedRouteSchedule{
|
2023-03-29 22:45:18 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Route: r.Route,
|
|
|
|
DepartureDate: r.DepartureDate,
|
|
|
|
Itinerary: itinerary,
|
|
|
|
})
|
|
|
|
counted = counted + 1
|
|
|
|
if counted == *count {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:35:12 +00:00
|
|
|
if len(journeys) > 0 {
|
2023-05-07 23:29:59 +00:00
|
|
|
err = h.Storage.StoreRouteSchedules(journeys)
|
2023-04-03 18:35:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error saving search results")
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-30 06:44:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 22:45:18 +00:00
|
|
|
return journeys, nil
|
|
|
|
}
|
|
|
|
|
2023-05-07 23:29:59 +00:00
|
|
|
// GetPassengerJourneys searches for matching punctual planned passenger journeys.
|
|
|
|
func (h *CarpoolServiceHandler) GetPassengerJourneys(departure orb.Point, arrival orb.Point, departureRadius *float64, arrivalRadius *float64, minDate time.Time, maxDate time.Time, count *int64) ([]internal.PlannedRouteSchedule, error) {
|
2023-03-29 22:45:18 +00:00
|
|
|
|
|
|
|
log.Debug().
|
|
|
|
Any("departure", departure).
|
|
|
|
Any("arrival", arrival).
|
|
|
|
Any("dep radius", departureRadius).
|
|
|
|
Any("arr radius", arrivalRadius).
|
|
|
|
Str("mindate", minDate.Format(time.RFC3339)).
|
|
|
|
Str("maxdate", maxDate.Format(time.RFC3339)).
|
|
|
|
Any("count", count).
|
|
|
|
Msg("Carpool service handler - GetDriverJourneys")
|
|
|
|
|
|
|
|
routePoints := []orb.Point{departure, arrival}
|
|
|
|
itinerary, err := h.Routing.Route(routePoints)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error getting route with viapoints")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == nil {
|
|
|
|
c := int64(10)
|
|
|
|
count = &c
|
|
|
|
}
|
|
|
|
|
|
|
|
drad := float64(1000)
|
|
|
|
if departureRadius != nil {
|
|
|
|
drad = *departureRadius * 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
arad := float64(1000)
|
|
|
|
if arrivalRadius != nil {
|
|
|
|
arad = *arrivalRadius * 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedPolyline := polylines.Decode(&itinerary.Summary.Polyline, 5)
|
|
|
|
|
|
|
|
points := []orb.Point{departure, arrival}
|
|
|
|
|
|
|
|
for _, p := range decodedPolyline {
|
|
|
|
points = append(points, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
tileset, err := h.Tiles.GetTiles("passenger", minDate, points...)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().
|
|
|
|
Str("date", minDate.Format(time.RFC3339)).
|
|
|
|
Any("departure", departure).
|
|
|
|
Any("arrival", arrival).
|
|
|
|
Err(err).
|
|
|
|
Msg("could not retrieve tiles")
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
candidate_routes := tileset.GetTiledRoutes()
|
|
|
|
|
2023-05-07 23:29:59 +00:00
|
|
|
journeys := []internal.PlannedRouteSchedule{}
|
2023-03-29 22:45:18 +00:00
|
|
|
|
|
|
|
counted := int64(0)
|
|
|
|
for _, r := range candidate_routes {
|
|
|
|
ls := decodedPolyline
|
|
|
|
|
|
|
|
distanceFromDeparture, indexDeparture := geoutils.DistanceFromLineString(r.Route.Features[0].Point(), ls)
|
|
|
|
distanceFromArrival, indexArrival := geoutils.DistanceFromLineString(r.Route.Features[1].Point(), ls)
|
|
|
|
|
|
|
|
if indexArrival >= indexDeparture && distanceFromDeparture <= drad && distanceFromArrival < arad {
|
2023-04-05 18:57:27 +00:00
|
|
|
routePoints := []orb.Point{r.Route.Features[0].Point(), departure, arrival, r.Route.Features[1].Point()}
|
|
|
|
way, err := h.Routing.Route(routePoints)
|
2023-03-29 10:50:25 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("error getting route with viapoints")
|
|
|
|
continue
|
|
|
|
}
|
2023-05-07 23:29:59 +00:00
|
|
|
journeys = append(journeys, internal.PlannedRouteSchedule{
|
2023-03-29 10:50:25 +00:00
|
|
|
ID: r.ID,
|
|
|
|
Route: r.Route,
|
|
|
|
DepartureDate: r.DepartureDate,
|
2023-04-05 18:57:27 +00:00
|
|
|
Itinerary: way,
|
2023-03-29 10:50:25 +00:00
|
|
|
})
|
|
|
|
counted = counted + 1
|
|
|
|
if counted == *count {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:35:12 +00:00
|
|
|
if len(journeys) > 0 {
|
2023-05-07 23:29:59 +00:00
|
|
|
err = h.Storage.StoreRouteSchedules(journeys)
|
2023-04-03 18:35:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Error saving search results")
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-30 06:44:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 10:50:25 +00:00
|
|
|
return journeys, nil
|
|
|
|
}
|