Adding Workspace Logic

This commit is contained in:
mr 2024-07-25 09:28:55 +02:00
parent 51e94e73e5
commit 094ae0a7f0
8 changed files with 203 additions and 7 deletions

View File

@ -163,6 +163,26 @@ func (m *MongoDB) DeleteMultiple(f map[string]interface{}, collection_name strin
return result.DeletedCount, 200, nil
}
func (m *MongoDB) UpdateMultiple(set interface{}, filter map[string]interface{}, collection_name string) (int64, int, error) {
var doc map[string]interface{}
b, _ := bson.Marshal(set)
bson.Unmarshal(b, &doc)
f := bson.D{}
for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.UpdateMany(MngoCtx, f, dbs.InputToBson(doc, true))
if err != nil {
m.Logger.Error().Msg("Couldn't update resource: " + err.Error())
return 0, 404, err
}
return res.UpsertedCount, 200, nil
}
func (m *MongoDB) UpdateOne(set interface{}, id string, collection_name string) (string, int, error) {
var doc map[string]interface{}
b, _ := bson.Marshal(set)
@ -214,6 +234,24 @@ func (m *MongoDB) LoadOne(id string, collection_name string) (*mongo.SingleResul
return res, 200, nil
}
func (m *MongoDB) LoadFilter(filter map[string]interface{}, collection_name string) (*mongo.Cursor, int, error) {
f := bson.D{}
for k, v := range filter {
f = append(f, bson.E{Key: k, Value: v})
}
targetDBCollection := CollectionMap[collection_name]
MngoCtx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetDBCollection.Find(MngoCtx, f)
if err != nil {
m.Logger.Error().Msg("Couldn't find any resources. Error : " + err.Error())
return nil, 404, err
}
return res, 200, nil
}
func (m *MongoDB) LoadAll(collection_name string) (*mongo.Cursor, int, error) {
targetDBCollection := CollectionMap[collection_name]

View File

@ -18,6 +18,7 @@ const (
DATACENTER_RESOURCE = utils.DATACENTER_RESOURCE
WORKFLOW_RESOURCE = utils.WORKFLOW_RESOURCE
WORKFLOW = utils.WORKFLOW
WORKSPACE = utils.WORKSPACE
)
func (d LibDataEnum) EnumIndex() int {

View File

@ -10,13 +10,9 @@ import (
)
type AbstractWorkflow struct {
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"`
Datas []string `bson:"datas,omitempty" json:"datas,omitempty"`
Storages []string `bson:"storages,omitempty" json:"storages,omitempty"`
ProcessingResource []string `bson:"processing,omitempty" json:"processing,omitempty"`
Datacenters []string `bson:"datacenters,omitempty" json:"datacenters,omitempty"`
Workflows []string `bson:"workflows,omitempty" json:"workflows,omitempty"`
Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"`
utils.ResourceSet
Graph *graph.Graph `bson:"graph,omitempty" json:"graph,omitempty"`
Schedule *WorkflowSchedule `bson:"schedule,omitempty" json:"schedule,omitempty"`
}
func (w *AbstractWorkflow) isDCLink(link graph.GraphLink) bool {

View File

@ -10,6 +10,14 @@ import (
var validate = validator.New(validator.WithRequiredStructEnabled())
type ResourceSet struct {
Datas []string `bson:"datas,omitempty" json:"datas,omitempty"`
Storages []string `bson:"storages,omitempty" json:"storages,omitempty"`
Processings []string `bson:"processing,omitempty" json:"processing,omitempty"`
Datacenters []string `bson:"datacenters,omitempty" json:"datacenters,omitempty"`
Workflows []string `bson:"workflows,omitempty" json:"workflows,omitempty"`
}
type AbstractObject struct {
UUID string `json:"id,omitempty" bson:"id,omitempty" validate:"required"`
Name string `json:"name,omitempty" bson:"name,omitempty" validate:"required"`

View File

@ -11,6 +11,7 @@ const (
WORKFLOW_RESOURCE
WORKFLOW
WORKFLOW_EXECUTION
WORKSPACE
)
var str = [...]string{
@ -22,6 +23,7 @@ var str = [...]string{
"workflow_resource",
"workflow",
"workflow_execution",
"workspace",
}
func FromInt(i int) string {

View File

@ -2,10 +2,12 @@ package oclib
import (
"errors"
"slices"
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
"cloud.o-forge.io/core/oc-lib/models/workspace"
"github.com/vk496/cron"
)
@ -89,6 +91,45 @@ func (wfa *WorkflowMongoAccessor) LoadOne(id string) (utils.DBObject, int, error
return nil, code, err
}
res_mongo.Decode(&workflow)
// add to workspace
access := workspace.WorkspaceMongoAccessor{}
ws, _, err := mongo.MONGOService.LoadFilter(map[string]interface{}{
"active": true,
}, utils.WORKSPACE.String())
if err == nil {
var results []workspace.Workspace
if err = ws.All(mongo.MngoCtx, &results); err != nil {
return nil, 404, err
}
for _, res := range results {
for _, w := range workflow.Workflows {
if !slices.Contains(res.Workflows, w) {
res.Workflows = append(res.Workflows, w)
}
}
for _, w := range workflow.Datas {
if !slices.Contains(res.Datas, w) {
res.Datas = append(res.Datas, w)
}
}
for _, w := range workflow.Datacenters {
if !slices.Contains(res.Datacenters, w) {
res.Datacenters = append(res.Datacenters, w)
}
}
for _, w := range workflow.Storages {
if !slices.Contains(res.Storages, w) {
res.Storages = append(res.Storages, w)
}
}
for _, w := range workflow.Processings {
if !slices.Contains(res.Processings, w) {
res.Processings = append(res.Processings, w)
}
}
access.UpdateOne(&res, res.GetID())
}
}
return &workflow, 200, nil
}

View File

@ -0,0 +1,51 @@
package workspace
import (
"encoding/json"
"cloud.o-forge.io/core/oc-lib/models/utils"
"github.com/google/uuid"
)
type Workspace struct {
utils.AbstractObject
utils.ResourceSet
Active bool `json:"active" bson:"active" default:"false"`
}
func (ao *Workspace) GetID() string {
return ao.UUID
}
func (r *Workspace) GenerateID() {
r.UUID = uuid.New().String()
}
func (d *Workspace) GetName() string {
return d.Name
}
func (d *Workspace) GetAccessor() utils.Accessor {
data := &WorkspaceMongoAccessor{}
data.SetLogger(utils.WORKFLOW)
return data
}
func (dma *Workspace) Deserialize(j map[string]interface{}) utils.DBObject {
b, err := json.Marshal(j)
if err != nil {
return nil
}
json.Unmarshal(b, dma)
return dma
}
func (dma *Workspace) Serialize() map[string]interface{} {
var m map[string]interface{}
b, err := json.Marshal(dma)
if err != nil {
return nil
}
json.Unmarshal(b, &m)
return m
}

View File

@ -0,0 +1,59 @@
package workspace
import (
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
"cloud.o-forge.io/core/oc-lib/models/utils"
)
type WorkspaceMongoAccessor struct {
utils.AbstractAccessor
}
func (wfa *WorkspaceMongoAccessor) DeleteOne(id string) (utils.DBObject, int, error) {
return wfa.GenericDeleteOne(id, wfa)
}
func (wfa *WorkspaceMongoAccessor) UpdateOne(set utils.DBObject, id string) (utils.DBObject, int, error) {
return wfa.GenericUpdateOne(set, id, wfa, &Workspace{})
}
func (wfa *WorkspaceMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
func (wfa *WorkspaceMongoAccessor) CopyOne(data utils.DBObject) (utils.DBObject, int, error) {
return wfa.GenericStoreOne(data, wfa)
}
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)
obj := &Workspace{Active: false}
mongo.MONGOService.UpdateMultiple(obj, map[string]interface{}{"active": true}, wfa.GetType())
obj = &Workspace{Active: true}
wfa.UpdateOne(obj, id)
return &workflow, 200, nil
}
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 {
objs = append(objs, &r.AbstractObject)
}
return objs, 200, nil
}