response times example needs some ergonomic love.

This commit is contained in:
Will Charczuk 2016-07-17 14:25:42 -07:00
parent 7ae7cc13a8
commit cea31c6c23
4 changed files with 305 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package chart
import (
"sync"
"time"
"github.com/golang/freetype/truetype"
"github.com/wcharczuk/go-chart/drawing"
@ -57,6 +58,8 @@ const (
DefaultDateFormat = "2006-01-02"
// DefaultDateHourFormat is the date format for hour timestamp formats.
DefaultDateHourFormat = "01-02 3PM"
// DefaultDateMinuteFormat is the date format for minute range timestamp formats.
DefaultDateMinuteFormat = time.Kitchen
// DefaultFloatFormat is the default float format.
DefaultFloatFormat = "%.2f"
// DefaultPercentValueFormat is the default percent format.

View File

@ -0,0 +1,294 @@
package main
import (
"bufio"
"bytes"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/wcharczuk/go-chart"
"github.com/wcharczuk/go-chart/drawing"
)
func drawChart(res http.ResponseWriter, req *http.Request) {
xv, yv := data()
times := chart.TimeSeries{
Name: "Elapsed ms",
XValues: xv,
YValues: yv,
}
tma := chart.SMASeries{
Style: chart.Style{
Show: true,
StrokeColor: drawing.ColorRed,
StrokeDashArray: []float64{5.0, 5.0},
},
InnerSeries: times,
Period: 50,
}
graph := chart.Chart{
XAxis: chart.XAxis{
Style: chart.Style{Show: true},
ValueFormatter: chart.TimeMinuteValueFormatter,
},
YAxis: chart.YAxis{
Style: chart.Style{Show: true},
ValueFormatter: func(v interface{}) string {
if typed, isTyped := v.(float64); isTyped {
return fmt.Sprintf("%0.2fms", typed)
}
return ""
},
},
Series: []chart.Series{
times,
tma,
},
}
res.Header().Set("Content-Type", "image/svg+xml")
graph.Render(chart.SVG, res)
}
func main() {
http.HandleFunc("/", drawChart)
http.ListenAndServe(":8080", nil)
}
func data() ([]time.Time, []float64) {
var timestamps []time.Time
var elapsed []float64
scanner := bufio.NewScanner(bytes.NewBuffer([]byte(rawData)))
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
pieces := strings.Split(scanner.Text(), ",")
hour, _ := strconv.Atoi(pieces[0])
minute, _ := strconv.Atoi(pieces[1])
second, _ := strconv.Atoi(pieces[2])
now := time.Now().UTC()
timestamps = append(timestamps, time.Date(now.Year(), now.Month(), now.Day(), hour, minute, second, 0, time.UTC))
parsedElapsed, err := strconv.ParseFloat(pieces[3], 64)
if err == nil {
elapsed = append(elapsed, parsedElapsed/1000.0)
} else {
println(err.Error())
}
}
return timestamps, elapsed
}
var (
rawData = `19,39,18,12667.583333333333
19,39,19,8027.0000000000000000
19,39,20,12562.857142857143
19,39,22,8724.8000000000000000
19,39,25,10162.7500000000000000
19,39,27,11827.0000000000000000
19,39,28,9618.1000000000000000
19,39,29,13576.818181818182
19,39,30,15887.750000000000
19,39,31,10618.2500000000000000
19,39,36,10933.7142857142857143
19,39,37,6867.0000000000000000
19,39,38,10123.8461538461538462
19,39,39,6610.0000000000000000
19,39,41,12300.000000000000
19,39,44,11224.7500000000000000
19,39,45,10408.2500000000000000
19,39,46,11958.5000000000000000
19,39,47,12380.000000000000
19,39,49,12254.5000000000000000
19,39,50,11668.2500000000000000
19,39,51,8328.0000000000000000
19,39,52,8662.0000000000000000
19,39,53,8210.0000000000000000
19,39,55,7966.5000000000000000
19,39,56,7199.0000000000000000
19,39,57,8827.9411764705882353
19,39,58,10015.4000000000000000
19,39,59,10315.5000000000000000
19,40,0,7072.0000000000000000
19,40,1,10165.3333333333333333
19,40,2,8491.7500000000000000
19,40,4,7943.5000000000000000
19,40,6,7833.8333333333333333
19,40,7,8368.0000000000000000
19,40,10,7771.1428571428571429
19,40,11,7342.0000000000000000
19,40,12,8838.0000000000000000
19,40,13,8859.0000000000000000
19,40,14,9342.0000000000000000
19,40,15,9068.5000000000000000
19,40,16,9038.8333333333333333
19,40,17,10534.5000000000000000
19,40,18,11313.375000000000
19,40,19,11042.5000000000000000
19,40,20,9595.6666666666666667
19,40,21,9488.0714285714285714
19,40,22,9494.9523809523809524
19,40,23,8496.3000000000000000
19,40,24,8383.0000000000000000
19,40,25,12987.000000000000
19,40,26,9842.0909090909090909
19,40,27,11258.625000000000
19,40,28,8300.4000000000000000
19,40,29,7879.3333333333333333
19,40,30,7080.0000000000000000
19,40,31,10902.0000000000000000
19,40,32,12349.0000000000000000
19,40,33,6883.5000000000000000
19,40,35,10097.6666666666666667
19,40,36,9014.7000000000000000
19,40,37,8808.6000000000000000
19,40,38,10870.0000000000000000
19,40,39,8465.5000000000000000
19,40,41,8491.5000000000000000
19,40,42,7662.7500000000000000
19,40,43,7608.8333333333333333
19,40,44,7739.5000000000000000
19,40,45,10960.916666666667
19,40,46,7899.1111111111111111
19,40,47,9148.0000000000000000
19,40,48,7699.5000000000000000
19,40,50,6846.5000000000000000
19,40,51,8368.0000000000000000
19,40,52,8289.8750000000000000
19,40,53,10589.2500000000000000
19,40,54,11763.0000000000000000
19,40,55,6925.5000000000000000
19,40,56,7717.5000000000000000
19,40,58,11074.5000000000000000
19,41,0,9444.5000000000000000
19,41,1,9895.5000000000000000
19,41,2,9601.5000000000000000
19,42,0,3358.5000000000000000
19,42,1,8377.5454545454545455
19,42,2,8457.5000000000000000
19,42,3,11676.0000000000000000
19,42,7,9066.5000000000000000
19,42,10,8528.5000000000000000
19,42,11,9981.3333333333333333
19,42,13,9923.1666666666666667
19,42,15,11943.0000000000000000
19,42,17,9073.5000000000000000
19,42,18,11792.0000000000000000
19,42,19,7716.7500000000000000
19,42,20,10501.4166666666666667
19,42,21,7109.5000000000000000
19,42,22,17030.166666666667
19,42,24,6466.0000000000000000
19,42,25,7661.5000000000000000
19,42,26,9274.7142857142857143
19,42,27,10534.6666666666666667
19,42,30,3491.0000000000000000
19,42,31,8535.8000000000000000
19,42,32,13026.666666666667
19,42,33,13179.500000000000
19,42,34,6155.0000000000000000
19,42,35,34231.125000000000
19,42,36,12951.0000000000000000
19,42,38,10411.7000000000000000
19,42,40,6747.2000000000000000
19,42,41,15279.875000000000
19,42,42,12241.166666666667
19,42,43,11893.500000000000
19,42,44,9098.9166666666666667
19,42,45,12216.000000000000
19,42,46,11550.0000000000000000
19,42,47,20060.555555555556
19,42,55,5052.5000000000000000
19,42,56,11508.583333333333
19,42,57,10053.4000000000000000
19,42,58,9173.4444444444444444
19,42,59,13186.333333333333
19,43,0,9756.2000000000000000
19,43,1,10780.2500000000000000
19,43,3,10821.0000000000000000
19,43,5,10301.7500000000000000
19,43,6,14655.666666666667
19,43,8,12076.823529411765
19,43,10,10305.3333333333333333
19,43,11,18108.571428571429
19,43,12,8751.0000000000000000
19,43,14,9973.5000000000000000
19,43,15,11840.7500000000000000
19,43,23,3367.5000000000000000
19,43,24,8991.1538461538461538
19,43,25,10751.0000000000000000
19,43,26,13764.125000000000
19,43,31,8537.2500000000000000
19,43,33,19791.111111111111
19,43,35,14261.444444444444
19,43,37,13769.750000000000
19,43,39,12506.000000000000
19,43,40,13695.000000000000
19,47,2,5118.0000000000000000
19,48,54,6951.0000000000000000
19,48,56,3433.0000000000000000
19,48,58,9402.4166666666666667
19,48,59,3292.0000000000000000
19,49,0,10277.0000000000000000
19,49,1,14803.875000000000
19,49,7,14470.500000000000
19,49,10,26686.000000000000
19,49,11,20091.000000000000
19,49,21,3691.0000000000000000
19,49,22,8155.0000000000000000
19,49,23,16555.181818181818
19,49,24,3348.0000000000000000
19,49,25,16269.875000000000
19,49,26,15848.500000000000
19,49,29,16668.500000000000
19,49,30,15028.250000000000
19,49,31,22999.875000000000
19,49,32,14734.083333333333
19,49,35,22447.500000000000
19,49,36,15578.000000000000
19,49,39,17626.250000000000
19,49,41,20711.250000000000
19,49,43,16976.937500000000
19,49,47,11968.0000000000000000
19,49,54,13458.125000000000
19,49,55,12559.000000000000
19,49,56,18347.750000000000
19,49,57,14620.500000000000
19,50,0,23275.750000000000
19,50,2,10016.7500000000000000
19,50,3,28256.250000000000
19,50,4,13031.500000000000
19,50,6,19709.500000000000
19,50,7,22084.375000000000
19,50,8,14460.062500000000
19,50,9,20490.000000000000
19,50,11,13978.000000000000
19,50,12,14834.500000000000
19,50,13,22603.833333333333
19,50,14,12853.250000000000
19,50,18,14137.250000000000
19,50,21,12959.000000000000
19,50,22,10084.5000000000000000
19,50,24,11822.909090909091
19,50,25,53480.000000000000
19,50,27,23069.750000000000
19,50,33,13941.875000000000
19,50,34,12612.500000000000
19,50,36,13657.500000000000
19,50,38,21747.333333333333
19,50,39,14191.000000000000
19,50,43,13695.000000000000
19,50,45,13022.500000000000
19,50,47,14497.250000000000
19,50,49,13374.166666666667
19,50,50,11087.7500000000000000
19,50,59,12045.0000000000000000`
)

View File

@ -17,7 +17,7 @@ func (f Float) String() string {
// TimeToFloat64 returns a float64 representation of a time.
func TimeToFloat64(t time.Time) float64 {
return float64(t.Unix())
return float64(t.UnixNano())
}
// MinAndMax returns both the min and max in one pass.

View File

@ -18,16 +18,21 @@ func TimeHourValueFormatter(v interface{}) string {
return TimeValueFormatterWithFormat(v, DefaultDateHourFormat)
}
// TimeMinuteValueFormatter is a ValueFormatter for timestamps.
func TimeMinuteValueFormatter(v interface{}) string {
return TimeValueFormatterWithFormat(v, DefaultDateMinuteFormat)
}
// TimeValueFormatterWithFormat is a ValueFormatter for timestamps with a given format.
func TimeValueFormatterWithFormat(v interface{}, dateFormat string) string {
if typed, isTyped := v.(time.Time); isTyped {
return typed.Format(dateFormat)
}
if typed, isTyped := v.(int64); isTyped {
return time.Unix(typed, 0).Format(dateFormat)
return time.Unix(0, typed).Format(dateFormat)
}
if typed, isTyped := v.(float64); isTyped {
return time.Unix(int64(typed), 0).Format(dateFormat)
return time.Unix(0, int64(typed)).Format(dateFormat)
}
return ""
}