节点运行日志增加记录源站ID

This commit is contained in:
GoEdgeLab
2021-08-01 21:54:44 +08:00
parent 85f7834411
commit ee825f5d89
11 changed files with 56 additions and 23 deletions

View File

@@ -346,7 +346,7 @@ func (this *DBNodeInitializer) loop() error {
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
// 创建节点日志
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
if createLogErr != nil {
logs.Println("[NODE_LOG]" + createLogErr.Error())
}
@@ -390,7 +390,7 @@ func (this *DBNodeInitializer) loop() error {
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
// 创建节点日志
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
if createLogErr != nil {
logs.Println("[NODE_LOG]" + createLogErr.Error())
}

View File

@@ -8,6 +8,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
@@ -275,6 +276,17 @@ func (this *HTTPLocationDAO) FindEnabledLocationIdWithWebId(tx *dbs.Tx, webId in
FindInt64Col(0)
}
// FindEnabledLocationIdWithReverseProxyId 查找包含某个反向代理的Server
func (this *HTTPLocationDAO) FindEnabledLocationIdWithReverseProxyId(tx *dbs.Tx, reverseProxyId int64) (serverId int64, err error) {
return this.Query(tx).
State(ServerStateEnabled).
Where("JSON_CONTAINS(reverseProxy, :jsonQuery)").
Param("jsonQuery", maps.Map{"reverseProxyId": reverseProxyId}.AsJSON()).
ResultPk().
FindInt64Col(0)
}
// NotifyUpdate 通知更新
func (this *HTTPLocationDAO) NotifyUpdate(tx *dbs.Tx, locationId int64) error {
webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithLocationId(tx, locationId)

View File

@@ -8,7 +8,7 @@ import (
func TestNodeDAO_FindAllNodeIdsMatch(t *testing.T) {
var tx *dbs.Tx
nodeIds, err := SharedNodeDAO.FindAllNodeIdsMatch(tx, 1, 0)
nodeIds, err := SharedNodeDAO.FindAllNodeIdsMatch(tx, 1, true, 0)
if err != nil {
t.Fatal(err)
}

View File

@@ -7,6 +7,7 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string"
timeutil "github.com/iwind/TeaGo/utils/time"
"strconv"
@@ -36,8 +37,8 @@ func init() {
}
// CreateLog 创建日志
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, level string, tag string, description string, createdAt int64) error {
hash := stringutil.Md5(nodeRole + "@" + strconv.FormatInt(nodeId, 10) + "@" + strconv.FormatInt(serverId, 10) + "@" + level + "@" + tag + "@" + description)
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64) error {
hash := stringutil.Md5(nodeRole + "@" + types.String(nodeId) + "@" + types.String(serverId) + "@" + types.String(originId) + "@" + level + "@" + tag + "@" + description)
// 检查是否在重复最后一条,避免重复创建
lastLog, err := this.Query(tx).
@@ -62,6 +63,7 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nod
op.Role = nodeRole
op.NodeId = nodeId
op.ServerId = serverId
op.OriginId = originId
op.Level = level
op.Tag = tag
op.Description = description
@@ -88,7 +90,7 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
}
// CountNodeLogs 计算节点日志数量
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, originId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
query := this.Query(tx).
Attr("role", role)
if nodeId > 0 {
@@ -104,6 +106,9 @@ func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, ser
if serverId > 0 {
query.Attr("serverId", serverId)
}
if originId > 0 {
query.Attr("originId", originId)
}
if len(dayFrom) > 0 {
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
query.Gte("day", dayFrom)
@@ -128,6 +133,7 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
role string,
nodeId int64,
serverId int64,
originId int64,
allServers bool,
dayFrom string,
dayTo string,
@@ -153,6 +159,9 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
} else if allServers {
query.Where("serverId>0")
}
if originId > 0 {
query.Attr("originId", originId)
}
if fixedState == configutils.BoolStateYes {
query.Attr("isFixed", 1)
} else if fixedState == configutils.BoolStateNo {

View File

@@ -11,6 +11,7 @@ type NodeLog struct {
NodeId uint32 `field:"nodeId"` // 节点ID
Day string `field:"day"` // 日期
ServerId uint32 `field:"serverId"` // 服务ID
OriginId uint32 `field:"originId"` // 源站ID
Hash string `field:"hash"` // 信息内容Hash
Count uint32 `field:"count"` // 重复次数
IsFixed uint8 `field:"isFixed"` // 是否已处理
@@ -26,6 +27,7 @@ type NodeLogOperator struct {
NodeId interface{} // 节点ID
Day interface{} // 日期
ServerId interface{} // 服务ID
OriginId interface{} // 源站ID
Hash interface{} // 信息内容Hash
Count interface{} // 重复次数
IsFixed interface{} // 是否已处理

View File

@@ -38,12 +38,12 @@ func init() {
})
}
// 初始化
// Init 初始化
func (this *OriginDAO) Init() {
_ = this.DAOObject.Init()
}
// 启用条目
// EnableOrigin 启用条目
func (this *OriginDAO) EnableOrigin(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
@@ -52,7 +52,7 @@ func (this *OriginDAO) EnableOrigin(tx *dbs.Tx, id int64) error {
return err
}
// 禁用条目
// DisableOrigin 禁用条目
func (this *OriginDAO) DisableOrigin(tx *dbs.Tx, originId int64) error {
_, err := this.Query(tx).
Pk(originId).
@@ -65,7 +65,7 @@ func (this *OriginDAO) DisableOrigin(tx *dbs.Tx, originId int64) error {
return this.NotifyUpdate(tx, originId)
}
// 查找启用中的条目
// FindEnabledOrigin 查找启用中的条目
func (this *OriginDAO) FindEnabledOrigin(tx *dbs.Tx, id int64) (*Origin, error) {
result, err := this.Query(tx).
Pk(id).
@@ -77,7 +77,7 @@ func (this *OriginDAO) FindEnabledOrigin(tx *dbs.Tx, id int64) (*Origin, error)
return result.(*Origin), err
}
// 根据主键查找名称
// FindOriginName 根据主键查找名称
func (this *OriginDAO) FindOriginName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
@@ -85,7 +85,7 @@ func (this *OriginDAO) FindOriginName(tx *dbs.Tx, id int64) (string, error) {
FindStringCol("")
}
// 创建源站
// CreateOrigin 创建源站
func (this *OriginDAO) CreateOrigin(tx *dbs.Tx, adminId int64, userId int64, name string, addrJSON string, description string, weight int32, isOn bool, connTimeout *shared.TimeDuration, readTimeout *shared.TimeDuration, idleTimeout *shared.TimeDuration, maxConns int32, maxIdleConns int32) (originId int64, err error) {
op := NewOriginOperator()
op.AdminId = adminId
@@ -139,7 +139,7 @@ func (this *OriginDAO) CreateOrigin(tx *dbs.Tx, adminId int64, userId int64, nam
return types.Int64(op.Id), nil
}
// 修改源站
// UpdateOrigin 修改源站
func (this *OriginDAO) UpdateOrigin(tx *dbs.Tx, originId int64, name string, addrJSON string, description string, weight int32, isOn bool, connTimeout *shared.TimeDuration, readTimeout *shared.TimeDuration, idleTimeout *shared.TimeDuration, maxConns int32, maxIdleConns int32) error {
if originId <= 0 {
return errors.New("invalid originId")
@@ -196,7 +196,7 @@ func (this *OriginDAO) UpdateOrigin(tx *dbs.Tx, originId int64, name string, add
return this.NotifyUpdate(tx, originId)
}
// 将源站信息转换为配置
// ComposeOriginConfig 将源站信息转换为配置
func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64) (*serverconfigs.OriginConfig, error) {
origin, err := this.FindEnabledOrigin(tx, originId)
if err != nil {
@@ -328,7 +328,7 @@ func (this *OriginDAO) ComposeOriginConfig(tx *dbs.Tx, originId int64) (*serverc
return config, nil
}
// 通知更新
// NotifyUpdate 通知更新
func (this *OriginDAO) NotifyUpdate(tx *dbs.Tx, originId int64) error {
reverseProxyId, err := SharedReverseProxyDAO.FindReverseProxyContainsOriginId(tx, originId)
if err != nil {

View File

@@ -385,5 +385,15 @@ func (this *ReverseProxyDAO) NotifyUpdate(tx *dbs.Tx, reverseProxyId int64) erro
if serverId > 0 {
return SharedServerDAO.NotifyUpdate(tx, serverId)
}
// locations
locationId, err := SharedHTTPLocationDAO.FindEnabledLocationIdWithReverseProxyId(tx, reverseProxyId)
if err != nil {
return err
}
if locationId > 0 {
return SharedHTTPLocationDAO.NotifyUpdate(tx, locationId)
}
return nil
}

View File

@@ -12,7 +12,7 @@ import (
func TestServerDAO_ComposeServerConfig(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
config, err := SharedServerDAO.ComposeServerConfig(tx, 1)
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1)
if err != nil {
t.Fatal(err)
}
@@ -22,7 +22,7 @@ func TestServerDAO_ComposeServerConfig(t *testing.T) {
func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
config, err := SharedServerDAO.ComposeServerConfig(tx, 14)
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 14)
if err != nil {
t.Fatal(err)
}
@@ -32,7 +32,7 @@ func TestServerDAO_ComposeServerConfig_AliasServerNames(t *testing.T) {
func TestServerDAO_UpdateServerConfig(t *testing.T) {
dbs.NotifyReady()
var tx *dbs.Tx
config, err := SharedServerDAO.ComposeServerConfig(tx, 1)
config, err := SharedServerDAO.ComposeServerConfigWithServerId(tx, 1)
if err != nil {
t.Fatal(err)
}

View File

@@ -100,7 +100,7 @@ Loop:
for {
select {
case log := <-logChan:
err := models.SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleAPI, log.NodeId, 0, log.Level, log.Tag, log.Description, log.CreatedAt)
err := models.SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleAPI, log.NodeId, log.ServerId, log.OriginId, log.Level, log.Tag, log.Description, log.CreatedAt)
if err != nil {
return err
}

View File

@@ -24,7 +24,7 @@ func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNo
tx := this.NullTx()
for _, nodeLog := range req.NodeLogs {
err := models.SharedNodeLogDAO.CreateLog(tx, nodeLog.Role, nodeLog.NodeId, nodeLog.ServerId, nodeLog.Level, nodeLog.Tag, nodeLog.Description, nodeLog.CreatedAt)
err := models.SharedNodeLogDAO.CreateLog(tx, nodeLog.Role, nodeLog.NodeId, nodeLog.ServerId, nodeLog.OriginId, nodeLog.Level, nodeLog.Tag, nodeLog.Description, nodeLog.CreatedAt)
if err != nil {
return nil, err
}
@@ -41,7 +41,7 @@ func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNode
tx := this.NullTx()
count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId, req.ServerId, req.DayFrom, req.DayTo, req.Keyword, req.Level)
count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId, req.ServerId, req.OriginId, req.DayFrom, req.DayTo, req.Keyword, req.Level)
if err != nil {
return nil, err
}
@@ -57,7 +57,7 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
tx := this.NullTx()
logs, err := models.SharedNodeLogDAO.ListNodeLogs(tx, req.Role, req.NodeId, req.ServerId, req.AllServers, req.DayFrom, req.DayTo, req.Keyword, req.Level, types.Int8(req.FixedState), req.Offset, req.Size)
logs, err := models.SharedNodeLogDAO.ListNodeLogs(tx, req.Role, req.NodeId, req.ServerId, req.OriginId, req.AllServers, req.DayFrom, req.DayTo, req.Keyword, req.Level, types.Int8(req.FixedState), req.Offset, req.Size)
if err != nil {
return nil, err
}

File diff suppressed because one or more lines are too long