-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
treemap.go
94 lines (80 loc) · 1.67 KB
/
treemap.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package treemap
import "strings"
// for numerical stability
const minHeatDifferenceForHeatmap float64 = 0.0000001
type Node struct {
Path string
Name string
Size float64
Heat float64
HasHeat bool
}
type Tree struct {
Nodes map[string]Node // node identifier (path) -> Node
To map[string][]string // node identifier (path) -> list of node identifiers (paths) for edges from it (to children)
Root string
}
func (t Tree) HasHeat() bool {
minHeat, maxHeat := t.HeatRange()
return (maxHeat - minHeat) > minHeatDifferenceForHeatmap
}
func (t Tree) HeatRange() (minHeat float64, maxHeat float64) {
first := true
for _, node := range t.Nodes {
if !node.HasHeat {
continue
}
h := node.Heat
if first {
minHeat = h
maxHeat = h
first = false
continue
}
if h > maxHeat {
maxHeat = h
}
if h < minHeat {
minHeat = h
}
}
return minHeat, maxHeat
}
func (t Tree) NormalizeHeat() {
minHeat, maxHeat := t.HeatRange()
if (maxHeat - minHeat) < minHeatDifferenceForHeatmap {
return
}
for path, node := range t.Nodes {
if !node.HasHeat {
continue
}
n := Node{
Path: node.Path,
Name: node.Name,
Size: node.Size,
Heat: (node.Heat - minHeat) / (maxHeat - minHeat),
HasHeat: true,
}
t.Nodes[path] = n
}
}
// SetNamesFromPaths will update each node to its path leaf as name.
func SetNamesFromPaths(t *Tree) {
if t == nil {
return
}
for path, node := range t.Nodes {
parts := strings.Split(node.Path, "/")
if len(parts) == 0 {
continue
}
t.Nodes[path] = Node{
Path: node.Path,
Name: parts[len(parts)-1],
Size: node.Size,
Heat: node.Heat,
HasHeat: node.HasHeat,
}
}
}