lightest + clearest code

This commit is contained in:
mr
2024-11-28 16:49:41 +01:00
parent 8a11805fe8
commit 8d701e67d8
34 changed files with 444 additions and 1448 deletions

View File

@@ -5,7 +5,6 @@ import (
"fmt"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/logs"
"cloud.o-forge.io/core/oc-lib/models/collaborative_area/shallow_collaborative_area"
"cloud.o-forge.io/core/oc-lib/models/peer"
@@ -26,7 +25,7 @@ func New(t tools.DataType, peerID string, groups []string, caller *tools.HTTPCal
Caller: caller,
PeerID: peerID,
Groups: groups, // Set the caller
Type: t.String(),
Type: t,
},
}
}
@@ -44,11 +43,7 @@ func (wfa *workspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, er
// UpdateOne updates a workspace in the database, given its ID, it automatically share to peers if the workspace is shared
func (wfa *workspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
d := set.(*Workspace) // Get the workspace from the set
d.DataResources = nil // Reset the resources
d.ComputeResources = nil
d.StorageResources = nil
d.ProcessingResources = nil
d.WorkflowResources = nil
d.Clear()
if d.Active { // If the workspace is active, deactivate all the other workspaces
res, _, err := wfa.LoadAll()
if err == nil {
@@ -80,11 +75,7 @@ func (wfa *workspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject
}
// reset the resources
d := data.(*Workspace)
d.DataResources = nil
d.ComputeResources = nil
d.StorageResources = nil
d.ProcessingResources = nil
d.WorkflowResources = nil
d.Clear()
return wfa.GenericStoreOne(d, wfa)
}
@@ -93,62 +84,25 @@ func (wfa *workspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject,
return wfa.GenericStoreOne(data, wfa)
}
// LoadOne loads a workspace from the database, given its ID
func (wfa *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
var workflow Workspace
res_mongo, code, err := mongo.MONGOService.LoadOne(id, wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve " + id + " from db. Error: " + err.Error())
return nil, code, err
}
res_mongo.Decode(&workflow)
workflow.Fill(wfa.PeerID, wfa.Groups)
return &workflow, 200, nil
func (a *workspaceMongoAccessor) LoadOne(id string) (utils.DBObject, int, error) {
return utils.GenericLoadOne[*Workspace](id, func(d utils.DBObject) (utils.DBObject, int, error) {
d.(*Workspace).Fill(a.PeerID, a.Groups)
return d, 200, nil
}, a)
}
// LoadAll loads all the workspaces from the database
func (wfa workspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
res_mongo, code, err := mongo.MONGOService.LoadAll(wfa.GetType())
if err != nil {
wfa.Logger.Error().Msg("Could not retrieve any from 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 {
r.Fill(wfa.PeerID, wfa.Groups)
objs = append(objs, &r)
}
return objs, 200, nil
func (wfa *workspaceMongoAccessor) LoadAll() ([]utils.ShallowDBObject, int, error) {
return utils.GenericLoadAll[*Workspace](func(d utils.DBObject) utils.ShallowDBObject {
d.(*Workspace).Fill(wfa.PeerID, wfa.Groups)
return d
}, wfa)
}
// Search searches for workspaces in the database, given some filters OR a search string
func (wfa *workspaceMongoAccessor) Search(filters *dbs.Filters, search string) ([]utils.ShallowDBObject, int, error) {
objs := []utils.ShallowDBObject{}
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
filters = &dbs.Filters{
Or: map[string][]dbs.Filter{ // filter by name if no filters are provided
"abstractobject.name": {{Operator: dbs.LIKE.String(), Value: search}},
},
}
}
res_mongo, code, err := mongo.MONGOService.Search(filters, 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 {
r.Fill(wfa.PeerID, wfa.Groups)
objs = append(objs, &r)
}
return objs, 200, nil
return utils.GenericSearch[*Workspace](filters, search, (&Workspace{}).GetObjectFilters(search), func(d utils.DBObject) utils.ShallowDBObject {
d.(*Workspace).Fill(wfa.PeerID, wfa.Groups)
return d
}, wfa)
}
/*