Update Js for logs.html
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"gemini-balancer/internal/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
@@ -28,13 +29,16 @@ func (s *LogService) Record(ctx context.Context, log *models.RequestLog) error {
|
||||
|
||||
// LogQueryParams 解耦 Gin,使用结构体传参
|
||||
type LogQueryParams struct {
|
||||
Page int
|
||||
PageSize int
|
||||
ModelName string
|
||||
IsSuccess *bool // 使用指针区分"未设置"和"false"
|
||||
StatusCode *int
|
||||
KeyID *uint64
|
||||
GroupID *uint64
|
||||
Page int
|
||||
PageSize int
|
||||
ModelName string
|
||||
IsSuccess *bool // 使用指针区分"未设置"和"false"
|
||||
StatusCode *int
|
||||
KeyIDs []string
|
||||
GroupIDs []string
|
||||
Q string
|
||||
ErrorCodes []string
|
||||
StatusCodes []string
|
||||
}
|
||||
|
||||
func (s *LogService) GetLogs(ctx context.Context, params LogQueryParams) ([]models.RequestLog, int64, error) {
|
||||
@@ -52,17 +56,12 @@ func (s *LogService) GetLogs(ctx context.Context, params LogQueryParams) ([]mode
|
||||
// 构建基础查询
|
||||
query := s.db.WithContext(ctx).Model(&models.RequestLog{})
|
||||
query = s.applyFilters(query, params)
|
||||
|
||||
// 计算总数
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to count logs: %w", err)
|
||||
}
|
||||
|
||||
if total == 0 {
|
||||
return []models.RequestLog{}, 0, nil
|
||||
}
|
||||
|
||||
// 分页查询
|
||||
offset := (params.Page - 1) * params.PageSize
|
||||
if err := query.Order("request_time DESC").
|
||||
Limit(params.PageSize).
|
||||
@@ -70,7 +69,6 @@ func (s *LogService) GetLogs(ctx context.Context, params LogQueryParams) ([]mode
|
||||
Find(&logs).Error; err != nil {
|
||||
return nil, 0, fmt.Errorf("failed to query logs: %w", err)
|
||||
}
|
||||
|
||||
return logs, total, nil
|
||||
}
|
||||
|
||||
@@ -84,11 +82,24 @@ func (s *LogService) applyFilters(query *gorm.DB, params LogQueryParams) *gorm.D
|
||||
if params.StatusCode != nil {
|
||||
query = query.Where("status_code = ?", *params.StatusCode)
|
||||
}
|
||||
if params.KeyID != nil {
|
||||
query = query.Where("key_id = ?", *params.KeyID)
|
||||
if len(params.KeyIDs) > 0 {
|
||||
query = query.Where("key_id IN (?)", params.KeyIDs)
|
||||
}
|
||||
if params.GroupID != nil {
|
||||
query = query.Where("group_id = ?", *params.GroupID)
|
||||
if len(params.GroupIDs) > 0 {
|
||||
query = query.Where("group_id IN (?)", params.GroupIDs)
|
||||
}
|
||||
if len(params.ErrorCodes) > 0 {
|
||||
query = query.Where("error_code IN (?)", params.ErrorCodes)
|
||||
}
|
||||
if len(params.StatusCodes) > 0 {
|
||||
query = query.Where("status_code IN (?)", params.StatusCodes)
|
||||
}
|
||||
if params.Q != "" {
|
||||
searchQuery := "%" + params.Q + "%"
|
||||
query = query.Where(
|
||||
"model_name LIKE ? OR error_code LIKE ? OR error_message LIKE ? OR CAST(status_code AS CHAR) LIKE ?",
|
||||
searchQuery, searchQuery, searchQuery, searchQuery,
|
||||
)
|
||||
}
|
||||
return query
|
||||
}
|
||||
@@ -132,21 +143,22 @@ func ParseLogQueryParams(queryParams map[string]string) (LogQueryParams, error)
|
||||
}
|
||||
}
|
||||
|
||||
if keyIDStr, ok := queryParams["key_id"]; ok {
|
||||
if keyID, err := strconv.ParseUint(keyIDStr, 10, 64); err == nil {
|
||||
params.KeyID = &keyID
|
||||
} else {
|
||||
return params, fmt.Errorf("invalid key_id parameter: %s", keyIDStr)
|
||||
}
|
||||
if keyIDsStr, ok := queryParams["key_ids"]; ok {
|
||||
params.KeyIDs = strings.Split(keyIDsStr, ",")
|
||||
}
|
||||
|
||||
if groupIDStr, ok := queryParams["group_id"]; ok {
|
||||
if groupID, err := strconv.ParseUint(groupIDStr, 10, 64); err == nil {
|
||||
params.GroupID = &groupID
|
||||
} else {
|
||||
return params, fmt.Errorf("invalid group_id parameter: %s", groupIDStr)
|
||||
}
|
||||
if groupIDsStr, ok := queryParams["group_ids"]; ok {
|
||||
params.GroupIDs = strings.Split(groupIDsStr, ",")
|
||||
}
|
||||
|
||||
if errorCodesStr, ok := queryParams["error_codes"]; ok {
|
||||
params.ErrorCodes = strings.Split(errorCodesStr, ",")
|
||||
}
|
||||
if statusCodesStr, ok := queryParams["status_codes"]; ok {
|
||||
params.StatusCodes = strings.Split(statusCodesStr, ",")
|
||||
}
|
||||
if q, ok := queryParams["q"]; ok {
|
||||
params.Q = q
|
||||
}
|
||||
return params, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user