adding search func

This commit is contained in:
mr 2024-07-26 13:45:10 +02:00
parent 6c83e54d37
commit 8c97fca96c
12 changed files with 170 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package mongo
import ( import (
"context" "context"
"errors" "errors"
"strings"
"time" "time"
"cloud.o-forge.io/core/oc-lib/dbs" "cloud.o-forge.io/core/oc-lib/dbs"
@ -234,6 +235,29 @@ func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResul
return res, 200, nil return res, 200, nil
} }
func (m *MongoDB) Search(search string, filter []string, collection_name string) (*mongo.Cursor, int, error) {
opts := options.Find()
opts.SetLimit(100)
if strings.TrimSpace(search) == "*" {
search = ""
} else {
search = `(?i).*` + search + `.*`
}
targetDBCollection := CollectionMap[collection_name]
list := []bson.M{}
for _, k := range filter {
list = append(list, bson.M{k: bson.M{"$regex": search}})
}
if cursor, err := targetDBCollection.Find(
MngoCtx, bson.M{"$or": list},
opts,
); err != nil {
return nil, 404, err
} else {
return cursor, 200, nil
}
}
func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) { func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) {
f := bson.D{} f := bson.D{}
for k, v := range filter { for k, v := range filter {

View File

@ -47,6 +47,14 @@ func GetLogger() zerolog.Logger {
return logs.GetLogger() return logs.GetLogger()
} }
func Search(word string, collection LibDataEnum) LibDataShallow {
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().Search(word)
if err != nil {
return LibDataShallow{Data: d, Code: code, Err: err.Error()}
}
return LibDataShallow{Data: d, Code: code}
}
func LoadAll(collection LibDataEnum) LibDataShallow { func LoadAll(collection LibDataEnum) LibDataShallow {
d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadAll() d, code, err := models.Model(collection.EnumIndex()).GetAccessor().LoadAll()
if err != nil { if err != nil {

View File

@ -53,3 +53,20 @@ func (wfa DataMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
} }
return objs, 200, nil return objs, 200, nil
} }
func (wfa *DataMongoAccessor) Search(word string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.Search(word, []string{"name", "short_description", "description", "owner", "source_url"}, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []DataResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@ -55,3 +55,20 @@ func (wfa DatacenterMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, erro
} }
return objs, 200, nil return objs, 200, nil
} }
func (wfa *DatacenterMongoAccessor) Search(word string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.Search(word, []string{"name", "short_description", "description", "owner", "source_url"}, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []DatacenterResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@ -56,3 +56,20 @@ func (wfa ProcessingMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, erro
} }
return objs, 200, nil return objs, 200, nil
} }
func (wfa *ProcessingMongoAccessor) Search(word string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.Search(word, []string{"name", "short_description", "description", "owner", "source_url"}, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []ProcessingResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@ -56,3 +56,20 @@ func (wfa StorageMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error)
} }
return objs, 200, nil return objs, 200, nil
} }
func (wfa *StorageMongoAccessor) Search(word string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.Search(word, []string{"name", "short_description", "description", "owner", "source_url"}, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []StorageResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@ -59,3 +59,20 @@ func (wfa WorkflowResourceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int
} }
return objs, 200, nil return objs, 200, nil
} }
func (wfa *WorkflowResourceMongoAccessor) Search(word string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.Search(word, []string{"name", "short_description", "description", "owner", "source_url"}, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowResource
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r.AbstractResource)
}
return objs, 200, nil
}

View File

@ -18,6 +18,7 @@ type DBObject interface {
type Accessor interface { type Accessor interface {
SetLogger(t DataType) SetLogger(t DataType)
GetType() string GetType() string
Search(word string) ([]ShallowDBObject, int, error)
LoadAll() ([]ShallowDBObject, int, error) LoadAll() ([]ShallowDBObject, int, error)
LoadOne(id string) (DBObject, int, error) LoadOne(id string) (DBObject, int, error)
DeleteOne(id string) (DBObject, int, error) DeleteOne(id string) (DBObject, int, error)

View File

@ -149,3 +149,20 @@ func (wfa WorkflowMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error)
} }
return objs, 200, nil return objs, 200, nil
} }
func (wfa *WorkflowMongoAccessor) Search(word string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.Search(word, []string{"name"}, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Workflow
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@ -4,7 +4,7 @@ import "time"
type WorkflowSchedule struct { type WorkflowSchedule struct {
Id string `json:"id"` Id string `json:"id"`
Start time.Time `json:"start" bson:"start validate:"required""` Start time.Time `json:"start" bson:"start" validate:"required"`
End time.Time `json:"end,omitempty" bson:"end,omitempty"` End time.Time `json:"end,omitempty" bson:"end,omitempty"`
Cron string `json:"cron,omitempty" bson:"cron,omitempty"` Cron string `json:"cron,omitempty" bson:"cron,omitempty"`
} }

View File

@ -52,3 +52,20 @@ func (wfa WorkflowExecutionMongoAccessor) LoadAll() ([]utils.ShallowDBObject, in
} }
return objs, 200, nil return objs, 200, nil
} }
func (wfa *WorkflowExecutionMongoAccessor) Search(word string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.Search(word, []string{}, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []WorkflowExecution
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}

View File

@ -119,3 +119,20 @@ func (wfa WorkspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error
} }
return objs, 200, nil return objs, 200, nil
} }
func (wfa *WorkspaceMongoAccessor) Search(word string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.Search(word, []string{"name"}, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not store to db. Error: " + err.Error())
return nil, code, err
}
var results []Workspace
if err = res_mongo.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, r := range results {
objs = append(objs, &r)
}
return objs, 200, nil
}