feat: 导出密钥功能
This commit is contained in:
@@ -23,6 +23,7 @@ type Server struct {
|
||||
KeyManualValidationService *services.KeyManualValidationService
|
||||
TaskService *services.TaskService
|
||||
KeyService *services.KeyService
|
||||
LogService *services.LogService
|
||||
CommonHandler *CommonHandler
|
||||
}
|
||||
|
||||
@@ -36,6 +37,7 @@ type NewServerParams struct {
|
||||
KeyManualValidationService *services.KeyManualValidationService
|
||||
TaskService *services.TaskService
|
||||
KeyService *services.KeyService
|
||||
LogService *services.LogService
|
||||
CommonHandler *CommonHandler
|
||||
}
|
||||
|
||||
@@ -49,6 +51,7 @@ func NewServer(params NewServerParams) *Server {
|
||||
KeyManualValidationService: params.KeyManualValidationService,
|
||||
TaskService: params.TaskService,
|
||||
KeyService: params.KeyService,
|
||||
LogService: params.LogService,
|
||||
CommonHandler: params.CommonHandler,
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import (
|
||||
app_errors "gpt-load/internal/errors"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/response"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -301,3 +302,38 @@ func (s *Server) ClearAllInvalidKeys(c *gin.Context) {
|
||||
|
||||
response.Success(c, gin.H{"message": fmt.Sprintf("%d invalid keys cleared.", rowsAffected)})
|
||||
}
|
||||
|
||||
// ExportKeys handles exporting keys to a text file.
|
||||
func (s *Server) ExportKeys(c *gin.Context) {
|
||||
groupID, err := validateGroupIDFromQuery(c)
|
||||
if err != nil {
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrBadRequest, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
statusFilter := c.Query("status")
|
||||
if statusFilter == "" {
|
||||
statusFilter = "all"
|
||||
}
|
||||
|
||||
switch statusFilter {
|
||||
case "all", models.KeyStatusActive, models.KeyStatusInvalid:
|
||||
default:
|
||||
response.Error(c, app_errors.NewAPIError(app_errors.ErrValidation, "Invalid status filter"))
|
||||
return
|
||||
}
|
||||
|
||||
group, ok := s.findGroupByID(c, groupID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("keys-%s-%s.txt", group.Name, statusFilter)
|
||||
c.Header("Content-Disposition", "attachment; filename="+filename)
|
||||
c.Header("Content-Type", "text/plain; charset=utf-8")
|
||||
|
||||
err = s.KeyService.StreamKeysToWriter(groupID, statusFilter, c.Writer)
|
||||
if err != nil {
|
||||
log.Printf("Failed to stream keys: %v", err)
|
||||
}
|
||||
}
|
||||
|
@@ -1,13 +1,12 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gpt-load/internal/db"
|
||||
"fmt"
|
||||
app_errors "gpt-load/internal/errors"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/response"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -17,43 +16,9 @@ type LogResponse struct {
|
||||
models.RequestLog
|
||||
}
|
||||
|
||||
// GetLogs Get request logs
|
||||
func GetLogs(c *gin.Context) {
|
||||
query := db.DB.Model(&models.RequestLog{})
|
||||
|
||||
if groupName := c.Query("group_name"); groupName != "" {
|
||||
query = query.Where("group_name LIKE ?", "%"+groupName+"%")
|
||||
}
|
||||
if keyValue := c.Query("key_value"); keyValue != "" {
|
||||
likePattern := "%" + keyValue[1:len(keyValue)-1] + "%"
|
||||
query = query.Where("key_value LIKE ?", likePattern)
|
||||
}
|
||||
if isSuccessStr := c.Query("is_success"); isSuccessStr != "" {
|
||||
if isSuccess, err := strconv.ParseBool(isSuccessStr); err == nil {
|
||||
query = query.Where("is_success = ?", isSuccess)
|
||||
}
|
||||
}
|
||||
if statusCodeStr := c.Query("status_code"); statusCodeStr != "" {
|
||||
if statusCode, err := strconv.Atoi(statusCodeStr); err == nil {
|
||||
query = query.Where("status_code = ?", statusCode)
|
||||
}
|
||||
}
|
||||
if sourceIP := c.Query("source_ip"); sourceIP != "" {
|
||||
query = query.Where("source_ip = ?", sourceIP)
|
||||
}
|
||||
if errorContains := c.Query("error_contains"); errorContains != "" {
|
||||
query = query.Where("error_message LIKE ?", "%"+errorContains+"%")
|
||||
}
|
||||
if startTimeStr := c.Query("start_time"); startTimeStr != "" {
|
||||
if startTime, err := time.Parse(time.RFC3339, startTimeStr); err == nil {
|
||||
query = query.Where("timestamp >= ?", startTime)
|
||||
}
|
||||
}
|
||||
if endTimeStr := c.Query("end_time"); endTimeStr != "" {
|
||||
if endTime, err := time.Parse(time.RFC3339, endTimeStr); err == nil {
|
||||
query = query.Where("timestamp <= ?", endTime)
|
||||
}
|
||||
}
|
||||
// GetLogs handles fetching request logs with filtering and pagination.
|
||||
func (s *Server) GetLogs(c *gin.Context) {
|
||||
query := s.LogService.GetLogsQuery(c)
|
||||
|
||||
var logs []models.RequestLog
|
||||
query = query.Order("timestamp desc")
|
||||
@@ -66,3 +31,18 @@ func GetLogs(c *gin.Context) {
|
||||
pagination.Items = logs
|
||||
response.Success(c, pagination)
|
||||
}
|
||||
|
||||
// ExportLogs handles exporting filtered log keys to a CSV file.
|
||||
func (s *Server) ExportLogs(c *gin.Context) {
|
||||
filename := fmt.Sprintf("log_keys_export_%s.csv", time.Now().Format("20060102150405"))
|
||||
c.Header("Content-Disposition", "attachment; filename="+filename)
|
||||
c.Header("Content-Type", "text/csv; charset=utf-8")
|
||||
|
||||
// Stream the response
|
||||
err := s.LogService.StreamLogKeysToCSV(c, c.Writer)
|
||||
if err != nil {
|
||||
log.Printf("Failed to stream log keys to CSV: %v", err)
|
||||
c.JSON(500, gin.H{"error": "Failed to export logs"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user