go-chart/_examples/request_timings/main.go

138 lines
3.2 KiB
Go
Raw Permalink Normal View History

2016-08-07 19:51:06 +02:00
package main
import (
2017-02-06 23:25:09 +01:00
"fmt"
2016-08-07 19:51:06 +02:00
"net/http"
2016-10-21 21:44:37 +02:00
"strconv"
2016-08-07 19:51:06 +02:00
"strings"
"time"
"github.com/wcharczuk/go-chart"
util "github.com/wcharczuk/go-chart/util"
2016-08-07 19:51:06 +02:00
)
2016-10-21 21:44:37 +02:00
func parseInt(str string) int {
v, _ := strconv.Atoi(str)
return v
}
func parseFloat64(str string) float64 {
v, _ := strconv.ParseFloat(str, 64)
return v
}
2016-08-07 19:51:06 +02:00
func readData() ([]time.Time, []float64) {
var xvalues []time.Time
var yvalues []float64
err := util.File.ReadByLines("requests.csv", func(line string) error {
2016-08-07 19:51:06 +02:00
parts := strings.Split(line, ",")
2016-10-21 21:44:37 +02:00
year := parseInt(parts[0])
month := parseInt(parts[1])
day := parseInt(parts[2])
hour := parseInt(parts[3])
elapsedMillis := parseFloat64(parts[4])
2016-08-07 19:51:06 +02:00
xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC))
yvalues = append(yvalues, elapsedMillis)
return nil
2016-08-07 19:51:06 +02:00
})
if err != nil {
fmt.Println(err.Error())
}
2016-08-07 19:51:06 +02:00
return xvalues, yvalues
}
2016-08-12 00:51:49 +02:00
func releases() []chart.GridLine {
return []chart.GridLine{
{Value: util.Time.ToFloat64(time.Date(2016, 8, 1, 9, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 2, 9, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 2, 15, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 4, 9, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(time.Date(2016, 8, 5, 9, 30, 0, 0, time.UTC))},
{Value: util.Time.ToFloat64(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(),
2016-10-21 21:50:40 +02:00
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
2017-02-06 23:25:09 +01:00
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%d ms", int(v.(float64)))
},
2016-08-07 19:51:06 +02:00
},
XAxis: chart.XAxis{
2016-10-21 21:44:37 +02:00
Style: chart.Style{
Show: true,
},
2016-08-07 19:51:06 +02:00
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", chart.ContentTypePNG)
graph.Render(chart.PNG, res)
2016-08-07 19:51:06 +02:00
}
func main() {
http.HandleFunc("/", drawChart)
http.ListenAndServe(":8080", nil)
}