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
|
package tachymeter
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"time"
)
type Timeline struct {
timeline []*timelineEvent
}
type timelineEvent struct {
Metrics *Metrics
Created time.Time
}
func (t *Timeline) AddEvent(m *Metrics) {
t.timeline = append(t.timeline, &timelineEvent{
Metrics: m,
Created: time.Now(),
})
}
func (t *Timeline) WriteHTML(p string) error {
path, err := filepath.Abs(p)
if err != nil {
return err
}
var b bytes.Buffer
b.WriteString(head)
for n := range t.timeline {
b.WriteString(fmt.Sprintf(`%s<div class="graph">%s`, tab, nl))
b.WriteString(fmt.Sprintf(`%s%s<canvas id="canvas-%d"></canvas>%s`, tab, tab, n, nl))
b.WriteString(fmt.Sprintf(`%s</div>%s`, tab, nl))
b.WriteString(fmt.Sprintf(`%s<div class="info">%s`, tab, nl))
b.WriteString(fmt.Sprintf(`%s<p><h2>Iteration %d</h2>%s`, tab, n+1, nl))
b.WriteString(t.timeline[n].Metrics.String())
b.WriteString(fmt.Sprintf("%s%s</p></div>%s", nl, tab, nl))
}
for id, m := range t.timeline {
s := genGraphHTML(m, id)
b.WriteString(s)
}
b.WriteString(tail)
d := []byte(b.String())
fname := fmt.Sprintf("%s/tachymeter-%d.html", path, time.Now().Unix())
err = ioutil.WriteFile(fname, d, 0644)
if err != nil {
return err
}
return nil
}
func genGraphHTML(te *timelineEvent, id int) string {
keys := []string{}
values := []uint64{}
for _, b := range *te.Metrics.Histogram {
for k, v := range b {
keys = append(keys, k)
values = append(values, v)
}
}
keysj, _ := json.Marshal(keys)
valuesj, _ := json.Marshal(values)
out := strings.Replace(graph, "XCANVASID", strconv.Itoa(id), 1)
out = strings.Replace(out, "XKEYS", string(keysj), 1)
out = strings.Replace(out, "XVALUES", string(valuesj), 1)
return out
}
|