86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
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 {
|
|
Codec string
|
|
Count int
|
|
}
|
|
|
|
func handleStats(w http.ResponseWriter, r *http.Request) {
|
|
data := StatsDisplay{}
|
|
|
|
rows, err := db.Query(r.Context(), `SELECT COALESCE(jsonb_path_query_first(ffprobe_data, '$.streams[*] ? (@.codec_type == "video") ? (@.disposition.attached_pic == 0).codec_name')::text, 'Unknown') AS codec, COUNT(*) AS count FROM files WHERE ffprobe_data IS NOT NULL GROUP BY jsonb_path_query_first(ffprobe_data, '$.streams[*] ? (@.codec_type == "video") ? (@.disposition.attached_pic == 0).codec_name');`)
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Query Stats", "err", err)
|
|
http.Error(w, "Error Query Stats: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
codecCounts, err := pgx.CollectRows[CodecCount](rows, pgx.RowToStructByName[CodecCount])
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Collect Rows", "err", err)
|
|
http.Error(w, "Error Query Libraries: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
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 {
|
|
slog.ErrorContext(r.Context(), "Executing Stats Template", "err", err)
|
|
http.Error(w, "Error Executing Template: "+err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
_, err = w.Write(buf.Bytes())
|
|
if err != nil {
|
|
slog.ErrorContext(r.Context(), "Writing http Response", "err", err)
|
|
}
|
|
}
|