forked from sourcegraph/appdash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmpl.go
148 lines (132 loc) · 3.37 KB
/
tmpl.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
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
package traceapp
import (
"bytes"
"fmt"
htmpl "html/template"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"sourcegraph.com/sourcegraph/appdash"
"sourcegraph.com/sourcegraph/appdash/traceapp/tmpl"
"github.com/gorilla/mux"
)
var (
// ReloadTemplates is whether to reload html/template templates
// before each request. It is useful during development.
ReloadTemplates = true
)
var templates = [][]string{
{"root.html", "layout.html"},
{"trace.html", "layout.html"},
{"traces.html", "layout.html"},
}
// TemplateCommon is data that is passed to (and available to) all templates.
type TemplateCommon struct {
CurrentRoute string
CurrentURI *url.URL
BaseURL *url.URL
}
func (a *App) renderTemplate(w http.ResponseWriter, r *http.Request, name string, status int, data interface{}) error {
a.tmplLock.Lock()
defer a.tmplLock.Unlock()
if a.tmpls == nil || ReloadTemplates {
if err := a.parseHTMLTemplates(templates); err != nil {
return err
}
}
w.WriteHeader(status)
if ct := w.Header().Get("content-type"); ct == "" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
}
t := a.tmpls[name]
if t == nil {
return fmt.Errorf("Template %s not found", name)
}
if data != nil {
// Set TemplateCommon values.
baseURL, err := a.URLTo(RootRoute)
if err != nil {
return err
}
reflect.ValueOf(data).Elem().FieldByName("TemplateCommon").Set(reflect.ValueOf(TemplateCommon{
CurrentRoute: mux.CurrentRoute(r).GetName(),
CurrentURI: r.URL,
BaseURL: baseURL,
}))
}
// Write to a buffer to properly catch errors and avoid partial output written to the http.ResponseWriter
var buf bytes.Buffer
err := t.Execute(&buf, data)
if err != nil {
return err
}
_, err = buf.WriteTo(w)
return err
}
// parseHTMLTemplates parses the HTML templates from their source.
func (a *App) parseHTMLTemplates(sets [][]string) error {
a.tmpls = map[string]*htmpl.Template{}
for _, set := range sets {
t := htmpl.New("")
t.Funcs(htmpl.FuncMap{
"urlTo": a.URLTo,
"urlToTrace": a.URLToTrace,
"itoa": strconv.Itoa,
"str": func(v interface{}) string { return fmt.Sprintf("%s", v) },
"durationClass": durationClass,
"filterAnnotations": filterAnnotations,
"descendTraces": func() bool { return false },
})
for _, tmp := range set {
tmplBytes, err := tmpl.Asset(tmp)
if err != nil {
return fmt.Errorf("template %v: %s", set, err)
}
if _, err := t.Parse(string(tmplBytes)); err != nil {
return fmt.Errorf("template %v: %s", set, err)
}
}
t = t.Lookup("ROOT")
if t == nil {
return fmt.Errorf("ROOT template not found in %v", set)
}
a.tmpls[set[0]] = t
}
return nil
}
func durationClass(usec int64) string {
msec := usec / 1000
if msec < 30 {
return "d0"
} else if msec < 60 {
return "d1"
} else if msec < 90 {
return "d2"
} else if msec < 150 {
return "d3"
} else if msec < 250 {
return "d4"
} else if msec < 400 {
return "d5"
} else if msec < 600 {
return "d6"
} else if msec < 900 {
return "d7"
} else if msec < 1300 {
return "d8"
} else if msec < 1900 {
return "d9"
}
return "d10"
}
func filterAnnotations(anns appdash.Annotations) appdash.Annotations {
var anns2 appdash.Annotations
for _, ann := range anns {
if ann.Key != "" && !strings.HasPrefix(ann.Key, "_") {
anns2 = append(anns2, ann)
}
}
return anns2
}