This commit is contained in:
2025-09-12 01:15:09 +08:00
parent c563b2266b
commit e05ad9e223
103 changed files with 20034 additions and 1041 deletions

View File

@@ -2,6 +2,7 @@ package api
import (
"context"
"fmt"
"time"
"tyapi-server/internal/domains/api/entities"
"tyapi-server/internal/domains/api/repositories"
@@ -228,6 +229,61 @@ func (r *GormApiCallRepository) CountByUserId(ctx context.Context, userId string
return r.CountWhere(ctx, &entities.ApiCall{}, "user_id = ?", userId)
}
// CountByUserIdAndDateRange 按用户ID和日期范围统计API调用次数
func (r *GormApiCallRepository) CountByUserIdAndDateRange(ctx context.Context, userId string, startDate, endDate time.Time) (int64, error) {
return r.CountWhere(ctx, &entities.ApiCall{}, "user_id = ? AND created_at >= ? AND created_at < ?", userId, startDate, endDate)
}
// GetDailyStatsByUserId 获取用户每日API调用统计
func (r *GormApiCallRepository) GetDailyStatsByUserId(ctx context.Context, userId string, startDate, endDate time.Time) ([]map[string]interface{}, error) {
var results []map[string]interface{}
// 构建SQL查询 - 使用PostgreSQL语法使用具体的日期范围
sql := `
SELECT
DATE(created_at) as date,
COUNT(*) as calls
FROM api_calls
WHERE user_id = $1
AND DATE(created_at) >= $2
AND DATE(created_at) <= $3
GROUP BY DATE(created_at)
ORDER BY date ASC
`
err := r.GetDB(ctx).Raw(sql, userId, startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).Scan(&results).Error
if err != nil {
return nil, err
}
return results, nil
}
// GetMonthlyStatsByUserId 获取用户每月API调用统计
func (r *GormApiCallRepository) GetMonthlyStatsByUserId(ctx context.Context, userId string, startDate, endDate time.Time) ([]map[string]interface{}, error) {
var results []map[string]interface{}
// 构建SQL查询 - 使用PostgreSQL语法使用具体的日期范围
sql := `
SELECT
TO_CHAR(created_at, 'YYYY-MM') as month,
COUNT(*) as calls
FROM api_calls
WHERE user_id = $1
AND created_at >= $2
AND created_at <= $3
GROUP BY TO_CHAR(created_at, 'YYYY-MM')
ORDER BY month ASC
`
err := r.GetDB(ctx).Raw(sql, userId, startDate, endDate).Scan(&results).Error
if err != nil {
return nil, err
}
return results, nil
}
func (r *GormApiCallRepository) FindByTransactionId(ctx context.Context, transactionId string) (*entities.ApiCall, error) {
var call entities.ApiCall
err := r.FindOne(ctx, &call, "transaction_id = ?", transactionId)
@@ -329,4 +385,135 @@ func (r *GormApiCallRepository) ListWithFiltersAndProductName(ctx context.Contex
}
return productNameMap, calls, total, nil
}
// GetSystemTotalCalls 获取系统总API调用次数
func (r *GormApiCallRepository) GetSystemTotalCalls(ctx context.Context) (int64, error) {
var count int64
err := r.GetDB(ctx).Model(&entities.ApiCall{}).Count(&count).Error
return count, err
}
// GetSystemCallsByDateRange 获取系统指定时间范围内的API调用次数
func (r *GormApiCallRepository) GetSystemCallsByDateRange(ctx context.Context, startDate, endDate time.Time) (int64, error) {
var count int64
err := r.GetDB(ctx).Model(&entities.ApiCall{}).
Where("created_at >= ? AND created_at <= ?", startDate, endDate).
Count(&count).Error
return count, err
}
// GetSystemDailyStats 获取系统每日API调用统计
func (r *GormApiCallRepository) GetSystemDailyStats(ctx context.Context, startDate, endDate time.Time) ([]map[string]interface{}, error) {
var results []map[string]interface{}
sql := `
SELECT
DATE(created_at) as date,
COUNT(*) as calls
FROM api_calls
WHERE DATE(created_at) >= $1
AND DATE(created_at) <= $2
GROUP BY DATE(created_at)
ORDER BY date ASC
`
err := r.GetDB(ctx).Raw(sql, startDate.Format("2006-01-02"), endDate.Format("2006-01-02")).Scan(&results).Error
if err != nil {
return nil, err
}
return results, nil
}
// GetSystemMonthlyStats 获取系统每月API调用统计
func (r *GormApiCallRepository) GetSystemMonthlyStats(ctx context.Context, startDate, endDate time.Time) ([]map[string]interface{}, error) {
var results []map[string]interface{}
sql := `
SELECT
TO_CHAR(created_at, 'YYYY-MM') as month,
COUNT(*) as calls
FROM api_calls
WHERE created_at >= $1
AND created_at <= $2
GROUP BY TO_CHAR(created_at, 'YYYY-MM')
ORDER BY month ASC
`
err := r.GetDB(ctx).Raw(sql, startDate, endDate).Scan(&results).Error
if err != nil {
return nil, err
}
return results, nil
}
// GetApiPopularityRanking 获取API受欢迎程度排行榜
func (r *GormApiCallRepository) GetApiPopularityRanking(ctx context.Context, period string, limit int) ([]map[string]interface{}, error) {
var sql string
var args []interface{}
switch period {
case "today":
sql = `
SELECT
p.id as product_id,
p.name as api_name,
p.description as api_description,
COUNT(ac.id) as call_count
FROM product p
LEFT JOIN api_calls ac ON p.id = ac.product_id
AND DATE(ac.created_at) = CURRENT_DATE
WHERE p.deleted_at IS NULL
GROUP BY p.id, p.name, p.description
HAVING COUNT(ac.id) > 0
ORDER BY call_count DESC
LIMIT $1
`
args = []interface{}{limit}
case "month":
sql = `
SELECT
p.id as product_id,
p.name as api_name,
p.description as api_description,
COUNT(ac.id) as call_count
FROM product p
LEFT JOIN api_calls ac ON p.id = ac.product_id
AND DATE_TRUNC('month', ac.created_at) = DATE_TRUNC('month', CURRENT_DATE)
WHERE p.deleted_at IS NULL
GROUP BY p.id, p.name, p.description
HAVING COUNT(ac.id) > 0
ORDER BY call_count DESC
LIMIT $1
`
args = []interface{}{limit}
case "total":
sql = `
SELECT
p.id as product_id,
p.name as api_name,
p.description as api_description,
COUNT(ac.id) as call_count
FROM product p
LEFT JOIN api_calls ac ON p.id = ac.product_id
WHERE p.deleted_at IS NULL
GROUP BY p.id, p.name, p.description
HAVING COUNT(ac.id) > 0
ORDER BY call_count DESC
LIMIT $1
`
args = []interface{}{limit}
default:
return nil, fmt.Errorf("不支持的时间周期: %s", period)
}
var results []map[string]interface{}
err := r.GetDB(ctx).Raw(sql, args...).Scan(&results).Error
if err != nil {
return nil, err
}
return results, nil
}