Commit 2d7aa2c2 authored by 陈子龙's avatar 陈子龙

任务管理 fix

parent 7d952a14
package request package request
type TaskHistoryReq struct {
TaskId int `json:"task_id" form:"task_id" binding:"required"` // 主键ID
Pagination
}
...@@ -25,9 +25,10 @@ type DetailsTaskManageReq struct { ...@@ -25,9 +25,10 @@ type DetailsTaskManageReq struct {
} }
type ListTaskManageReq struct { type ListTaskManageReq struct {
Search string `json:"search" form:"search"` //关键词 Search string `json:"search" form:"search"` //关键词
CreateDateFrom string `json:"create_date_from" form:"createDateFrom"` //创建时间从 CreateDateFrom string `json:"create_date_from" form:"create_date_from"` //创建时间从
CreateDateTo string `json:"create_date_to" form:"createDateTo"` //创建时间至 CreateDateTo string `json:"create_date_to" form:"create_date_to"` //创建时间至
HostGroupId int `json:"host_group_id" form:"host_group_id"` //主机分组ID
Pagination Pagination
} }
......
...@@ -26,13 +26,13 @@ type HostManage struct { ...@@ -26,13 +26,13 @@ type HostManage struct {
} }
type HostList struct { type HostList struct {
Id int `json:"id"` // id Id int `json:"id"` // id
Ip string `json:"ip"` // ip Ip string `json:"ip"` // ip
Port string `json:"port"` // 端口 Port string `json:"port"` // 端口
VoucherType int `json:"voucher_type"` // 凭证类型(0密码验证 密钥验证) VoucherType int `json:"voucher_type"` // 凭证类型(0密码验证 密钥验证)
UserName string `json:"user_name"` // 用户名 UserName string `json:"user_name"` // 用户名
Password string `json:"password"` // 密码 Password string `json:"password"` // 密码
HostFileUrl string `json:"host_file_url"` // 主机文件url HostFileUrl string `json:"-"` // 主机文件url
} }
type TaskList struct { type TaskList struct {
......
package response package response
import "time"
type TaskHistoryListRes struct {
State int `json:"state"` // 状态(0执行中 1成功 2失败)
TaskId int `json:"task_id"` // 任务id
TaskName string `json:"task_name" ` // 任务名称
ExecStartTime time.Time `json:"exec_start_time"` // 执行开始时间
ExecEndTime time.Time `json:"exec_end_time"` // 执行结束时间
ExecTime int `json:"exec_time" xorm:"-"` // 执行耗时
CreateUser string `json:"create_user" ` // 操作人
}
type TaskInfoListRes struct {
Id int `json:"id"` // 主键id
TaskId int `json:"task_id"` // 任务id
ExecStartTime time.Time `json:"exec_start_time" ` // 执行开始时间
ExecEndTime time.Time `json:"exec_end_time"` // 执行结束时间
ExecTime int `json:"exec_time" xorm:"-"` // 执行耗时
CreateUser string `json:"create_user"` // 操作人
ExecDesc string `json:"exec_desc"` // 执行说明
State int `json:"state"` // 状态(0执行中 1成功 2失败)
}
type TaskExecLogRes struct {
Id int `json:"id"` // 主键id
ExecStartTime time.Time `json:"exec_start_time" ` // 执行开始时间
ExecEndTime time.Time `json:"exec_end_time"` // 执行结束时间
ExecTime int `json:"exec_time" xorm:"-"` // 执行耗时
State int `json:"state"` // 状态(0执行中 1成功 2失败)
ExecLog string `json:"exec_log"` // 执行日志
}
...@@ -2,18 +2,26 @@ package response ...@@ -2,18 +2,26 @@ package response
import "time" import "time"
type TaskManageListRes struct {
Id int `json:"id" xorm:"pk autoincr" ` // id
TaskName string `json:"task_name" xorm:"task_name"` // 任务名称
TaskDesc string `json:"task_desc" xorm:"task_desc"` // 任务描述
ExecCnt int `json:"exec_cnt" xorm:"exec_cnt"` // 执行次数
CreateUser string `json:"create_user" xorm:"create_user" ` // 创建人
CreateTime time.Time `json:"create_time" xorm:"create_time"` // 创建时间
}
type TaskManageRes struct { type TaskManageRes struct {
Id int `json:"id" xorm:"pk autoincr" ` // id Id int `json:"id"` // id
TaskName string `json:"task_name" xorm:"task_name"` // 任务名称 TaskName string `json:"task_name"` // 任务名称
TaskDesc string `json:"task_desc" xorm:"task_desc"` // 任务描述 TaskDesc string `json:"task_desc"` // 任务描述
YamlDesc string `json:"yaml_desc" xorm:"yaml_desc"` // yaml文件 YamlDesc string `json:"yaml_desc"` // yaml文件
YamlUrl string `json:"yaml_url" xorm:"yaml_url"` // yaml文件地址 YamlUrl string `json:"yaml_url"` // yaml文件地址
ExecCnt int `json:"exec_cnt" xorm:"exec_cnt"` // 执行次数 ExecCnt int `json:"exec_cnt"` // 执行次数
SuccessCnt int `json:"success_cnt" xorm:"success_cnt"` // 执行成功次数 SuccessCnt int `json:"success_cnt"` // 执行成功次数
FailCnt int `json:"fail_cnt" xorm:"fail_cnt"` // 执行失败次数 FailCnt int `json:"fail_cnt"` // 执行失败次数
HostGroupId int `json:"host_group_id" xorm:"host_group_id"` // 主机分组ID HostList []HostList `json:"host_list" xorm:"-"` // 主机列表
CreateUser string `json:"create_user" xorm:"create_user" ` // 创建人 HostFileUrl string `json:"host_file_url" xorm:"-"` // 主机文件url
CreateTime time.Time `json:"create_time" xorm:"create_time"` // 创建时间 CreateUser string `json:"create_user"` // 创建人
UpdateUser string `json:"update_user" xorm:"update_user" ` // 修改人 CreateTime time.Time `json:"create_time"` // 创建时间
UpdateTime time.Time `json:"update_time" xorm:"update_time"` // 修改时间
} }
package controller package controller
import ( import (
"errors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/spf13/cast"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/service" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/service"
) )
// TaskHistoryList 任务历史 // TaskHistoryList 任务历史列表
func TaskHistoryList(c *gin.Context) { func TaskHistoryList(c *gin.Context) {
var req request.ListTaskManageReq var req request.Pagination
if err := c.ShouldBindJSON(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return return
} }
taskManageSvc := service.TaskManageSvc{} taskHistorySvc := service.TaskHistorySvc{}
total, list, err := taskManageSvc.ListTaskManage(req) total, list, err := taskHistorySvc.TaskHistoryList(req)
if err != nil { if err != nil {
SendJsonPageResponse(c, err, nil, 0) SendJsonPageResponse(c, err, nil, 0)
return return
} }
SendJsonPageResponse(c, resp.OK, list, total) SendJsonPageResponse(c, resp.OK, list, total)
} }
// TaskInfoList 任务历史详情列表
func TaskInfoList(c *gin.Context) {
var req request.TaskHistoryReq
if err := c.ShouldBind(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
taskHistorySvc := service.TaskHistorySvc{}
total, list, err := taskHistorySvc.TaskInfoList(req)
if err != nil {
SendJsonPageResponse(c, err, nil, 0)
return
}
SendJsonPageResponse(c, resp.OK, list, total)
}
// TaskExecLog 任务执行日志
func TaskExecLog(c *gin.Context) {
var (
err error
id string
)
if id = c.Query("id"); id == "" {
id = c.Param("id")
}
if id == "" {
SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("id为空")), nil)
return
}
taskHistorySvc := service.TaskHistorySvc{}
data, err := taskHistorySvc.TaskExecLog(cast.ToInt(id))
if err != nil {
SendJsonResponse(c, err, nil)
return
}
SendJsonResponse(c, resp.OK, data)
}
...@@ -24,7 +24,9 @@ func InitAutomatedMaintenRouter(e *gin.Engine) { ...@@ -24,7 +24,9 @@ func InitAutomatedMaintenRouter(e *gin.Engine) {
//任务历史 //任务历史
taskHistory := so.Group("/task_history") taskHistory := so.Group("/task_history")
{ {
taskHistory.GET("/list", controller.TaskHistoryList) // 任务历史 taskHistory.GET("/list", controller.TaskHistoryList) // 任务历史列表
taskHistory.GET("/task_info_list", controller.TaskInfoList) // 任务历史详情列表
taskHistory.GET("/task_exec_log", controller.TaskExecLog) // 任务执行日志
} }
//主机管理 //主机管理
......
package service package service
import (
"github.com/pkg/errors"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"time"
)
type TaskHistorySvc struct {
User *entity.SystemUser
}
// TaskHistoryList 任务历史列表
func (t *TaskHistorySvc) TaskHistoryList(req request.Pagination) (total int64, taskHistoryListRes []response.TaskHistoryListRes, err error) {
var (
//dueDate int
start time.Time
end time.Time
)
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
finder := db.Table("task_manage").Alias("t1").
Join("RIGHT", "(SELECT task_id,exec_start_time,exec_end_time,create_user,state FROM task_history"+
" WHERE ID IN (SELECT MAX(ID) FROM task_history GROUP BY task_id) ORDER BY ID DESC) t2", "t1.id = t2.task_id").
Where("t1.is_delete = 0")
//查询任务历史
total, err = finder.Select("t2.state,t2.task_id,t1.task_name,t2.exec_start_time,t2.exec_end_time,t2.create_user").
Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskHistoryListRes)
if err != nil {
err = resp.DbSelectError.WithError(err)
return
}
for k, v := range taskHistoryListRes {
//获取执行耗时
start, err = time.Parse("2006-01-02 15:04:05", v.ExecStartTime.Format("2006-01-02 15:04:05"))
if err != nil {
err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!"))
return
}
end, err = time.Parse("2006-01-02 15:04:05", v.ExecEndTime.Format("2006-01-02 15:04:05"))
if err != nil {
err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!"))
return
}
execTime := int(end.Sub(start).Seconds())
if execTime > 0 {
taskHistoryListRes[k].ExecTime = execTime
}
}
return
}
// TaskInfoList 任务历史详情列表
func (t *TaskHistorySvc) TaskInfoList(req request.TaskHistoryReq) (total int64, taskInfoListRes []response.TaskInfoListRes, err error) {
var (
//dueDate int
start time.Time
end time.Time
)
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
finder := db.Table("task_history").Where("task_id = ?", req.TaskId)
//查询任务历史
total, err = finder.OrderBy("id desc").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskInfoListRes)
if err != nil {
err = resp.DbSelectError.WithError(err)
return
}
for k, v := range taskInfoListRes {
//获取执行耗时
start, err = time.Parse("2006-01-02 15:04:05", v.ExecStartTime.Format("2006-01-02 15:04:05"))
if err != nil {
err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!"))
return
}
end, err = time.Parse("2006-01-02 15:04:05", v.ExecEndTime.Format("2006-01-02 15:04:05"))
if err != nil {
err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!"))
return
}
execTime := int(end.Sub(start).Seconds())
if execTime > 0 {
taskInfoListRes[k].ExecTime = execTime
}
}
return
}
// TaskExecLog 任务执行日志
func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLogRes, err error) {
var (
//dueDate int
start time.Time
end time.Time
)
db, err := client.GetDbClient()
if err != nil {
err = resp.DbConnectError.WithError(err)
return
}
//查询
_, err = db.Table("task_history").Where("id = ?", id).Get(&taskExecLogRes)
if err != nil {
err = resp.DbSelectError.WithError(err)
return
}
//获取执行耗时
start, err = time.Parse("2006-01-02 15:04:05", taskExecLogRes.ExecStartTime.Format("2006-01-02 15:04:05"))
if err != nil {
err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!"))
return
}
end, err = time.Parse("2006-01-02 15:04:05", taskExecLogRes.ExecEndTime.Format("2006-01-02 15:04:05"))
if err != nil {
err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!"))
return
}
execTime := int(end.Sub(start).Seconds())
if execTime > 0 {
taskExecLogRes.ExecTime = execTime
}
return
}
...@@ -110,37 +110,62 @@ func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskMa ...@@ -110,37 +110,62 @@ func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskMa
return return
} }
//查询任务 //查询任务详情
_, err = db.Table("task_manage").Where("is_delete = 0 AND id = ?", id).Get(&taskManageRes) finder := db.Table("task_manage").Alias("tm").
Where("is_delete = 0 AND id = ?", id)
_, err = finder.Select("tm.id,tm.task_name,tm.task_desc,tm.yaml_desc,tm.yaml_url,tm.create_user,tm.create_time," +
"(select count(1) from task_history th where th.task_id = tm.id) as exec_cnt," +
"(select count(1) from task_history th where th.task_id = tm.id and th.state = 1) as success_cnt," +
"(select count(1) from task_history th where th.task_id = tm.id and th.state = 2) as fail_cnt").Get(&taskManageRes)
if err != nil {
err = resp.DbSelectError.WithError(err)
return
}
//查询主机列表
hostList := make([]response.HostList, 0)
err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", id).Find(&hostList)
if err != nil { if err != nil {
err = resp.DbSelectError.WithError(err) err = resp.DbSelectError.WithError(err)
return return
} }
for _, v := range hostList {
if v.HostFileUrl != "" {
taskManageRes.HostFileUrl = v.HostFileUrl
}
}
taskManageRes.HostList = hostList
return return
} }
// ListTaskManage 任务列表 // ListTaskManage 任务列表
func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int64, taskManageListRes []response.TaskManageRes, err error) { func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int64, taskManageListRes []response.TaskManageListRes, err error) {
db, err := client.GetDbClient() db, err := client.GetDbClient()
if err != nil { if err != nil {
err = resp.DbConnectError.WithError(err) err = resp.DbConnectError.WithError(err)
return return
} }
finder := db.Table("task_manage").Where("is_delete = 0") finder := db.Table("task_manage").Alias("tm").Where("tm.is_delete = 0")
if req.Search != "" { if req.Search != "" {
finder.Where(fmt.Sprintf("(task_name LIKE %s OR task_desc LIKE %s OR create_user LIKE %s)", finder.Where(fmt.Sprintf("(tm.task_name LIKE %s OR tm.task_desc LIKE %s OR tm.create_user LIKE %s)",
"%"+req.Search+"%", "%"+req.Search+"%", "%"+req.Search+"%")) "%"+req.Search+"%", "%"+req.Search+"%", "%"+req.Search+"%"))
} }
if req.CreateDateFrom != "" { if req.CreateDateFrom != "" {
finder.Where("create_time >= ?", req.CreateDateFrom) finder.Where("tm.create_time >= ?", req.CreateDateFrom)
} }
if req.CreateDateTo != "" { if req.CreateDateTo != "" {
finder.Where("create_time <= ?", req.CreateDateTo) finder.Where("tm.create_time <= ?", req.CreateDateTo)
}
if req.HostGroupId != 0 {
finder.Where("tm.host_group_id = ?", req.HostGroupId)
} }
finder.OrderBy("id") finder.OrderBy("tm.id")
//查询任务 //查询任务
total, err = finder.Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskManageListRes) total, err = finder.Select("tm.id,tm.task_name,tm.task_desc,(select count(*) from task_history th "+
"where th.task_id = tm.id group by th.task_id) as exec_cnt,tm.create_user,tm.create_time").
Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskManageListRes)
if err != nil { if err != nil {
err = resp.DbSelectError.WithError(err) err = resp.DbSelectError.WithError(err)
return return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment