regular routes journeys, persistent KV to store return states, ...
This commit is contained in:
@@ -23,6 +23,7 @@ type MongoDBStorage struct {
|
||||
|
||||
func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
|
||||
var (
|
||||
mongodb_uri = cfg.GetString("storage.db.mongodb.uri")
|
||||
mongodb_host = cfg.GetString("storage.db.mongodb.host")
|
||||
mongodb_port = cfg.GetString("storage.db.mongodb.port")
|
||||
mongodb_dbname = cfg.GetString("storage.db.mongodb.db_name")
|
||||
@@ -31,9 +32,14 @@ func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
|
||||
mongodb_bookings = cfg.GetString("storage.db.mongodb.collections.bookings")
|
||||
mongodb_driver_candidate_journeys = cfg.GetString("storage.db.mongodb.collections.driver_candidate_journeys")
|
||||
mongodb_passenger_candidate_journeys = cfg.GetString("storage.db.mongodb.collections.passenger_candidate_journeys")
|
||||
mongodb_persisted_kv = cfg.GetString("storage.db.mongodb.collections.persisted_kv")
|
||||
)
|
||||
|
||||
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://" + mongodb_host + ":" + mongodb_port))
|
||||
if mongodb_uri == "" {
|
||||
mongodb_uri = fmt.Sprintf("mongodb://%s:%s/%s", mongodb_host, mongodb_port, mongodb_dbname)
|
||||
}
|
||||
|
||||
client, err := mongo.NewClient(options.Client().ApplyURI(mongodb_uri))
|
||||
if err != nil {
|
||||
return MongoDBStorage{}, err
|
||||
}
|
||||
@@ -53,6 +59,7 @@ func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
|
||||
"bookings": mongodb_bookings,
|
||||
"driver_candidate_journeys": mongodb_driver_candidate_journeys,
|
||||
"passenger_candidate_journeys": mongodb_passenger_candidate_journeys,
|
||||
"persisted_kv": mongodb_persisted_kv,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -215,29 +222,67 @@ func (s MongoDBStorage) GetPassengerRegularRoutesForTile(day string, gridId int6
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) StoreSearchResults(driverOrPassenger string, searchresults []internal.SearchResult) error {
|
||||
|
||||
log.Debug().Msg("Storage - CreateRegularRoutes")
|
||||
|
||||
documents := []any{}
|
||||
for _, sr := range searchresults {
|
||||
documents = append(documents, sr)
|
||||
}
|
||||
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections[fmt.Sprintf("%s_candidate_journeys", driverOrPassenger)])
|
||||
func (s MongoDBStorage) PersistedKVPut(documents []any) error {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["persisted_kv"])
|
||||
if _, err := collection.InsertMany(context.TODO(), documents); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
func (s MongoDBStorage) PersistedKVGet(id string, document any) error {
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections["persisted_kv"])
|
||||
err := collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(document)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetSearchResult(driverOrPassenger, id string) (searchResult *internal.SearchResult, err error) {
|
||||
func (s MongoDBStorage) StoreSearchResults(searchresults []internal.SearchResult) error {
|
||||
|
||||
collection := s.Client.Database(s.DbName).Collection(s.Collections[fmt.Sprintf("%s_candidate_journeys", driverOrPassenger)])
|
||||
err = collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(searchResult)
|
||||
log.Debug().Msg("Storage - StoreSearchResults")
|
||||
|
||||
return
|
||||
documents := []any{}
|
||||
for _, sr := range searchresults {
|
||||
documents = append(documents, sr)
|
||||
}
|
||||
|
||||
return s.PersistedKVPut(documents)
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetSearchResult(id string) (*internal.SearchResult, error) {
|
||||
|
||||
var result internal.SearchResult
|
||||
err := s.PersistedKVGet(id, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) StoreRouteSchedules(js []internal.PlannedRouteSchedule) error {
|
||||
|
||||
log.Debug().Msg("Storage - StoreRouteSchedules")
|
||||
|
||||
documents := []any{}
|
||||
for _, sr := range js {
|
||||
documents = append(documents, sr)
|
||||
}
|
||||
|
||||
return s.PersistedKVPut(documents)
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) GetRouteSchedules(id string) (*internal.PlannedRouteSchedule, error) {
|
||||
|
||||
var result internal.PlannedRouteSchedule
|
||||
err := s.PersistedKVGet(id, &result)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s MongoDBStorage) CreateBooking(booking internal.Booking) error {
|
||||
|
||||
@@ -14,11 +14,16 @@ type Storage interface {
|
||||
GetDriverRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error)
|
||||
GetPassengerRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error)
|
||||
|
||||
StoreSearchResults(string, []internal.SearchResult) error
|
||||
GetSearchResult(driverOrPassenger, id string) (*internal.SearchResult, error)
|
||||
|
||||
CreateBooking(internal.Booking) error
|
||||
GetBooking(id string) (*internal.Booking, error)
|
||||
|
||||
// Caching temporary results
|
||||
PersistedKVPut(documents []any) error
|
||||
PersistedKVGet(id string, document any) error
|
||||
StoreSearchResults([]internal.SearchResult) error
|
||||
GetSearchResult(id string) (*internal.SearchResult, error)
|
||||
StoreRouteSchedules([]internal.PlannedRouteSchedule) error
|
||||
GetRouteSchedules(id string) (*internal.PlannedRouteSchedule, error)
|
||||
}
|
||||
|
||||
func NewStorage(cfg *viper.Viper) (Storage, error) {
|
||||
|
||||
Reference in New Issue
Block a user