diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index 7492945b..c594ea85 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -268,13 +268,7 @@ func main() { if *confExpvar { mux.Handle("/debug/vars", expvar.Handler()) expvar.Publish("stats", expvar.Func(func() any { - var stats struct{ Folders, Albums, Tracks, Artists, InternetRadioStations, Podcasts uint } - dbc.Model(db.Track{}).Count(&stats.Tracks) - dbc.Model(db.Album{}).Count(&stats.Folders) - dbc.Model(db.Album{}).Joins("JOIN album_artists ON album_artists.album_id=albums.id").Group("albums.id").Count(&stats.Albums) - dbc.Model(db.Artist{}).Count(&stats.Artists) - dbc.Model(db.InternetRadioStation{}).Count(&stats.InternetRadioStations) - dbc.Model(db.Podcast{}).Count(&stats.Podcasts) + stats, _ := dbc.Stats() return stats })) } diff --git a/db/db.go b/db/db.go index 2a93b569..ab261aa5 100644 --- a/db/db.go +++ b/db/db.go @@ -80,6 +80,22 @@ func (db *DB) InsertBulkLeftMany(table string, head []string, left int, col []in return db.Exec(q, values...).Error } +type Stats struct { + Folders, Albums, Artists, AlbumArtists, Tracks, InternetRadioStations, Podcasts uint +} + +func (db *DB) Stats() (Stats, error) { + var stats Stats + db.Model(Album{}).Count(&stats.Folders) + db.Model(AlbumArtist{}).Group("album_id").Count(&stats.Albums) + db.Model(TrackArtist{}).Group("artist_id").Count(&stats.Artists) + db.Model(AlbumArtist{}).Group("artist_id").Count(&stats.AlbumArtists) + db.Model(Track{}).Count(&stats.Tracks) + db.Model(InternetRadioStation{}).Count(&stats.InternetRadioStations) + db.Model(Podcast{}).Count(&stats.Podcasts) + return stats, nil +} + func (db *DB) GetUserByID(id int) *User { var user User err := db. diff --git a/server/ctrladmin/adminui/pages/home.tmpl b/server/ctrladmin/adminui/pages/home.tmpl index baa0aaf7..d330d504 100644 --- a/server/ctrladmin/adminui/pages/home.tmpl +++ b/server/ctrladmin/adminui/pages/home.tmpl @@ -7,12 +7,20 @@ "Desc" "total items found in all watched folders" ) }}
-
artists
-
{{ .ArtistCount }}
+
folders
+
{{ .Stats.Folders }}
albums
-
{{ .AlbumCount }}
+
{{ .Stats.Albums }}
+
artists
+
{{ .Stats.Artists }}
+
album artists
+
{{ .Stats.AlbumArtists }}
tracks
-
{{ .TrackCount }}
+
{{ .Stats.Tracks }}
+
internet radio stations
+
{{ .Stats.InternetRadioStations }}
+
podcasts
+
{{ .Stats.Podcasts }}
{{ end }} diff --git a/server/ctrladmin/ctrl.go b/server/ctrladmin/ctrl.go index 5b9fa78a..063869f6 100644 --- a/server/ctrladmin/ctrl.go +++ b/server/ctrladmin/ctrl.go @@ -274,10 +274,9 @@ type templateData struct { Flashes []interface{} User *db.User Version string + // home - AlbumCount int - ArtistCount int - TrackCount int + Stats db.Stats RequestRoot string RecentFolders []*db.Album AllUsers []*db.User diff --git a/server/ctrladmin/handlers.go b/server/ctrladmin/handlers.go index c5f80037..cb081983 100644 --- a/server/ctrladmin/handlers.go +++ b/server/ctrladmin/handlers.go @@ -36,9 +36,7 @@ func (c *Controller) ServeHome(r *http.Request) *Response { data := &templateData{} // stats box - c.dbc.Model(&db.Artist{}).Count(&data.ArtistCount) - c.dbc.Model(&db.Album{}).Count(&data.AlbumCount) - c.dbc.Table("tracks").Count(&data.TrackCount) + data.Stats, _ = c.dbc.Stats() // lastfm box data.RequestRoot = handlerutil.BaseURL(r) data.CurrentLastFMAPIKey, _ = c.dbc.GetSetting(db.LastFMAPIKey)