forked from sourcegraph/appdash
/
tmpl.go
167 lines (150 loc) · 3.94 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package traceapp
import (
"bytes"
"errors"
"fmt"
htmpl "html/template"
"io/ioutil"
"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"},
{"dashboard.html", "layout.html"},
{"aggregate.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.
reflect.ValueOf(data).Elem().FieldByName("TemplateCommon").Set(reflect.ValueOf(TemplateCommon{
CurrentRoute: mux.CurrentRoute(r).GetName(),
CurrentURI: r.URL,
BaseURL: a.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 },
"dict": dict,
})
for _, tmp := range set {
tmplFile, err := tmpl.Data.Open("/" + tmp)
if err != nil {
return fmt.Errorf("template %v: %s", set, err)
}
tmplBytes, err := ioutil.ReadAll(tmplFile)
tmplFile.Close()
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
}
// dict builds a map of paired items, allowing you to invoke a template with
// multiple parameters.
func dict(pairs ...interface{}) (map[string]interface{}, error) {
if len(pairs)%2 != 0 {
return nil, errors.New("expected pairs")
}
m := make(map[string]interface{}, len(pairs)/2)
for i := 0; i < len(pairs); i += 2 {
m[pairs[i].(string)] = pairs[i+1]
}
return m, nil
}