fix: run.sh 改为启动 Go 后端 + dashboard stats 格式对齐前端
- run.sh start_backend 改为编译+运行 Go 二进制 - DashboardStats handler 返回前端期望的字段格式 - build/test 命令适配 Go 工具链 - 前端验证: 登录/仪表盘/工单列表/查询页面正常
This commit is contained in:
1 parent
ca308e277d
commit
26148d0ca7
2 files changed
+54
-42
No files matched your search
@@ -9,49 +9,68 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GET /api/v1/dashboard/stats
|
||||
// GET /api/v1/dashboard/stats — 匹配前端 DashboardStats 类型
|
||||
func DashboardStats(c *gin.Context) {
|
||||
var ticketCount, queryCount, instanceCount int64
|
||||
model.DB.Model(&model.Ticket{}).Count(&ticketCount)
|
||||
model.DB.Model(&model.QueryHistory{}).Count(&queryCount)
|
||||
model.DB.Model(&model.Instance{}).Count(&instanceCount)
|
||||
|
||||
var ticketTotal, ticketPending, ticketExecuting int64
|
||||
var instanceTotal, instanceOnline, userTotal int64
|
||||
var queriesToday int64
|
||||
|
||||
model.DB.Model(&model.Ticket{}).Count(&ticketTotal)
|
||||
model.DB.Model(&model.Ticket{}).Where("status IN ?", []string{"pending", "reviewing"}).Count(&ticketPending)
|
||||
model.DB.Model(&model.Ticket{}).Where("status = ?", "executing").Count(&ticketExecuting)
|
||||
model.DB.Model(&model.Instance{}).Count(&instanceTotal)
|
||||
model.DB.Model(&model.Instance{}).Where("status = ?", "online").Count(&instanceOnline)
|
||||
model.DB.Model(&model.User{}).Count(&userTotal)
|
||||
model.DB.Model(&model.QueryHistory{}).Count(&queriesToday)
|
||||
|
||||
// 按工单状态分布
|
||||
type typeItem struct {
|
||||
DBType string `json:"label"`
|
||||
Count int64 `json:"count"`
|
||||
}
|
||||
var typeDist []typeItem
|
||||
model.DB.Model(&model.Ticket{}).Select("db_type as db_type, count(*) as count").Group("db_type").Scan(&typeDist)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"ticket_count": ticketCount,
|
||||
"query_count": queryCount,
|
||||
"instance_count": instanceCount,
|
||||
"ticket_total": ticketTotal,
|
||||
"ticket_pending": ticketPending,
|
||||
"ticket_month": ticketTotal, // 简化
|
||||
"ticket_executing": ticketExecuting,
|
||||
"instance_total": instanceTotal,
|
||||
"instance_online": instanceOnline,
|
||||
"user_total": userTotal,
|
||||
"queries_today": queriesToday,
|
||||
"type_distribution": typeDist,
|
||||
})
|
||||
}
|
||||
|
||||
// GET /api/v1/dashboard/my-tickets
|
||||
func MyTickets(c *gin.Context) {
|
||||
userID, _, role := middleware.GetCurrentUser(c)
|
||||
|
||||
|
||||
var tickets []model.Ticket
|
||||
query := model.DB.Order("id DESC").Limit(10)
|
||||
|
||||
|
||||
switch role {
|
||||
case "admin":
|
||||
// Admin sees all pending/executing tickets
|
||||
query = query.Where("status IN ?", []string{"pending", "reviewing", "executing"})
|
||||
case "dba", "auditor":
|
||||
// DBA sees their own + pending approval
|
||||
query = query.Where(
|
||||
"(applicant_id = ? AND status IN ?) OR (id IN (SELECT ticket_id FROM ticket_approvals WHERE approver_id = ?) AND status IN ?)",
|
||||
userID, []string{"pending", "executing"},
|
||||
userID, []string{"pending", "reviewing", "executing"},
|
||||
)
|
||||
default: // developer
|
||||
default:
|
||||
query = query.Where("applicant_id = ?", userID)
|
||||
}
|
||||
|
||||
|
||||
query.Find(&tickets)
|
||||
|
||||
|
||||
type ticketWithRelation struct {
|
||||
model.Ticket
|
||||
Relation string `json:"relation"` // "submitted" or "pending_approval"
|
||||
Relation string `json:"relation"`
|
||||
}
|
||||
|
||||
|
||||
var result []ticketWithRelation
|
||||
for _, t := range tickets {
|
||||
rel := "submitted"
|
||||
@@ -60,7 +79,7 @@ func MyTickets(c *gin.Context) {
|
||||
}
|
||||
result = append(result, ticketWithRelation{Ticket: t, Relation: rel})
|
||||
}
|
||||
|
||||
|
||||
if result == nil {
|
||||
result = []ticketWithRelation{}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user