first try
This commit is contained in:
11
storage/Diag.go
Normal file
11
storage/Diag.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package storage
|
||||
|
||||
type Diag struct {
|
||||
ID string `json:"id" bson:"_id"`
|
||||
Name string `json:"name"`
|
||||
Namespace string `json:"namespace"`
|
||||
Json_schema string `json:"json_schema"`
|
||||
Ui_schema string `json:"ui_schema"`
|
||||
Data map[string]any `json:"data"`
|
||||
Deleted bool `json:"deleted"`
|
||||
}
|
||||
114
storage/mongodb.go
Normal file
114
storage/mongodb.go
Normal file
@@ -0,0 +1,114 @@
|
||||
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
|
||||
}
|
||||
29
storage/storage.go
Normal file
29
storage/storage.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
CreateDiag(Diag) error
|
||||
GetDiag(string) (*Diag, error)
|
||||
GetDiags(namespaces []string) ([]Diag, error)
|
||||
UpdateDiag(Diag) error}
|
||||
type StorageImpl struct {
|
||||
}
|
||||
|
||||
func NewStorage(cfg *viper.Viper) (Storage, error) {
|
||||
var (
|
||||
storage_type = cfg.GetString("storage.db.type")
|
||||
)
|
||||
|
||||
switch storage_type {
|
||||
case "mongodb":
|
||||
s, err := NewMongoDBStorage(cfg)
|
||||
return s, err
|
||||
default:
|
||||
return nil, fmt.Errorf("storage type %v is not supported", storage_type)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user