1.4.5.2
This commit is contained in:
101
EdgeAdmin/internal/web/actions/default/log/clean.go
Normal file
101
EdgeAdmin/internal/web/actions/default/log/clean.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/utils/numberutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
type CleanAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *CleanAction) Init() {
|
||||
this.Nav("", "", "clean")
|
||||
}
|
||||
|
||||
func (this *CleanAction) RunGet(params struct{}) {
|
||||
// 读取配置
|
||||
config, err := configloaders.LoadLogConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if !config.CanClean {
|
||||
this.WriteString("已设置不能清理")
|
||||
return
|
||||
}
|
||||
this.Data["logConfig"] = config
|
||||
|
||||
sizeResp, err := this.RPC().LogRPC().SumLogsSize(this.AdminContext(), &pb.SumLogsSizeRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
sizeHuman := ""
|
||||
if sizeResp.SizeBytes < 1024 {
|
||||
sizeHuman = numberutils.FormatInt64(sizeResp.SizeBytes) + "字节"
|
||||
} else if sizeResp.SizeBytes < 1024*1024 {
|
||||
sizeHuman = fmt.Sprintf("%.2fK", float64(sizeResp.SizeBytes)/1024)
|
||||
} else if sizeResp.SizeBytes < 1024*1024*1024 {
|
||||
sizeHuman = fmt.Sprintf("%.2fM", float64(sizeResp.SizeBytes)/1024/1024)
|
||||
} else {
|
||||
sizeHuman = fmt.Sprintf("%.2fG", float64(sizeResp.SizeBytes)/1024/1024/1024)
|
||||
}
|
||||
this.Data["size"] = sizeHuman
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *CleanAction) RunPost(params struct {
|
||||
Type string
|
||||
Days int32
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
// 读取配置
|
||||
config, err := configloaders.LoadLogConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if !config.CanClean {
|
||||
this.WriteString("已设置不能清理")
|
||||
return
|
||||
}
|
||||
|
||||
switch params.Type {
|
||||
case "all":
|
||||
defer this.CreateLogInfo(codes.Log_LogCleanAllLogs)
|
||||
|
||||
_, err := this.RPC().LogRPC().CleanLogsPermanently(this.AdminContext(), &pb.CleanLogsPermanentlyRequest{
|
||||
Days: 0,
|
||||
ClearAll: true,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
case "days":
|
||||
defer this.CreateLogInfo(codes.Log_LogCleanLogsDaysBefore, params.Days)
|
||||
|
||||
_, err := this.RPC().LogRPC().CleanLogsPermanently(this.AdminContext(), &pb.CleanLogsPermanentlyRequest{
|
||||
Days: params.Days,
|
||||
ClearAll: false,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
default:
|
||||
this.Fail("不支持的清理方式 '" + params.Type + "'")
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
38
EdgeAdmin/internal/web/actions/default/log/delete.go
Normal file
38
EdgeAdmin/internal/web/actions/default/log/delete.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
type DeleteAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *DeleteAction) RunPost(params struct {
|
||||
LogId int64
|
||||
}) {
|
||||
// 记录日志
|
||||
defer this.CreateLogInfo(codes.Log_LogDeleteLog, params.LogId)
|
||||
|
||||
// 读取配置
|
||||
config, err := configloaders.LoadLogConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if !config.CanDelete {
|
||||
this.Fail("已设置不能删除")
|
||||
}
|
||||
|
||||
// 执行删除
|
||||
_, err = this.RPC().LogRPC().DeleteLogPermanently(this.AdminContext(), &pb.DeleteLogPermanentlyRequest{LogId: params.LogId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
115
EdgeAdmin/internal/web/actions/default/log/exportExcel.go
Normal file
115
EdgeAdmin/internal/web/actions/default/log/exportExcel.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"github.com/tealeg/xlsx/v3"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type ExportExcelAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *ExportExcelAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *ExportExcelAction) RunGet(params struct {
|
||||
DayFrom string
|
||||
DayTo string
|
||||
Keyword string
|
||||
UserType string
|
||||
Level string
|
||||
}) {
|
||||
logsResp, err := this.RPC().LogRPC().ListLogs(this.AdminContext(), &pb.ListLogsRequest{
|
||||
Offset: 0,
|
||||
Size: 10000, // 日志最大导出10000条,TODO 将来可以配置
|
||||
DayFrom: params.DayFrom,
|
||||
DayTo: params.DayTo,
|
||||
Keyword: params.Keyword,
|
||||
UserType: params.UserType,
|
||||
Level: params.Level,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var wb = xlsx.NewFile()
|
||||
sheet, err := wb.AddSheet("default")
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 头部
|
||||
{
|
||||
var row = sheet.AddRow()
|
||||
row.SetHeight(25)
|
||||
row.AddCell().SetString("ID")
|
||||
row.AddCell().SetString("日期")
|
||||
row.AddCell().SetString("用户")
|
||||
row.AddCell().SetString("描述")
|
||||
row.AddCell().SetString("IP")
|
||||
row.AddCell().SetString("区域")
|
||||
row.AddCell().SetString("运营商")
|
||||
row.AddCell().SetString("页面地址")
|
||||
row.AddCell().SetString("级别")
|
||||
}
|
||||
|
||||
// 数据
|
||||
for _, log := range logsResp.Logs {
|
||||
var regionName = ""
|
||||
var ispName = ""
|
||||
|
||||
var ipRegion = iplibrary.LookupIP(log.Ip)
|
||||
if ipRegion != nil && ipRegion.IsOk() {
|
||||
regionName = ipRegion.RegionSummary()
|
||||
ispName = ipRegion.ProviderName()
|
||||
}
|
||||
|
||||
var row = sheet.AddRow()
|
||||
row.SetHeight(25)
|
||||
row.AddCell().SetInt64(log.Id)
|
||||
row.AddCell().SetString(timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt))
|
||||
if log.UserId > 0 {
|
||||
row.AddCell().SetString("用户 | " + log.UserName)
|
||||
} else {
|
||||
row.AddCell().SetString(log.UserName)
|
||||
}
|
||||
row.AddCell().SetString(log.Description)
|
||||
row.AddCell().SetString(log.Ip)
|
||||
row.AddCell().SetString(regionName)
|
||||
row.AddCell().SetString(ispName)
|
||||
row.AddCell().SetString(log.Action)
|
||||
|
||||
var levelName = ""
|
||||
switch log.Level {
|
||||
case "info":
|
||||
levelName = "信息"
|
||||
case "warn", "warning":
|
||||
levelName = "警告"
|
||||
case "error":
|
||||
levelName = "错误"
|
||||
}
|
||||
row.AddCell().SetString(levelName)
|
||||
}
|
||||
|
||||
this.AddHeader("Content-Type", "application/vnd.ms-excel")
|
||||
this.AddHeader("Content-Disposition", "attachment; filename=\"LOG-"+timeutil.Format("YmdHis")+".xlsx\"")
|
||||
this.AddHeader("Cache-Control", "max-age=0")
|
||||
|
||||
var buf = bytes.NewBuffer([]byte{})
|
||||
err = wb.Write(buf)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.AddHeader("Content-Length", strconv.Itoa(buf.Len()))
|
||||
_, _ = this.Write(buf.Bytes())
|
||||
}
|
||||
17
EdgeAdmin/internal/web/actions/default/log/helper.go
Normal file
17
EdgeAdmin/internal/web/actions/default/log/helper.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Helper struct {
|
||||
}
|
||||
|
||||
func (this *Helper) BeforeAction(action *actions.ActionObject) {
|
||||
if action.Request.Method != http.MethodGet {
|
||||
return
|
||||
}
|
||||
|
||||
action.Data["teaMenu"] = "log"
|
||||
}
|
||||
111
EdgeAdmin/internal/web/actions/default/log/index.go
Normal file
111
EdgeAdmin/internal/web/actions/default/log/index.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("log", "log", "list")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
DayFrom string
|
||||
DayTo string
|
||||
Keyword string
|
||||
UserType string
|
||||
Level string
|
||||
}) {
|
||||
// 读取配置
|
||||
config, err := configloaders.LoadLogConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
this.Data["logConfig"] = config
|
||||
|
||||
this.Data["dayFrom"] = params.DayFrom
|
||||
this.Data["dayTo"] = params.DayTo
|
||||
this.Data["keyword"] = params.Keyword
|
||||
this.Data["userType"] = params.UserType
|
||||
|
||||
// 级别
|
||||
this.Data["level"] = params.Level
|
||||
this.Data["levelOptions"] = []maps.Map{
|
||||
{
|
||||
"code": "info",
|
||||
"name": this.Lang(codes.Level_Info),
|
||||
},
|
||||
{
|
||||
"code": "warn",
|
||||
"name": this.Lang(codes.Level_Warn),
|
||||
},
|
||||
{
|
||||
"code": "error",
|
||||
"name": this.Lang(codes.Level_Error),
|
||||
},
|
||||
}
|
||||
|
||||
countResp, err := this.RPC().LogRPC().CountLogs(this.AdminContext(), &pb.CountLogRequest{
|
||||
DayFrom: params.DayFrom,
|
||||
DayTo: params.DayTo,
|
||||
Keyword: params.Keyword,
|
||||
UserType: params.UserType,
|
||||
Level: params.Level,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var count = countResp.Count
|
||||
var page = this.NewPage(count)
|
||||
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,
|
||||
Level: params.Level,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var logMaps = []maps.Map{}
|
||||
for _, log := range logsResp.Logs {
|
||||
var regionName = ""
|
||||
var ipRegion = iplibrary.LookupIP(log.Ip)
|
||||
if ipRegion != nil && ipRegion.IsOk() {
|
||||
regionName = ipRegion.Summary()
|
||||
}
|
||||
|
||||
logMaps = append(logMaps, maps.Map{
|
||||
"id": log.Id,
|
||||
"adminId": log.AdminId,
|
||||
"userId": log.UserId,
|
||||
"description": log.Description,
|
||||
"userName": log.UserName,
|
||||
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt),
|
||||
"level": log.Level,
|
||||
"type": log.Type,
|
||||
"ip": log.Ip,
|
||||
"region": regionName,
|
||||
"action": log.Action,
|
||||
})
|
||||
}
|
||||
this.Data["logs"] = logMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
22
EdgeAdmin/internal/web/actions/default/log/init.go
Normal file
22
EdgeAdmin/internal/web/actions/default/log/init.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/iwind/TeaGo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||
server.
|
||||
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodeLog)).
|
||||
Helper(new(Helper)).
|
||||
Prefix("/log").
|
||||
Get("", new(IndexAction)).
|
||||
Get("/exportExcel", new(ExportExcelAction)).
|
||||
Post("/delete", new(DeleteAction)).
|
||||
GetPost("/clean", new(CleanAction)).
|
||||
GetPost("/settings", new(SettingsAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
70
EdgeAdmin/internal/web/actions/default/log/settings.go
Normal file
70
EdgeAdmin/internal/web/actions/default/log/settings.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
)
|
||||
|
||||
type SettingsAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *SettingsAction) Init() {
|
||||
this.Nav("", "", "setting")
|
||||
}
|
||||
|
||||
func (this *SettingsAction) RunGet(params struct{}) {
|
||||
config, err := configloaders.LoadLogConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Data["logConfig"] = config
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *SettingsAction) RunPost(params struct {
|
||||
CanDelete bool
|
||||
CanClean bool
|
||||
CapacityJSON []byte
|
||||
Days int
|
||||
CanChange bool
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
defer this.CreateLogInfo(codes.Log_LogUpdateSettings)
|
||||
|
||||
capacity := &shared.SizeCapacity{}
|
||||
err := json.Unmarshal(params.CapacityJSON, capacity)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
config, err := configloaders.LoadLogConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if config.CanChange {
|
||||
config.CanDelete = params.CanDelete
|
||||
config.CanClean = params.CanClean
|
||||
config.Days = params.Days
|
||||
}
|
||||
config.Capacity = capacity
|
||||
config.CanChange = params.CanChange
|
||||
err = configloaders.UpdateLogConfig(config)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user