forked from JointCloud/pcm-coordinator
任务列表添加类型筛选
This commit is contained in:
parent
30f77b4ee1
commit
915155fe8f
|
@ -406,7 +406,9 @@ type (
|
|||
}
|
||||
|
||||
pageTaskReq {
|
||||
UserId int64 `form:"userId,optional"`
|
||||
Name string `form:"name,optional"`
|
||||
Type string `form:"type,optional"`
|
||||
PageInfo
|
||||
}
|
||||
|
||||
|
@ -430,6 +432,7 @@ type (
|
|||
UpdatedTime string `json:"updatedTime,omitempty" db:"updated_time"`
|
||||
AdapterTypeDict string `json:"adapterTypeDict" db:"adapter_type_dict" gorm:"adapter_type_dict"` //适配器类型(对应字典表的值
|
||||
TaskTypeDict string `json:"taskTypeDict" db:"task_type_dict" gorm:"task_type_dict"` //任务类型(对应字典表的值
|
||||
UserId int64 `json:"userId,omitempty" db:"user_id"`
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
@ -5,7 +5,9 @@ import (
|
|||
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/core"
|
||||
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
|
||||
"gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
|
||||
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
|
||||
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
|
||||
"k8s.io/apimachinery/pkg/util/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
@ -16,6 +18,11 @@ func PageListTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|||
result.ParamErrorResult(r, w, err)
|
||||
return
|
||||
}
|
||||
// 获取用户信息
|
||||
userStr := r.Header.Get("User")
|
||||
user := &models.JccUserInfo{}
|
||||
json.Unmarshal([]byte(userStr), user)
|
||||
req.UserId = user.Id
|
||||
l := core.NewPageListTaskLogic(r.Context(), svcCtx)
|
||||
resp, err := l.PageListTask(&req)
|
||||
result.HttpResult(r, w, resp, err)
|
||||
|
|
|
@ -27,27 +27,35 @@ func NewPageListTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Page
|
|||
}
|
||||
|
||||
func (l *PageListTaskLogic) PageListTask(req *types.PageTaskReq) (resp *types.PageResult, err error) {
|
||||
res := &types.PageResult{}
|
||||
// 检查请求参数的有效性
|
||||
if req.PageSize <= 0 || req.PageNum <= 0 {
|
||||
return nil, result.NewDefaultError("Invalid page size or page number")
|
||||
}
|
||||
|
||||
limit := req.PageSize
|
||||
offset := req.PageSize * (req.PageNum - 1)
|
||||
resp = &types.PageResult{}
|
||||
var list []*types.TaskModel
|
||||
db := l.svcCtx.DbEngin.Model(&types.TaskModel{}).Table("task")
|
||||
|
||||
// 构建数据库查询
|
||||
db := l.svcCtx.DbEngin.Model(&types.TaskModel{}).Table("task")
|
||||
db = db.Where("user_id = ?", req.UserId)
|
||||
db = db.Where("deleted_at is null")
|
||||
if req.Name != "" {
|
||||
db = db.Where("name LIKE ?", "%"+req.Name+"%")
|
||||
}
|
||||
|
||||
//count total
|
||||
var total int64
|
||||
err = db.Count(&total).Error
|
||||
db.Limit(limit).Offset(offset)
|
||||
|
||||
if err != nil {
|
||||
return resp, err
|
||||
if req.Type != "" {
|
||||
db = db.Where("adapter_type_dict = ?", req.Type)
|
||||
}
|
||||
err = db.Order("created_time desc").Find(&list).Error
|
||||
if err != nil {
|
||||
|
||||
// 计算总数
|
||||
var total int64
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
return nil, result.NewDefaultError(err.Error())
|
||||
}
|
||||
|
||||
// 查询任务列表
|
||||
if err := db.Limit(limit).Offset(offset).Order("created_time desc").Find(&list).Error; err != nil {
|
||||
return nil, result.NewDefaultError(err.Error())
|
||||
}
|
||||
|
||||
|
@ -55,21 +63,29 @@ func (l *PageListTaskLogic) PageListTask(req *types.PageTaskReq) (resp *types.Pa
|
|||
go status.UpdateTaskStatus(l.svcCtx, list)
|
||||
go status.UpdateAiTaskStatus(l.svcCtx, list)
|
||||
|
||||
// 计算每个任务的运行时间
|
||||
for _, model := range list {
|
||||
if model.StartTime != "" && model.EndTime == "" {
|
||||
startTime := timeutils.TimeStringToGoTime(model.StartTime)
|
||||
model.RunningTime = int64(time.Now().Sub(startTime).Seconds())
|
||||
}
|
||||
if model.StartTime != "" && model.EndTime != "" {
|
||||
startTime := timeutils.TimeStringToGoTime(model.StartTime)
|
||||
endTime := timeutils.TimeStringToGoTime(model.EndTime)
|
||||
model.RunningTime = int64(endTime.Sub(startTime).Seconds())
|
||||
}
|
||||
model.RunningTime = calculateRunningTime(model.StartTime, model.EndTime)
|
||||
}
|
||||
resp.List = &list
|
||||
resp.PageSize = req.PageSize
|
||||
resp.PageNum = req.PageNum
|
||||
resp.Total = total
|
||||
|
||||
return
|
||||
// 填充响应数据
|
||||
res.List = &list
|
||||
res.PageSize = req.PageSize
|
||||
res.PageNum = req.PageNum
|
||||
res.Total = total
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// calculateRunningTime 计算任务的运行时间
|
||||
func calculateRunningTime(startTimeStr, endTimeStr string) int64 {
|
||||
if startTimeStr == "" {
|
||||
return 0
|
||||
}
|
||||
startTime := timeutils.TimeStringToGoTime(startTimeStr)
|
||||
if endTimeStr == "" {
|
||||
return int64(time.Now().Sub(startTime).Seconds())
|
||||
}
|
||||
endTime := timeutils.TimeStringToGoTime(endTimeStr)
|
||||
return int64(endTime.Sub(startTime).Seconds())
|
||||
}
|
||||
|
|
|
@ -349,7 +349,9 @@ type Task struct {
|
|||
}
|
||||
|
||||
type PageTaskReq struct {
|
||||
Name string `form:"name,optional"`
|
||||
UserId int64 `form:"userId,optional"`
|
||||
Name string `form:"name,optional"`
|
||||
Type string `form:"type,optional"`
|
||||
PageInfo
|
||||
}
|
||||
|
||||
|
@ -373,6 +375,7 @@ type TaskModel struct {
|
|||
UpdatedTime string `json:"updatedTime,omitempty" db:"updated_time"`
|
||||
AdapterTypeDict string `json:"adapterTypeDict" db:"adapter_type_dict" gorm:"adapter_type_dict"` //适配器类型(对应字典表的值
|
||||
TaskTypeDict string `json:"taskTypeDict" db:"task_type_dict" gorm:"task_type_dict"` //任务类型(对应字典表的值
|
||||
UserId int64 `json:"userId,omitempty" db:"user_id"`
|
||||
}
|
||||
|
||||
type TaskDetailReq struct {
|
||||
|
|
Loading…
Reference in New Issue