服务访问日志增加今天和历史的访问日志查看

This commit is contained in:
刘祥超
2020-10-31 17:44:48 +08:00
parent 0b6460232f
commit 66f726a3e9
14 changed files with 280 additions and 20 deletions

View File

@@ -142,13 +142,14 @@ func (this *NodeAction) RunGet(params struct {
"isOn": node.IsOn,
"status": maps.Map{
"isActive": status.IsActive,
"updatedAt": status.UpdatedAt,
"hostname": status.Hostname,
"cpuUsage": status.CPUUsage,
"cpuUsageText": fmt.Sprintf("%.2f%%", status.CPUUsage*100),
"memUsage": status.MemoryUsage,
"memUsageText": fmt.Sprintf("%.2f%%", status.MemoryUsage*100),
"isActive": status.IsActive,
"updatedAt": status.UpdatedAt,
"hostname": status.Hostname,
"cpuUsage": status.CPUUsage,
"cpuUsageText": fmt.Sprintf("%.2f%%", status.CPUUsage*100),
"memUsage": status.MemoryUsage,
"memUsageText": fmt.Sprintf("%.2f%%", status.MemoryUsage*100),
"connectionCount": status.ConnectionCount,
},
"group": groupMap,

View File

@@ -9,7 +9,7 @@ import (
)
const (
SettingCodeGlobalConfig = "globalConfig"
SettingCodeServerGlobalConfig = "serverGlobalConfig"
)
type IndexAction struct {
@@ -22,7 +22,7 @@ func (this *IndexAction) Init() {
}
func (this *IndexAction) RunGet(params struct{}) {
valueJSONResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: SettingCodeGlobalConfig})
valueJSONResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.AdminContext(), &pb.ReadSysSettingRequest{Code: SettingCodeServerGlobalConfig})
if err != nil {
this.ErrorPage(err)
return
@@ -57,7 +57,7 @@ func (this *IndexAction) RunPost(params struct {
// 修改配置
_, err = this.RPC().SysSettingRPC().UpdateSysSetting(this.AdminContext(), &pb.UpdateSysSettingRequest{
Code: SettingCodeGlobalConfig,
Code: SettingCodeServerGlobalConfig,
ValueJSON: params.GlobalConfigJSON,
})
if err != nil {

View File

@@ -0,0 +1,87 @@
package log
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
"strings"
)
type HistoryAction struct {
actionutils.ParentAction
}
func (this *HistoryAction) Init() {
this.Nav("", "log", "")
this.SecondMenu("history")
}
func (this *HistoryAction) RunGet(params struct {
ServerId int64
Day string
RequestId string
HasError int
}) {
if len(params.Day) == 0 {
params.Day = timeutil.Format("Y-m-d")
}
this.Data["path"] = this.Request.URL.Path
this.Data["day"] = params.Day
this.Data["accessLogs"] = []interface{}{}
this.Data["hasError"] = params.HasError
day := params.Day
if len(day) > 0 && regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(day) {
day = strings.ReplaceAll(day, "-", "")
size := int64(10)
this.Data["hasError"] = params.HasError
resp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
RequestId: params.RequestId,
ServerId: params.ServerId,
HasError: params.HasError > 0,
Day: day,
Size: size,
})
if err != nil {
this.ErrorPage(err)
return
}
if len(resp.AccessLogs) == 0 {
this.Data["accessLogs"] = []interface{}{}
} else {
this.Data["accessLogs"] = resp.AccessLogs
}
this.Data["hasMore"] = resp.HasMore
this.Data["nextRequestId"] = resp.RequestId
// 上一个requestId
this.Data["hasPrev"] = false
this.Data["lastRequestId"] = ""
if len(params.RequestId) > 0 {
this.Data["hasPrev"] = true
prevResp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
RequestId: params.RequestId,
ServerId: params.ServerId,
HasError: params.HasError > 0,
Day: day,
Size: size,
Reverse: true,
})
if err != nil {
this.ErrorPage(err)
return
}
if int64(len(prevResp.AccessLogs)) == size {
this.Data["lastRequestId"] = prevResp.RequestId
}
}
}
this.Show()
}

View File

@@ -13,6 +13,8 @@ func init() {
Helper(serverutils.NewServerHelper()).
Prefix("/servers/server/log").
GetPost("", new(IndexAction)).
GetPost("/today", new(TodayAction)).
GetPost("/history", new(HistoryAction)).
EndAll()
})
}

View File

@@ -0,0 +1,71 @@
package log
import (
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type TodayAction struct {
actionutils.ParentAction
}
func (this *TodayAction) Init() {
this.Nav("", "log", "")
this.SecondMenu("today")
}
func (this *TodayAction) RunGet(params struct {
RequestId string
ServerId int64
HasError int
}) {
size := int64(10)
this.Data["path"] = this.Request.URL.Path
this.Data["hasError"] = params.HasError
resp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
RequestId: params.RequestId,
ServerId: params.ServerId,
HasError: params.HasError > 0,
Day: timeutil.Format("Ymd"),
Size: size,
})
if err != nil {
this.ErrorPage(err)
return
}
if len(resp.AccessLogs) == 0 {
this.Data["accessLogs"] = []interface{}{}
} else {
this.Data["accessLogs"] = resp.AccessLogs
}
this.Data["hasMore"] = resp.HasMore
this.Data["nextRequestId"] = resp.RequestId
// 上一个requestId
this.Data["hasPrev"] = false
this.Data["lastRequestId"] = ""
if len(params.RequestId) > 0 {
this.Data["hasPrev"] = true
prevResp, err := this.RPC().HTTPAccessLogRPC().ListHTTPAccessLogs(this.AdminContext(), &pb.ListHTTPAccessLogsRequest{
RequestId: params.RequestId,
ServerId: params.ServerId,
HasError: params.HasError > 0,
Day: timeutil.Format("Ymd"),
Size: size,
Reverse: true,
})
if err != nil {
this.ErrorPage(err)
return
}
if int64(len(prevResp.AccessLogs)) == size {
this.Data["lastRequestId"] = prevResp.RequestId
}
}
this.Show()
}

View File

@@ -125,6 +125,16 @@ func (this *ServerHelper) createLogMenu(secondMenuItem string, serverIdString st
"url": "/servers/server/log?serverId=" + serverIdString,
"isActive": secondMenuItem == "index",
})
menuItems = append(menuItems, maps.Map{
"name": "今天",
"url": "/servers/server/log/today?serverId=" + serverIdString,
"isActive": secondMenuItem == "today",
})
menuItems = append(menuItems, maps.Map{
"name": "历史",
"url": "/servers/server/log/history?serverId=" + serverIdString,
"isActive": secondMenuItem == "history",
})
return menuItems
}