/
prune.go
40 lines (36 loc) · 1.15 KB
/
prune.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package utils
import (
"strings"
"time"
"github.com/weaveworks/scope/report"
)
// Prune returns a copy of the Nodes with all information not strictly
// necessary for rendering nodes and edges in the UI cut away.
func Prune(nodes report.Nodes) report.Nodes {
result := report.Nodes{}
for id, node := range nodes {
result[id] = PruneNode(node)
}
return result
}
// PruneNode returns a copy of the Node with all information not strictly
// necessary for rendering nodes and edges stripped away. Specifically, that
// means cutting out parts of the Node.
func PruneNode(node report.Node) report.Node {
prunedChildren := report.MakeNodeSet()
node.Children.ForEach(func(child report.Node) {
prunedChildren = prunedChildren.Add(PruneNode(child))
})
prunedNode := report.MakeNode(
node.ID).
WithTopology(node.Topology).
WithAdjacent(node.Adjacency...).
WithChildren(prunedChildren)
// Copy counters across, but with zero timestamp so they compare equal
node.Latest.ForEach(func(k string, _ time.Time, v string) {
if strings.HasPrefix(k, report.CounterPrefix) {
prunedNode.Latest = prunedNode.Latest.Set(k, time.Time{}, v)
}
})
return prunedNode
}