improved structure, doc and naming

This commit is contained in:
pb 2024-05-03 10:58:06 +02:00
parent b7441c7430
commit c79b693ba3
6 changed files with 93 additions and 43 deletions

View File

@ -6,6 +6,7 @@ package main
import (
"fmt"
"os"
"slices"
"github.com/beego/beego/v2/core/logs"
@ -15,14 +16,11 @@ import (
type ArgoBuilder struct {
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 {

View File

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

2
go.mod
View File

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

View File

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

View File

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

40
template_models.go Normal file
View File

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