go-chart/examples/request_timings/main.go

134 lines
3.1 KiB
Go
Raw Permalink Normal View History

2016-08-07 13:51:06 -04:00
package main
2019-09-10 00:02:48 -04:00
//go:generate go run main.go
2016-08-07 13:51:06 -04:00
import (
2017-02-06 17:25:09 -05:00
"fmt"
2016-08-07 13:51:06 -04:00
"net/http"
2019-09-10 00:02:48 -04:00
"os"
2016-10-21 15:44:37 -04:00
"strconv"
2016-08-07 13:51:06 -04:00
"time"
2024-10-27 22:52:38 -04:00
"git.smarteching.com/zeni/go-chart/v2"
2016-08-07 13:51:06 -04:00
)
2019-09-10 00:02:48 -04:00
func main() {
log := chart.NewLogger()
drawChart(log)
}
2016-10-21 15:44:37 -04: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 13:51:06 -04:00
func readData() ([]time.Time, []float64) {
var xvalues []time.Time
var yvalues []float64
2019-02-13 19:09:26 -05:00
err := chart.ReadLines("requests.csv", func(line string) error {
2019-02-13 21:55:13 -05:00
parts := chart.SplitCSV(line)
2016-10-21 15:44:37 -04:00
year := parseInt(parts[0])
month := parseInt(parts[1])
day := parseInt(parts[2])
hour := parseInt(parts[3])
elapsedMillis := parseFloat64(parts[4])
2016-08-07 13:51:06 -04: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 13:51:06 -04:00
})
if err != nil {
fmt.Println(err.Error())
}
2016-08-07 13:51:06 -04:00
return xvalues, yvalues
}
2016-08-11 18:51:49 -04:00
func releases() []chart.GridLine {
return []chart.GridLine{
2019-02-13 19:09:26 -05: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, 2, 15, 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-11 18:51:49 -04:00
}
}
2019-02-13 21:55:13 -05:00
func drawChart(log chart.Logger) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
xvalues, yvalues := readData()
mainSeries := chart.TimeSeries{
Name: "Prod Request Timings",
Style: chart.Style{
StrokeColor: chart.ColorBlue,
FillColor: chart.ColorBlue.WithAlpha(100),
},
XValues: xvalues,
YValues: yvalues,
}
2016-08-07 13:51:06 -04:00
2019-02-13 21:55:13 -05:00
linreg := &chart.LinearRegressionSeries{
Name: "Linear Regression",
Style: chart.Style{
StrokeColor: chart.ColorAlternateBlue,
StrokeDashArray: []float64{5.0, 5.0},
},
InnerSeries: mainSeries,
}
2016-08-07 13:51:06 -04:00
2019-02-13 21:55:13 -05:00
sma := &chart.SMASeries{
Name: "SMA",
Style: chart.Style{
StrokeColor: chart.ColorRed,
StrokeDashArray: []float64{5.0, 5.0},
},
InnerSeries: mainSeries,
}
2016-08-07 13:51:06 -04:00
2019-02-13 21:55:13 -05:00
graph := chart.Chart{
Log: log,
Width: 1280,
Height: 720,
Background: chart.Style{
Padding: chart.Box{
Top: 50,
},
2016-08-27 16:45:38 -04:00
},
2019-02-13 21:55:13 -05:00
YAxis: chart.YAxis{
Name: "Elapsed Millis",
TickStyle: chart.Style{
TextRotationDegrees: 45.0,
},
ValueFormatter: func(v interface{}) string {
return fmt.Sprintf("%d ms", int(v.(float64)))
},
2016-10-21 15:50:40 -04:00
},
2019-02-13 21:55:13 -05:00
XAxis: chart.XAxis{
ValueFormatter: chart.TimeHourValueFormatter,
GridMajorStyle: chart.Style{
StrokeColor: chart.ColorAlternateGray,
StrokeWidth: 1.0,
},
GridLines: releases(),
2017-02-06 17:25:09 -05:00
},
2019-02-13 21:55:13 -05:00
Series: []chart.Series{
mainSeries,
linreg,
2019-09-10 00:02:48 -04:00
chart.LastValueAnnotationSeries(linreg),
2019-02-13 21:55:13 -05:00
sma,
2019-09-10 00:02:48 -04:00
chart.LastValueAnnotationSeries(sma),
2016-08-11 23:38:53 -04:00
},
2019-02-13 21:55:13 -05:00
}
2016-08-07 13:51:06 -04:00
2019-02-13 21:55:13 -05:00
graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}
2016-08-07 13:51:06 -04:00
2019-09-10 00:02:48 -04:00
f, _ := os.Create("output.png")
defer f.Close()
graph.Render(chart.PNG, f)
2019-02-13 21:55:13 -05:00
}
2016-08-07 13:51:06 -04:00
}