From c79b693ba3d984d5c4c8dae7c3be02fd2e62ddd9 Mon Sep 17 00:00:00 2001 From: pb Date: Fri, 3 May 2024 10:58:06 +0200 Subject: [PATCH] improved structure, doc and naming --- argo_builder.go | 37 +++++++++++++++-------- docs/uml/generating_graph_branches.puml | 26 +++++++++------- go.mod | 2 +- graph.go | 29 +++++++----------- main.go | 2 +- template_models.go | 40 +++++++++++++++++++++++++ 6 files changed, 93 insertions(+), 43 deletions(-) create mode 100644 template_models.go diff --git a/argo_builder.go b/argo_builder.go index 40345a7..d1df24b 100644 --- a/argo_builder.go +++ b/argo_builder.go @@ -6,6 +6,7 @@ package main import ( "fmt" + "os" "slices" "github.com/beego/beego/v2/core/logs" @@ -13,16 +14,13 @@ import ( ) type ArgoBuilder struct { - graph Graph + graph Graph branches [][]string - steps []DagStep + Workflow Workflow } -type DagStep struct { - Name string `yaml:"name"` - Template string `yaml:"template"` - Arguments []string `yaml:"arguments"` - Dependencies []string `yaml:"dependencies"` +type Workflow struct { + Templates []Template `yaml:"templates"` } @@ -31,22 +29,32 @@ func (b *ArgoBuilder) CreateDAG() bool { fmt.Println("list of branches : ", b.branches) b.createDAGstep() - yamlified, err := yaml.Marshal(b.steps) + yamlified, err := yaml.Marshal(b.Workflow) if err != nil { - logs.Error("Could not produce yaml file") + logs.Error("Could not transform object to yaml file") } fmt.Println(string(yamlified)) - + err = os.WriteFile("argo.yml", []byte(yamlified), 0660) + if err != nil { + logs.Error("Could not write the yaml file") + } return true } func (b *ArgoBuilder) createDAGstep() { + + new_dag := Dag{} + for _, comp := range b.graph.Computings{ unique_name := comp.Name + "_" + comp.ID - step := DagStep{Name: unique_name, Template: unique_name, Arguments: comp.Arguments} + var comp_params []Parameter + comp_params = append(comp_params, Parameter{"toto","titi"}) + step := Task{Name: unique_name, Template: unique_name} + step.Arguments.Parameters = append(step.Arguments.Parameters, comp_params...) + // For each branch, check if the computing has a dependency for _, branch := range b.branches { if b.componentInBranch(comp.ID,branch) { @@ -57,8 +65,13 @@ func (b *ArgoBuilder) createDAGstep() { } } } - b.steps = append(b.steps, step) + + new_dag.Tasks = append(new_dag.Tasks, step) + } + + b.Workflow.Templates = append (b.Workflow.Templates, Template{Name: "dag", Dag: new_dag}) + } func (b *ArgoBuilder) getDependency (current_computing_id string, branch []string) string { diff --git a/docs/uml/generating_graph_branches.puml b/docs/uml/generating_graph_branches.puml index 7b2b0a2..d321053 100644 --- a/docs/uml/generating_graph_branches.puml +++ b/docs/uml/generating_graph_branches.puml @@ -25,23 +25,27 @@ group if g.Links size > 0 end + graph -> graph : get list branches (end_lists, unvisited_links, current_branch) + note left: unvisited links and current branch\n are nilfor the first entry + loop link in endLinks + + graph -> graph : create a new list representing the current branch + graph -> graph : copy Links id on a list - loop while hasParent - graph -> graph : currentLink = idList[i] + graph -> graph : get list previous links - group if link.dst == currentLink.Src - graph -> graph : do something - note right : TEST ALGO : edit a\n string that shows every branch - graph -> graph : change currentSrc with link.Src - graph -> graph : remove currentLink from idList - end - group else - graph -> graph : hasParent = false - end + group if len(previous_links) == 0 + graph -> graph : add current list + end + loop for link in previous_links + graph -> graph : add the link to the current branch + graph -> graph : remove current link from unvisited links list + graph -> graph : get list branches (end_lists, unvisited_links, current_branch) + note right: for each link we retrieve the branch that\nprecedes it recursively,\n so once we hit the end of a branch\n the entire branch is returned\n to the node where a split happened\nor that is its source end end diff --git a/go.mod b/go.mod index c741d4c..ffcb3b4 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/rs/zerolog v1.32.0 github.com/sbabiv/xml2map v1.2.1 github.com/tidwall/gjson v1.17.1 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -46,5 +47,4 @@ require ( golang.org/x/text v0.14.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.32.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/graph.go b/graph.go index ca5c374..6214a17 100644 --- a/graph.go +++ b/graph.go @@ -261,55 +261,48 @@ func (g *Graph) ExportToArgo(id string) error { fmt.Println(str) } + fmt.Println("Identified branches : ", list_branches) - argo_builder := ArgoBuilder{*g,list_branches,nil} + argo_builder := ArgoBuilder{graph : *g, branches: list_branches} 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) { +func (g *Graph) getListBranches(end_links map[string]models.Link, unvisited_links_list map[string]models.Link, current_branch []string) (list_branches [][]string) { 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) + if unvisited_links_list == nil { + unvisited_links_list = make(map[string]models.Link,len(g.Links)) + maps.Copy(unvisited_links_list,g.Links) + fmt.Println(unvisited_links_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) + previous_index := g.getPreviousLink(j, unvisited_links_list) if len(previous_index) == 0 { - // new_branches = append(new_branches, []string{link_id}) - // return new_branches list_branches = append(list_branches, []string{link_id}) } for _, id_link := range previous_index { current_branch = append([]string{link_id},current_branch...) - delete(editable_link_list, link_id) + delete(unvisited_links_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) + new_branches = g.getListBranches(new_end_link,unvisited_links_list,current_branch) for _, new_branch := range new_branches{ current_branch = append(new_branch,link_id) list_branches = append(list_branches, current_branch) } - - } - - } return @@ -350,7 +343,7 @@ 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,map_link map[string]models.Link) (previous_id []string) { +func (g *Graph) getPreviousLink(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) diff --git a/main.go b/main.go index de73395..e6acb80 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,7 @@ func main() { } g.LoadFrom(list["test-alpr"]) - g.ExportToArgo("test") + g.ExportToArgo("test-alpr") fmt.Print("stop") diff --git a/template_models.go b/template_models.go new file mode 100644 index 0000000..552f561 --- /dev/null +++ b/template_models.go @@ -0,0 +1,40 @@ +package main + +type Parameter struct { + Name string `yaml:"name"` + Value string `yaml:"value"` +} + +type Container struct { + Image string `yaml:"image"` + Command []string `yaml:"command,omitempty"` + Args []string `yaml:"args,omitempty"` + VolumeMounts []VolumeMount `yaml:"volumeMounts,omitempty"` +} + +type VolumeMount struct { + Name string `yaml:"name"` + MountPath string `yaml:"mountPath"` +} + +type Task struct { + Name string `yaml:"name"` + Template string `yaml:"template"` + Dependencies []string `yaml:"dependencies,omitempty"` + Arguments struct { + Parameters []Parameter `yaml:"parameters,omitempty"` + } `yaml:"arguments,omitempty"` +} + +type Dag struct { + Tasks []Task `yaml:"tasks,omitempty"` +} + +type Template struct { + Name string `yaml:"name"` + Inputs struct { + Parameters []Parameter `yaml:"parameters"` + } `yaml:"inputs,omitempty"` + Container Container `yaml:"container,omitempty"` + Dag Dag `yaml:"dag,omitempty"` +}