114 lines
2.6 KiB
Go
114 lines
2.6 KiB
Go
|
package storage
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/rs/zerolog/log"
|
||
|
"github.com/spf13/viper"
|
||
|
"go.mongodb.org/mongo-driver/mongo"
|
||
|
"go.mongodb.org/mongo-driver/mongo/options"
|
||
|
"gopkg.in/mgo.v2/bson"
|
||
|
)
|
||
|
|
||
|
type MongoDBStorage struct {
|
||
|
*mongo.Client
|
||
|
DbName string
|
||
|
Collections map[string]string
|
||
|
}
|
||
|
|
||
|
func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
|
||
|
var (
|
||
|
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_diags = cfg.GetString("storage.db.mongodb.collections.diags")
|
||
|
)
|
||
|
|
||
|
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{
|
||
|
"diags": mongodb_diags,
|
||
|
},
|
||
|
}
|
||
|
//TODO Indexes
|
||
|
return storage, err
|
||
|
}
|
||
|
|
||
|
func (s MongoDBStorage) CreateDiag(diag Diag) error {
|
||
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["diags"])
|
||
|
if _, err := collection.InsertOne(context.TODO(), diag); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (s MongoDBStorage) GetDiag(id string) (*Diag, error) {
|
||
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["diags"])
|
||
|
|
||
|
diag := &Diag{}
|
||
|
if err := collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(diag); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return diag, nil
|
||
|
}
|
||
|
|
||
|
func (s MongoDBStorage) GetDiags(namespaces []string) (diags []Diag, err error) {
|
||
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["diags"])
|
||
|
|
||
|
var cur *mongo.Cursor
|
||
|
|
||
|
findOptions := options.Find()
|
||
|
//findOptions.SetSort(bson.D{{"startdate", 1}})
|
||
|
|
||
|
if len(namespaces) == 0 {
|
||
|
cur, err = collection.Find(context.TODO(), bson.D{}, findOptions)
|
||
|
if err != nil {
|
||
|
log.Error().Err(err).Msg("")
|
||
|
return diags, err
|
||
|
}
|
||
|
} else {
|
||
|
cur, err = collection.Find(context.TODO(), bson.M{"namespace": bson.M{"$in": namespaces}}, findOptions)
|
||
|
if err != nil {
|
||
|
log.Error().Err(err).Msg("")
|
||
|
return diags, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for cur.Next(context.TODO()) {
|
||
|
var diag Diag
|
||
|
//var elem bson.M
|
||
|
|
||
|
if err := cur.Decode(&diag); err != nil {
|
||
|
return diags, err
|
||
|
}
|
||
|
|
||
|
diags = append(diags, diag)
|
||
|
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (s MongoDBStorage) UpdateDiag(diag Diag) error {
|
||
|
collection := s.Client.Database(s.DbName).Collection(s.Collections["diags"])
|
||
|
if _, err := collection.ReplaceOne(context.TODO(), bson.M{"_id": diag.ID}, diag); err != nil {
|
||
|
log.Error().Err(err).Msg("")
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|