You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

task_status_statistics.go 4.4 kB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package xjlab
  2. import (
  3. "context"
  4. "github.com/zeromicro/go-zero/core/logx"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
  8. )
  9. // 任务状态常量定义
  10. const (
  11. StatusCompleted = "Completed"
  12. StatusFailed = "Failed"
  13. StatusRunning = "Running"
  14. StatusSaved = "Saved"
  15. StatusStopped = "Stopped"
  16. StatusSucceeded = "Succeeded"
  17. StatusUndefined = "undefined"
  18. StatusWaiting = "Waiting"
  19. StatusWaitRestart = "WaitRestart"
  20. )
  21. type TaskStatusStatistics struct {
  22. logx.Logger
  23. ctx context.Context
  24. svcCtx *svc.ServiceContext
  25. }
  26. func NewTaskStatusStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TaskStatusStatistics {
  27. return &TaskStatusStatistics{
  28. Logger: logx.WithContext(ctx),
  29. ctx: ctx,
  30. svcCtx: svcCtx,
  31. }
  32. }
  33. // TaskStatus 任务状态统计结构体
  34. type TaskStatus struct {
  35. // 总数
  36. Total int64 `json:"total"`
  37. // 按状态分类统计
  38. Completed int64 `json:"completed"` // 已完成
  39. Failed int64 `json:"failed"` // 失败
  40. Running int64 `json:"running"` // 运行中
  41. Saved int64 `json:"saved"` // 已保存
  42. Stopped int64 `json:"stopped"` // 已停止
  43. Succeeded int64 `json:"succeeded"` // 成功
  44. Undefined int64 `json:"undefined"` // 未定义
  45. Waiting int64 `json:"waiting"` // 等待中
  46. WaitRestart int64 `json:"waitRestart"` // 等待重启
  47. // 业务分类统计
  48. NormalCount int64 `json:"normalCount"` // 正常任务数 (Completed + Succeeded + Saved + Running + Waiting + WaitRestart)
  49. ErrorCount int64 `json:"errorCount"` // 异常任务数 (Failed + Stopped + Undefined)
  50. }
  51. // StatusResult 数据库查询结果结构体
  52. type StatusResult struct {
  53. Status string `gorm:"column:status" json:"status"`
  54. Count int64 `gorm:"column:count" json:"count"`
  55. }
  56. // GetTaskStatusStatistics 获取任务状态统计
  57. func (l *TaskStatusStatistics) GetTaskStatusStatistics(req *types.XJLABCommonReq) (*TaskStatus, error) {
  58. // 构建数据库查询
  59. db := l.svcCtx.DbEngin.Model(&types.TaskModel{}).
  60. Table("task").
  61. Where("deleted_at IS NULL")
  62. // 查询状态统计
  63. var results []StatusResult
  64. err := db.
  65. Select("status, COUNT(*) as count").
  66. Group("status").
  67. Find(&results).Error
  68. if err != nil {
  69. l.Errorf("Failed to query task status statistics: %v", err)
  70. return nil, result.NewDefaultError("Failed to get task status statistics")
  71. }
  72. // 初始化统计结构
  73. stats := &TaskStatus{}
  74. // 填充统计数据
  75. for _, r := range results {
  76. stats.Total += r.Count
  77. l.categorizeTaskStatus(stats, r.Status, r.Count)
  78. }
  79. l.Infof("Task status statistics retrieved successfully. Total: %d, Normal: %d, Error: %d",
  80. stats.Total, stats.NormalCount, stats.ErrorCount)
  81. return stats, nil
  82. }
  83. // categorizeTaskStatus 根据状态分类统计数据
  84. func (l *TaskStatusStatistics) categorizeTaskStatus(stats *TaskStatus, status string, count int64) {
  85. switch status {
  86. case StatusCompleted:
  87. stats.Completed = count
  88. stats.NormalCount += count
  89. case StatusFailed:
  90. stats.Failed = count
  91. stats.ErrorCount += count
  92. case StatusRunning:
  93. stats.Running = count
  94. stats.NormalCount += count
  95. case StatusSaved:
  96. stats.Saved = count
  97. stats.NormalCount += count
  98. case StatusStopped:
  99. stats.Stopped = count
  100. stats.ErrorCount += count
  101. case StatusSucceeded:
  102. stats.Succeeded = count
  103. stats.NormalCount += count
  104. case StatusUndefined:
  105. stats.Undefined = count
  106. stats.ErrorCount += count
  107. case StatusWaiting:
  108. stats.Waiting = count
  109. stats.NormalCount += count
  110. case StatusWaitRestart:
  111. stats.WaitRestart = count
  112. stats.NormalCount += count
  113. default:
  114. // 记录未知状态
  115. l.Logger.Errorf("Unknown task status encountered: %s with count: %d", status, count)
  116. stats.ErrorCount += count
  117. }
  118. }
  119. // GetSimpleTaskStatistics 获取简化的任务统计数据
  120. func (l *TaskStatusStatistics) GetSimpleTaskStatistics(req *types.XJLABCommonReq) (*map[string]interface{}, error) {
  121. // 获取完整统计
  122. resp, err := l.GetTaskStatusStatistics(req)
  123. if err != nil {
  124. return nil, err
  125. }
  126. simpleStats := map[string]interface{}{
  127. "totalCount": resp.Total, // 任务总数
  128. "normalCount": resp.NormalCount, // 正常任务数
  129. "errorCount": resp.ErrorCount, // 任务告警数
  130. }
  131. return &simpleStats, nil
  132. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.