From 2d7aa2c23d3323e7e1b7d7b7746f558084a5d342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=AD=90=E9=BE=99?= Date: Sun, 2 Jul 2023 19:35:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E7=AE=A1=E7=90=86=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bean/vo/request/task_history.go | 5 + src/bean/vo/request/task_manage.go | 7 +- src/bean/vo/response/host_manage.go | 14 +-- src/bean/vo/response/task_history.go | 32 ++++++ src/bean/vo/response/task_manage.go | 34 ++++--- src/controller/task_history.go | 53 +++++++++- src/router/automatedmaintenrouter.go | 4 +- src/service/task_history.go | 141 +++++++++++++++++++++++++++ src/service/task_manage.go | 43 ++++++-- 9 files changed, 295 insertions(+), 38 deletions(-) diff --git a/src/bean/vo/request/task_history.go b/src/bean/vo/request/task_history.go index 725b8fc..4b7db6b 100644 --- a/src/bean/vo/request/task_history.go +++ b/src/bean/vo/request/task_history.go @@ -1 +1,6 @@ package request + +type TaskHistoryReq struct { + TaskId int `json:"task_id" form:"task_id" binding:"required"` // 主键ID + Pagination +} diff --git a/src/bean/vo/request/task_manage.go b/src/bean/vo/request/task_manage.go index fae25e2..b030dde 100644 --- a/src/bean/vo/request/task_manage.go +++ b/src/bean/vo/request/task_manage.go @@ -25,9 +25,10 @@ type DetailsTaskManageReq struct { } type ListTaskManageReq struct { - Search string `json:"search" form:"search"` //关键词 - CreateDateFrom string `json:"create_date_from" form:"createDateFrom"` //创建时间从 - CreateDateTo string `json:"create_date_to" form:"createDateTo"` //创建时间至 + Search string `json:"search" form:"search"` //关键词 + CreateDateFrom string `json:"create_date_from" form:"create_date_from"` //创建时间从 + CreateDateTo string `json:"create_date_to" form:"create_date_to"` //创建时间至 + HostGroupId int `json:"host_group_id" form:"host_group_id"` //主机分组ID Pagination } diff --git a/src/bean/vo/response/host_manage.go b/src/bean/vo/response/host_manage.go index 8ad6dd4..c659c63 100644 --- a/src/bean/vo/response/host_manage.go +++ b/src/bean/vo/response/host_manage.go @@ -26,13 +26,13 @@ type HostManage struct { } type HostList struct { - Id int `json:"id"` // id - Ip string `json:"ip"` // ip - Port string `json:"port"` // 端口 - VoucherType int `json:"voucher_type"` // 凭证类型(0密码验证 密钥验证) - UserName string `json:"user_name"` // 用户名 - Password string `json:"password"` // 密码 - HostFileUrl string `json:"host_file_url"` // 主机文件url + Id int `json:"id"` // id + Ip string `json:"ip"` // ip + Port string `json:"port"` // 端口 + VoucherType int `json:"voucher_type"` // 凭证类型(0密码验证 密钥验证) + UserName string `json:"user_name"` // 用户名 + Password string `json:"password"` // 密码 + HostFileUrl string `json:"-"` // 主机文件url } type TaskList struct { diff --git a/src/bean/vo/response/task_history.go b/src/bean/vo/response/task_history.go index a467149..5606d6b 100644 --- a/src/bean/vo/response/task_history.go +++ b/src/bean/vo/response/task_history.go @@ -1 +1,33 @@ 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"` // 执行日志 +} diff --git a/src/bean/vo/response/task_manage.go b/src/bean/vo/response/task_manage.go index cfdf685..b231330 100644 --- a/src/bean/vo/response/task_manage.go +++ b/src/bean/vo/response/task_manage.go @@ -2,18 +2,26 @@ package response 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 { - Id int `json:"id" xorm:"pk autoincr" ` // id - TaskName string `json:"task_name" xorm:"task_name"` // 任务名称 - TaskDesc string `json:"task_desc" xorm:"task_desc"` // 任务描述 - YamlDesc string `json:"yaml_desc" xorm:"yaml_desc"` // yaml文件 - YamlUrl string `json:"yaml_url" xorm:"yaml_url"` // yaml文件地址 - ExecCnt int `json:"exec_cnt" xorm:"exec_cnt"` // 执行次数 - SuccessCnt int `json:"success_cnt" xorm:"success_cnt"` // 执行成功次数 - FailCnt int `json:"fail_cnt" xorm:"fail_cnt"` // 执行失败次数 - HostGroupId int `json:"host_group_id" xorm:"host_group_id"` // 主机分组ID - CreateUser string `json:"create_user" xorm:"create_user" ` // 创建人 - CreateTime time.Time `json:"create_time" xorm:"create_time"` // 创建时间 - UpdateUser string `json:"update_user" xorm:"update_user" ` // 修改人 - UpdateTime time.Time `json:"update_time" xorm:"update_time"` // 修改时间 + Id int `json:"id"` // id + TaskName string `json:"task_name"` // 任务名称 + TaskDesc string `json:"task_desc"` // 任务描述 + YamlDesc string `json:"yaml_desc"` // yaml文件 + YamlUrl string `json:"yaml_url"` // yaml文件地址 + ExecCnt int `json:"exec_cnt"` // 执行次数 + SuccessCnt int `json:"success_cnt"` // 执行成功次数 + FailCnt int `json:"fail_cnt"` // 执行失败次数 + HostList []HostList `json:"host_list" xorm:"-"` // 主机列表 + HostFileUrl string `json:"host_file_url" xorm:"-"` // 主机文件url + CreateUser string `json:"create_user"` // 创建人 + CreateTime time.Time `json:"create_time"` // 创建时间 } diff --git a/src/controller/task_history.go b/src/controller/task_history.go index c436693..c52494c 100644 --- a/src/controller/task_history.go +++ b/src/controller/task_history.go @@ -1,25 +1,68 @@ package controller import ( + "errors" "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/pkg/beagle/resp" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/service" ) -// TaskHistoryList 任务历史 +// TaskHistoryList 任务历史列表 func TaskHistoryList(c *gin.Context) { - var req request.ListTaskManageReq - if err := c.ShouldBindJSON(&req); err != nil { + var req request.Pagination + if err := c.ShouldBind(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) return } - taskManageSvc := service.TaskManageSvc{} - total, list, err := taskManageSvc.ListTaskManage(req) + taskHistorySvc := service.TaskHistorySvc{} + total, list, err := taskHistorySvc.TaskHistoryList(req) if err != nil { SendJsonPageResponse(c, err, nil, 0) return } 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) +} diff --git a/src/router/automatedmaintenrouter.go b/src/router/automatedmaintenrouter.go index a8bdfd3..7f73efd 100644 --- a/src/router/automatedmaintenrouter.go +++ b/src/router/automatedmaintenrouter.go @@ -24,7 +24,9 @@ func InitAutomatedMaintenRouter(e *gin.Engine) { //任务历史 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) // 任务执行日志 } //主机管理 diff --git a/src/service/task_history.go b/src/service/task_history.go index 6d43c33..8b5b430 100644 --- a/src/service/task_history.go +++ b/src/service/task_history.go @@ -1 +1,142 @@ 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 +} diff --git a/src/service/task_manage.go b/src/service/task_manage.go index c63329f..2c7edbe 100644 --- a/src/service/task_manage.go +++ b/src/service/task_manage.go @@ -110,37 +110,62 @@ func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskMa 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 { err = resp.DbSelectError.WithError(err) return } + for _, v := range hostList { + if v.HostFileUrl != "" { + taskManageRes.HostFileUrl = v.HostFileUrl + } + + } + taskManageRes.HostList = hostList return } // 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() if err != nil { err = resp.DbConnectError.WithError(err) 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 != "" { - 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+"%")) } if req.CreateDateFrom != "" { - finder.Where("create_time >= ?", req.CreateDateFrom) + finder.Where("tm.create_time >= ?", req.CreateDateFrom) } 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 { err = resp.DbSelectError.WithError(err) return -- 2.26.0