Skip to content

Commit

Permalink
feat: add more and unify stats
Browse files Browse the repository at this point in the history
Release-As: 0.16.1
  • Loading branch information
sentriz committed Oct 31, 2023
1 parent c1a34dc commit 2fdc1f4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
8 changes: 1 addition & 7 deletions cmd/gonic/gonic.go
Expand Up @@ -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
}))
}
Expand Down
16 changes: 16 additions & 0 deletions db/db.go
Expand Up @@ -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.
Expand Down
16 changes: 12 additions & 4 deletions server/ctrladmin/adminui/pages/home.tmpl
Expand Up @@ -7,12 +7,20 @@
"Desc" "total items found in all watched folders"
) }}
<div class="grid grid-cols-[auto_min-content] gap-2 gap-x-5 text-right">
<div class="text-gray-500">artists</div>
<div class="font-bold">{{ .ArtistCount }}</div>
<div class="text-gray-500">folders</div>
<div class="font-bold">{{ .Stats.Folders }}</div>
<div class="text-gray-500">albums</div>
<div class="font-bold">{{ .AlbumCount }}</div>
<div class="font-bold">{{ .Stats.Albums }}</div>
<div class="text-gray-500">artists</div>
<div class="font-bold">{{ .Stats.Artists }}</div>
<div class="text-gray-500">album artists</div>
<div class="font-bold">{{ .Stats.AlbumArtists }}</div>
<div class="text-gray-500">tracks</div>
<div class="font-bold">{{ .TrackCount }}</div>
<div class="font-bold">{{ .Stats.Tracks }}</div>
<div class="text-gray-500">internet radio stations</div>
<div class="font-bold">{{ .Stats.InternetRadioStations }}</div>
<div class="text-gray-500">podcasts</div>
<div class="font-bold">{{ .Stats.Podcasts }}</div>
</div>
{{ end }}

Expand Down
5 changes: 2 additions & 3 deletions server/ctrladmin/ctrl.go
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions server/ctrladmin/handlers.go
Expand Up @@ -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)
Expand Down

0 comments on commit 2fdc1f4

Please sign in to comment.