44"encoding/json"
55"fmt"
66"os"
7+ "strings"
78
89"github.com/compspec/compspec-go/pkg/utils"
910"github.com/converged-computing/jsongraph-go/jsongraph/metadata"
@@ -20,6 +21,9 @@ type ClusterGraph struct {
2021// Top level counter for node labels (JGF v2) that maps to ids (JGF v1)
2122nodeCounter int32
2223
24+ // Easy reference to root name
25+ rootName string
26+
2327// Counters for specific resource types (e.g., rack, node)
2428resourceCounters map [string ]int32
2529}
@@ -41,7 +45,7 @@ func (c *ClusterGraph) SaveGraph(path string) error {
4145fmt .Printf ("Graph %s already exists, will not overwrite\n " , path )
4246return nil
4347}
44- content , err := json .MarshalIndent (c . Graph , "" , " " )
48+ content , err := json .MarshalIndent (c , "" , " " )
4549if err != nil {
4650return err
4751}
@@ -55,10 +59,15 @@ func (c *ClusterGraph) SaveGraph(path string) error {
5559
5660// Path gets a new path
5761func getNodePath (root , subpath string ) string {
62+ var path string
5863if subpath == "" {
59- return fmt .Sprintf ("/%s" , root )
64+ path = fmt .Sprintf ("/%s" , root )
65+ } else {
66+ path = fmt .Sprintf ("/%s/%s" , root , subpath )
6067}
61- return fmt .Sprintf ("/%s/%s" , root , subpath )
68+ // Hack to allow for imperfection of slash placement
69+ path = strings .ReplaceAll (path , "//" , "/" )
70+ return path
6271}
6372
6473// AddNode adds a node to the graph
@@ -69,8 +78,9 @@ func (c *ClusterGraph) AddNode(
6978size int32 ,
7079exclusive bool ,
7180unit string ,
81+ path string ,
7282) * graph.Node {
73- node := c .getNode (resource , name , size , exclusive , unit )
83+ node := c .getNode (resource , name , size , exclusive , unit , path )
7484c .Graph .Nodes [* node .Label ] = * node
7585return node
7686}
@@ -88,6 +98,7 @@ func (c *ClusterGraph) getNode(
8898size int32 ,
8999exclusive bool ,
90100unit string ,
101+ path string ,
91102) * graph.Node {
92103
93104// Get the identifier for the resource type
@@ -101,24 +112,26 @@ func (c *ClusterGraph) getNode(
101112
102113// The id in the metadata is the counter for that resource type
103114resourceCounter := fmt .Sprintf ("%d" , counter )
115+ nameWithCount := fmt .Sprintf ("%s%d" , name , counter )
104116
105117// The resource name is the type + the resource counter
106- resourceName := fmt .Sprintf ("%s%d" , name , counter )
118+ // path should be assembled from parents up to this node
119+ resourceName := fmt .Sprintf ("%s/%s%d" , path , name , counter )
107120
108121// New Metadata with expected fluxion data
109122m := metadata.Metadata {}
110123m .AddElement ("type" , resource )
111124m .AddElement ("basename" , name )
112125m .AddElement ("id" , resourceCounter )
113- m .AddElement ("name" , resourceName )
126+ m .AddElement ("name" , nameWithCount )
114127
115128// uniq_id should be the same as the label, but as an integer
116129m .AddElement ("uniq_id" , count )
117130m .AddElement ("rank" , - 1 )
118131m .AddElement ("exclusive" , exclusive )
119132m .AddElement ("unit" , unit )
120133m .AddElement ("size" , size )
121- m .AddElement ("paths" , map [string ]string {"containment" : getNodePath (name , "" )})
134+ m .AddElement ("paths" , map [string ]string {"containment" : getNodePath (c . rootName , resourceName )})
122135
123136// Update the resource counter
124137counter += 1
@@ -154,7 +167,7 @@ func NewClusterGraph(name string) (ClusterGraph, error) {
154167m .AddElement ("exclusive" , false )
155168m .AddElement ("unit" , "" )
156169m .AddElement ("size" , 1 )
157- m .AddElement ("paths" , map [string ]string {"containment" : getNodePath (name , "" )})
170+ m .AddElement ("paths" , map [string ]string {"containment" : getNodePath (clusterName , "" )})
158171
159172// Root cluster node
160173label := "0"
@@ -166,7 +179,7 @@ func NewClusterGraph(name string) (ClusterGraph, error) {
166179// Create a new cluster!
167180// Start counting at 1 - index 0 is the cluster root
168181resourceCounters := map [string ]int32 {"cluster" : int32 (1 )}
169- cluster := ClusterGraph {g , name , 1 , resourceCounters }
182+ cluster := ClusterGraph {g , name , 1 , clusterName , resourceCounters }
170183
171184return cluster , nil
172185}
0 commit comments