resource are lightest enough

This commit is contained in:
mr 2024-10-10 08:55:22 +02:00
parent 84024a143e
commit 17749c6c0b
2 changed files with 22 additions and 68 deletions

View File

@ -1,6 +1,10 @@
package graph package graph
import "cloud.o-forge.io/core/oc-lib/models/resources" import (
"cloud.o-forge.io/core/oc-lib/models/resources"
"cloud.o-forge.io/core/oc-lib/models/utils"
"cloud.o-forge.io/core/oc-lib/tools"
)
// Graph is a struct that represents a graph // Graph is a struct that represents a graph
type Graph struct { type Graph struct {
@ -9,6 +13,23 @@ type Graph struct {
Links []GraphLink `bson:"links" json:"links" default:"{}" validate:"required"` // Links is the list of links between elements in the graph Links []GraphLink `bson:"links" json:"links" default:"{}" validate:"required"` // Links is the list of links between elements in the graph
} }
func (g *Graph) GetResource(id string) (string, utils.DBObject) {
if item, ok := g.Items[id]; ok {
if item.Data != nil {
return tools.DATA_RESOURCE.String(), item.Data
} else if item.Datacenter != nil {
return tools.DATACENTER_RESOURCE.String(), item.Datacenter
} else if item.Workflow != nil {
return tools.WORKFLOW_RESOURCE.String(), item.Workflow
} else if item.Processing != nil {
return tools.PROCESSING_RESOURCE.String(), item.Processing
} else if item.Storage != nil {
return tools.STORAGE_RESOURCE.String(), item.Storage
}
}
return "", nil
}
// GraphItem is a struct that represents an item in a graph // GraphItem is a struct that represents an item in a graph
type GraphItem struct { type GraphItem struct {
ID string `bson:"id" json:"id" validate:"required"` // ID is the unique identifier of the item ID string `bson:"id" json:"id" validate:"required"` // ID is the unique identifier of the item

View File

@ -35,73 +35,6 @@ func (w *AbstractWorkflow) GetWorkflows() (list_computings []graph.GraphItem) {
return return
} }
func (w *AbstractWorkflow) GetStoragesByRelatedProcessing(processingID string, relatedToData bool, ignoreRelation bool) (map[string][]utils.DBObject, map[string]map[string][]utils.DBObject) {
storages := make(map[string][]utils.DBObject)
datasRelatedToStorage := make(map[string]map[string][]utils.DBObject)
for _, link := range w.Graph.Links {
inout := "in"
storageID := link.Source.ID // Default value because we are looking for the input storage cause processing is destination
nodeID := link.Destination.ID // we considers that the processing is the destination
node := w.Graph.Items[link.Source.ID].Storage // we are looking for the storage as source
if node == nil { // if the source is not a storage, we consider that the destination is the storage
inout = "out"
storageID = link.Destination.ID // then we are looking for the output storage
nodeID = link.Source.ID // and the processing is the source
node = w.Graph.Items[link.Destination.ID].Storage // we are looking for the storage as destination
}
if processingID == nodeID && node != nil { // if the storage is linked to the processing
if storages[inout] == nil {
storages[inout] = []utils.DBObject{}
}
if !ignoreRelation {
datasRelatedToStorage[storageID], _ = w.GetDatasByRelatedProcessing(processingID, false, true)
if relatedToData && len(datasRelatedToStorage[storageID]) > 0 {
storages[inout] = append(storages[inout], node)
} else if !relatedToData && len(datasRelatedToStorage[storageID]) == 0 {
storages[inout] = append(storages[inout], node)
}
} else {
storages[inout] = append(storages[inout], node)
}
}
}
return storages, datasRelatedToStorage
}
func (w *AbstractWorkflow) GetDatasByRelatedProcessing(dataID string, relatedToStorage bool, ignoreRelation bool) (map[string][]utils.DBObject, map[string]map[string][]utils.DBObject) {
datas := make(map[string][]utils.DBObject)
datasRelatedToData := make(map[string]map[string][]utils.DBObject)
for _, link := range w.Graph.Links {
inout := "in"
dataID := link.Source.ID // Default value because we are looking for the input storage cause processing is destination
nodeID := link.Destination.ID // we considers that the processing is the destination
node := w.Graph.Items[link.Source.ID].Data // we are looking for the storage as source
if node == nil { // if the source is not a storage, we consider that the destination is the storage
inout = "out"
dataID = link.Destination.ID // then we are looking for the output storage
nodeID = link.Source.ID // and the processing is the source
node = w.Graph.Items[link.Destination.ID].Data // we are looking for the storage as destination
}
if dataID == nodeID && node != nil { // if the storage is linked to the processing
if datas[inout] == nil {
datas[inout] = []utils.DBObject{}
}
datas[inout] = append(datas[inout], node)
if !ignoreRelation {
datasRelatedToData[dataID], _ = w.GetStoragesByRelatedProcessing(dataID, false, true)
if relatedToStorage && len(datasRelatedToData[dataID]) > 0 {
datas[inout] = append(datas[inout], node)
} else if !relatedToStorage && len(datasRelatedToData[dataID]) == 0 {
datas[inout] = append(datas[inout], node)
}
} else {
datas[inout] = append(datas[inout], node)
}
}
}
return datas, datasRelatedToData
}
func (w *AbstractWorkflow) GetProcessings() (list_computings []graph.GraphItem) { func (w *AbstractWorkflow) GetProcessings() (list_computings []graph.GraphItem) {
for _, item := range w.Graph.Items { for _, item := range w.Graph.Items {
if item.Processing != nil { if item.Processing != nil {