101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package plugin
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"net/http"
|
||
"net/url"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
func (a *App) businessEvents(query url.Values) ManagementResponse {
|
||
limit, _ := strconv.Atoi(query.Get("limit"))
|
||
store, ok := a.currentStore()
|
||
if !ok {
|
||
return managementError(http.StatusServiceUnavailable, "database_unavailable", "业务日志数据库尚未初始化")
|
||
}
|
||
events, err := store.ListBusinessEvents(context.Background(), limit)
|
||
if err != nil {
|
||
return managementError(http.StatusInternalServerError, "database_error", err.Error())
|
||
}
|
||
return jsonManagementResponse(http.StatusOK, map[string]any{"events": events})
|
||
}
|
||
|
||
func (a *App) auditFailedManagement(response ManagementResponse, event string) ManagementResponse {
|
||
if response.StatusCode < http.StatusBadRequest {
|
||
return response
|
||
}
|
||
store, ok := a.currentStore()
|
||
if !ok {
|
||
return response
|
||
}
|
||
status := "失败"
|
||
var payload struct {
|
||
Error struct {
|
||
Message string `json:"message"`
|
||
} `json:"error"`
|
||
}
|
||
if json.Unmarshal(response.Body, &payload) == nil {
|
||
message := strings.TrimSpace(payload.Error.Message)
|
||
message = strings.Join(strings.Fields(message), " ")
|
||
if len([]rune(message)) > 160 {
|
||
message = string([]rune(message)[:160])
|
||
}
|
||
if message != "" {
|
||
status += ":" + message
|
||
}
|
||
}
|
||
_ = store.RecordBusinessEvent(context.Background(), event, status, time.Now().UTC())
|
||
return response
|
||
}
|
||
|
||
func keyEvent(body []byte, action string) string {
|
||
var value struct {
|
||
ID string `json:"id"`
|
||
Name string `json:"name"`
|
||
}
|
||
_ = json.Unmarshal(body, &value)
|
||
target := strings.TrimSpace(value.Name)
|
||
if target == "" {
|
||
target = strings.TrimSpace(value.ID)
|
||
}
|
||
if target == "" {
|
||
return "管理员" + action + "用户 Key"
|
||
}
|
||
return fmt.Sprintf("管理员%s用户 `%s` 的 Key", action, safeEventValue(target))
|
||
}
|
||
|
||
func priceEvent(body []byte, action string) string {
|
||
var value struct {
|
||
Model string `json:"model"`
|
||
}
|
||
_ = json.Unmarshal(body, &value)
|
||
model := strings.TrimSpace(value.Model)
|
||
if model == "" {
|
||
return "管理员" + action + "模型价格"
|
||
}
|
||
return fmt.Sprintf("管理员%s模型 `%s` 的价格", action, safeEventValue(model))
|
||
}
|
||
|
||
func billingResetEvent(body []byte) string {
|
||
var value struct {
|
||
ID string `json:"id"`
|
||
All bool `json:"all"`
|
||
}
|
||
_ = json.Unmarshal(body, &value)
|
||
if value.All {
|
||
return "管理员手动重置全部用户额度"
|
||
}
|
||
if strings.TrimSpace(value.ID) == "" {
|
||
return "管理员手动重置用户额度"
|
||
}
|
||
return fmt.Sprintf("管理员手动重置用户 `%s` 的额度", safeEventValue(value.ID))
|
||
}
|
||
|
||
func safeEventValue(value string) string {
|
||
return strings.ReplaceAll(strings.TrimSpace(value), "`", "")
|
||
}
|