Debian Codegraph @

examples/rerender/main.go

raw symbols SCIP
1 package main
2
3 import (
4 "log"
5 "net/http"
6 "sync"
7 "time"
8
9 "github.com/wcharczuk/go-chart/v2"
10 )
11
12 var lock sync.Mutex
13 var graph *chart.Chart
14 var ts *chart.TimeSeries
15
16 func addData(t time.Time, e time.Duration) {
17 lock.Lock()
18 ts.XValues = append(ts.XValues, t)
19 ts.YValues = append(ts.YValues, chart.TimeMillis(e))
20 lock.Unlock()
21 }
22
23 func drawChart(res http.ResponseWriter, req *http.Request) {
24 start := time.Now()
25 defer func() {
26 addData(start, time.Since(start))
27 }()
28 if len(ts.XValues) == 0 {
29 http.Error(res, "no data (yet)", http.StatusBadRequest)
30 return
31 }
32 res.Header().Set("Content-Type", "image/png")
33 if err := graph.Render(chart.PNG, res); err != nil {
34 log.Printf("%v", err)
35 }
36 }
37
38 func main() {
39 ts = &chart.TimeSeries{
40 XValues: []time.Time{},
41 YValues: []float64{},
42 }
43 graph = &chart.Chart{
44 Series: []chart.Series{ts},
45 }
46 http.HandleFunc("/", drawChart)
47 log.Fatal(http.ListenAndServe(":8080", nil))
48 }