fixed bug preventing one endlink to be the end of several paths

This commit is contained in:
pb 2024-04-30 14:43:38 +02:00
parent 5e3049a083
commit 603dd62f99

149
graph.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"maps"
"net/url" "net/url"
"os" "os"
"strings" "strings"
@ -133,20 +134,6 @@ func (g *Graph) LoadFrom(workspace string) error {
return nil return nil
} }
// func (g *Graph) addElementByType(element map[string]interface{}) {
// if element["@rType"] == "data" {
// g.AddDataModel(element["@rID"].(string))
// }
// if element["@rType"] == "datacenter" {
// g.AddDatacenterModel(element["@rID"].(string))
// }
// if element["@rType"] == "computing" {
// g.AddComputingModel(element["@rID"].(string))
// }
// if element["@rType"] == "storage" {
// g.AddStorageModel(element["@rID"].(string))
// }
// }
// TODO : extract all the JSON/data processing to a new object that takes // 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 // the current graph as an attribute and deals with adding new objects
@ -193,11 +180,8 @@ func (g *Graph) GetLinks(workflow string){
if (result.Type != gjson.Null) { if (result.Type != gjson.Null) {
result.ForEach(func(id, value gjson.Result) bool{ result.ForEach(func(id, value gjson.Result) bool{
var l models.Link var l models.Link
fmt.Print(id)
fmt.Println(value.Str)
json.Unmarshal([]byte(value.Raw),&l) json.Unmarshal([]byte(value.Raw),&l)
g.Links[id.Str] = l g.Links[id.Str] = l
return true return true
}) })
@ -245,7 +229,7 @@ func (g *Graph) AddComputingModel(id string, user_input gjson.Result, wf_id stri
func (g *Graph) AddStorageModel(id string, user_input gjson.Result, wf_id string) error { func (g *Graph) AddStorageModel(id string, user_input gjson.Result, wf_id string) error {
var s models.StorageModel var s models.StorageModel
resp, err := g.ws.Get("v1/data/" + id) resp, err := g.ws.Get("v1/storage/" + id)
if err != nil { if err != nil {
return err return err
} }
@ -258,9 +242,6 @@ func (g *Graph) AddStorageModel(id string, user_input gjson.Result, wf_id string
func (g *Graph) ExportToArgo(id string) error { func (g *Graph) ExportToArgo(id string) error {
end_links := make(map[string]models.Link) end_links := make(map[string]models.Link)
editable_link_list := make(map[string]models.Link)
editable_link_list = g.Links
for i, link := range g.Links { for i, link := range g.Links {
if (!link.DCLink && !g.isSource(link.Destination,i)){ if (!link.DCLink && !g.isSource(link.Destination,i)){
@ -268,36 +249,73 @@ func (g *Graph) ExportToArgo(id string) error {
} }
} }
for index, end_link := range end_links { // index_list := make([]int, len(g.Links))
has_parent := true // list_branches := make([][]string,0)
j := index list_branches := g.getListBranches(end_links, nil,nil)
// index_list := make([]int, len(g.Links)) for _, branch := range list_branches{
current_link := end_link str := ""
for _, link := range branch{
current_src := current_link.Source str = str + " --> " + g.getComponentName(g.Links[link].Source) + " linked with " + g.getComponentName(g.Links[link].Destination)
_ = current_src
str_links := g.getComponentName(current_link.Source) + " linked with " + g.getComponentName(current_link.Destination)
for has_parent {
previous, previous_index := g.hasPreviousLink(j,editable_link_list)
if previous {
current_link = g.Links[previous_index]
str_links = g.getComponentName(current_link.Source) + " linked with " + g.getComponentName(current_link.Destination) + " --> " + str_links
delete(editable_link_list,j)
j = previous_index
} else {
has_parent = false
}
} }
fmt.Println(str_links) fmt.Println(str)
} }
fmt.Println("Identified branches : ", list_branches)
argo_builder := ArgoBuilder{*g,list_branches,nil}
argo_builder.CreateDAG()
return nil return nil
} }
// Return a list containing the IDs of each link that make up a branch in the graph
func (g *Graph) getListBranches(end_links map[string]models.Link, editable_link_list map[string]models.Link, current_branch []string) (list_branches [][]string) {
fmt.Printf("Working on %v \n", current_branch)
if current_branch == nil {
current_branch = make([]string, 0)
}
if editable_link_list == nil {
editable_link_list = make(map[string]models.Link,len(g.Links))
maps.Copy(editable_link_list,g.Links)
fmt.Println(editable_link_list)
}
for link_id, _ := range end_links {
// branch := []string{link_id}
j := link_id
new_branches := make([][]string,0)
previous_index := g.hasPreviousLink(j, editable_link_list)
if len(previous_index) == 0 {
new_branches = append(new_branches, []string{link_id})
return new_branches
}
for _, id_link := range previous_index {
current_branch = append([]string{link_id},current_branch...)
delete(editable_link_list, link_id)
// create a new branch for each previous link, appending the current path to this node to the created branch
new_end_link := make(map[string]models.Link,0)
new_end_link[id_link] = g.Links[id_link]
new_branches = g.getListBranches(new_end_link,editable_link_list,current_branch)
for _, new_branch := range new_branches{
current_branch = append(new_branch,link_id)
list_branches = append(list_branches, current_branch)
}
}
return
}
return
}
func (g *Graph) ExportToHelm(id string) error { func (g *Graph) ExportToHelm(id string) error {
return nil return nil
@ -333,14 +351,14 @@ func (g *Graph) isSource(comp_id string,link_id string) bool {
// with the same Destination id that the Source id in g.Links[linkIndex] // with the same Destination id that the Source id in g.Links[linkIndex]
// or nil if not // or nil if not
func (g *Graph) hasPreviousLink(link_id string,list_link map[string]models.Link) (hasPrevious bool, previous_id string) { func (g *Graph) hasPreviousLink(link_id string,map_link map[string]models.Link) (previous_id []string) {
for i, link := range list_link{ for k, link := range map_link{
if(i != link_id && link.Destination == g.Links[link_id].Source){ if(k != link_id && link.Destination == g.Links[link_id].Source){
return true, i previous_id = append(previous_id, k)
} }
} }
return false, "" return
} }
func (g *Graph) getComponentName(id string) string { func (g *Graph) getComponentName(id string) string {
@ -373,3 +391,38 @@ func getMapKeys(m map[int]interface{}) []int {
return list_keys return list_keys
} }
func (g *Graph) getComponentType(component_id string) string {
for _, comp := range g.Computings {
if comp.ID == component_id{
return "computing"
}
}
for _, data := range g.Datas {
if data.ID == component_id{
return "data"
}
}
for _, storage := range g.Storages {
if storage.ID == component_id{
return "storage"
}
}
return ""
}
// Returns a slice of id, in case the link is made of twice the same type of component
func (g *Graph) getComponentByType(compType string, link models.Link) (ids []string){
if(g.getComponentType(link.Source) == compType){
ids = append(ids, link.Source)
}
if(g.getComponentType(link.Destination) == compType){
ids = append(ids, link.Destination)
}
return
}