Refactors daily avg. chart and adds hourly avg. chart
This commit is contained in:
parent
427a47d054
commit
6cc20e337d
40
charts.go
40
charts.go
@ -106,29 +106,22 @@ func makeHistoryChart(showLegend bool, points []ServerPoint) chart.Chart {
|
||||
return c
|
||||
}
|
||||
|
||||
// NOTE: The chart won't be renderable unless we've got at least two days of history
|
||||
func makeDayAverageChart(points []ServerPoint) chart.BarChart {
|
||||
days := make(map[time.Weekday][]int)
|
||||
for _, p := range points {
|
||||
day := p.Time.Weekday()
|
||||
days[day] = append(days[day], p.Players)
|
||||
}
|
||||
|
||||
avgDays := make(map[time.Weekday]float64)
|
||||
for day, vals := range days {
|
||||
// NOTE: The chart won't be renderable unless we've got at least two days/hours of history
|
||||
func makeAverageChart(values map[int][]int, fnFormat func(int, float64) string) chart.BarChart {
|
||||
avg := make(map[int]float64)
|
||||
for i, vl := range values {
|
||||
sum := 0
|
||||
for _, v := range vals {
|
||||
for _, v := range vl {
|
||||
sum += v
|
||||
}
|
||||
avg := sum / len(vals)
|
||||
avgDays[day] = float64(avg)
|
||||
avg[i] = float64(sum / len(vl))
|
||||
}
|
||||
|
||||
var bars []chart.Value
|
||||
for _, d := range weekDaysOrder {
|
||||
for i := 0; i < len(avg); i++ {
|
||||
bars = append(bars, chart.Value{
|
||||
Label: fmt.Sprintf("%s (%.0f)", d, avgDays[d]),
|
||||
Value: avgDays[d],
|
||||
Label: fnFormat(i, avg[i]),
|
||||
Value: avg[i],
|
||||
Style: chart.Style{
|
||||
StrokeColor: chart.ColorBlue,
|
||||
FillColor: chart.ColorBlue,
|
||||
@ -136,11 +129,20 @@ func makeDayAverageChart(points []ServerPoint) chart.BarChart {
|
||||
})
|
||||
}
|
||||
|
||||
barW, barS := 50, 100
|
||||
if len(avg) > 7 {
|
||||
barW, barS = 20, 20
|
||||
}
|
||||
s := chart.Style{
|
||||
Show: true,
|
||||
StrokeWidth: 1,
|
||||
}
|
||||
return chart.BarChart{
|
||||
BarWidth: 50,
|
||||
XAxis: chart.StyleShow(),
|
||||
BarWidth: barW,
|
||||
BarSpacing: barS,
|
||||
XAxis: s,
|
||||
YAxis: chart.YAxis{
|
||||
Style: chart.StyleShow(),
|
||||
Style: s,
|
||||
ValueFormatter: func(v interface{}) string {
|
||||
return fmt.Sprintf("%.0f", v)
|
||||
},
|
||||
|
||||
39
handlers.go
39
handlers.go
@ -3,6 +3,7 @@ package ss13_se
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (a *App) pageIndex(w http.ResponseWriter, r *http.Request, vars handlerVars) error {
|
||||
@ -91,7 +92,7 @@ func (a *App) pageWeeklyChart(w http.ResponseWriter, r *http.Request, vars handl
|
||||
return a.renderChart(w, c)
|
||||
}
|
||||
|
||||
func (a *App) pageAverageChart(w http.ResponseWriter, r *http.Request, vars handlerVars) error {
|
||||
func (a *App) pageAverageDailyChart(w http.ResponseWriter, r *http.Request, vars handlerVars) error {
|
||||
id := vars["id"]
|
||||
points, err := a.store.GetSingleServerHistory(id, 30)
|
||||
if err != nil {
|
||||
@ -104,6 +105,40 @@ func (a *App) pageAverageChart(w http.ResponseWriter, r *http.Request, vars hand
|
||||
}
|
||||
}
|
||||
|
||||
c := makeDayAverageChart(points)
|
||||
days := make(map[int][]int)
|
||||
for _, p := range points {
|
||||
d := int(p.Time.Weekday())
|
||||
days[d] = append(days[d], p.Players)
|
||||
}
|
||||
formatter := func(i int, f float64) string {
|
||||
d := time.Weekday(i)
|
||||
return fmt.Sprintf("%s", d)
|
||||
}
|
||||
c := makeAverageChart(days, formatter)
|
||||
return a.renderChart(w, c)
|
||||
}
|
||||
|
||||
func (a *App) pageAverageHourlyChart(w http.ResponseWriter, r *http.Request, vars handlerVars) error {
|
||||
id := vars["id"]
|
||||
points, err := a.store.GetSingleServerHistory(id, 30)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(points) < 1 {
|
||||
return HttpError{
|
||||
Status: 404,
|
||||
Err: fmt.Errorf("server not found"),
|
||||
}
|
||||
}
|
||||
|
||||
hours := make(map[int][]int)
|
||||
for _, p := range points {
|
||||
h := p.Time.Hour()
|
||||
hours[h] = append(hours[h], p.Players)
|
||||
}
|
||||
formatter := func(i int, f float64) string {
|
||||
return fmt.Sprintf("%02d:00", i)
|
||||
}
|
||||
c := makeAverageChart(hours, formatter)
|
||||
return a.renderChart(w, c)
|
||||
}
|
||||
|
||||
3
main.go
3
main.go
@ -64,7 +64,8 @@ func New(c Conf) (*App, error) {
|
||||
r.Handle("/server/{id}", handler(a.pageServer))
|
||||
r.Handle("/server/{id}/daily", handler(a.pageDailyChart))
|
||||
r.Handle("/server/{id}/weekly", handler(a.pageWeeklyChart))
|
||||
r.Handle("/server/{id}/average", handler(a.pageAverageChart))
|
||||
r.Handle("/server/{id}/averagedaily", handler(a.pageAverageDailyChart))
|
||||
r.Handle("/server/{id}/averagehourly", handler(a.pageAverageHourlyChart))
|
||||
a.web.Handler = r
|
||||
|
||||
return a, nil
|
||||
|
||||
@ -185,7 +185,9 @@ var tmplList = map[string]string{
|
||||
<h2>Weekly History</h2>
|
||||
<img src="/server/{{.Server.ID}}/weekly" alt="Unable to show a pretty graph">
|
||||
<h2>Average per day</h2>
|
||||
<img src="/server/{{.Server.ID}}/average" alt="Unable to show a pretty graph">
|
||||
<img src="/server/{{.Server.ID}}/averagedaily" alt="Unable to show a pretty graph">
|
||||
<h2>Average per hour</h2>
|
||||
<img src="/server/{{.Server.ID}}/averagehourly" alt="Unable to show a pretty graph">
|
||||
{{end}}
|
||||
`,
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user