Add stats page
All checks were successful
/ release (push) Successful in 29s

This commit is contained in:
Samuel Lorch 2024-07-13 01:45:43 +02:00
parent 8ba0e8f2ab
commit 0f5d842a64
6 changed files with 77 additions and 2 deletions

View file

@ -17,6 +17,7 @@ const MESSAGE_TEMPLATE_NAME = "message.tmpl"
const TASKS_TEMPLATE_NAME = "tasks.tmpl"
const TASK_TEMPLATE_NAME = "task.tmpl"
const FFMPEG_COMMANDS_TEMPLATE_NAME = "ffmpeg_commands.tmpl"
const STATS_TEMPLATE_NAME = "stats.tmpl"
const FORM_FILE_KEY = "file"

View file

@ -103,6 +103,7 @@ func Start(_conf config.Config, tmplFS embed.FS, staticFS embed.FS, migrationsFS
mux.HandleFunc("/libraries", handleLibraries)
mux.HandleFunc("/ffmpeg_commands", handleFfmpegCommands)
mux.HandleFunc("/queue_enable", HandleSetQueueEnable)
mux.HandleFunc("/stats", handleStats)
mux.HandleFunc("/", handleIndex)

50
server/stats.go Normal file
View file

@ -0,0 +1,50 @@
package server
import (
"bytes"
"log/slog"
"net/http"
"git.lastassault.de/speatzle/morffix/constants"
"github.com/jackc/pgx/v5"
)
type StatsDisplay struct {
CodecCounts []CodecCount
}
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
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)
}
}

View file

@ -77,4 +77,4 @@
<input type="checkbox" name="health_ok_queue_transcode">
<input type="submit" value="Submit">
</form>
{{template "tail"}}
{{template "tail"}}

View file

@ -4,5 +4,6 @@
<a class="button" href="/libraries">Libraries</a>
<a class="button" href="/tasks">Tasks</a>
<a class="button" href="/ffmpeg_commands">FFmpeg Commands</a>
<a class="button" href="/stats">Stats</a>
</nav>
{{end}}
{{end}}

22
tmpl/stats.tmpl Normal file
View file

@ -0,0 +1,22 @@
{{template "head"}}
<h2>Stats</h2>
<h2>Codec Counts</h2>
<div>
<table>
<tr>
<th>Codec</th>
<th>Count</th>
</tr>
{{range $f := .CodecCounts}}
<tr>
<td>
{{ $f.Codec }}
</td>
<td>
{{ $f.Count }}
</td>
</tr>
{{end}}
</table>
</div>
{{template "tail"}}