go-chart/_examples/request_timings/main.go

114 lines
2.8 KiB
Go
Raw Normal View History

2016-08-07 19:51:06 +02:00
package main
import (
"net/http"
"strings"
"time"
util "github.com/blendlabs/go-util"
"github.com/wcharczuk/go-chart"
)
func readData() ([]time.Time, []float64) {
var xvalues []time.Time
var yvalues []float64
util.ReadFileByLines("requests.csv", func(line string) {
parts := strings.Split(line, ",")
year := util.ParseInt(parts[0])
month := util.ParseInt(parts[1])
day := util.ParseInt(parts[2])
hour := util.ParseInt(parts[3])
elapsedMillis := util.ParseFloat64(parts[4])
xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC))
yvalues = append(yvalues, elapsedMillis)
})
return xvalues, yvalues
}
2016-08-12 00:51:49 +02:00
func releases() []chart.GridLine {
return []chart.GridLine{
2016-08-12 05:38:53 +02:00
{Value: chart.TimeToFloat64(time.Date(2016, 8, 1, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 2, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 3, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 4, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 5, 9, 30, 0, 0, time.UTC))},
{Value: chart.TimeToFloat64(time.Date(2016, 8, 6, 9, 30, 0, 0, time.UTC))},
2016-08-12 00:51:49 +02:00
}
}
2016-08-07 19:51:06 +02:00
func drawChart(res http.ResponseWriter, req *http.Request) {
xvalues, yvalues := readData()
mainSeries := chart.TimeSeries{
Name: "Prod Request Timings",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorBlue,
FillColor: chart.ColorBlue.WithAlpha(100),
},
XValues: xvalues,
YValues: yvalues,
}
linreg := &chart.LinearRegressionSeries{
Name: "Linear Regression",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorAlternateBlue,
StrokeDashArray: []float64{5.0, 5.0},
},
InnerSeries: mainSeries,
}
sma := &chart.SMASeries{
Name: "SMA",
Style: chart.Style{
Show: true,
StrokeColor: chart.ColorRed,
StrokeDashArray: []float64{5.0, 5.0},
},
InnerSeries: mainSeries,
}
graph := chart.Chart{
Width: 1280,
Height: 720,
2016-08-27 22:45:38 +02:00
Background: chart.Style{
Padding: chart.Box{
Top: 50,
},
},
2016-08-07 19:51:06 +02:00
YAxis: chart.YAxis{
Name: "Elapsed Millis",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
},
XAxis: chart.XAxis{
Style: chart.StyleShow(),
ValueFormatter: chart.TimeHourValueFormatter,
2016-08-12 05:38:53 +02:00
GridMajorStyle: chart.Style{
Show: true,
StrokeColor: chart.ColorAlternateGray,
StrokeWidth: 1.0,
},
GridLines: releases(),
2016-08-07 19:51:06 +02:00
},
Series: []chart.Series{
mainSeries,
linreg,
chart.LastValueAnnotation(linreg),
sma,
chart.LastValueAnnotation(sma),
},
}
2016-08-27 22:45:38 +02:00
graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}
2016-08-07 19:51:06 +02:00
res.Header().Set("Content-Type", "image/png")
graph.Render(chart.PNG, res)
}
func main() {
http.HandleFunc("/", drawChart)
http.ListenAndServe(":8080", nil)
}