From 5e3049a083ce0445fdf29cd8889816305ba7d5dc Mon Sep 17 00:00:00 2001 From: pb Date: Fri, 19 Apr 2024 16:13:41 +0200 Subject: [PATCH] Implemented functions to recreate a 3 node graph --- graph.go | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- main.go | 3 +- 2 files changed, 114 insertions(+), 6 deletions(-) diff --git a/graph.go b/graph.go index 7a816d8..a839215 100644 --- a/graph.go +++ b/graph.go @@ -24,7 +24,7 @@ type Graph struct { Computings []models.ComputingModel Datacenters []models.DatacenterModel Storages []models.StorageModel - Links []models.Link + Links map[string]models.Link ws HttpQuery } @@ -187,7 +187,7 @@ func (g *Graph) GetWorkflowComponents(workflow string){ } func (g *Graph) GetLinks(workflow string){ - + g.Links = make(map[string]models.Link) result := gjson.Get(workflow, "link") if (result.Type != gjson.Null) { @@ -197,9 +197,8 @@ func (g *Graph) GetLinks(workflow string){ fmt.Println(value.Str) json.Unmarshal([]byte(value.Raw),&l) - - g.Links = append(g.Links, l) - fmt.Println(l) + + g.Links[id.Str] = l return true }) } @@ -258,6 +257,44 @@ 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 + + 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 + } + } + + fmt.Println(str_links) + } + return nil } @@ -266,3 +303,73 @@ func (g *Graph) ExportToHelm(id string) error { return nil } +// Return if it exists a link where Destination is the same as comp_id +func (g *Graph) isDestination(comp_id string,link_id string) bool { + + for i, link := range g.Links{ + if(i !=link_id && link.Destination == comp_id){ + return true + } + } + + return false + +} + +// Return if it exists a link where Source is the same as comp_id +func (g *Graph) isSource(comp_id string,link_id string) bool { + + for i, link := range g.Links{ + if(i !=link_id && link.Source == comp_id && !link.DCLink){ + return true + } + } + + return false + +} + +// Returns an index number if their is a link in g.Links +// 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 + } + } + + return false, "" +} + +func (g *Graph) getComponentName(id string) string { + + for _, comp := range g.Computings{ + if comp.ID == id { + return comp.Name + } + } + for _, storage := range g.Storages{ + if storage.ID == id { + return storage.Name + } + } + for _, data := range g.Datas{ + if data.ID == id { + return data.Name + } + } + + return "" +} + +func getMapKeys(m map[int]interface{}) []int { + var list_keys []int + + for key := range m { + fmt.Println("Key:", key) + } + + return list_keys +} \ No newline at end of file diff --git a/main.go b/main.go index c3068ca..de73395 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,8 @@ func main() { } g.LoadFrom(list["test-alpr"]) + g.ExportToArgo("test") - fmt.Print("stop") + fmt.Print("stop") }