Implemented functions to recreate a 3 node graph

This commit is contained in:
pb 2024-04-19 16:13:41 +02:00
parent 07bf7193f4
commit 5e3049a083
2 changed files with 114 additions and 6 deletions

117
graph.go
View File

@ -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
}

View File

@ -39,7 +39,8 @@ func main() {
}
g.LoadFrom(list["test-alpr"])
g.ExportToArgo("test")
fmt.Print("stop")
fmt.Print("stop")
}