Commit a9ef0990 authored by 陈子龙's avatar 陈子龙

Merge branch 'dev-czl' into dev

parents 49660de9 25560e75
...@@ -378,13 +378,12 @@ func (h *HostManageSvc) PageListHostManage(req request.ListHostManageReq) (total ...@@ -378,13 +378,12 @@ func (h *HostManageSvc) PageListHostManage(req request.ListHostManageReq) (total
if req.CreateDateTo != "" { if req.CreateDateTo != "" {
finder.Where("hm.create_time <= ?", req.CreateDateTo) finder.Where("hm.create_time <= ?", req.CreateDateTo)
} }
finder.OrderBy("hm.id")
//查询任务 //查询任务
total, err = finder.Select("hm.id,hm.host_name,hm.create_user,hm.create_time,(SELECT COUNT(*) FROM "+ total, err = finder.Select("hm.id,hm.host_name,hm.create_user,hm.create_time,(SELECT COUNT(*) FROM "+
"task_manage tm WHERE tm.is_delete = 0 AND tm.host_group_id = hm.ID) AS task_cnt,(SELECT COUNT(*) FROM "+ "task_manage tm WHERE tm.is_delete = 0 AND tm.host_group_id = hm.ID) AS task_cnt,(SELECT COUNT(*) FROM "+
"host_manage_list hml WHERE hml.is_delete = 0 AND hml.conn_status = 1 AND hml.host_group_id = hm.ID) AS "+ "host_manage_list hml WHERE hml.is_delete = 0 AND hml.conn_status = 1 AND hml.host_group_id = hm.ID) AS "+
"ip_cnt_err,(SELECT COUNT(*) FROM host_manage_list hml WHERE hml.is_delete = 0 AND hml.host_group_id = hm.ID) AS ip_cnt"). "ip_cnt_err,(SELECT COUNT(*) FROM host_manage_list hml WHERE hml.is_delete = 0 AND hml.host_group_id = hm.ID) AS ip_cnt").
OrderBy("hm.create_time").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&hostManageListRes) OrderBy("hm.create_time desc").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&hostManageListRes)
if err != nil { if err != nil {
err = resp.DbSelectError.WithError(err) err = resp.DbSelectError.WithError(err)
return return
......
...@@ -2,12 +2,16 @@ package service ...@@ -2,12 +2,16 @@ package service
import ( import (
"fmt" "fmt"
json "github.com/json-iterator/go"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cast"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "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/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "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/common/client"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"go.uber.org/zap"
"time" "time"
) )
...@@ -117,6 +121,7 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo ...@@ -117,6 +121,7 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo
) )
db, err := client.GetDbClient() db, err := client.GetDbClient()
if err != nil { if err != nil {
conf.Logger.Error("db err", zap.Error(err))
err = resp.DbConnectError.WithError(err) err = resp.DbConnectError.WithError(err)
return return
} }
...@@ -130,6 +135,30 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo ...@@ -130,6 +135,30 @@ func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLo
return return
} }
if taskExecLogRes.State == 0 {
redis, err := client.GetRedisClient()
if err != nil {
conf.Logger.Error("redis err", zap.Error(err))
err = resp.RedisConnectError.ErrorDetail(err)
return
}
value, err := redis.HGet(conf.AutoExecHistory, cast.ToString(id))
if err != nil {
conf.Logger.Error("redis HGet err", zap.Error(err))
err = resp.RedisExecError.ErrorDetail(err)
return
}
var execCache ExecCache
err = json.Unmarshal([]byte(value), &execCache)
if err != nil {
conf.Logger.Error("redis value Unmarshal err", zap.Error(err))
err = resp.FAIL.WithError(err)
return
}
taskExecLogRes.ExecLog = execCache.ExecLog
taskExecLogRes.ExecEndTime = execCache.ExecEndTime
}
//获取执行耗时 //获取执行耗时
start, err = time.Parse("2006-01-02 15:04:05", taskExecLogRes.ExecStartTime.Format("2006-01-02 15:04:05")) start, err = time.Parse("2006-01-02 15:04:05", taskExecLogRes.ExecStartTime.Format("2006-01-02 15:04:05"))
if err != nil { if err != nil {
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"errors" "errors"
"fmt" "fmt"
json "github.com/json-iterator/go"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "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/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response"
...@@ -169,10 +170,9 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int ...@@ -169,10 +170,9 @@ func (t *TaskManageSvc) ListTaskManage(req request.ListTaskManageReq) (total int
if req.HostGroupId != 0 { if req.HostGroupId != 0 {
finder.Where("tm.host_group_id = ?", req.HostGroupId) finder.Where("tm.host_group_id = ?", req.HostGroupId)
} }
finder.OrderBy("tm.id")
//查询任务 //查询任务
total, err = finder.Select("tm.id,tm.task_name,tm.task_desc,(select count(*) from task_history th "+ total, err = finder.Select("tm.id,tm.task_name,tm.task_desc,(select count(*) from task_history th "+
"where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time").OrderBy("tm.create_time"). "where th.task_id = tm.id) as exec_cnt,tm.create_user,tm.create_time").OrderBy("tm.create_time desc").
Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskManageListRes) 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)
...@@ -354,6 +354,11 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id ...@@ -354,6 +354,11 @@ func (t *TaskManageSvc) ExecScript(req request.ExecScriptReq, script string) (id
return return
} }
type ExecCache struct {
ExecLog string `json:"exec_log"`
ExecEndTime time.Time `json:"exec_end_time"` // 执行结束时间
}
// ExecAnsible 执行ansible命令 // ExecAnsible 执行ansible命令
func ExecAnsible(id, taskId int, value string) { func ExecAnsible(id, taskId int, value string) {
var cmd *exec.Cmd var cmd *exec.Cmd
...@@ -388,11 +393,12 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -388,11 +393,12 @@ func ExecAnsible(id, taskId int, value string) {
readerr := bufio.NewReader(stderr) readerr := bufio.NewReader(stderr)
var out, outErr int var out, outErr int
var execLog string //var execLog string
var execObj []byte
var execCache ExecCache
redis, err := client.GetRedisClient() redis, err := client.GetRedisClient()
if err != nil { if err != nil {
zap.L().Error(err.Error()) conf.Logger.Error("redis err", zap.Error(err))
} }
for { for {
//逐行输出日志 //逐行输出日志
...@@ -401,11 +407,17 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -401,11 +407,17 @@ func ExecAnsible(id, taskId int, value string) {
out = 1 out = 1
} else if out == 0 { } else if out == 0 {
//存储执行日志 //存储执行日志
execLog = execLog + lineOut + " \n " //execLog = execLog + lineOut + " \n "
execCache.ExecLog = execCache.ExecLog + lineOut + " \n "
execCache.ExecEndTime = time.Now()
execObj, err = json.Marshal(execCache)
if err != nil {
conf.Logger.Error("lineOut Marshal execCache err", zap.Error(err))
}
// 写缓存 // 写缓存
err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), execLog) err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), fmt.Sprintf("%s", execObj))
if err != nil { if err != nil {
conf.Logger.Error("Store Execution Log", zap.Error(err)) conf.Logger.Error("Store Execution Log err", zap.Error(err))
} }
} }
...@@ -414,11 +426,17 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -414,11 +426,17 @@ func ExecAnsible(id, taskId int, value string) {
outErr = 1 outErr = 1
} else if outErr == 0 { } else if outErr == 0 {
//存储异常执行日志 //存储异常执行日志
execLog = execLog + lineErr + " \n " //execLog = execLog + lineErr + " \n "
execCache.ExecLog = execCache.ExecLog + lineErr + " \n "
execCache.ExecEndTime = time.Now()
execObj, err = json.Marshal(execCache)
if err != nil {
conf.Logger.Error("lineErr Marshal execCache err", zap.Error(err))
}
// 写缓存 // 写缓存
err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), execLog) err = redis.HSet(conf.AutoExecHistory, strconv.Itoa(id), fmt.Sprintf("%s", execObj))
if err != nil { if err != nil {
conf.Logger.Error("Store Execution Log", zap.Error(err)) conf.Logger.Error("Store Execution Log err", zap.Error(err))
} }
} }
if out == 1 && outErr == 1 { if out == 1 && outErr == 1 {
...@@ -431,7 +449,7 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -431,7 +449,7 @@ func ExecAnsible(id, taskId int, value string) {
//任务执行成功 //任务执行成功
err = UpdateExecHistory(request.UpdateExecHistory{ err = UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id, TaskHistoryId: id,
ExecLog: execLog, ExecLog: execCache.ExecLog,
State: 1, State: 1,
}) })
if err != nil { if err != nil {
...@@ -443,11 +461,11 @@ func ExecAnsible(id, taskId int, value string) { ...@@ -443,11 +461,11 @@ func ExecAnsible(id, taskId int, value string) {
//任务执行失败 //任务执行失败
err = UpdateExecHistory(request.UpdateExecHistory{ err = UpdateExecHistory(request.UpdateExecHistory{
TaskHistoryId: id, TaskHistoryId: id,
ExecLog: execLog, ExecLog: execCache.ExecLog,
State: 2, State: 2,
}) })
if err != nil { if err != nil {
conf.Logger.Error("Modify Execution Status", zap.Error(err)) conf.Logger.Error("Modify Execution Status err", zap.Error(err))
//return //return
} }
redis.HDel(conf.AutoExecHistory, strconv.Itoa(id)) redis.HDel(conf.AutoExecHistory, strconv.Itoa(id))
......
...@@ -263,7 +263,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderManage(req request.ListWorkOrderManage ...@@ -263,7 +263,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderManage(req request.ListWorkOrderManage
if req.CreateDateTo != "" { if req.CreateDateTo != "" {
finder.Where("create_time <= ?", req.CreateDateTo) finder.Where("create_time <= ?", req.CreateDateTo)
} }
finder.OrderBy("create_time") finder.OrderBy("create_time desc")
//查询任务 //查询任务
total, err = finder.Select("id,order_name,order_level,order_cnt,push_obj,timing_type,timing_state,create_user,create_time"). total, err = finder.Select("id,order_name,order_level,order_cnt,push_obj,timing_type,timing_state,create_user,create_time").
Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&workOrderListRes) Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&workOrderListRes)
...@@ -456,7 +456,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderIssuance(req request.ListWorkOrderReq) ...@@ -456,7 +456,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderIssuance(req request.ListWorkOrderReq)
if req.Id != 0 { if req.Id != 0 {
finder.Where("wom.id = ?", req.Id) finder.Where("wom.id = ?", req.Id)
} }
finder.OrderBy("woi.create_time") finder.OrderBy("woi.create_time desc")
//查询任务 //查询任务
total, err = finder.Select("woi.\"id\",woi.order_id,wom.order_name,wom.order_level,woi.order_state,woi.push_obj,woi.create_user,woi.create_time,woi.complete_time,"+ total, err = finder.Select("woi.\"id\",woi.order_id,wom.order_name,wom.order_level,woi.order_state,woi.push_obj,woi.create_user,woi.create_time,woi.complete_time,"+
"(select count(1) from work_order_me wome where wome.order_issuance_id = woi.id and wome.order_state = woi.order_state) as state_cnt,"+ "(select count(1) from work_order_me wome where wome.order_issuance_id = woi.id and wome.order_state = woi.order_state) as state_cnt,"+
...@@ -615,7 +615,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderMe(req request.ListWorkOrderReq) (tota ...@@ -615,7 +615,7 @@ func (w *WorkOrderManageSvc) ListWorkOrderMe(req request.ListWorkOrderReq) (tota
if req.CompleteTimeTo != "" { if req.CompleteTimeTo != "" {
finder.Where("wome.complete_time <= ?", req.CompleteTimeTo) finder.Where("wome.complete_time <= ?", req.CompleteTimeTo)
} }
finder.OrderBy("woi.create_time") finder.OrderBy("woi.create_time desc")
//查询任务 //查询任务
total, err = finder.Select("wome.\"id\",wome.order_id,wome.order_issuance_id,wom.order_name,wom.order_level,"+ total, err = finder.Select("wome.\"id\",wome.order_id,wome.order_issuance_id,wom.order_name,wom.order_level,"+
"wome.order_state,woi.push_obj,woi.create_user,woi.create_time,wome.complete_time"). "wome.order_state,woi.push_obj,woi.create_user,woi.create_time,wome.complete_time").
......
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