prune.go 1.2 KB

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