- 全局替换 ApiKey → APIKey(类型、字段、方法、变量) - 修复所有 initialism 命名(API, SMTP, HTML, URL 等) - 添加所有缺失的包注释 - 修复导出符号的注释格式 主要修改: - ApiKey → APIKey(所有出现的地方) - ApiKeyID → APIKeyID - ApiKeyIDs → APIKeyIDs - TestSmtpConnection → TestSMTPConnection - HtmlURL → HTMLURL - 添加 20+ 个包注释 - 修复 10+ 个导出符号注释格式 验证结果: - ✓ golangci-lint: 0 issues - ✓ 单元测试: 通过 - ✓ 集成测试: 通过
77 lines
2.7 KiB
Go
77 lines
2.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/usagestats"
|
|
)
|
|
|
|
// DashboardService provides aggregated statistics for admin dashboard.
|
|
type DashboardService struct {
|
|
usageRepo UsageLogRepository
|
|
}
|
|
|
|
func NewDashboardService(usageRepo UsageLogRepository) *DashboardService {
|
|
return &DashboardService{
|
|
usageRepo: usageRepo,
|
|
}
|
|
}
|
|
|
|
func (s *DashboardService) GetDashboardStats(ctx context.Context) (*usagestats.DashboardStats, error) {
|
|
stats, err := s.usageRepo.GetDashboardStats(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get dashboard stats: %w", err)
|
|
}
|
|
return stats, nil
|
|
}
|
|
|
|
func (s *DashboardService) GetUsageTrendWithFilters(ctx context.Context, startTime, endTime time.Time, granularity string, userID, apiKeyID int64) ([]usagestats.TrendDataPoint, error) {
|
|
trend, err := s.usageRepo.GetUsageTrendWithFilters(ctx, startTime, endTime, granularity, userID, apiKeyID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get usage trend with filters: %w", err)
|
|
}
|
|
return trend, nil
|
|
}
|
|
|
|
func (s *DashboardService) GetModelStatsWithFilters(ctx context.Context, startTime, endTime time.Time, userID, apiKeyID int64) ([]usagestats.ModelStat, error) {
|
|
stats, err := s.usageRepo.GetModelStatsWithFilters(ctx, startTime, endTime, userID, apiKeyID, 0)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get model stats with filters: %w", err)
|
|
}
|
|
return stats, nil
|
|
}
|
|
|
|
func (s *DashboardService) GetAPIKeyUsageTrend(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.APIKeyUsageTrendPoint, error) {
|
|
trend, err := s.usageRepo.GetAPIKeyUsageTrend(ctx, startTime, endTime, granularity, limit)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get api key usage trend: %w", err)
|
|
}
|
|
return trend, nil
|
|
}
|
|
|
|
func (s *DashboardService) GetUserUsageTrend(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.UserUsageTrendPoint, error) {
|
|
trend, err := s.usageRepo.GetUserUsageTrend(ctx, startTime, endTime, granularity, limit)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get user usage trend: %w", err)
|
|
}
|
|
return trend, nil
|
|
}
|
|
|
|
func (s *DashboardService) GetBatchUserUsageStats(ctx context.Context, userIDs []int64) (map[int64]*usagestats.BatchUserUsageStats, error) {
|
|
stats, err := s.usageRepo.GetBatchUserUsageStats(ctx, userIDs)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get batch user usage stats: %w", err)
|
|
}
|
|
return stats, nil
|
|
}
|
|
|
|
func (s *DashboardService) GetBatchAPIKeyUsageStats(ctx context.Context, apiKeyIDs []int64) (map[int64]*usagestats.BatchAPIKeyUsageStats, error) {
|
|
stats, err := s.usageRepo.GetBatchAPIKeyUsageStats(ctx, apiKeyIDs)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get batch api key usage stats: %w", err)
|
|
}
|
|
return stats, nil
|
|
}
|