Files
tokens-reef/backend/internal/handler/admin/sora_handler.go
pham 7bf0ed8681
Some checks failed
CI / test (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
Security Scan / backend-security (push) Has been cancelled
Security Scan / frontend-security (push) Has been cancelled
remove dead proxy service and sora storage action
2026-04-20 23:05:30 +08:00

143 lines
4.3 KiB
Go

package admin
import (
"github.com/Wei-Shaw/sub2api/internal/pkg/pagination"
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/gin-gonic/gin"
)
// SoraHandler handles admin Sora statistics and management.
type SoraHandler struct {
soraGenService *service.SoraGenerationService
soraQuotaService *service.SoraQuotaService
userRepo service.UserRepository
}
// NewSoraHandler creates a new admin Sora handler.
func NewSoraHandler(
soraGenService *service.SoraGenerationService,
soraQuotaService *service.SoraQuotaService,
userRepo service.UserRepository,
) *SoraHandler {
return &SoraHandler{
soraGenService: soraGenService,
soraQuotaService: soraQuotaService,
userRepo: userRepo,
}
}
type SoraSystemStatsResponse struct {
TotalUsers int64 `json:"total_users"`
TotalGenerations int64 `json:"total_generations"`
TotalStorageBytes int64 `json:"total_storage_bytes"`
ActiveGenerations int64 `json:"active_generations"`
ByStatus map[string]int64 `json:"by_status"`
ByModel map[string]int64 `json:"by_model"`
}
// GetSystemStats returns aggregate admin Sora statistics.
func (h *SoraHandler) GetSystemStats(c *gin.Context) {
ctx := c.Request.Context()
users, _, err := h.userRepo.List(ctx, pagination.PaginationParams{Page: 1, PageSize: 10000})
if err != nil {
response.Error(c, 500, "Failed to get users")
return
}
resp := SoraSystemStatsResponse{
TotalUsers: int64(len(users)),
TotalGenerations: 0,
TotalStorageBytes: 0,
ActiveGenerations: 0,
ByStatus: map[string]int64{},
ByModel: map[string]int64{},
}
response.Success(c, resp)
}
type SoraUserStatsResponse struct {
UserID int64 `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
QuotaBytes int64 `json:"quota_bytes"`
UsedBytes int64 `json:"used_bytes"`
AvailableBytes int64 `json:"available_bytes"`
QuotaSource string `json:"quota_source"`
GenerationsCount int64 `json:"generations_count"`
ActiveCount int64 `json:"active_count"`
TotalFileSizeBytes int64 `json:"total_file_size_bytes"`
}
// ListUserStats returns per-user admin Sora usage rows.
func (h *SoraHandler) ListUserStats(c *gin.Context) {
ctx := c.Request.Context()
page, pageSize := response.ParsePagination(c)
search := c.Query("search")
users, result, err := h.userRepo.ListWithFilters(ctx, pagination.PaginationParams{
Page: page,
PageSize: pageSize,
}, service.UserListFilters{Search: search})
if err != nil {
response.Error(c, 500, "Failed to get users")
return
}
results := make([]SoraUserStatsResponse, len(users))
for i, u := range users {
quota, _ := h.soraQuotaService.GetQuota(ctx, u.ID)
activeCount, _ := h.soraGenService.CountActiveByUser(ctx, u.ID)
quotaBytes := int64(0)
availableBytes := int64(0)
quotaSource := "unlimited"
if quota != nil {
quotaBytes = quota.QuotaBytes
availableBytes = quota.AvailableBytes
quotaSource = quota.QuotaSource
}
results[i] = SoraUserStatsResponse{
UserID: u.ID,
Username: u.Username,
Email: u.Email,
QuotaBytes: quotaBytes,
UsedBytes: 0,
AvailableBytes: availableBytes,
QuotaSource: quotaSource,
GenerationsCount: 0,
ActiveCount: activeCount,
TotalFileSizeBytes: 0,
}
}
response.Paginated(c, results, result.Total, page, pageSize)
}
type SoraGenerationAdminResponse struct {
ID int64 `json:"id"`
UserID int64 `json:"user_id"`
Username string `json:"username"`
Email string `json:"email"`
Model string `json:"model"`
Prompt string `json:"prompt"`
MediaType string `json:"media_type"`
Status string `json:"status"`
StorageType string `json:"storage_type"`
MediaURL string `json:"media_url"`
FileSizeBytes int64 `json:"file_size_bytes"`
ErrorMessage string `json:"error_message"`
CreatedAt string `json:"created_at"`
CompletedAt *string `json:"completed_at"`
}
// ListGenerations returns admin-visible generation rows.
func (h *SoraHandler) ListGenerations(c *gin.Context) {
response.Paginated(c, []SoraGenerationAdminResponse{}, int64(0), 1, 20)
}