Debian Codegraph @

examples/request_timings/main.go

raw symbols SCIP
1 package main
2
3 //go:generate go run main.go
4
5 import (
6 "fmt"
7 "net/http"
8 "os"
9 "strconv"
10 "time"
11
12 "github.com/wcharczuk/go-chart/v2"
13 )
14
15 func main() {
16 log := chart.NewLogger()
17 drawChart(log)
18 }
19
20 func parseInt(str string) int {
21 v, _ := strconv.Atoi(str)
22 return v
23 }
24
25 func parseFloat64(str string) float64 {
26 v, _ := strconv.ParseFloat(str, 64)
27 return v
28 }
29
30 func readData() ([]time.Time, []float64) {
31 var xvalues []time.Time
32 var yvalues []float64
33 err := chart.ReadLines("requests.csv", func(line string) error {
34 parts := chart.SplitCSV(line)
35 year := parseInt(parts[0])
36 month := parseInt(parts[1])
37 day := parseInt(parts[2])
38 hour := parseInt(parts[3])
39 elapsedMillis := parseFloat64(parts[4])
40 xvalues = append(xvalues, time.Date(year, time.Month(month), day, hour, 0, 0, 0, time.UTC))
41 yvalues = append(yvalues, elapsedMillis)
42 return nil
43 })
44 if err != nil {
45 fmt.Println(err.Error())
46 }
47 return xvalues, yvalues
48 }
49
50 func releases() []chart.GridLine {
51 return []chart.GridLine{
52 {Value: chart.TimeToFloat64(time.Date(2016, 8, 1, 9, 30, 0, 0, time.UTC))},
53 {Value: chart.TimeToFloat64(time.Date(2016, 8, 2, 9, 30, 0, 0, time.UTC))},
54 {Value: chart.TimeToFloat64(time.Date(2016, 8, 2, 15, 30, 0, 0, time.UTC))},
55 {Value: chart.TimeToFloat64(time.Date(2016, 8, 4, 9, 30, 0, 0, time.UTC))},
56 {Value: chart.TimeToFloat64(time.Date(2016, 8, 5, 9, 30, 0, 0, time.UTC))},
57 {Value: chart.TimeToFloat64(time.Date(2016, 8, 6, 9, 30, 0, 0, time.UTC))},
58 }
59 }
60
61 func drawChart(log chart.Logger) http.HandlerFunc {
62 return func(res http.ResponseWriter, req *http.Request) {
63 xvalues, yvalues := readData()
64 mainSeries := chart.TimeSeries{
65 Name: "Prod Request Timings",
66 Style: chart.Style{
67 StrokeColor: chart.ColorBlue,
68 FillColor: chart.ColorBlue.WithAlpha(100),
69 },
70 XValues: xvalues,
71 YValues: yvalues,
72 }
73
74 linreg := &chart.LinearRegressionSeries{
75 Name: "Linear Regression",
76 Style: chart.Style{
77 StrokeColor: chart.ColorAlternateBlue,
78 StrokeDashArray: []float64{5.0, 5.0},
79 },
80 InnerSeries: mainSeries,
81 }
82
83 sma := &chart.SMASeries{
84 Name: "SMA",
85 Style: chart.Style{
86 StrokeColor: chart.ColorRed,
87 StrokeDashArray: []float64{5.0, 5.0},
88 },
89 InnerSeries: mainSeries,
90 }
91
92 graph := chart.Chart{
93 Log: log,
94 Width: 1280,
95 Height: 720,
96 Background: chart.Style{
97 Padding: chart.Box{
98 Top: 50,
99 },
100 },
101 YAxis: chart.YAxis{
102 Name: "Elapsed Millis",
103 TickStyle: chart.Style{
104 TextRotationDegrees: 45.0,
105 },
106 ValueFormatter: func(v interface{}) string {
107 return fmt.Sprintf("%d ms", int(v.(float64)))
108 },
109 },
110 XAxis: chart.XAxis{
111 ValueFormatter: chart.TimeHourValueFormatter,
112 GridMajorStyle: chart.Style{
113 StrokeColor: chart.ColorAlternateGray,
114 StrokeWidth: 1.0,
115 },
116 GridLines: releases(),
117 },
118 Series: []chart.Series{
119 mainSeries,
120 linreg,
121 chart.LastValueAnnotationSeries(linreg),
122 sma,
123 chart.LastValueAnnotationSeries(sma),
124 },
125 }
126
127 graph.Elements = []chart.Renderable{chart.LegendThin(&graph)}
128
129 f, _ := os.Create("output.png")
130 defer f.Close()
131 graph.Render(chart.PNG, f)
132 }
133 }