-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
288 lines (246 loc) · 9.54 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
//initally taken from gist linked by https://www.sanarias.com/blog/1214PlayingwithimagesinHTTPresponseingolang
import (
"bytes"
//"encoding/base64"
"flag"
//"html/template"
"image"
"image/color"
//"image/draw"
"image/png"
"log"
"net/http"
"strconv"
"github.com/aoanla/chart"
"github.com/aoanla/chart/imgg"
"sort"
)
func web_server(port *string) {
//
//http.Handle("/img",http.StripPrefix("/img/",http.))
http.HandleFunc("/img/", imgHandler)
//http.HandleFunc("/red/", redHandler)
http.HandleFunc("/", htmlHandler)
var str string
//check for missing :
if (*port)[0] != ':' {
str = ":" + *port
} else {
str = *port
}
log.Printf("Listening on %s\n", str)
err := http.ListenAndServe(str, nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
var root = flag.String("root", ".", "file system path")
//Pager is a map[string]func(bytebuffer)bool
// these map paths to the function to generate their in-memory representation
var Pager map[string]func(*bytes.Buffer)bool
var Plotter map[string]func(*imgg.ImageGraphics)bool
//thes map paths to the doublebuffer which holds their in-memory representation
var Pages map[string]*Page_buffer
var Plots map[string]*Page_buffer
func writeHTMLwithPaths(paths []string) {
for _, p := range paths {
var buffer bytes.Buffer
//write to buffer
if !Pager[p](&buffer) {
log.Printf("error writing page with path %s", p)
}
Pages[p].Write(buffer.Bytes())
Pages[p].Switch()
}
}
func drawPlotswithPaths(paths []string) {
for _, p := range paths {
//igr := imgg.New(1024,768, color.RGBA{0xff,0xff,0xff,0xff}, nil, nil)
//reusing shared buffer, so need to blank the image with a white rect first
ImgBuff.Rect(0,0,1024,768, chart.Style{LineColor: color.RGBA{0xff,0xff,0xff,0xff}, FillColor: color.RGBA{0xff,0xff,0xff,0xff}})
//success := Plotter[p](&igr)
if !Plotter[p](ImgBuff) {
log.Printf("failure encoding image path %s", p)
}
var img image.Image = ImgBuff.Image
// what we should actually do is all of this stuff, including the write image, ahead ot time, into a data structure, and then just serve the contents in this handler
var buffer bytes.Buffer
//Plots[p].Writable() // new(bytes.Buffer) //this buffer is actually going to be part of the double-buffered type
if err := png.Encode(&buffer, img); err != nil {
log.Println("unable to encode image.")
}
Plots[p].Write(buffer.Bytes())
Plots[p].Switch()
}
}
func getsortedJams(map_ map[string]*JamStat) ([]string, int) {
i := 0
l := len(map_)
keys := make([]string, l, l)
for k := range map_ {
keys[i] = k
i++
}
sort.Strings(keys)
return keys, l
}
func drawPtsPerTeam( igr *imgg.ImageGraphics) bool {
//igr := imgg.New(1024,768, color.RGBA{0xff,0xff,0xff,0xff}, nil, nil)
//c is a chart.Chart type
c := chart.ScatterChart{Title: "Total Points Per Team"}
c.XRange.TicSetting.Mirror = 1
c.YRange.TicSetting.Mirror = 1
c.XRange.TicSetting.Minor = 0
c.XRange.TicSetting.Grid = chart.GridLines
//larger fonts for Key etc. This does break some of the borders of plot :(
c.Options = chart.DefaultOptions
c.Options[chart.KeyElement] = chart.Style{LineColor: color.NRGBA{0x20, 0x20, 0x20, 0xff},LineWidth: 2, LineStyle: chart.SolidLine,FillColor: color.NRGBA{0xf0, 0xf0, 0xf0, 0xc0},Font: chart.Font{Size:chart.HugeFontSize}}
c.Options[chart.MajorTicElement] = chart.Style{LineColor: color.NRGBA{0x20, 0x20, 0x20, 0xff},LineWidth: 2, LineStyle: chart.SolidLine,FillColor: color.NRGBA{0xf0, 0xf0, 0xf0, 0xc0},Font: chart.Font{Size:chart.HugeFontSize}}
c.Options[chart.TitleElement] = chart.Style{LineColor: color.NRGBA{0x20, 0x20, 0x20, 0xff},LineWidth: 2, LineStyle: chart.SolidLine,FillColor: color.NRGBA{0xf0, 0xf0, 0xf0, 0xc0},Font: chart.Font{Size:chart.HugeFontSize}}
//
var x,y,y2 []float64
var cc []string
c.YRange.MinMode.Fixed = true
c.YRange.MinMode.Value = 0.0
c.YRange.TicSetting.Format = chart.FmtIntFloat
//something like
jams, l := getsortedJams(Stats.Jams)
if l < 1 { //we have no actual data yet, still initing probably
x = []float64{0.0}
y = []float64{0.0}
y2 = []float64{0.0}
cc = []string{"",""}
c.XRange.Fixed(0.0,1.0,1.0)
} else {
c.XRange.Fixed(0.0,float64(l),1.0)
x = make([]float64,l,l)
y = make([]float64,l,l)
y2 = make([]float64,l,l)
cc = make([]string,l+1,l+1)
//cc[0] = ""
cc[l] = ""
for i,j := range jams {
x[i] = float64(i)
y[i] = float64(Stats.Jams[j].TotalScores[0])
y2[i] = float64(Stats.Jams[j].TotalScores[1])
cc[i] = j
}
}
c.XRange.Category = cc
c.AddDataPair(Teams[0].Name, x, y, chart.PlotStyleLinesPoints, chart.Style{Symbol: '#', SymbolColor: color.NRGBA{0x00, 0xee, 0x00, 0xff}})
c.AddDataPair(Teams[1].Name, x, y2, chart.PlotStyleLinesPoints, chart.Style{Symbol: '#', SymbolColor: color.NRGBA{0x00, 0xee, 0xff, 0xff}})
c.Plot(igr)
return true
}
func drawDeltaPtsPerTeam( igr *imgg.ImageGraphics) bool {
//c is a chart.Chart type
c := chart.BarChart{Title: "Total Points Per Jam", ShowVal: 5}
c.XRange.TicSetting.Mirror = 1
c.YRange.TicSetting.Mirror = 1
c.XRange.TicSetting.Minor = 0
c.XRange.TicSetting.Grid = chart.GridLines
//larger fonts for Key etc. This does break some of the borders of plot :(
c.Options = chart.DefaultOptions
c.Options[chart.KeyElement] = chart.Style{LineColor: color.NRGBA{0x20, 0x20, 0x20, 0xff},LineWidth: 2, LineStyle: chart.SolidLine,FillColor: color.NRGBA{0xf0, 0xf0, 0xf0, 0xc0},Font: chart.Font{Size:chart.HugeFontSize}}
c.Options[chart.MajorTicElement] = chart.Style{LineColor: color.NRGBA{0x20, 0x20, 0x20, 0xff},LineWidth: 2, LineStyle: chart.SolidLine,FillColor: color.NRGBA{0xf0, 0xf0, 0xf0, 0xc0},Font: chart.Font{Size:chart.HugeFontSize}}
c.Options[chart.TitleElement] = chart.Style{LineColor: color.NRGBA{0x20, 0x20, 0x20, 0xff},LineWidth: 2, LineStyle: chart.SolidLine,FillColor: color.NRGBA{0xf0, 0xf0, 0xf0, 0xc0},Font: chart.Font{Size:chart.HugeFontSize}}
//
var x,y,y2 []float64
var cc []string
c.YRange.MinMode.Fixed = true
c.YRange.MinMode.Value = 0
c.YRange.TicSetting.Format = chart.FmtIntFloat
//something like
jams, l := getsortedJams(Stats.Jams)
if l < 1 { //we have no actual data yet, still initing probably
x = []float64{0}
y = []float64{0}
y2 = []float64{0}
cc = []string{""}
c.XRange.Fixed(-0.5,1.5,1.0)
} else {
c.XRange.Fixed(-0.5,float64(l)+0.5,1.0) //fix autoscaling bug in graph lib
x = make([]float64,l,l)
y = make([]float64,l,l)
y2 = make([]float64,l,l)
//cc = make([]string,l+1,l+1)
//cc[0] = ""
//cc[l] = ""
for i,j := range jams {
x[i] = float64(i)
y[i] = float64(Stats.Jams[j].ScoreDeltas[0])
y2[i] = float64(Stats.Jams[j].ScoreDeltas[1])
//cc[i] = j
}
cc = jams
}
c.XRange.Category = cc
c.AddDataPair(Teams[0].Name, x, y, chart.Style{Symbol: 'o', SymbolColor: color.NRGBA{0x00, 0xee, 0x00, 0xff}, LineColor: color.NRGBA{0xcc, 0x00, 0x00, 0xff},
FillColor: color.NRGBA{0xff, 0x80, 0x80, 0xff}, LineStyle: chart.SolidLine})
c.AddDataPair(Teams[1].Name, x, y2, chart.Style{Symbol: '#', SymbolColor: color.NRGBA{0x00, 0xee, 0xff, 0xff}, LineColor: color.NRGBA{0xcc, 0x00, 0x00, 0xff},
FillColor: color.NRGBA{0x80, 0x80, 0xff, 0xff}, LineStyle: chart.SolidLine})
c.Plot(igr)
return true
}
func drawLeadsPerTeam( igr *imgg.ImageGraphics) bool {
//c is a chart.Chart type
c := chart.PieChart{Title: "Total Leads Per Team"}
//larger fonts for Key etc. This does break some of the borders of plot :(
c.Options = chart.DefaultOptions
c.Options[chart.KeyElement] = chart.Style{LineColor: color.NRGBA{0x20, 0x20, 0x20, 0xff},LineWidth: 2, LineStyle: chart.SolidLine,FillColor: color.NRGBA{0xf0, 0xf0, 0xf0, 0xc0},Font: chart.Font{Size:chart.HugeFontSize}}
c.Options[chart.MajorTicElement] = chart.Style{LineColor: color.NRGBA{0x20, 0x20, 0x20, 0xff},LineWidth: 2, LineStyle: chart.SolidLine,FillColor: color.NRGBA{0xf0, 0xf0, 0xf0, 0xc0},Font: chart.Font{Size:chart.HugeFontSize}}
c.Options[chart.TitleElement] = chart.Style{LineColor: color.NRGBA{0x20, 0x20, 0x20, 0xff},LineWidth: 2, LineStyle: chart.SolidLine,FillColor: color.NRGBA{0xf0, 0xf0, 0xf0, 0xc0},Font: chart.Font{Size:chart.HugeFontSize}}
//
x := []int{0,0,0}
cc := []string{Teams[0].Name, Teams[1].Name, "None"}
//something like
jams, l := getsortedJams(Stats.Jams)
if l > 0 {
for _,j := range jams {
switch lead := Stats.Jams[j].Lead; lead {
case -1:
x[2] += 1
case 0,1:
x[lead] += 1
}
}
}
c.AddIntDataPair("Fraction of Leads", cc, x)
c.Plot(igr)
return true
}
func imgHandler(w http.ResponseWriter, r *http.Request) {
// get path p
p := r.URL.Path[5:] //the part of the path after /img/
if _, prsnt := Plots[p]; !prsnt {
//respond with 404
w.WriteHeader(404)
return
}
bufferb := Plots[p].Display()
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Length", strconv.Itoa(len(bufferb)))
if _, err := w.Write(bufferb); err != nil {
log.Println("unable to write image.")
}
}
func htmlHandler(w http.ResponseWriter, r *http.Request) {
var p string
if r.URL.Path == "/" {
p = "index.html"
} else {
p = r.URL.Path[1:] //remove leading /
}
if _, prsnt := Pages[p]; !prsnt {
w.WriteHeader(404)
return
}
log.Printf("Serving %s",p)
bufferb := Pages[p].Display()
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Content-Length", strconv.Itoa(len(bufferb)))
if _, err := w.Write(bufferb); err != nil {
log.Println("unable to serve html")
}
}