2016-08-07 13:51:06 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-06 17:25:09 -05:00
|
|
|
"fmt"
|
2016-08-07 13:51:06 -04:00
|
|
|
"net/http"
|
2016-10-21 15:44:37 -04:00
|
|
|
"strconv"
|
2016-08-07 13:51:06 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/wcharczuk/go-chart"
|
2017-05-12 20:06:45 -04:00
|
|
|
util "github.com/wcharczuk/go-chart/util"
|
2016-08-07 13:51:06 -04:00
|
|
|
)
|
|
|
|
|
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
|
2017-05-12 20:06:45 -04:00
|
|
|
err := util.File.ReadByLines("requests.csv", func(line string) error {
|
2016-08-07 13:51:06 -04:00
|
|
|
parts := strings.Split(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)
|
2017-05-12 20:06:45 -04:00
|
|
|
return nil
|
2016-08-07 13:51:06 -04:00
|
|
|
})
|
2017-03-05 19:54:40 -05: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{
|
2017-05-12 20:06:45 -04:00
|
|
|
{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-11 18:51:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-07 13:51:06 -04: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 16:45:38 -04:00
|
|
|
Background: chart.Style{
|
|
|
|
Padding: chart.Box{
|
|
|
|
Top: 50,
|
|
|
|
},
|
|
|
|
},
|
2016-08-07 13:51:06 -04:00
|
|
|
YAxis: chart.YAxis{
|
|
|
|
Name: "Elapsed Millis",
|
|
|
|
NameStyle: chart.StyleShow(),
|
|
|
|
Style: chart.StyleShow(),
|
2016-10-21 15:50:40 -04:00
|
|
|
TickStyle: chart.Style{
|
|
|
|
TextRotationDegrees: 45.0,
|
|
|
|
},
|
2017-02-06 17:25:09 -05:00
|
|
|
ValueFormatter: func(v interface{}) string {
|
|
|
|
return fmt.Sprintf("%d ms", int(v.(float64)))
|
|
|
|
},
|
2016-08-07 13:51:06 -04:00
|
|
|
},
|
|
|
|
XAxis: chart.XAxis{
|
2016-10-21 15:44:37 -04:00
|
|
|
Style: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
},
|
2016-08-07 13:51:06 -04:00
|
|
|
ValueFormatter: chart.TimeHourValueFormatter,
|
2016-08-11 23:38:53 -04:00
|
|
|
GridMajorStyle: chart.Style{
|
|
|
|
Show: true,
|
|
|
|
StrokeColor: chart.ColorAlternateGray,
|
|
|
|
StrokeWidth: 1.0,
|
|
|
|
},
|
|
|
|
GridLines: releases(),
|
2016-08-07 13:51:06 -04:00
|
|
|
},
|
|
|
|
Series: []chart.Series{
|
|
|
|
mainSeries,
|
|
|
|
linreg,
|
|
|
|
chart.LastValueAnnotation(linreg),
|
|
|
|
sma,
|
|
|
|
chart.LastValueAnnotation(sma),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-08-27 16:45:38 -04:00
|
|
|
graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}
|
2016-08-07 13:51:06 -04:00
|
|
|
|
2017-05-12 20:06:45 -04:00
|
|
|
res.Header().Set("Content-Type", chart.ContentTypePNG)
|
|
|
|
graph.Render(chart.PNG, res)
|
2016-08-07 13:51:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/", drawChart)
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|