feat: add option for /debug/vars endpoint to expose database and media stats

closes #372
closes #150
This commit is contained in:
sentriz
2023-09-13 01:11:13 +01:00
parent 7a29c881e8
commit 2a7a455ce2
2 changed files with 17 additions and 0 deletions

View File

@@ -78,6 +78,7 @@ password can then be changed from the web interface
| `GONIC_EXCLUDE_PATTERN` | `-exclude-pattern` | **optional** files matching this regex pattern will not be imported |
| `GONIC_MULTI_VALUE_GENRE` | `-multi-value-genre` | **optional** setting for multi-valued genre tags when scanning ([see more](#multi-valued-tags)) |
| `GONIC_MULTI_VALUE_ALBUM_ARTIST` | `-multi-value-album-artist` | **optional** setting for multi-valued album artist tags when scanning ([see more](#multi-valued-tags)) |
| `GONIC_EXPVAR` | `-expvar` | **optional** enable the /debug/vars endpoint (exposes useful debugging attributes as well as database stats) |
## multi valued tags

View File

@@ -5,6 +5,7 @@ package main
import (
"errors"
"expvar"
"flag"
"fmt"
"log"
@@ -78,6 +79,8 @@ func main() {
set.Var(&confMultiValueGenre, "multi-value-genre", "setting for mutli-valued genre scanning (optional)")
set.Var(&confMultiValueAlbumArtist, "multi-value-album-artist", "setting for mutli-valued album artist scanning (optional)")
confExpvar := set.Bool("expvar", false, "enable the /debug/vars endpoint (optional)")
deprecatedConfGenreSplit := set.String("genre-split", "", "(deprecated, see multi-value settings)")
if _, err := regexp.Compile(*confExcludePatterns); err != nil {
@@ -233,6 +236,19 @@ func main() {
ctrladmin.AddRoutes(ctrlAdmin, mux.PathPrefix("/admin").Subrouter())
ctrlsubsonic.AddRoutes(ctrlSubsonic, mux.PathPrefix("/rest").Subrouter())
if *confExpvar {
mux.Handle("/debug/vars", expvar.Handler())
expvar.Publish("stats", expvar.Func(func() any {
var stats struct{ Albums, Tracks, Artists, InternetRadioStations, Podcasts uint }
dbc.Model(db.Album{}).Count(&stats.Albums)
dbc.Model(db.Track{}).Count(&stats.Tracks)
dbc.Model(db.Artist{}).Count(&stats.Artists)
dbc.Model(db.InternetRadioStation{}).Count(&stats.InternetRadioStations)
dbc.Model(db.Podcast{}).Count(&stats.Podcasts)
return stats
}))
}
var g run.Group
g.Add(func() error {
log.Print("starting job 'http'\n")