Files
MusicServer/internal/backend/statistics.go
T
Sansan 4c2db11cc5 feat: Implement Statistics API with 8 endpoints under /api/v1/statistics/
- Add statistics.sql with 8 SQL queries for play count statistics
- Generate repository code via sqlc
- Add backend/statistics.go with business logic
- Add server/statistics_handler.go with Echo handlers
- Register protected routes under /api/v1/statistics/ with token auth
- Endpoints: games/most-played, games/least-played, games/never-played,
  games/last-played, games/oldest-played, songs/most-played,
  songs/least-played, summary

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
2026-06-08 20:33:29 +02:00

278 lines
7.4 KiB
Go

package backend
import (
"encoding/json"
"time"
"music-server/internal/logging"
"go.uber.org/zap"
)
// GameWithSongs represents a game with its songs for statistics
type GameWithSongs struct {
GameID int32 `json:"game_id"`
GameName string `json:"game_name"`
GamePlayed int32 `json:"game_played"`
GameLastPlayed *time.Time `json:"game_last_played,omitempty"`
Songs []SongInfoForStats `json:"songs"`
}
// SongInfoForStats represents a song with game info for statistics
type SongInfoForStats struct {
GameID int32 `json:"game_id"`
GameName string `json:"game_name"`
SongName string `json:"song_name"`
Path string `json:"path"`
TimesPlayed int32 `json:"times_played"`
FileName *string `json:"file_name,omitempty"`
}
// StatisticsSummary holds overall statistics
type StatisticsSummary struct {
TotalGames int64 `json:"total_games"`
PlayedGames int64 `json:"played_games"`
NeverPlayedGames int64 `json:"never_played_games"`
TotalGamePlays int64 `json:"total_game_plays"`
AvgGamePlays float64 `json:"avg_game_plays"`
MaxGamePlays int64 `json:"max_game_plays"`
MinGamePlays int64 `json:"min_game_plays"`
}
// StatisticsHandler manages statistics operations
type StatisticsHandler struct {
// Uses the global backend repo initialized via InitBackend
}
// NewStatisticsHandler creates a new StatisticsHandler
func NewStatisticsHandler() *StatisticsHandler {
return &StatisticsHandler{}
}
// GetMostPlayedGamesWithSongs returns the top N most played games with their songs
func (h *StatisticsHandler) GetMostPlayedGamesWithSongs(limit int32) ([]GameWithSongs, error) {
queries := BackendRepo()
ctx := BackendCtx()
// Get raw results
rows, err := queries.GetMostPlayedGamesWithSongs(ctx, limit)
if err != nil {
return nil, err
}
// Convert to GameWithSongs
var result []GameWithSongs
for _, row := range rows {
var songs []SongInfoForStats
if row.Songs != nil {
// Parse JSON songs array
if err := json.Unmarshal(row.Songs, &songs); err != nil {
// Fallback: if JSON parsing fails, create empty song entries
songs = make([]SongInfoForStats, 0)
}
}
result = append(result, GameWithSongs{
GameID: row.GameID,
GameName: row.GameName,
GamePlayed: row.GamePlayed,
GameLastPlayed: row.GameLastPlayed,
Songs: songs,
})
}
return result, nil
}
// GetLeastPlayedGamesWithSongs returns the top N least played games with their songs
func (h *StatisticsHandler) GetLeastPlayedGamesWithSongs(limit int32) ([]GameWithSongs, error) {
queries := BackendRepo()
ctx := BackendCtx()
rows, err := queries.GetLeastPlayedGamesWithSongs(ctx, limit)
if err != nil {
return nil, err
}
var result []GameWithSongs
for _, row := range rows {
var songs []SongInfoForStats
if row.Songs != nil {
if err := json.Unmarshal(row.Songs, &songs); err != nil {
songs = make([]SongInfoForStats, 0)
}
}
result = append(result, GameWithSongs{
GameID: row.GameID,
GameName: row.GameName,
GamePlayed: row.GamePlayed,
GameLastPlayed: row.GameLastPlayed,
Songs: songs,
})
}
return result, nil
}
// GetMostPlayedSongsWithGame returns the top N most played songs with their game info
func (h *StatisticsHandler) GetMostPlayedSongsWithGame(limit int32) ([]SongInfoForStats, error) {
queries := BackendRepo()
ctx := BackendCtx()
rows, err := queries.GetMostPlayedSongsWithGame(ctx, limit)
if err != nil {
return nil, err
}
var result []SongInfoForStats
for _, row := range rows {
result = append(result, SongInfoForStats{
GameID: row.GameID,
GameName: row.GameName,
SongName: row.SongName,
Path: row.Path,
TimesPlayed: row.TimesPlayed,
FileName: row.FileName,
})
}
return result, nil
}
// GetLeastPlayedSongsWithGame returns the top N least played songs with their game info
func (h *StatisticsHandler) GetLeastPlayedSongsWithGame(limit int32) ([]SongInfoForStats, error) {
queries := BackendRepo()
ctx := BackendCtx()
rows, err := queries.GetLeastPlayedSongsWithGame(ctx, limit)
if err != nil {
return nil, err
}
var result []SongInfoForStats
for _, row := range rows {
result = append(result, SongInfoForStats{
GameID: row.GameID,
GameName: row.GameName,
SongName: row.SongName,
Path: row.Path,
TimesPlayed: row.TimesPlayed,
FileName: row.FileName,
})
}
return result, nil
}
// GetNeverPlayedGames returns games that have never been played
func (h *StatisticsHandler) GetNeverPlayedGames() ([]GameWithSongs, error) {
queries := BackendRepo()
ctx := BackendCtx()
rows, err := queries.GetNeverPlayedGames(ctx)
if err != nil {
return nil, err
}
var result []GameWithSongs
for _, row := range rows {
var songs []SongInfoForStats
if row.Songs != nil {
if err := json.Unmarshal(row.Songs, &songs); err != nil {
songs = make([]SongInfoForStats, 0)
}
}
result = append(result, GameWithSongs{
GameID: row.GameID,
GameName: row.GameName,
GamePlayed: row.GamePlayed,
GameLastPlayed: nil,
Songs: songs,
})
}
return result, nil
}
// GetLastPlayedGames returns the most recently played games
func (h *StatisticsHandler) GetLastPlayedGames(limit int32) ([]GameWithSongs, error) {
queries := BackendRepo()
ctx := BackendCtx()
rows, err := queries.GetLastPlayedGames(ctx, limit)
if err != nil {
return nil, err
}
var result []GameWithSongs
for _, row := range rows {
var songs []SongInfoForStats
if row.Songs != nil {
if err := json.Unmarshal(row.Songs, &songs); err != nil {
songs = make([]SongInfoForStats, 0)
}
}
result = append(result, GameWithSongs{
GameID: row.GameID,
GameName: row.GameName,
GamePlayed: row.GamePlayed,
GameLastPlayed: row.GameLastPlayed,
Songs: songs,
})
}
return result, nil
}
// GetOldestPlayedGames returns the least recently played games
func (h *StatisticsHandler) GetOldestPlayedGames(limit int32) ([]GameWithSongs, error) {
queries := BackendRepo()
ctx := BackendCtx()
rows, err := queries.GetOldestPlayedGames(ctx, limit)
if err != nil {
return nil, err
}
var result []GameWithSongs
for _, row := range rows {
var songs []SongInfoForStats
if row.Songs != nil {
if err := json.Unmarshal(row.Songs, &songs); err != nil {
songs = make([]SongInfoForStats, 0)
}
}
result = append(result, GameWithSongs{
GameID: row.GameID,
GameName: row.GameName,
GamePlayed: row.GamePlayed,
GameLastPlayed: row.GameLastPlayed,
Songs: songs,
})
}
return result, nil
}
// GetStatisticsSummary returns overall statistics
func (h *StatisticsHandler) GetStatisticsSummary() (*StatisticsSummary, error) {
queries := BackendRepo()
ctx := BackendCtx()
row, err := queries.GetStatisticsSummary(ctx)
if err != nil {
return nil, err
}
return &StatisticsSummary{
TotalGames: int64(row.TotalGames),
PlayedGames: int64(row.PlayedGames),
NeverPlayedGames: int64(row.NeverPlayedGames),
TotalGamePlays: int64(row.TotalGamePlays),
AvgGamePlays: float64(row.AvgGamePlays),
MaxGamePlays: int64(row.MaxGamePlays),
MinGamePlays: int64(row.MinGamePlays),
}, nil
}
// Log helper for statistics operations
func logStatisticsError(err error, operation string) {
if err != nil {
logging.GetLogger().Error("Statistics error",
zap.String("operation", operation),
zap.String("error", err.Error()))
}
}