cleaned code from comments and minor errors

This commit is contained in:
pb 2024-05-07 11:43:12 +02:00
parent 6b6c54795c
commit a4a8f8cb51
2 changed files with 5 additions and 96 deletions

View File

@ -73,10 +73,9 @@ func (b *ArgoBuilder) createDAGstep() {
for _, comp := range b.graph.Computings{ for _, comp := range b.graph.Computings{
unique_name := comp.Name + "_" + comp.ID unique_name := comp.Name + "_" + comp.ID
step := Task{Name: unique_name, Template: unique_name} step := Task{Name: unique_name, Template: unique_name}
comp_envs := getComputingEnvironment(comp.Environment) comp_envs := getComputingEnvironment(comp.Environment)
for name, value := range comp_envs { for name, value := range comp_envs {
step.Arguments.Parameters = append(step.Arguments.Parameters, Parameter{Name: name, Value: value}) step.Arguments.Parameters = append(step.Arguments.Parameters, Parameter{Name: name, Value: value})
} }
@ -91,7 +90,6 @@ func (b *ArgoBuilder) createDAGstep() {
} }
} }
} }
new_dag.Tasks = append(new_dag.Tasks, step) new_dag.Tasks = append(new_dag.Tasks, step)
} }
@ -109,7 +107,6 @@ func (b *ArgoBuilder) getDependency (current_computing_id string, branch []strin
if current_link.Source == current_computing_id || current_link.Destination == current_computing_id { if current_link.Source == current_computing_id || current_link.Destination == current_computing_id {
current_computing_found = true current_computing_found = true
} }
if current_computing_found { if current_computing_found {
return b.findPreviousComputing(current_computing_id,branch,i-1) return b.findPreviousComputing(current_computing_id,branch,i-1)
} }
@ -160,7 +157,6 @@ func getComputingArgs(user_input []string) (list_args []string) {
return return
} }
for _, arg := range user_input{ for _, arg := range user_input{
list_args = append(list_args, "'" + arg + "'") list_args = append(list_args, "'" + arg + "'")
} }
@ -187,8 +183,7 @@ func getComputingEnvironment(user_input []string) (map_env map[string]string) {
panic(0) panic(0)
} }
map_env[new_pair[0]] = new_pair[1] map_env[new_pair[0]] = new_pair[1]
} }
return return
@ -196,8 +191,9 @@ func getComputingEnvironment(user_input []string) (map_env map[string]string) {
func getComputingEnvironmentName(user_input []string) (list_names []string){ func getComputingEnvironmentName(user_input []string) (list_names []string){
env_map := getComputingEnvironment(user_input) env_map := getComputingEnvironment(user_input)
for name, _ := range env_map { for name := range env_map {
list_names = append(list_names, name) list_names = append(list_names, name)
} }
return return
} }

View File

@ -6,12 +6,10 @@ import (
"maps" "maps"
"net/url" "net/url"
"os" "os"
"strings"
"cloud.o-forge.io/core/oc-catalog/models" "cloud.o-forge.io/core/oc-catalog/models"
"github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/core/logs"
"github.com/sbabiv/xml2map"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
@ -30,7 +28,6 @@ type Graph struct {
} }
// Create a dictionnaries with each each existing workflow from a workspace, associated to the JSON representation of its content // Create a dictionnaries with each each existing workflow from a workspace, associated to the JSON representation of its content
func (g *Graph) GetGraphList(apiurl string) (map[string]string, error) { func (g *Graph) GetGraphList(apiurl string) (map[string]string, error) {
g.ws.Init(apiurl) g.ws.Init(apiurl)
body, err := g.ws.Get("v1/workspace/list") body, err := g.ws.Get("v1/workspace/list")
@ -47,7 +44,6 @@ func (g *Graph) GetGraphList(apiurl string) (map[string]string, error) {
} }
// Create the objects from the mxgraphxml stored in the workflow given as a parameter // Create the objects from the mxgraphxml stored in the workflow given as a parameter
func (g *Graph) LoadFrom(workspace string) error { func (g *Graph) LoadFrom(workspace string) error {
// Extract the xmlgraph from the given workspace // Extract the xmlgraph from the given workspace
xml := gjson.Get(workspace, "MxgraphXML").String() xml := gjson.Get(workspace, "MxgraphXML").String()
@ -57,91 +53,17 @@ func (g *Graph) LoadFrom(workspace string) error {
} }
os.WriteFile("graph.xml", []byte(decodedValue), 0660) os.WriteFile("graph.xml", []byte(decodedValue), 0660)
decoder := xml2map.NewDecoder(strings.NewReader(decodedValue))
result, err := decoder.Decode()
if err != nil {
return err
}
// Retrieve the content of mxcell and object elements
cells := result["mxGraphModel"].(map[string]interface{})["root"].(map[string]interface{})["mxCell"].([]map[string]interface{})
obj := result["mxGraphModel"].(map[string]interface{})["root"].(map[string]interface{})["object"]
// Append the content of obj to cells depending on if it's a single key/value pair or a list of it
switch v := obj.(type) {
case map[string]interface{}:
cells = append(cells, obj.(map[string]interface{}))
_ = v
// fmt.Printf("One: %v", v)
case []map[string]interface{}:
cells = append(cells, obj.([]map[string]interface{})...)
// fmt.Printf("Many: %v", v)
}
// dictionnary := make(map[string]string)
var idlinks []Link
g.GetWorkflowComponents(workspace) g.GetWorkflowComponents(workspace)
g.GetLinks(workspace) g.GetLinks(workspace)
for _, element := range cells {
// id := element["@id"].(string)
// // Case MXCell
// if _, ok := element["@style"]; ok {
// if _, ok2 := element["@rID"]; ok2 {
// // Resolve elements
// // fmt.Print(id + ": ")
// // fmt.Println(element["@rID"], element["@rType"])
// // fmt.Println(element)
// dictionnary[id] = element["@rID"].(string)
// g.addElementByType(element)
// }
// } else {
// // Case object : contains user's input through the GUI
// if _, ok := element["mxCell"]; ok {
// // Attribute values
// // Extracts the cell ids
// element = element["mxCell"].(map[string]interface{})
// if _, ok := element["@style"]; ok {
// if _, ok2 := element["@rID"]; ok2 {
// // Resolve elements
// // fmt.Print(id + ": ")
// // fmt.Println(element["@rID"], element["@rType"])
// // fmt.Println(element)
// dictionnary[id] = element["@rID"].(string)
// g.addElementByType(element)
// }
// }
// }
// }
// register links
if src, ok := element["@source"]; ok {
//src = element["@source"].(string)
idlinks = append(idlinks, Link{Src: src.(string), Dst: element["@target"].(string)})
// fmt.Println("Link: " + src.(string) + " " + element["@target"].(string))
}
}
// translate links
// for _, link := range idlinks {
// g.Links = append(g.Links, Link{Src: dictionnary[link.Src], Dst: dictionnary[link.Dst]})
// fmt.Println("Link: " + link.Src + " " + link.Dst + " : " + dictionnary[link.Src] + " " + dictionnary[link.Dst])
// }
return nil return nil
} }
// TODO : extract all the JSON/data processing to a new object that takes
// the current graph as an attribute and deals with adding new objects
// to it, depending on their type
// Create the objects that correspond to each component // Create the objects that correspond to each component
// in a workflow, combining the user input and the base components attributes // in a workflow, combining the user input and the base components attributes
func (g *Graph) GetWorkflowComponents(workflow string){ func (g *Graph) GetWorkflowComponents(workflow string){
types := []string{"computing","datacenter","data","storage"} // create a constant for more maintainability OR even better get the list of all component's type for this WF types := []string{"computing","datacenter","data","storage"} // create a constant for more maintainability OR even better get the list of all component's type for this WF
for _, component_type := range types { for _, component_type := range types {
@ -374,15 +296,6 @@ func (g *Graph) getComponentName(id string) string {
return "" return ""
} }
func getMapKeys(m map[int]interface{}) []int {
var list_keys []int
for key := range m {
fmt.Println("Key:", key)
}
return list_keys
}
func (g *Graph) getComponentType(component_id string) string { func (g *Graph) getComponentType(component_id string) string {
for _, comp := range g.Computings { for _, comp := range g.Computings {