-
Notifications
You must be signed in to change notification settings - Fork 0
/
treemap.go
97 lines (83 loc) · 1.65 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
95
96
97
package treemap
import (
"context"
"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
To map[string][]string
Root string
}
func (t Tree) HasHeat(ctx context.Context) bool {
minHeat, maxHeat := t.HeatRange(ctx)
return (maxHeat - minHeat) > minHeatDifferenceForHeatmap
}
func (t Tree) HeatRange(ctx context.Context) (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(ctx context.Context) {
minHeat, maxHeat := t.HeatRange(ctx)
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(ctx context.Context, 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,
}
}
}