chore: make codes the repository root
This commit is contained in:
40
internal/dashboard/handler.go
Normal file
40
internal/dashboard/handler.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package dashboard
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/auth"
|
||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/model"
|
||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Handler struct{ db *gorm.DB }
|
||||
|
||||
func NewHandler(db *gorm.DB) *Handler { return &Handler{db: db} }
|
||||
|
||||
func (h *Handler) Summary(c *gin.Context) {
|
||||
p, _ := auth.PrincipalFromContext(c)
|
||||
counts := map[string]int64{}
|
||||
queries := []struct {
|
||||
key string
|
||||
model interface{}
|
||||
where string
|
||||
args []interface{}
|
||||
}{
|
||||
{key: "scenarios", model: &model.Scenario{}, where: "tenant_id = ? AND status <> ?", args: []interface{}{p.TenantID, "archived"}},
|
||||
{key: "published_sops", model: &model.SOP{}, where: "tenant_id = ? AND status = ?", args: []interface{}{p.TenantID, "published"}},
|
||||
{key: "runs", model: &model.SOPRun{}, where: "tenant_id = ?", args: []interface{}{p.TenantID}},
|
||||
{key: "completed_runs", model: &model.SOPRun{}, where: "tenant_id = ? AND status = ?", args: []interface{}{p.TenantID, "completed"}},
|
||||
}
|
||||
for _, query := range queries {
|
||||
var count int64
|
||||
if err := h.db.Model(query.model).Where(query.where, query.args...).Count(&count).Error; err != nil {
|
||||
response.Error(c, http.StatusInternalServerError, "QUERY_FAILED", "查询统计数据失败")
|
||||
return
|
||||
}
|
||||
counts[query.key] = count
|
||||
}
|
||||
response.OK(c, counts)
|
||||
}
|
||||
Reference in New Issue
Block a user