carpool-service/storage/mongodb.go

286 lines
8.2 KiB
Go
Raw Normal View History

2023-03-27 18:54:56 +00:00
package storage
import (
"context"
"encoding/json"
2023-03-30 06:44:58 +00:00
"fmt"
2023-03-27 18:54:56 +00:00
2023-03-30 06:44:58 +00:00
"git.coopgo.io/coopgo-platform/carpool-service/internal"
2023-03-27 18:54:56 +00:00
"github.com/paulmach/orb/geojson"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MongoDBStorage struct {
*mongo.Client
DbName string
Collections map[string]string
}
func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
var (
2023-03-30 06:44:58 +00:00
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")
mongodb_regular_routes = cfg.GetString("storage.db.mongodb.collections.regular_routes")
mongodb_punctual_routes = cfg.GetString("storage.db.mongodb.collections.punctual_routes")
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")
2023-03-27 18:54:56 +00:00
)
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://" + mongodb_host + ":" + mongodb_port))
if err != nil {
return MongoDBStorage{}, err
}
err = client.Connect(context.TODO())
if err != nil {
return MongoDBStorage{}, err
}
storage := MongoDBStorage{
Client: client,
DbName: mongodb_dbname,
Collections: map[string]string{
2023-03-30 06:44:58 +00:00
"regular_routes": mongodb_regular_routes,
"punctual_routes": mongodb_punctual_routes,
"bookings": mongodb_bookings,
"driver_candidate_journeys": mongodb_driver_candidate_journeys,
"passenger_candidate_journeys": mongodb_passenger_candidate_journeys,
2023-03-27 18:54:56 +00:00
},
}
return storage, nil
}
func (s MongoDBStorage) CreateRegularRoutes(routes []*geojson.FeatureCollection) error {
log.Debug().Msg("Storage - CreateRegularRoutes")
documents := []any{}
for _, fc := range routes {
if fc != nil {
fc.ExtraMembers["_id"] = fc.ExtraMembers.MustString("id")
delete(fc.ExtraMembers, "id")
documents = append(documents, fc)
}
}
collection := s.Client.Database(s.DbName).Collection(s.Collections["regular_routes"])
if _, err := collection.InsertMany(context.TODO(), documents); err != nil {
return err
}
return nil
}
func (s MongoDBStorage) GetUserRegularRoutes(userid string) ([]*geojson.FeatureCollection, error) {
findOptions := options.Find()
collection := s.Client.Database(s.DbName).Collection(s.Collections["regular_routes"])
cur, err := collection.Find(context.TODO(), bson.M{"properties.user.id": userid}, findOptions)
if err != nil {
return nil, err
}
results := []*geojson.FeatureCollection{}
for cur.Next(context.TODO()) {
var elem bson.M
if err := cur.Decode(&elem); err != nil {
return nil, err
}
bsonBytes, _ := bson.Marshal(elem)
fc := geojson.NewFeatureCollection()
err := fc.UnmarshalBSON(bsonBytes)
if err != nil {
return nil, err
2023-03-29 10:50:25 +00:00
}
fc.ExtraMembers["id"] = fc.ExtraMembers.MustString("_id")
delete(fc.ExtraMembers, "_id")
for k, v := range fc.ExtraMembers {
if val, ok := v.(primitive.D); ok {
em := map[string]any{}
jsonbytes, _ := bson.MarshalExtJSON(val, true, true)
json.Unmarshal(jsonbytes, &em)
fc.ExtraMembers[k] = em
}
}
results = append(results, fc)
}
return results, nil
}
func (s MongoDBStorage) GetDriverRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error) {
2023-03-29 22:45:18 +00:00
log.Debug().
Str("day", day).
Int64("grid id", gridId).
Msg("GetDriverRegularRoutesForTile")
2023-03-29 10:50:25 +00:00
findOptions := options.Find()
collection := s.Client.Database(s.DbName).Collection(s.Collections["regular_routes"])
cur, err := collection.Find(context.TODO(), bson.M{
2023-03-29 22:45:18 +00:00
"properties.schedules.day": day,
"grid_ids": gridId,
"properties.is_driver": true,
2023-03-29 10:50:25 +00:00
}, findOptions)
if err != nil {
2023-03-29 22:45:18 +00:00
log.Error().Err(err).Msg("GetDriverRegularRoutesForTile - mongodb issue")
2023-03-29 10:50:25 +00:00
return nil, err
}
results := []*geojson.FeatureCollection{}
for cur.Next(context.TODO()) {
var elem bson.M
if err := cur.Decode(&elem); err != nil {
2023-03-29 22:45:18 +00:00
log.Error().Err(err).Msg("GetDriverRegularRoutesForTile - decode error")
2023-03-29 10:50:25 +00:00
return nil, err
}
bsonBytes, _ := bson.Marshal(elem)
fc := geojson.NewFeatureCollection()
err := fc.UnmarshalBSON(bsonBytes)
if err != nil {
2023-03-29 22:45:18 +00:00
log.Error().Err(err).Msg("GetDriverRegularRoutesForTile - unmarshalling error")
2023-03-29 10:50:25 +00:00
return nil, err
}
fc.ExtraMembers["id"] = fc.ExtraMembers.MustString("_id")
delete(fc.ExtraMembers, "_id")
for k, v := range fc.ExtraMembers {
if val, ok := v.(primitive.D); ok {
em := map[string]any{}
jsonbytes, _ := bson.MarshalExtJSON(val, true, true)
json.Unmarshal(jsonbytes, &em)
fc.ExtraMembers[k] = em
}
}
results = append(results, fc)
}
return results, nil
}
func (s MongoDBStorage) GetPassengerRegularRoutesForTile(day string, gridId int64) ([]*geojson.FeatureCollection, error) {
findOptions := options.Find()
collection := s.Client.Database(s.DbName).Collection(s.Collections["regular_routes"])
cur, err := collection.Find(context.TODO(), bson.M{
2023-03-29 22:45:18 +00:00
"properties.schedules.day": day,
"grid_ids": gridId,
"properties.is_passenger": true,
2023-03-29 10:50:25 +00:00
}, findOptions)
if err != nil {
return nil, err
}
results := []*geojson.FeatureCollection{}
for cur.Next(context.TODO()) {
var elem bson.M
if err := cur.Decode(&elem); err != nil {
return nil, err
}
bsonBytes, _ := bson.Marshal(elem)
fc := geojson.NewFeatureCollection()
err := fc.UnmarshalBSON(bsonBytes)
if err != nil {
return nil, err
2023-03-27 18:54:56 +00:00
}
fc.ExtraMembers["id"] = fc.ExtraMembers.MustString("_id")
delete(fc.ExtraMembers, "_id")
for k, v := range fc.ExtraMembers {
if val, ok := v.(primitive.D); ok {
em := map[string]any{}
jsonbytes, _ := bson.MarshalExtJSON(val, true, true)
json.Unmarshal(jsonbytes, &em)
fc.ExtraMembers[k] = em
}
}
results = append(results, fc)
}
return results, nil
}
2023-03-30 06:44:58 +00:00
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)])
if _, err := collection.InsertMany(context.TODO(), documents); err != nil {
return err
}
return nil
}
func (s MongoDBStorage) GetSearchResult(driverOrPassenger, id string) (searchResult *internal.SearchResult, err 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)
return
}
func (s MongoDBStorage) CreateBooking(booking internal.Booking) error {
collection := s.Client.Database(s.DbName).Collection(s.Collections["bookings"])
_, err := collection.InsertOne(context.TODO(), booking)
if err != nil {
return err
}
return nil
}
func (s MongoDBStorage) GetBooking(id string) (booking *internal.Booking, err error) {
var b internal.Booking
log.Debug().Str("booking id", id).Msg("get booking in DB")
collection := s.Client.Database(s.DbName).Collection(s.Collections["bookings"])
err = collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(&b)
if err != nil {
log.Error().Err(err).Msg("error")
return nil, err
}
return &b, nil
}
2023-03-27 18:54:56 +00:00
// func (s MongoDBStorage) CreatePassengerRegularTrips(trips []*geojson.FeatureCollection) error {
// log.Debug().Msg("Storage - CreatePassengerRegularTrips")
// documents := []any{}
// for _, fc := range trips {
// if fc != nil {
// fc.ExtraMembers["_id"] = fc.ExtraMembers.MustString("id")
// delete(fc.ExtraMembers, "id")
// documents = append(documents, fc)
// }
// }
// collection := s.Client.Database(s.DbName).Collection(s.Collections["passenger_regular_trips"])
// if _, err := collection.InsertMany(context.TODO(), documents); err != nil {
// return err
// }
// return nil
// }