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

1
go.mod
View file

@ -12,6 +12,7 @@ require (
) )
require ( require (
github.com/go-echarts/go-echarts/v2 v2.4.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 // indirect github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 // indirect

2
go.sum
View file

@ -2,6 +2,8 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-echarts/go-echarts/v2 v2.4.0 h1:efD46dmAvaZEWrBHAGjE8cfDK48vvFTHz5N9VqW5rYc=
github.com/go-echarts/go-echarts/v2 v2.4.0/go.mod h1:56YlvzhW/a+du15f3S2qUGNDfKnFOeJSThBIrVFHDtI=
github.com/golang-migrate/migrate/v4 v4.17.1 h1:4zQ6iqL6t6AiItphxJctQb3cFqWiSpMnX7wLTPnnYO4= github.com/golang-migrate/migrate/v4 v4.17.1 h1:4zQ6iqL6t6AiItphxJctQb3cFqWiSpMnX7wLTPnnYO4=
github.com/golang-migrate/migrate/v4 v4.17.1/go.mod h1:m8hinFyWBn0SA4QKHuKh175Pm9wjmxj3S2Mia7dbXzM= github.com/golang-migrate/migrate/v4 v4.17.1/go.mod h1:m8hinFyWBn0SA4QKHuKh175Pm9wjmxj3S2Mia7dbXzM=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=

View file

@ -2,15 +2,21 @@ package server
import ( import (
"bytes" "bytes"
"html/template"
"log/slog" "log/slog"
"net/http" "net/http"
"git.lastassault.de/speatzle/morffix/constants" "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" "github.com/jackc/pgx/v5"
) )
type StatsDisplay struct { type StatsDisplay struct {
CodecCounts []CodecCount CodecCounts []CodecCount
Element template.HTML
Script template.HTML
} }
type CodecCount struct { type CodecCount struct {
@ -35,6 +41,36 @@ func handleStats(w http.ResponseWriter, r *http.Request) {
} }
data.CodecCounts = codecCounts 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{} buf := bytes.Buffer{}
err = templates.ExecuteTemplate(&buf, constants.STATS_TEMPLATE_NAME, data) err = templates.ExecuteTemplate(&buf, constants.STATS_TEMPLATE_NAME, data)
if err != nil { if err != nil {

45
static/js/echarts.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -3,7 +3,8 @@
<html> <html>
<head> <head>
<link rel="stylesheet" href="/static/style/style.css"> <link rel="stylesheet" href="/static/style/style.css">
<script src="/static/js/echarts.min.js"></script>
</head> </head>
<body> <body>
{{template "navbar"}} {{template "navbar"}}
{{end}} {{end}}

View file

@ -19,4 +19,5 @@
{{end}} {{end}}
</table> </table>
</div> </div>
{{.Element}} {{.Script}}
{{template "tail"}} {{template "tail"}}