refactor dependencies retrieval
This commit is contained in:
		| @@ -9,7 +9,6 @@ import ( | ||||
| 	"oc-monitor/logger" | ||||
| 	. "oc-monitor/models" | ||||
| 	"os" | ||||
| 	"slices" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| @@ -21,7 +20,6 @@ import ( | ||||
|  | ||||
| type ArgoBuilder struct { | ||||
| 	graph    graph.Graph | ||||
| 	branches [][]int | ||||
| 	Workflow Workflow | ||||
| } | ||||
|  | ||||
| @@ -42,7 +40,6 @@ type Spec struct { | ||||
| } | ||||
|  | ||||
| func (b *ArgoBuilder) CreateDAG() (string, error) { | ||||
| 	fmt.Println("list of branches : ", b.branches) | ||||
|  | ||||
| 	b.createTemplates() | ||||
| 	b.createDAGstep() | ||||
| @@ -52,7 +49,7 @@ func (b *ArgoBuilder) CreateDAG() (string, error) { | ||||
| 	b.Workflow.ApiVersion = "argoproj.io/v1alpha1" | ||||
| 	b.Workflow.Kind = "Workflow" | ||||
| 	random_name := generateWfName() | ||||
| 	b.Workflow.Metadata.GenerateName = "oc-test-" + random_name | ||||
| 	b.Workflow.Metadata.GenerateName = "oc-monitor-" + random_name | ||||
|  | ||||
| 	yamlified, err := yaml.Marshal(b.Workflow) | ||||
| 	if err != nil { | ||||
| @@ -147,21 +144,15 @@ func (b *ArgoBuilder) createVolumes() { | ||||
| } | ||||
|  | ||||
| func (b *ArgoBuilder) getDependency(current_computing_id string) (dependencies []string) { | ||||
| 	var dependencies_id []string | ||||
|  | ||||
| 	for _, link := range b.graph.Links { | ||||
| 		source := b.graph.Items[link.Source.ID].Processing // Instead of searching for the AbstractResource we load the Processing pointer and test if it's nil to know if the item is a processing | ||||
| 		if current_computing_id == link.Destination.ID && source != nil && !slices.Contains(dependencies_id, link.Source.ID) { | ||||
| 			dependencies_id = append(dependencies_id, link.Source.ID) | ||||
| 		source := b.graph.Items[link.Source.ID].Processing  | ||||
| 		if current_computing_id == link.Destination.ID && source != nil { | ||||
| 			dependency_name := getArgoName(source.GetName(), link.Source.ID) | ||||
| 			dependencies = append(dependencies, dependency_name) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	for _, dependency := range dependencies_id { | ||||
| 		source := b.graph.Items[dependency].Processing | ||||
| 		dependency_name := getArgoName(source.GetName(), dependency) | ||||
| 		dependencies = append(dependencies, dependency_name) | ||||
| 	} | ||||
|  | ||||
| 	return | ||||
|  | ||||
| } | ||||
| @@ -217,9 +208,7 @@ func getComputingArgs(user_input []string, command string) (list_args []string) | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	for _, arg := range user_input { | ||||
| 		list_args = append(list_args, arg) | ||||
| 	} | ||||
| 	list_args = append(list_args, user_input...) | ||||
|  | ||||
| 	return | ||||
| } | ||||
| @@ -312,4 +301,5 @@ func getStringValue(comp resource_model.AbstractResource, key string) string { | ||||
|         return res.(string) | ||||
|     } | ||||
|     return "" | ||||
| } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -3,7 +3,6 @@ package workflow_builder | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"fmt" | ||||
| 	"maps" | ||||
|  | ||||
| 	"oc-monitor/logger" | ||||
| 	models "oc-monitor/models" | ||||
| @@ -18,7 +17,6 @@ import ( | ||||
| type WorflowDB struct { | ||||
| 	workflow_name	string							// used to test if the graph has been instatiated, private so can only be set by a graph's method | ||||
| 	graph 			*graph.Graph | ||||
| 	links			map[int]graph.GraphLink | ||||
| 	ws          	models.HttpQuery | ||||
| } | ||||
|  | ||||
| @@ -31,7 +29,6 @@ func (w *WorflowDB) LoadFrom(workflow_id string) error { | ||||
| 	} | ||||
|  | ||||
| 	w.graph = new_wf.Graph  | ||||
| 	w.links = w.getLinks() | ||||
|  | ||||
| 	w.workflow_name = new_wf.Name | ||||
| 	 | ||||
| @@ -57,43 +54,13 @@ func (w *WorflowDB) getWorkflow( workflow_id string) (workflow *workflow.Workflo | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
| func (w *WorflowDB) getLinks()  map[int]graph.GraphLink { | ||||
| 	links := make(map[int]graph.GraphLink) | ||||
| 	for i, link := range(w.graph.Links) { | ||||
| 		links[i] = link | ||||
| 	} | ||||
|  | ||||
| 	return links | ||||
| } | ||||
|  | ||||
| func (w *WorflowDB) ExportToArgo() (string, error) { | ||||
| 	if len(w.workflow_name) == 0 || &w.graph == nil { | ||||
| 		return "",fmt.Errorf("can't export a graph that has not been loaded yet") | ||||
| 	} | ||||
| 	end_links := make(map[int]graph.GraphLink) | ||||
| 	 | ||||
| 	for i, link := range w.links { | ||||
| 		if (!w.isDCLink(i) && !w.isSource(link.Destination.ID,i)){ | ||||
| 			end_links[i] = link | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	// index_list := make([]int, len(w.links)) | ||||
| 	// list_branches := make([][]string,0) | ||||
| 	list_branches := w.getListBranches(end_links, nil,nil) | ||||
| 		 | ||||
| 	// for _, branch := range list_branches{ | ||||
| 	// 	str := "" | ||||
| 	// 	for _, link := range branch{ | ||||
| 	// 		str =  str + " --> " + w.getComponentName(w.graph.Links[link].Source) + " linked with " + w.getComponentName(w.links[link].Destination)  | ||||
| 	// 	} | ||||
|  | ||||
| 	// 	fmt.Println(str) | ||||
| 	// } | ||||
|  | ||||
| 	fmt.Println("Identified branches : ", list_branches) | ||||
| 	argo_builder := ArgoBuilder{graph : *w.graph, branches:  list_branches} | ||||
| 	argo_builder := ArgoBuilder{graph : *w.graph} | ||||
| 	filename, err := argo_builder.CreateDAG() | ||||
| 	if err != nil { | ||||
| 		logger.Logger.Error().Msg("Could not create the argo file for " + w.workflow_name) | ||||
| @@ -102,46 +69,6 @@ func (w *WorflowDB) ExportToArgo() (string, error) { | ||||
|  	return filename, nil | ||||
| } | ||||
|  | ||||
| // Return a list containing the IDs of each link that make up a branch in the graph | ||||
| func (w *WorflowDB) getListBranches(end_links map[int]graph.GraphLink, unvisited_links_list map[int]graph.GraphLink, current_branch []int) (list_branches [][]int) { | ||||
| 	 | ||||
| 	if current_branch == nil { | ||||
| 		current_branch = make([]int, 0) | ||||
| 	} | ||||
|  | ||||
| 	if unvisited_links_list == nil { | ||||
| 		unvisited_links_list = make(map[int]graph.GraphLink,len(w.graph.Links)) | ||||
| 		maps.Copy(unvisited_links_list,w.links) | ||||
| 		fmt.Println(unvisited_links_list) | ||||
| 	} | ||||
| 	 | ||||
| 	for link_id, _ := range end_links { | ||||
| 		j := link_id | ||||
| 		new_branches := make([][]int,0) | ||||
|  | ||||
| 		previous_index := w.getPreviousLink(j, unvisited_links_list) | ||||
| 		if len(previous_index) == 0 { | ||||
| 			list_branches = append(list_branches, []int{link_id}) | ||||
| 		} | ||||
|  | ||||
| 		for _, id_link := range previous_index { | ||||
| 			current_branch = append([]int{link_id},current_branch...) | ||||
| 			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[int]graph.GraphLink,0) | ||||
| 			new_end_link[id_link] = w.links[id_link] | ||||
| 			new_branches = w.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  | ||||
| } | ||||
|  | ||||
| func (w *WorflowDB) ExportToHelm(id string) error { | ||||
|  | ||||
| 	return nil | ||||
| @@ -150,7 +77,7 @@ func (w *WorflowDB) ExportToHelm(id string) error { | ||||
| // Return if it exists a link where Destination is the same as comp_id | ||||
| func (w *WorflowDB) isDestination(comp_id string,link_id int) bool { | ||||
| 	 | ||||
| 	for i, link := range w.links{ | ||||
| 	for i, link := range w.graph.Links{ | ||||
| 		if(i !=link_id && link.Destination.ID == comp_id){ | ||||
| 			return true | ||||
| 		} | ||||
| @@ -163,7 +90,7 @@ func (w *WorflowDB) isDestination(comp_id string,link_id int) bool { | ||||
| // Return if it exists a link where Source is the same as comp_id | ||||
| func (w *WorflowDB) isSource(comp_id string, link_id int) bool { | ||||
| 	 | ||||
| 	for i, link := range w.links{ | ||||
| 	for i, link := range  w.graph.Links{ | ||||
| 		if(i !=link_id && link.Source.ID == comp_id && !w.isDCLink(i)){ | ||||
| 			return true | ||||
| 		} | ||||
| @@ -173,20 +100,6 @@ func (w *WorflowDB) isSource(comp_id string, link_id int) bool { | ||||
| 	 | ||||
| } | ||||
|  | ||||
| // Returns an index number if their is a link in w.links | ||||
| // with the same Destination id that the Source id in w.links[linkIndex] | ||||
| // or nil if not | ||||
|  | ||||
| func (w *WorflowDB) getPreviousLink(link_id int,map_link map[int]graph.GraphLink) (previous_id []int) { | ||||
| 	for k, link := range map_link{ | ||||
| 		if(k != link_id && link.Destination == w.links[link_id].Source){ | ||||
| 			previous_id = append(previous_id, k) | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	return  | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
| // returns either computing, data or storage | ||||
| @@ -228,7 +141,7 @@ func (w *WorflowDB) getComponentByType(compType string, link graph.GraphLink) (i | ||||
| } | ||||
|  | ||||
| func (w *WorflowDB) isDCLink(link_id int) bool { | ||||
| 	link := w.links[link_id] | ||||
| 	link :=  w.graph.Links[link_id] | ||||
|  | ||||
| 	dest := w.graph.Items[link.Destination.ID] | ||||
| 	dest_id := dest.GetAbstractRessource().GetID() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user