Add Stats Chart
All checks were successful
/ release (push) Successful in 39s

This commit is contained in:
Samuel Lorch 2024-07-13 03:33:43 +02:00
parent 0f5d842a64
commit 38219f7b07
6 changed files with 87 additions and 1 deletions

View file

@ -2,15 +2,21 @@ package server
import (
"bytes"
"html/template"
"log/slog"
"net/http"
"git.lastassault.de/speatzle/morffix/constants"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/go-echarts/go-echarts/v2/types"
"github.com/jackc/pgx/v5"
)
type StatsDisplay struct {
CodecCounts []CodecCount
Element template.HTML
Script template.HTML
}
type CodecCount struct {
@ -35,6 +41,36 @@ func handleStats(w http.ResponseWriter, r *http.Request) {
}
data.CodecCounts = codecCounts
chartData := []opts.PieData{}
for _, c := range codecCounts {
chartData = append(chartData, opts.PieData{
Name: c.Codec,
Value: c.Count,
})
}
pie := charts.NewPie()
pie.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
Theme: types.ThemePurplePassion,
}),
charts.WithTitleOpts(opts.Title{
Title: "Codecs",
}))
pie.AddSeries("Codecs", chartData).SetSeriesOptions(charts.WithLabelOpts(
opts.Label{
Show: opts.Bool(true),
Formatter: "{b}: {c}",
Color: "white",
}),
)
snippet := pie.RenderSnippet()
data.Element = template.HTML(snippet.Element)
data.Script = template.HTML(snippet.Script)
buf := bytes.Buffer{}
err = templates.ExecuteTemplate(&buf, constants.STATS_TEMPLATE_NAME, data)
if err != nil {