62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/access"
|
|
"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) {
|
|
principal, _ := auth.PrincipalFromContext(c)
|
|
counts := map[string]int64{}
|
|
|
|
scenarioQuery := access.ScopeScenarios(h.db.Model(&model.Scenario{}), principal, "scenarios").Where("scenarios.status <> ?", "archived")
|
|
var scenarios int64
|
|
if err := scenarioQuery.Count(&scenarios).Error; err != nil {
|
|
queryFailed(c)
|
|
return
|
|
}
|
|
counts["scenarios"] = scenarios
|
|
|
|
sopQuery := h.db.Table("sops s").Joins("JOIN scenarios sc ON sc.id = s.scenario_id")
|
|
sopQuery = access.ScopeScenarios(sopQuery, principal, "sc").Where("s.status = ?", "published")
|
|
var publishedSOPs int64
|
|
if err := sopQuery.Count(&publishedSOPs).Error; err != nil {
|
|
queryFailed(c)
|
|
return
|
|
}
|
|
counts["published_sops"] = publishedSOPs
|
|
|
|
runQuery := h.db.Model(&model.SOPRun{}).Where("tenant_id = ?", principal.TenantID)
|
|
if !auth.HasPermission(principal, "runs.view_all") && !auth.HasPermission(principal, "*") {
|
|
runQuery = runQuery.Where("operator_id = ?", principal.UserID)
|
|
}
|
|
var runs int64
|
|
if err := runQuery.Count(&runs).Error; err != nil {
|
|
queryFailed(c)
|
|
return
|
|
}
|
|
counts["runs"] = runs
|
|
var completedRuns int64
|
|
if err := runQuery.Where("status = ?", "completed").Count(&completedRuns).Error; err != nil {
|
|
queryFailed(c)
|
|
return
|
|
}
|
|
counts["completed_runs"] = completedRuns
|
|
|
|
response.OK(c, counts)
|
|
}
|
|
|
|
func queryFailed(c *gin.Context) {
|
|
response.Error(c, http.StatusInternalServerError, "QUERY_FAILED", "查询统计数据失败")
|
|
}
|