diff --git a/graph.go b/graph.go index a839215..975c454 100644 --- a/graph.go +++ b/graph.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "maps" "net/url" "os" "strings" @@ -133,20 +134,6 @@ func (g *Graph) LoadFrom(workspace string) error { 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 // 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) { result.ForEach(func(id, value gjson.Result) bool{ 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 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 { var s models.StorageModel - resp, err := g.ws.Get("v1/data/" + id) + resp, err := g.ws.Get("v1/storage/" + id) if err != nil { return err } @@ -258,46 +242,80 @@ func (g *Graph) AddStorageModel(id string, user_input gjson.Result, wf_id string func (g *Graph) ExportToArgo(id string) error { 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 { if (!link.DCLink && !g.isSource(link.Destination,i)){ end_links[i] = link } } - for index, end_link := range end_links { - has_parent := true - j := index - - // index_list := make([]int, len(g.Links)) - current_link := end_link + // index_list := make([]int, len(g.Links)) + // list_branches := make([][]string,0) + list_branches := g.getListBranches(end_links, nil,nil) - current_src := current_link.Source - _ = 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 - } + for _, branch := range list_branches{ + str := "" + for _, link := range branch{ + str = str + " --> " + g.getComponentName(g.Links[link].Source) + " linked with " + g.getComponentName(g.Links[link].Destination) } - - 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 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 { 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] // or nil if not -func (g *Graph) hasPreviousLink(link_id string,list_link map[string]models.Link) (hasPrevious bool, previous_id string) { - for i, link := range list_link{ - if(i != link_id && link.Destination == g.Links[link_id].Source){ - return true, i +func (g *Graph) hasPreviousLink(link_id string,map_link map[string]models.Link) (previous_id []string) { + for k, link := range map_link{ + if(k != link_id && link.Destination == g.Links[link_id].Source){ + previous_id = append(previous_id, k) } } - return false, "" + return } func (g *Graph) getComponentName(id string) string { @@ -372,4 +390,39 @@ func getMapKeys(m map[int]interface{}) []int { } return list_keys -} \ No newline at end of file +} + +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 +}