solidarity transport updates

This commit is contained in:
Arnaud Delcasse
2025-09-09 05:47:56 +02:00
parent 95b60ea737
commit 9ab7b66b68
40 changed files with 3240 additions and 601 deletions

View File

@@ -1,9 +1,15 @@
package sorting
import (
"cmp"
"encoding/json"
"strings"
"git.coopgo.io/coopgo-platform/fleets/storage"
fleetsstorage "git.coopgo.io/coopgo-platform/fleets/storage"
"github.com/paulmach/orb/geo"
"github.com/paulmach/orb/geojson"
"github.com/rs/zerolog/log"
)
type VehiclesByLicencePlate []fleetsstorage.Vehicle
@@ -21,3 +27,47 @@ func (a BookingsByStartdate) Less(i, j int) bool {
return a[i].Startdate.Before(a[j].Startdate)
}
func (a BookingsByStartdate) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// Functions
func VehiclesByDistanceFrom(from geojson.Feature) func(vehicle1, vehicle2 storage.Vehicle) int {
return func(vehicle1, vehicle2 storage.Vehicle) int {
vehicle1Address, ok := vehicle1.Data["address"]
if !ok {
return 1
}
vehicle1Json, err := json.Marshal(vehicle1Address)
if err != nil {
log.Error().Err(err).Msg("failed marshalling vehicle 1 json")
return 1
}
vehicle1Geojson, err := geojson.UnmarshalFeature(vehicle1Json)
if err != nil {
log.Error().Err(err).Msg("failed unmarshalling vehicle 1 geojson")
return 1
}
vehicle2Address, ok := vehicle2.Data["address"]
if !ok {
log.Debug().Msg("Vehicle 2 does not have an address")
return -1
}
vehicle2Json, err := json.Marshal(vehicle2Address)
if err != nil {
log.Error().Err(err).Msg("failed marshalling vehicle 2 json")
return -1
}
vehicle2Geojson, err := geojson.UnmarshalFeature(vehicle2Json)
if err != nil {
log.Error().Err(err).Msg("failed unmarshalling vehicle 2 geojson")
return -1
}
distance1 := geo.Distance(from.Point(), vehicle1Geojson.Point())
distance2 := geo.Distance(from.Point(), vehicle2Geojson.Point())
return cmp.Compare(distance1, distance2)
}
}

View File

@@ -1,13 +0,0 @@
package sorting
import (
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
)
type SolidarityDriversByName []mobilityaccountsstorage.Account
func (e SolidarityDriversByName) Len() int { return len(e) }
func (e SolidarityDriversByName) Less(i, j int) bool {
return e[i].Data["first_name"].(string) < e[j].Data["first_name"].(string)
}
func (e SolidarityDriversByName) Swap(i, j int) { e[i], e[j] = e[j], e[i] }

View File

@@ -0,0 +1,32 @@
package sorting
import (
"strings"
mobilityaccountsstorage "git.coopgo.io/coopgo-platform/mobility-accounts/storage"
"git.coopgo.io/coopgo-platform/solidarity-transport/servers/grpc/proto/gen"
)
type SolidarityDriversByName []mobilityaccountsstorage.Account
func (e SolidarityDriversByName) Len() int { return len(e) }
func (e SolidarityDriversByName) Less(i, j int) bool {
return e[i].Data["first_name"].(string) < e[j].Data["first_name"].(string)
}
func (e SolidarityDriversByName) Swap(i, j int) { e[i], e[j] = e[j], e[i] }
type SolidarityAvailabilitiesByDay []*gen.DriverRegularAvailability
func (e SolidarityAvailabilitiesByDay) Len() int { return len(e) }
func (e SolidarityAvailabilitiesByDay) Less(i, j int) bool {
if e[i].Day == e[j].Day {
return strings.Compare(e[i].StartTime, e[j].StartTime) < 0
}
if e[i].Day == 0 {
return false
}
return e[i].Day < e[j].Day
}
func (e SolidarityAvailabilitiesByDay) Swap(i, j int) { e[i], e[j] = e[j], e[i] }