feat[Go]: implement /system/stats and refactor /system/config/log (#15407)

### What problem does this PR solve?

As title

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
- [x] Refactoring
This commit is contained in:
Haruko386
2026-05-29 19:32:21 +08:00
committed by GitHub
parent d2f0a18f42
commit d766e49128
6 changed files with 314 additions and 47 deletions

69
internal/handler/stats.go Normal file
View File

@@ -0,0 +1,69 @@
//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package handler
import (
"errors"
"net/http"
"time"
"ragflow/internal/common"
"ragflow/internal/service"
"github.com/gin-gonic/gin"
)
// GetStats returns API conversation statistics for the current user's tenant.
func (h *SystemHandler) GetStats(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
now := time.Now()
fromDate := c.DefaultQuery("from_date", now.AddDate(0, 0, -7).Format("2006-01-02 00:00:00"))
toDate := c.DefaultQuery("to_date", now.Format("2006-01-02 15:04:05"))
if len(toDate) == 10 {
toDate += " 23:59:59"
}
var source *string
if _, ok := c.GetQuery("canvas_id"); ok {
agentSource := "agent"
source = &agentSource
}
stats, err := h.systemService.GetStats(user.ID, fromDate, toDate, source)
if err != nil {
code := common.CodeExceptionError
if errors.Is(err, service.ErrTenantNotFound) {
code = common.CodeDataError
}
c.JSON(http.StatusOK, gin.H{
"code": code,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": common.CodeSuccess,
"message": "success",
"data": stats,
})
}

View File

@@ -164,44 +164,46 @@ func (h *SystemHandler) GetVersion(c *gin.Context) {
// GetLogLevel returns the current log level
func (h *SystemHandler) GetLogLevel(c *gin.Context) {
level := common.GetLevel()
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": gin.H{"level": level},
"data": common.GetLogLevels(),
})
}
// SetLogLevelRequest set log level request
type SetLogLevelRequest struct {
Level string `json:"level" binding:"required"`
PkgName string `json:"pkg_name" binding:"required"`
Level string `json:"level" binding:"required"`
}
// SetLogLevel sets the log level at runtime
func (h *SystemHandler) SetLogLevel(c *gin.Context) {
var req SetLogLevelRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": "level is required",
c.JSON(http.StatusOK, gin.H{
"code": common.CodeDataError,
"message": "pkg_name and level are required",
})
return
}
if err := common.SetLevel(req.Level); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": 400,
"message": err.Error(),
if err := common.SetPackageLogLevel(req.PkgName, req.Level); err != nil {
c.JSON(http.StatusOK, gin.H{
"code": common.CodeDataError,
"message": "Invalid log level: " + req.Level,
})
return
}
config := server.GetConfig()
config.Log.Level = req.Level
if req.PkgName == "root" && config != nil {
config.Log.Level = common.GetLevel()
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "Log level updated successfully",
"data": gin.H{"level": req.Level},
"message": "success",
"data": gin.H{"pkg_name": req.PkgName, "level": req.Level},
})
}