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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package memsizeui
import (
"bytes"
"fmt"
"html/template"
"net/http"
"reflect"
"sort"
"strings"
"sync"
"time"
"github.com/fjl/memsize"
)
type Handler struct {
init sync.Once
mux http.ServeMux
mu sync.Mutex
reports map[int]Report
roots map[string]interface{}
reportID int
}
type Report struct {
ID int
Date time.Time
Duration time.Duration
RootName string
Sizes memsize.Sizes
}
type templateInfo struct {
Roots []string
Reports map[int]Report
PathDepth int
Data interface{}
}
func (ti *templateInfo) Link(path ...string) string {
prefix := strings.Repeat("../", ti.PathDepth)
return prefix + strings.Join(path, "")
}
func (h *Handler) Add(name string, v interface{}) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
panic("root must be non-nil pointer")
}
h.mu.Lock()
if h.roots == nil {
h.roots = make(map[string]interface{})
}
h.roots[name] = v
h.mu.Unlock()
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.init.Do(func() {
h.reports = make(map[int]Report)
h.mux.HandleFunc("/", h.handleRoot)
h.mux.HandleFunc("/scan", h.handleScan)
h.mux.HandleFunc("/report/", h.handleReport)
})
h.mux.ServeHTTP(w, r)
}
func (h *Handler) templateInfo(r *http.Request, data interface{}) *templateInfo {
h.mu.Lock()
roots := make([]string, 0, len(h.roots))
for name := range h.roots {
roots = append(roots, name)
}
h.mu.Unlock()
sort.Strings(roots)
return &templateInfo{
Roots: roots,
Reports: h.reports,
PathDepth: strings.Count(r.URL.Path, "/") - 1,
Data: data,
}
}
func (h *Handler) handleRoot(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
serveHTML(w, rootTemplate, http.StatusOK, h.templateInfo(r, nil))
}
func (h *Handler) handleScan(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "invalid HTTP method, want POST", http.StatusMethodNotAllowed)
return
}
ti := h.templateInfo(r, "Unknown root")
id, ok := h.scan(r.URL.Query().Get("root"))
if !ok {
serveHTML(w, notFoundTemplate, http.StatusNotFound, ti)
return
}
w.Header().Add("Location", ti.Link(fmt.Sprintf("report/%d", id)))
w.WriteHeader(http.StatusSeeOther)
}
func (h *Handler) handleReport(w http.ResponseWriter, r *http.Request) {
var id int
fmt.Sscan(strings.TrimPrefix(r.URL.Path, "/report/"), &id)
h.mu.Lock()
report, ok := h.reports[id]
h.mu.Unlock()
if !ok {
serveHTML(w, notFoundTemplate, http.StatusNotFound, h.templateInfo(r, "Report not found"))
} else {
serveHTML(w, reportTemplate, http.StatusOK, h.templateInfo(r, report))
}
}
func (h *Handler) scan(root string) (int, bool) {
h.mu.Lock()
defer h.mu.Unlock()
val, ok := h.roots[root]
if !ok {
return 0, false
}
id := h.reportID
start := time.Now()
sizes := memsize.Scan(val)
h.reports[id] = Report{
ID: id,
RootName: root,
Date: start.Truncate(1 * time.Second),
Duration: time.Since(start),
Sizes: sizes,
}
h.reportID++
return id, true
}
func serveHTML(w http.ResponseWriter, tpl *template.Template, status int, ti *templateInfo) {
w.Header().Set("content-type", "text/html")
var buf bytes.Buffer
if err := tpl.Execute(&buf, ti); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
buf.WriteTo(w)
}