initial commit

This commit is contained in:
2023-03-27 20:54:56 +02:00
commit 77c8576254
40 changed files with 7460 additions and 0 deletions

18
handler/handler.go Normal file
View File

@@ -0,0 +1,18 @@
package handler
import (
"git.coopgo.io/coopgo-platform/carpool-service/storage"
"github.com/spf13/viper"
)
type CarpoolServiceHandler struct {
Config *viper.Viper
Storage storage.Storage
}
func NewCarpoolServiceHandler(cfg *viper.Viper, storage storage.Storage) (*CarpoolServiceHandler, error) {
return &CarpoolServiceHandler{
Config: cfg,
Storage: storage,
}, nil
}

84
handler/management.go Normal file
View File

@@ -0,0 +1,84 @@
package handler
import (
"errors"
"time"
"git.coopgo.io/coopgo-platform/routing-service/encoding/polylines"
"github.com/google/uuid"
"github.com/paulmach/orb/geojson"
"github.com/rs/zerolog/log"
)
func (h *CarpoolServiceHandler) CreateRegularRoutes(routes []*geojson.FeatureCollection) error {
groupid := uuid.NewString()
for _, r := range routes {
r.ExtraMembers["id"] = uuid.NewString()
r.ExtraMembers["routes_group_id"] = groupid
properties, ok := r.ExtraMembers["properties"].(map[string]any)
if !ok {
return errors.New("no properties found in feature collection")
}
polyline, ok := properties["polyline"].(string)
if !ok {
return errors.New("no polyline found in properties from feature collection")
}
lineString := geojson.NewFeature(polylines.Decode(&polyline, 5))
lineString.Properties["encoded_polyline"] = polyline
r.Append(lineString)
}
if err := h.Storage.CreateRegularRoutes(routes); err != nil {
return err
}
return nil
}
func (h *CarpoolServiceHandler) GetUserPlanning(userid string, minDepartureDate time.Time, maxDepartureDate time.Time) (map[string][]PlannedRouteSchedule, error) {
log.Debug().
Str("user_id", userid).
Time("min_departure_date", minDepartureDate).
Time("max_departure_date", maxDepartureDate).
Msg("carpool service handler - GetUserPlanning")
results := map[string][]PlannedRouteSchedule{}
current_date := minDepartureDate
for current_date.Before(maxDepartureDate) {
results[current_date.Format("2006-01-02")] = []PlannedRouteSchedule{}
current_date = current_date.Add(24 * time.Hour)
}
routes, err := h.Storage.GetUserRegularRoutes(userid)
if err != nil {
log.Error().Err(err).Msg("error in storage - GetUserRegularRoutes")
return nil, err
}
for _, r := range routes {
rr := RegularRoute(*r)
schedules, err := rr.PlannedJourneySchedules(minDepartureDate, maxDepartureDate)
if err != nil {
log.Error().Err(err)
return nil, err
}
for _, s := range schedules {
log.Debug().
Str("route id", s.Route.ExtraMembers.MustString("id")).
Any("datetime", s.DepartureDate.Format(time.RFC3339)).
Msg("new schedule")
dateString := s.DepartureDate.Format("2006-01-02")
results[dateString] = append(results[dateString], s)
}
}
return results, nil
}

78
handler/routes_helpers.go Normal file
View File

@@ -0,0 +1,78 @@
package handler
import (
"errors"
"strconv"
"strings"
"time"
"github.com/paulmach/orb/geojson"
"github.com/rs/zerolog/log"
)
type PlannedRouteSchedule struct {
Route RegularRoute
DepartureDate time.Time
}
type RegularRoute geojson.FeatureCollection
func (rr RegularRoute) PlannedJourneySchedules(mindate time.Time, maxdate time.Time) ([]PlannedRouteSchedule, error) {
log.Debug().
Str("regular_route.id", rr.ExtraMembers.MustString("id", "")).
Str("mindate", mindate.Format(time.RFC3339)).
Str("maxdate", maxdate.Format(time.RFC3339)).
Msg("carpool service handler - PlannedJourneySchedules")
results := []PlannedRouteSchedule{}
current_date := mindate
for current_date.Before(maxdate) {
day := strings.ToUpper(current_date.Format("Mon"))
time_of_day, err := rr.checkSchedules(day)
if err != nil {
log.Error().
Err(err).
Str("day", day).
Msg("schedules not found")
}
if time_of_day != "" {
splitted := strings.Split(time_of_day, ":")
h, _ := strconv.Atoi(splitted[0])
m, _ := strconv.Atoi(splitted[1])
t := time.Date(current_date.Year(), current_date.Month(), current_date.Day(), h, m, 0, 0, time.Local)
results = append(results, PlannedRouteSchedule{
Route: rr,
DepartureDate: t,
})
}
current_date = current_date.Add(24 * time.Hour)
}
return results, nil
}
func (rr RegularRoute) checkSchedules(day string) (timeOfDay string, err error) {
if properties, ok := rr.ExtraMembers["properties"]; ok {
if propertiesMap, ok := properties.(map[string]any); ok {
if schedules, ok := propertiesMap["schedules"]; ok {
if schedulesSlice, ok := schedules.([]any); ok {
for _, sched := range schedulesSlice {
if daySchedule, ok := sched.(map[string]any); ok {
if daySchedule["day"].(string) == day {
return daySchedule["time_of_day"].(string), nil
}
}
}
}
}
}
}
return "", errors.New("not found")
}
func (rr RegularRoute) FeatureCollection() *geojson.FeatureCollection {
fc := geojson.FeatureCollection(rr)
return &fc
}

12
handler/search.go Normal file
View File

@@ -0,0 +1,12 @@
package handler
import (
"time"
"git.coopgo.io/coopgo-platform/carpool-service/interoperability/ocss"
"github.com/paulmach/orb"
)
func (h *CarpoolServiceHandler) GetDriverJourneys(departure orb.Point, arrival orb.Point, minDate time.Time, maxDate time.Time) ([]ocss.DriverJourney, error) {
return nil, nil
}