mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(api): add grouped statistics and request_body to log endpoints
Add group_by parameter to /admin/logs/stats endpoint supporting: - group_by=model: aggregate stats per model with avg latency - group_by=day: daily aggregation with token counts - group_by=month: monthly aggregation with token counts Also include request_body field in admin ListLogs response for full visibility into logged requests.
This commit is contained in:
@@ -29,6 +29,7 @@ type LogView struct {
|
||||
RequestSize int64 `json:"request_size"`
|
||||
ResponseSize int64 `json:"response_size"`
|
||||
AuditReason string `json:"audit_reason"`
|
||||
RequestBody string `json:"request_body,omitempty"`
|
||||
}
|
||||
|
||||
func toLogView(r model.LogRecord) LogView {
|
||||
@@ -50,6 +51,7 @@ func toLogView(r model.LogRecord) LogView {
|
||||
RequestSize: r.RequestSize,
|
||||
ResponseSize: r.ResponseSize,
|
||||
AuditReason: r.AuditReason,
|
||||
RequestBody: r.RequestBody,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,11 +209,12 @@ func (h *Handler) ListLogs(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list logs", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
out := make([]MasterLogView, 0, len(rows))
|
||||
// Admin sees full LogView including request_body
|
||||
out := make([]LogView, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, toMasterLogView(r))
|
||||
out = append(out, toLogView(r))
|
||||
}
|
||||
c.JSON(http.StatusOK, ListMasterLogsResponse{Total: total, Limit: limit, Offset: offset, Items: out})
|
||||
c.JSON(http.StatusOK, ListLogsResponse{Total: total, Limit: limit, Offset: offset, Items: out})
|
||||
}
|
||||
|
||||
type LogStatsResponse struct {
|
||||
@@ -222,6 +225,26 @@ type LogStatsResponse struct {
|
||||
ByStatus map[string]int64 `json:"by_status"`
|
||||
}
|
||||
|
||||
// GroupedStatsItem represents a single group in grouped statistics
|
||||
type GroupedStatsItem struct {
|
||||
// For group_by=model
|
||||
Model string `json:"model,omitempty"`
|
||||
// For group_by=day
|
||||
Date string `json:"date,omitempty"`
|
||||
// For group_by=month
|
||||
Month string `json:"month,omitempty"`
|
||||
|
||||
Count int64 `json:"count"`
|
||||
TokensIn int64 `json:"tokens_in"`
|
||||
TokensOut int64 `json:"tokens_out"`
|
||||
AvgLatencyMs float64 `json:"avg_latency_ms,omitempty"`
|
||||
}
|
||||
|
||||
// GroupedStatsResponse is returned when group_by is specified
|
||||
type GroupedStatsResponse struct {
|
||||
Items []GroupedStatsItem `json:"items"`
|
||||
}
|
||||
|
||||
type DeleteLogsRequest struct {
|
||||
Before string `json:"before"`
|
||||
KeyID uint `json:"key_id"`
|
||||
@@ -323,12 +346,13 @@ func (h *Handler) deleteLogsBefore(cutoff time.Time, keyID uint, modelName strin
|
||||
|
||||
// LogStats godoc
|
||||
// @Summary Log stats (admin)
|
||||
// @Description Aggregate log stats with basic filtering
|
||||
// @Description Aggregate log stats with basic filtering. Use group_by param for grouped statistics.
|
||||
// @Tags admin
|
||||
// @Produce json
|
||||
// @Security AdminAuth
|
||||
// @Param since query int false "unix seconds"
|
||||
// @Param until query int false "unix seconds"
|
||||
// @Param since query int false "unix seconds"
|
||||
// @Param until query int false "unix seconds"
|
||||
// @Param group_by query string false "group by dimension: model, day, month"
|
||||
// @Success 200 {object} LogStatsResponse
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/logs/stats [get]
|
||||
@@ -341,6 +365,20 @@ func (h *Handler) LogStats(c *gin.Context) {
|
||||
q = q.Where("created_at <= ?", t)
|
||||
}
|
||||
|
||||
groupBy := strings.TrimSpace(c.Query("group_by"))
|
||||
switch groupBy {
|
||||
case "model":
|
||||
h.logStatsByModel(c, q)
|
||||
return
|
||||
case "day":
|
||||
h.logStatsByDay(c, q)
|
||||
return
|
||||
case "month":
|
||||
h.logStatsByMonth(c, q)
|
||||
return
|
||||
}
|
||||
|
||||
// Default: aggregated stats (backward compatible)
|
||||
var total int64
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to count logs", "details": err.Error()})
|
||||
@@ -381,6 +419,94 @@ func (h *Handler) LogStats(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// logStatsByModel handles group_by=model
|
||||
func (h *Handler) logStatsByModel(c *gin.Context, q *gorm.DB) {
|
||||
type modelStats struct {
|
||||
ModelName string
|
||||
Cnt int64
|
||||
TokensIn int64
|
||||
TokensOut int64
|
||||
AvgLatencyMs float64
|
||||
}
|
||||
var rows []modelStats
|
||||
if err := q.Select(`model_name, COUNT(*) as cnt, COALESCE(SUM(tokens_in),0) as tokens_in, COALESCE(SUM(tokens_out),0) as tokens_out, COALESCE(AVG(latency_ms),0) as avg_latency_ms`).
|
||||
Group("model_name").
|
||||
Order("cnt DESC").
|
||||
Scan(&rows).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to aggregate by model", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
items := make([]GroupedStatsItem, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
items = append(items, GroupedStatsItem{
|
||||
Model: r.ModelName,
|
||||
Count: r.Cnt,
|
||||
TokensIn: r.TokensIn,
|
||||
TokensOut: r.TokensOut,
|
||||
AvgLatencyMs: r.AvgLatencyMs,
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, GroupedStatsResponse{Items: items})
|
||||
}
|
||||
|
||||
// logStatsByDay handles group_by=day
|
||||
func (h *Handler) logStatsByDay(c *gin.Context, q *gorm.DB) {
|
||||
type dayStats struct {
|
||||
Day string
|
||||
Cnt int64
|
||||
TokensIn int64
|
||||
TokensOut int64
|
||||
}
|
||||
var rows []dayStats
|
||||
// PostgreSQL DATE function; SQLite uses date()
|
||||
if err := q.Select(`TO_CHAR(created_at, 'YYYY-MM-DD') as day, COUNT(*) as cnt, COALESCE(SUM(tokens_in),0) as tokens_in, COALESCE(SUM(tokens_out),0) as tokens_out`).
|
||||
Group("day").
|
||||
Order("day ASC").
|
||||
Scan(&rows).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to aggregate by day", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
items := make([]GroupedStatsItem, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
items = append(items, GroupedStatsItem{
|
||||
Date: r.Day,
|
||||
Count: r.Cnt,
|
||||
TokensIn: r.TokensIn,
|
||||
TokensOut: r.TokensOut,
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, GroupedStatsResponse{Items: items})
|
||||
}
|
||||
|
||||
// logStatsByMonth handles group_by=month
|
||||
func (h *Handler) logStatsByMonth(c *gin.Context, q *gorm.DB) {
|
||||
type monthStats struct {
|
||||
Month string
|
||||
Cnt int64
|
||||
TokensIn int64
|
||||
TokensOut int64
|
||||
}
|
||||
var rows []monthStats
|
||||
// PostgreSQL format
|
||||
if err := q.Select(`TO_CHAR(created_at, 'YYYY-MM') as month, COUNT(*) as cnt, COALESCE(SUM(tokens_in),0) as tokens_in, COALESCE(SUM(tokens_out),0) as tokens_out`).
|
||||
Group("month").
|
||||
Order("month ASC").
|
||||
Scan(&rows).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to aggregate by month", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
items := make([]GroupedStatsItem, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
items = append(items, GroupedStatsItem{
|
||||
Month: r.Month,
|
||||
Count: r.Cnt,
|
||||
TokensIn: r.TokensIn,
|
||||
TokensOut: r.TokensOut,
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, GroupedStatsResponse{Items: items})
|
||||
}
|
||||
|
||||
// ListSelfLogs godoc
|
||||
// @Summary List logs (master)
|
||||
// @Description List request logs for the authenticated master
|
||||
|
||||
Reference in New Issue
Block a user