日志管理列表

This commit is contained in:
陈俊宏 2024-11-28 10:50:51 +08:00
parent 52d05d89a6
commit 53eab06ce8
11 changed files with 157 additions and 30 deletions

View File

@ -12,12 +12,13 @@ import (
func CronFuncLogsList(c *gin.Context) {
request := controllers.GetRequest(c).(*backend.CronFuncLogsListRequest)
count, DbListInfo, err := services.CronFuncLogsList(request, request.Page, request.Limit)
count, funcLogsList, err := services.CronFuncLogsList(request, request.Page, request.Limit)
if err != nil {
controllers.HandRes(c, nil, errorcode.ParamError)
} else {
var DbListResponse []backend.CronFuncLogsListResponse
_ = mapstructure.DecodeWithTime(DbListInfo, &DbListResponse, helper.DefaultFormatLayout)
controllers.HandRes(c, gin.H{"data": DbListResponse, "count": count}, err)
var funcLogsListResponse []backend.CronFuncLogsListResponse
_ = mapstructure.DecodeWithTime(funcLogsList, &funcLogsListResponse, helper.DefaultFormatLayout)
// todo 获取当前列表下的CmdName
controllers.HandRes(c, gin.H{"data": funcLogsListResponse, "count": count}, err)
}
}

View File

@ -0,0 +1,60 @@
package backend
import (
"cron_admin/app/constants/errorcode"
"cron_admin/app/http/controllers"
"cron_admin/app/http/entities/backend"
"cron_admin/app/models/cronusermodel"
"cron_admin/app/services"
"cron_admin/app/utils/helper"
"cron_admin/app/utils/mapstructure"
"github.com/gin-gonic/gin"
"strings"
)
func CronReportLogsList(c *gin.Context) {
request := controllers.GetRequest(c).(*backend.CronReportLogsListRequest)
count, reportLogsList, err := services.CronReportLogsList(request, request.Page, request.Limit)
if err != nil {
controllers.HandRes(c, nil, errorcode.ParamError)
} else {
var reportLogsListResponse []backend.CronReportLogsListResponse
_ = mapstructure.DecodeWithTime(reportLogsList, &reportLogsListResponse, helper.DefaultFormatLayout)
// 合并reportLogsListResponse下的所有user_id 并去重
var userIds []string
userIdsMap := make(map[int64]bool)
for _, v := range reportLogsListResponse {
userIdsStr := strings.Split(v.UserId, ",")
for _, userId := range userIdsStr {
if userId != "" {
if _, ok := userIdsMap[helper.StringToInt64(userId)]; ok {
continue
}
userIdsMap[helper.StringToInt64(userId)] = true
userIds = append(userIds, userId)
}
}
}
_, userList, err := services.GetListByWhere(&backend.UserListRequest{UserIds: userIds}, 1, 100)
if err != nil {
return
}
userListMap := make(map[int64]cronusermodel.CronUser)
for _, v := range userList {
userListMap[int64(v.UserId)] = v
}
for k, v := range reportLogsListResponse {
userIdsStr := strings.Split(v.UserId, ",")
var userNames []string
for _, userId := range userIdsStr {
if userId != "" {
userNames = append(userNames, userListMap[helper.StringToInt64(userId)].Name)
}
}
userNamesStr := strings.Join(userNames, ",")
reportLogsListResponse[k].UserName = userNamesStr
}
controllers.HandRes(c, gin.H{"data": reportLogsListResponse, "count": count}, err)
}
}

View File

@ -0,0 +1,26 @@
package backend
import (
"cron_admin/app/http/entities"
)
type CronReportLogsListRequest struct {
entities.PageRequest
FuncLogId int64 `json:"func_log_id"` // 日志ID
CmdId int64 `json:"cmd_id"` // 任务ID
Status int `json:"status"` // 状态
UserId int64 `json:"user_id"` // 用户ID
}
type CronReportLogsListResponse struct {
ReportId int64 `json:"report_id"`
FuncLogId int64 `json:"func_log_id"`
CmdId int64 `json:"cmd_id"`
UserId string `json:"user_id"`
UserName string `json:"user_name"`
Data string `json:"data"`
FailReason string `json:"fail_reason"`
CreateTime string `json:"create_time"`
UpdateTime string `json:"update_time"`
Status int `json:"status"`
}

View File

@ -6,11 +6,12 @@ import (
)
type UserListRequest struct {
Page int `json:"page" validate:"required" form:"page" example:"1"`
Limit int `json:"limit" validate:"required" form:"limit" example:"10"`
Tel string `json:"tel" form:"tel" example:"155555555"`
Status int `json:"status" form:"status" example:"1"`
Name string `json:"name" form:"name" example:"46516"`
Page int `json:"page" validate:"required" form:"page" example:"1"`
Limit int `json:"limit" validate:"required" form:"limit" example:"10"`
Tel string `json:"tel" form:"tel" example:"155555555"`
Status int `json:"status" form:"status" example:"1"`
Name string `json:"name" form:"name" example:"46516"`
UserIds []string `json:"user_ids"`
}
type UserInfoRequest struct {

View File

@ -24,4 +24,5 @@ var BackendRequestMap = map[string]func() interface{}{
// 日志
common.ADMIN_OAUTH_V1 + "/log/cmd/list": func() interface{} { return new(backend.CronFuncLogsListRequest) },
common.ADMIN_OAUTH_V1 + "/log/mes/list": func() interface{} { return new(backend.CronReportLogsListRequest) },
}

View File

@ -72,7 +72,7 @@ func RegisterAdminRoute(router *gin.Engine) {
//消息日志
mesLog := log.Group("/mes")
{
mesLog.GET("/list", backend.Empty)
mesLog.POST("/list", backend.CronReportLogsList)
}
}

View File

@ -13,14 +13,14 @@ var (
// 实体
type CronFuncLogs struct {
LogId int64 `xorm:"'log_id' UNSIGNED INT"`
CmdId int64 `xorm:"'cmd_id' INT"`
ReadExecute string `xorm:"'read_execute' json"`
WriteExecute string `xorm:"'write_execute' text"`
FailReason string `xorm:"'fail_reason' text"`
CreateTime time.Time `xorm:"'create_time' datetime"`
UpdateTime time.Time `xorm:"'update_time' timestamp"`
Status int `xorm:"'status' tinyint"`
LogId int64 `xorm:"'log_id' UNSIGNED INT"`
CmdId int64 `xorm:"'cmd_id' INT"`
ReadExecute string `xorm:"'read_execute' json"`
WriteExecute string `xorm:"'write_execute' text"`
FailReason string `xorm:"'fail_reason' text"`
CreateTime *time.Time `xorm:"'create_time' datetime"`
UpdateTime *time.Time `xorm:"'update_time' timestamp"`
Status int `xorm:"'status' tinyint"`
}
// 表名

View File

@ -1,4 +1,4 @@
package crondbmodel
package cronreportlogsmodel
import (
"github.com/qit-team/snow-core/db"
@ -13,15 +13,15 @@ var (
// 实体
type CronReportLogs struct {
ReportId int64 `xorm:"'report_id' UNSIGNED INT"`
FuncLogId int64 `xorm:"'func_log_id' INT"`
CmdId int64 `xorm:"'cmd_id' INT"`
UserId string `xorm:"'user_id' varchar(100)"`
Data string `xorm:"'data' json"`
FailReason string `xorm:"'fail_reason' varchar(200)"`
CreateTime time.Time `xorm:"'create_time' datetime"`
UpdateTime time.Time `xorm:"'update_time' timestamp"`
Status int `xorm:"'status' tinyint"`
ReportId int64 `xorm:"'report_id' UNSIGNED INT"`
FuncLogId int64 `xorm:"'func_log_id' INT"`
CmdId int64 `xorm:"'cmd_id' INT"`
UserId string `xorm:"'user_id' varchar(100)"`
Data string `xorm:"'data' json"`
FailReason string `xorm:"'fail_reason' varchar(200)"`
CreateTime *time.Time `xorm:"'create_time' datetime"`
UpdateTime *time.Time `xorm:"'update_time' timestamp"`
Status int `xorm:"'status' tinyint"`
}
// 表名

View File

@ -16,7 +16,7 @@ func CronFuncLogsList(request *backend.CronFuncLogsListRequest, page int, limit
}
session := cronfunclogsmodel.GetInstance().GetDb().Where(conn)
if page != 0 && limit != 0 {
session = session.Limit(limit, (page-1)*limit)
session = session.OrderBy("create_time desc").Limit(limit, (page-1)*limit)
}
count, err = session.FindAndCount(&listInfo)
if err != nil {

View File

@ -0,0 +1,34 @@
package services
import (
"cron_admin/app/http/entities/backend"
"cron_admin/app/models/cronreportlogsmodel"
"xorm.io/builder"
)
func CronReportLogsList(request *backend.CronReportLogsListRequest, page int, limit int) (count int64, listInfo []cronreportlogsmodel.CronReportLogs, err error) {
conn := builder.NewCond()
if request.FuncLogId != 0 {
conn = conn.And(builder.Eq{"FuncLogId": request.FuncLogId})
}
if request.CmdId != 0 {
conn = conn.And(builder.Eq{"CmdId": request.CmdId})
}
if request.Status != 0 {
conn = conn.And(builder.Eq{"Status": request.Status})
}
// 使用FIND_IN_SET 函数
if request.UserId != 0 {
conn = conn.And(builder.Expr("FIND_IN_SET(?, user_id)", request.UserId))
}
session := cronreportlogsmodel.GetInstance().GetDb().Where(conn)
if page != 0 && limit != 0 {
session = session.OrderBy("create_time desc").Limit(limit, (page-1)*limit)
}
count, err = session.FindAndCount(&listInfo)
if err != nil {
return
}
return
}

View File

@ -18,11 +18,15 @@ func GetListByWhere(request *backend.UserListRequest, page int, limit int) (coun
if request.Status != 0 {
cond = cond.And(builder.Eq{"status": request.Status})
}
if len(request.UserIds) > 0 {
// 使用IN查询
cond = cond.And(builder.In("user_id", request.UserIds))
}
//model := repository.NewCommonRepo[userinfomodel.CronUserModel](userinfomodel.GetInstance().GetDb().NewSession())
session := cronusermodel.GetInstance().GetDb().Where(cond)
if page != 0 && limit != 0 {
session = session.Limit(page, (page-1)*limit)
session = session.Limit(limit, (page-1)*limit)
}
count, err = session.FindAndCount(&UserListInfo)