Files
EdgeAdmin/internal/web/actions/default/log/index.go

112 lines
2.5 KiB
Go
Raw Normal View History

2020-09-13 20:37:07 +08:00
package log
2020-11-10 20:30:47 +08:00
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
2020-11-10 20:30:47 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
2023-06-28 19:07:42 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
2020-11-10 20:30:47 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
2020-09-13 20:37:07 +08:00
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("log", "log", "list")
2020-09-13 20:37:07 +08:00
}
2020-11-20 16:36:03 +08:00
func (this *IndexAction) RunGet(params struct {
DayFrom string
DayTo string
Keyword string
UserType string
2023-04-06 10:06:22 +08:00
Level string
2020-11-20 16:36:03 +08:00
}) {
// 读取配置
config, err := configloaders.LoadLogConfig()
if err != nil {
this.ErrorPage(err)
return
}
this.Data["logConfig"] = config
2020-11-20 16:36:03 +08:00
this.Data["dayFrom"] = params.DayFrom
this.Data["dayTo"] = params.DayTo
this.Data["keyword"] = params.Keyword
this.Data["userType"] = params.UserType
2020-11-20 16:36:03 +08:00
2023-04-06 10:06:22 +08:00
// 级别
this.Data["level"] = params.Level
this.Data["levelOptions"] = []maps.Map{
{
"code": "info",
2023-06-30 18:08:30 +08:00
"name": this.Lang(codes.Level_Info),
2023-04-06 10:06:22 +08:00
},
{
"code": "warn",
2023-06-30 18:08:30 +08:00
"name": this.Lang(codes.Level_Warn),
2023-04-06 10:06:22 +08:00
},
{
"code": "error",
2023-06-30 18:08:30 +08:00
"name": this.Lang(codes.Level_Error),
2023-04-06 10:06:22 +08:00
},
}
2020-11-20 16:36:03 +08:00
countResp, err := this.RPC().LogRPC().CountLogs(this.AdminContext(), &pb.CountLogRequest{
DayFrom: params.DayFrom,
DayTo: params.DayTo,
Keyword: params.Keyword,
UserType: params.UserType,
2023-04-06 10:06:22 +08:00
Level: params.Level,
2020-11-20 16:36:03 +08:00
})
2020-11-10 20:30:47 +08:00
if err != nil {
this.ErrorPage(err)
return
}
2023-04-06 10:06:22 +08:00
var count = countResp.Count
var page = this.NewPage(count)
2020-11-10 20:30:47 +08:00
this.Data["page"] = page.AsHTML()
logsResp, err := this.RPC().LogRPC().ListLogs(this.AdminContext(), &pb.ListLogsRequest{
Offset: page.Offset,
Size: page.Size,
DayFrom: params.DayFrom,
DayTo: params.DayTo,
Keyword: params.Keyword,
UserType: params.UserType,
2023-04-06 10:06:22 +08:00
Level: params.Level,
2020-11-10 20:30:47 +08:00
})
if err != nil {
this.ErrorPage(err)
return
}
2023-04-06 10:06:22 +08:00
var logMaps = []maps.Map{}
2020-11-10 20:30:47 +08:00
for _, log := range logsResp.Logs {
var regionName = ""
var ipRegion = iplibrary.LookupIP(log.Ip)
if ipRegion != nil && ipRegion.IsOk() {
regionName = ipRegion.Summary()
}
2020-11-10 20:30:47 +08:00
logMaps = append(logMaps, maps.Map{
"id": log.Id,
"adminId": log.AdminId,
"userId": log.UserId,
2020-11-10 20:30:47 +08:00
"description": log.Description,
"userName": log.UserName,
2020-11-10 20:30:47 +08:00
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt),
"level": log.Level,
"type": log.Type,
"ip": log.Ip,
"region": regionName,
2020-11-20 16:36:03 +08:00
"action": log.Action,
2020-11-10 20:30:47 +08:00
})
}
this.Data["logs"] = logMaps
2020-09-13 20:37:07 +08:00
this.Show()
}