Add NodeMetadata.Counters with appropriate merge semantics

This commit is contained in:
Tom Wilkie
2015-08-19 14:24:21 +00:00
parent 8c4bbdae8c
commit 7508df7e5b
4 changed files with 17 additions and 16 deletions

View File

@@ -22,6 +22,9 @@ func fixNodeMetadatas(nodes render.RenderableNodes) render.RenderableNodes {
if node.NodeMetadata.Metadata == nil {
node.NodeMetadata.Metadata = map[string]string{}
}
if node.NodeMetadata.Counters == nil {
node.NodeMetadata.Counters = map[string]int{}
}
result[id] = node
}
return result

View File

@@ -20,8 +20,8 @@ const (
TheInternetID = "theinternet"
TheInternetMajor = "The Internet"
containersPrefix = "container-"
processesPrefix = "process-"
containersKey = "containers"
processesKey = "processes"
)
// LeafMapFunc is anything which can take an arbitrary NodeMetadata, which is
@@ -252,20 +252,10 @@ func MapProcess2Name(n RenderableNode) (RenderableNode, bool) {
node := newDerivedNode(name, n)
node.LabelMajor = name
node.Rank = name
node.NodeMetadata.Metadata[processesPrefix+n.ID] = ""
node.NodeMetadata.Counters[processesKey] = 1
return node, true
}
func countPrefix(prefix string, n RenderableNode) int {
count := 0
for key := range n.NodeMetadata.Metadata {
if strings.HasPrefix(key, prefix) {
count++
}
}
return count
}
// MapCountProcessName maps 1:1 process name nodes, counting
// the number of processes grouped together and putting
// that info in the minor label.
@@ -274,7 +264,7 @@ func MapCountProcessName(n RenderableNode) (RenderableNode, bool) {
return n, true
}
processes := countPrefix(processesPrefix, n)
processes := n.NodeMetadata.Counters[processesKey]
if processes == 1 {
n.LabelMinor = "1 process"
} else {
@@ -309,7 +299,7 @@ func MapContainer2ContainerImage(n RenderableNode) (RenderableNode, bool) {
// Add container-<id> key to NMD, which will later be counted to produce the minor label
result := newDerivedNode(id, n)
result.NodeMetadata.Metadata[containersPrefix+n.ID] = ""
result.NodeMetadata.Counters[containersKey] = 1
return result, true
}
@@ -349,7 +339,7 @@ func MapCountContainers(n RenderableNode) (RenderableNode, bool) {
return n, true
}
containers := countPrefix(containersPrefix, n)
containers := n.NodeMetadata.Counters[containersKey]
if containers == 1 {
n.LabelMinor = "1 container"
} else {

View File

@@ -48,6 +48,9 @@ func (nm NodeMetadata) Merge(other NodeMetadata) NodeMetadata {
for k, v := range other.Metadata {
nm.Metadata[k] = v // other takes precedence
}
for k, v := range other.Counters {
nm.Counters[k] = nm.Counters[k] + v
}
return nm
}

View File

@@ -42,6 +42,7 @@ type EdgeMetadata struct {
// about a given node in a given topology.
type NodeMetadata struct {
Metadata map[string]string
Counters map[string]int
}
// MakeNodeMetadata creates a new NodeMetadata with no initial metadata.
@@ -53,6 +54,7 @@ func MakeNodeMetadata() NodeMetadata {
func MakeNodeMetadataWith(m map[string]string) NodeMetadata {
return NodeMetadata{
Metadata: m,
Counters: map[string]int{},
}
}
@@ -62,6 +64,9 @@ func (nm NodeMetadata) Copy() NodeMetadata {
for k, v := range nm.Metadata {
cp.Metadata[k] = v
}
for k, v := range nm.Counters {
cp.Counters[k] = v
}
return cp
}