|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package xjlab
-
- import (
- "context"
-
- "github.com/zeromicro/go-zero/core/logx"
- "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
- "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
- "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
- )
-
- // 任务状态常量定义
- const (
- StatusCompleted = "Completed"
- StatusFailed = "Failed"
- StatusRunning = "Running"
- StatusSaved = "Saved"
- StatusStopped = "Stopped"
- StatusSucceeded = "Succeeded"
- StatusUndefined = "undefined"
- StatusWaiting = "Waiting"
- StatusWaitRestart = "WaitRestart"
- )
-
- type TaskStatusStatistics struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
-
- func NewTaskStatusStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TaskStatusStatistics {
- return &TaskStatusStatistics{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
-
- // TaskStatus 任务状态统计结构体
- type TaskStatus struct {
- // 总数
- Total int64 `json:"total"`
-
- // 按状态分类统计
- Completed int64 `json:"completed"` // 已完成
- Failed int64 `json:"failed"` // 失败
- Running int64 `json:"running"` // 运行中
- Saved int64 `json:"saved"` // 已保存
- Stopped int64 `json:"stopped"` // 已停止
- Succeeded int64 `json:"succeeded"` // 成功
- Undefined int64 `json:"undefined"` // 未定义
- Waiting int64 `json:"waiting"` // 等待中
- WaitRestart int64 `json:"waitRestart"` // 等待重启
-
- // 业务分类统计
- NormalCount int64 `json:"normalCount"` // 正常任务数 (Completed + Succeeded + Saved + Running + Waiting + WaitRestart)
- ErrorCount int64 `json:"errorCount"` // 异常任务数 (Failed + Stopped + Undefined)
- }
-
- // StatusResult 数据库查询结果结构体
- type StatusResult struct {
- Status string `gorm:"column:status" json:"status"`
- Count int64 `gorm:"column:count" json:"count"`
- }
-
- // GetTaskStatusStatistics 获取任务状态统计
- func (l *TaskStatusStatistics) GetTaskStatusStatistics(req *types.XJLABCommonReq) (*TaskStatus, error) {
- // 构建数据库查询
- db := l.svcCtx.DbEngin.Model(&types.TaskModel{}).
- Table("task").
- Where("deleted_at IS NULL")
-
- // 查询状态统计
- var results []StatusResult
- err := db.
- Select("status, COUNT(*) as count").
- Group("status").
- Find(&results).Error
-
- if err != nil {
- l.Errorf("Failed to query task status statistics: %v", err)
- return nil, result.NewDefaultError("Failed to get task status statistics")
- }
-
- // 初始化统计结构
- stats := &TaskStatus{}
-
- // 填充统计数据
- for _, r := range results {
- stats.Total += r.Count
- l.categorizeTaskStatus(stats, r.Status, r.Count)
- }
-
- l.Infof("Task status statistics retrieved successfully. Total: %d, Normal: %d, Error: %d",
- stats.Total, stats.NormalCount, stats.ErrorCount)
-
- return stats, nil
- }
-
- // categorizeTaskStatus 根据状态分类统计数据
- func (l *TaskStatusStatistics) categorizeTaskStatus(stats *TaskStatus, status string, count int64) {
- switch status {
- case StatusCompleted:
- stats.Completed = count
- stats.NormalCount += count
- case StatusFailed:
- stats.Failed = count
- stats.ErrorCount += count
- case StatusRunning:
- stats.Running = count
- stats.NormalCount += count
- case StatusSaved:
- stats.Saved = count
- stats.NormalCount += count
- case StatusStopped:
- stats.Stopped = count
- stats.ErrorCount += count
- case StatusSucceeded:
- stats.Succeeded = count
- stats.NormalCount += count
- case StatusUndefined:
- stats.Undefined = count
- stats.ErrorCount += count
- case StatusWaiting:
- stats.Waiting = count
- stats.NormalCount += count
- case StatusWaitRestart:
- stats.WaitRestart = count
- stats.NormalCount += count
- default:
- // 记录未知状态
- l.Logger.Errorf("Unknown task status encountered: %s with count: %d", status, count)
- stats.ErrorCount += count
- }
- }
-
- // GetSimpleTaskStatistics 获取简化的任务统计数据
- func (l *TaskStatusStatistics) GetSimpleTaskStatistics(req *types.XJLABCommonReq) (*map[string]interface{}, error) {
- // 获取完整统计
- resp, err := l.GetTaskStatusStatistics(req)
- if err != nil {
- return nil, err
- }
-
- simpleStats := map[string]interface{}{
- "totalCount": resp.Total, // 任务总数
- "normalCount": resp.NormalCount, // 正常任务数
- "errorCount": resp.ErrorCount, // 任务告警数
- }
-
- return &simpleStats, nil
- }
|