Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d31245b4a | ||
|
|
f26bc337c9 | ||
|
|
05c2955211 | ||
|
|
f0ffa48390 | ||
|
|
a472b8a3bd | ||
|
|
cfa2717838 | ||
|
|
aece12854e | ||
|
|
02692029ee |
@@ -6,3 +6,4 @@
|
||||
./build.sh linux mips64
|
||||
./build.sh linux mips64le
|
||||
./build.sh darwin amd64
|
||||
./build.sh darwin arm64
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package teaconst
|
||||
|
||||
const (
|
||||
Version = "0.1.0"
|
||||
Version = "0.1.1"
|
||||
|
||||
ProductName = "Edge API"
|
||||
ProcessName = "edge-api"
|
||||
@@ -18,8 +18,9 @@ const (
|
||||
|
||||
// 其他节点版本号,用来检测是否有需要升级的节点
|
||||
|
||||
NodeVersion = "0.1.0"
|
||||
NodeVersion = "0.1.1"
|
||||
UserNodeVersion = "0.0.7"
|
||||
AuthorityNodeVersion = "0.0.1"
|
||||
MonitorNodeVersion = "0.0.1"
|
||||
DNSNodeVersion = "0.0.1"
|
||||
)
|
||||
|
||||
@@ -218,7 +218,7 @@ func (this *DBNodeInitializer) loop() error {
|
||||
logs.Println("[DB_NODE]create first table in database node failed: " + err.Error())
|
||||
|
||||
// 创建节点日志
|
||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, NodeRoleDatabase, nodeId, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
||||
createLogErr := SharedNodeLogDAO.CreateLog(nil, NodeRoleDatabase, nodeId, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
|
||||
if createLogErr != nil {
|
||||
logs.Println("[NODE_LOG]" + createLogErr.Error())
|
||||
}
|
||||
|
||||
@@ -37,12 +37,12 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// 初始化
|
||||
// Init 初始化
|
||||
func (this *HTTPCachePolicyDAO) Init() {
|
||||
_ = this.DAOObject.Init()
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
// EnableHTTPCachePolicy 启用条目
|
||||
func (this *HTTPCachePolicyDAO) EnableHTTPCachePolicy(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -51,7 +51,7 @@ func (this *HTTPCachePolicyDAO) EnableHTTPCachePolicy(tx *dbs.Tx, id int64) erro
|
||||
return err
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
// DisableHTTPCachePolicy 禁用条目
|
||||
func (this *HTTPCachePolicyDAO) DisableHTTPCachePolicy(tx *dbs.Tx, policyId int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(policyId).
|
||||
@@ -63,7 +63,7 @@ func (this *HTTPCachePolicyDAO) DisableHTTPCachePolicy(tx *dbs.Tx, policyId int6
|
||||
return this.NotifyUpdate(tx, policyId)
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
// FindEnabledHTTPCachePolicy 查找启用中的条目
|
||||
func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicy(tx *dbs.Tx, id int64) (*HTTPCachePolicy, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -75,7 +75,7 @@ func (this *HTTPCachePolicyDAO) FindEnabledHTTPCachePolicy(tx *dbs.Tx, id int64)
|
||||
return result.(*HTTPCachePolicy), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
// FindHTTPCachePolicyName 根据主键查找名称
|
||||
func (this *HTTPCachePolicyDAO) FindHTTPCachePolicyName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -83,7 +83,7 @@ func (this *HTTPCachePolicyDAO) FindHTTPCachePolicyName(tx *dbs.Tx, id int64) (s
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 查找所有可用的缓存策略
|
||||
// FindAllEnabledCachePolicies 查找所有可用的缓存策略
|
||||
func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result []*HTTPCachePolicy, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(HTTPCachePolicyStateEnabled).
|
||||
@@ -93,7 +93,7 @@ func (this *HTTPCachePolicyDAO) FindAllEnabledCachePolicies(tx *dbs.Tx) (result
|
||||
return
|
||||
}
|
||||
|
||||
// 创建缓存策略
|
||||
// CreateCachePolicy 创建缓存策略
|
||||
func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte) (int64, error) {
|
||||
op := NewHTTPCachePolicyOperator()
|
||||
op.State = HTTPCachePolicyStateEnabled
|
||||
@@ -118,7 +118,7 @@ func (this *HTTPCachePolicyDAO) CreateCachePolicy(tx *dbs.Tx, isOn bool, name st
|
||||
return types.Int64(op.Id), nil
|
||||
}
|
||||
|
||||
// 修改缓存策略
|
||||
// UpdateCachePolicy 修改缓存策略
|
||||
func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, isOn bool, name string, description string, capacityJSON []byte, maxKeys int64, maxSizeJSON []byte, storageType string, storageOptionsJSON []byte) error {
|
||||
if policyId <= 0 {
|
||||
return errors.New("invalid policyId")
|
||||
@@ -147,7 +147,7 @@ func (this *HTTPCachePolicyDAO) UpdateCachePolicy(tx *dbs.Tx, policyId int64, is
|
||||
return this.NotifyUpdate(tx, policyId)
|
||||
}
|
||||
|
||||
// 组合配置
|
||||
// ComposeCachePolicy 组合配置
|
||||
func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64) (*serverconfigs.HTTPCachePolicy, error) {
|
||||
policy, err := this.FindEnabledHTTPCachePolicy(tx, policyId)
|
||||
if err != nil {
|
||||
@@ -196,17 +196,27 @@ func (this *HTTPCachePolicyDAO) ComposeCachePolicy(tx *dbs.Tx, policyId int64) (
|
||||
config.Options = m
|
||||
}
|
||||
|
||||
// refs
|
||||
if IsNotNull(policy.Refs) {
|
||||
refs := []*serverconfigs.HTTPCacheRef{}
|
||||
err = json.Unmarshal([]byte(policy.Refs), &refs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.CacheRefs = refs
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// 计算可用缓存策略数量
|
||||
// CountAllEnabledHTTPCachePolicies 计算可用缓存策略数量
|
||||
func (this *HTTPCachePolicyDAO) CountAllEnabledHTTPCachePolicies(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(HTTPCachePolicyStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// 列出单页的缓存策略
|
||||
// ListEnabledHTTPCachePolicies 列出单页的缓存策略
|
||||
func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, offset int64, size int64) ([]*serverconfigs.HTTPCachePolicy, error) {
|
||||
ones, err := this.Query(tx).
|
||||
State(HTTPCachePolicyStateEnabled).
|
||||
@@ -237,7 +247,22 @@ func (this *HTTPCachePolicyDAO) ListEnabledHTTPCachePolicies(tx *dbs.Tx, offset
|
||||
return cachePolicies, nil
|
||||
}
|
||||
|
||||
// 通知更新
|
||||
// UpdatePolicyRefs 设置默认的缓存条件
|
||||
func (this *HTTPCachePolicyDAO) UpdatePolicyRefs(tx *dbs.Tx, policyId int64, refsJSON []byte) error {
|
||||
if len(refsJSON) == 0 {
|
||||
return nil
|
||||
}
|
||||
err := this.Query(tx).
|
||||
Pk(policyId).
|
||||
Set("refs", refsJSON).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.NotifyUpdate(tx, policyId)
|
||||
}
|
||||
|
||||
// NotifyUpdate 通知更新
|
||||
func (this *HTTPCachePolicyDAO) NotifyUpdate(tx *dbs.Tx, policyId int64) error {
|
||||
webIds, err := SharedHTTPWebDAO.FindAllWebIdsWithCachePolicyId(tx, policyId)
|
||||
if err != nil {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package models
|
||||
|
||||
// HTTP缓存策略
|
||||
// HTTPCachePolicy HTTP缓存策略
|
||||
type HTTPCachePolicy struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
AdminId uint32 `field:"adminId"` // 管理员ID
|
||||
@@ -16,6 +16,7 @@ type HTTPCachePolicy struct {
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
State uint8 `field:"state"` // 状态
|
||||
Description string `field:"description"` // 描述
|
||||
Refs string `field:"refs"` // 默认的缓存设置
|
||||
}
|
||||
|
||||
type HTTPCachePolicyOperator struct {
|
||||
@@ -33,6 +34,7 @@ type HTTPCachePolicyOperator struct {
|
||||
CreatedAt interface{} // 创建时间
|
||||
State interface{} // 状态
|
||||
Description interface{} // 描述
|
||||
Refs interface{} // 默认的缓存设置
|
||||
}
|
||||
|
||||
func NewHTTPCachePolicyOperator() *HTTPCachePolicyOperator {
|
||||
|
||||
@@ -34,7 +34,7 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
// EnableNodeGrant 启用条目
|
||||
func (this *NodeGrantDAO) EnableNodeGrant(tx *dbs.Tx, id uint32) (rowsAffected int64, err error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -42,7 +42,7 @@ func (this *NodeGrantDAO) EnableNodeGrant(tx *dbs.Tx, id uint32) (rowsAffected i
|
||||
Update()
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
// DisableNodeGrant 禁用条目
|
||||
func (this *NodeGrantDAO) DisableNodeGrant(tx *dbs.Tx, id int64) (err error) {
|
||||
_, err = this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -51,7 +51,7 @@ func (this *NodeGrantDAO) DisableNodeGrant(tx *dbs.Tx, id int64) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
// FindEnabledNodeGrant 查找启用中的条目
|
||||
func (this *NodeGrantDAO) FindEnabledNodeGrant(tx *dbs.Tx, id int64) (*NodeGrant, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -63,7 +63,7 @@ func (this *NodeGrantDAO) FindEnabledNodeGrant(tx *dbs.Tx, id int64) (*NodeGrant
|
||||
return result.(*NodeGrant), err
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
// FindNodeGrantName 根据主键查找名称
|
||||
func (this *NodeGrantDAO) FindNodeGrantName(tx *dbs.Tx, id uint32) (string, error) {
|
||||
name, err := this.Query(tx).
|
||||
Pk(id).
|
||||
@@ -72,7 +72,7 @@ func (this *NodeGrantDAO) FindNodeGrantName(tx *dbs.Tx, id uint32) (string, erro
|
||||
return name.(string), err
|
||||
}
|
||||
|
||||
// 创建认证信息
|
||||
// CreateGrant 创建认证信息
|
||||
func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, method string, username string, password string, privateKey string, description string, nodeId int64) (grantId int64, err error) {
|
||||
op := NewNodeGrantOperator()
|
||||
op.AdminId = adminId
|
||||
@@ -94,7 +94,7 @@ func (this *NodeGrantDAO) CreateGrant(tx *dbs.Tx, adminId int64, name string, me
|
||||
return types.Int64(op.Id), err
|
||||
}
|
||||
|
||||
// 修改认证信息
|
||||
// UpdateGrant 修改认证信息
|
||||
func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, method string, username string, password string, privateKey string, description string, nodeId int64) error {
|
||||
if grantId <= 0 {
|
||||
return errors.New("invalid grantId")
|
||||
@@ -119,17 +119,26 @@ func (this *NodeGrantDAO) UpdateGrant(tx *dbs.Tx, grantId int64, name string, me
|
||||
return err
|
||||
}
|
||||
|
||||
// 计算所有认证信息数量
|
||||
func (this *NodeGrantDAO) CountAllEnabledGrants(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(NodeGrantStateEnabled).
|
||||
Count()
|
||||
// CountAllEnabledGrants 计算所有认证信息数量
|
||||
func (this *NodeGrantDAO) CountAllEnabledGrants(tx *dbs.Tx, keyword string) (int64, error) {
|
||||
query := this.Query(tx).
|
||||
State(NodeGrantStateEnabled)
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword OR username LIKE :keyword OR description LIKE :keyword)").
|
||||
Param("keyword", "%"+keyword+"%")
|
||||
}
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// 列出单页的认证信息
|
||||
func (this *NodeGrantDAO) ListEnabledGrants(tx *dbs.Tx, offset int64, size int64) (result []*NodeGrant, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(NodeGrantStateEnabled).
|
||||
// ListEnabledGrants 列出单页的认证信息
|
||||
func (this *NodeGrantDAO) ListEnabledGrants(tx *dbs.Tx, keyword string, offset int64, size int64) (result []*NodeGrant, err error) {
|
||||
query := this.Query(tx).
|
||||
State(NodeGrantStateEnabled)
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(name LIKE :keyword OR username LIKE :keyword OR description LIKE :keyword)").
|
||||
Param("keyword", "%"+keyword+"%")
|
||||
}
|
||||
_, err = query.
|
||||
Offset(offset).
|
||||
Size(size).
|
||||
DescPk().
|
||||
@@ -138,7 +147,7 @@ func (this *NodeGrantDAO) ListEnabledGrants(tx *dbs.Tx, offset int64, size int64
|
||||
return
|
||||
}
|
||||
|
||||
// 列出所有的认证信息
|
||||
// FindAllEnabledGrants 列出所有的认证信息
|
||||
func (this *NodeGrantDAO) FindAllEnabledGrants(tx *dbs.Tx) (result []*NodeGrant, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(NodeGrantStateEnabled).
|
||||
|
||||
@@ -2,18 +2,19 @@ package models
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NodeLogDAO dbs.DAO
|
||||
|
||||
const ()
|
||||
|
||||
func NewNodeLogDAO() *NodeLogDAO {
|
||||
return dbs.NewDAO(&NodeLogDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
@@ -33,21 +34,44 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
// 创建日志
|
||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole NodeRole, nodeId int64, level string, tag string, description string, createdAt int64) error {
|
||||
// CreateLog 创建日志
|
||||
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole 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)
|
||||
|
||||
// 检查是否在重复最后一条,避免重复创建
|
||||
lastLog, err := this.Query(tx).
|
||||
Result("id", "hash", "createdAt").
|
||||
DescPk().
|
||||
Find()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if lastLog != nil {
|
||||
nodeLog := lastLog.(*NodeLog)
|
||||
if nodeLog.Hash == hash && time.Now().Unix()-int64(nodeLog.CreatedAt) < 1800 {
|
||||
err = this.Query(tx).
|
||||
Pk(nodeLog.Id).
|
||||
Set("count", dbs.SQL("count+1")).
|
||||
UpdateQuickly()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
op := NewNodeLogOperator()
|
||||
op.Role = nodeRole
|
||||
op.NodeId = nodeId
|
||||
op.ServerId = serverId
|
||||
op.Level = level
|
||||
op.Tag = tag
|
||||
op.Description = description
|
||||
op.CreatedAt = createdAt
|
||||
op.Day = timeutil.FormatTime("Ymd", createdAt)
|
||||
err := this.Save(tx, op)
|
||||
op.Hash = hash
|
||||
err = this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 清除超出一定日期的日志
|
||||
// DeleteExpiredLogs 清除超出一定日期的日志
|
||||
func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
|
||||
if days <= 0 {
|
||||
return errors.New("invalid days '" + strconv.Itoa(days) + "'")
|
||||
@@ -61,19 +85,89 @@ func (this *NodeLogDAO) DeleteExpiredLogs(tx *dbs.Tx, days int) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 计算节点数量
|
||||
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Attr("nodeId", nodeId).
|
||||
Attr("role", role).
|
||||
Count()
|
||||
// CountNodeLogs 计算节点日志数量
|
||||
func (this *NodeLogDAO) CountNodeLogs(tx *dbs.Tx, role string, nodeId int64, serverId int64, dayFrom string, dayTo string, keyword string, level string) (int64, error) {
|
||||
query := this.Query(tx).
|
||||
Attr("role", role)
|
||||
if nodeId > 0 {
|
||||
query.Attr("nodeId", nodeId)
|
||||
} else {
|
||||
switch role {
|
||||
case NodeRoleNode:
|
||||
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
|
||||
}
|
||||
}
|
||||
if serverId > 0 {
|
||||
query.Attr("serverId", serverId)
|
||||
}
|
||||
if len(dayFrom) > 0 {
|
||||
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
||||
query.Gte("day", dayFrom)
|
||||
}
|
||||
if len(dayTo) > 0 {
|
||||
dayTo = strings.ReplaceAll(dayTo, "-", "")
|
||||
query.Lte("day", dayTo)
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(tag LIKE :keyword OR description LIKE :keyword)").
|
||||
Param("keyword", "%"+keyword+"%")
|
||||
}
|
||||
if len(level) > 0 {
|
||||
query.Attr("level", level)
|
||||
}
|
||||
|
||||
return query.Count()
|
||||
}
|
||||
|
||||
// 列出单页日志
|
||||
func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, offset int64, size int64) (result []*NodeLog, err error) {
|
||||
_, err = this.Query(tx).
|
||||
Attr("nodeId", nodeId).
|
||||
Attr("role", role).
|
||||
// ListNodeLogs 列出单页日志
|
||||
func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx,
|
||||
role string,
|
||||
nodeId int64,
|
||||
serverId int64,
|
||||
allServers bool,
|
||||
dayFrom string,
|
||||
dayTo string,
|
||||
keyword string,
|
||||
level string,
|
||||
fixedState configutils.BoolState,
|
||||
offset int64,
|
||||
size int64) (result []*NodeLog, err error) {
|
||||
query := this.Query(tx).
|
||||
Attr("role", role)
|
||||
if nodeId > 0 {
|
||||
query.Attr("nodeId", nodeId)
|
||||
} else {
|
||||
switch role {
|
||||
case NodeRoleNode:
|
||||
query.Where("nodeId IN (SELECT id FROM " + SharedNodeDAO.Table + " WHERE state=1)")
|
||||
}
|
||||
}
|
||||
if serverId > 0 {
|
||||
query.Attr("serverId", serverId)
|
||||
} else if allServers {
|
||||
query.Where("serverId>0")
|
||||
}
|
||||
if fixedState == configutils.BoolStateYes {
|
||||
query.Attr("isFixed", 1)
|
||||
} else if fixedState == configutils.BoolStateNo {
|
||||
query.Attr("isFixed", 0)
|
||||
}
|
||||
if len(dayFrom) > 0 {
|
||||
dayFrom = strings.ReplaceAll(dayFrom, "-", "")
|
||||
query.Gte("day", dayFrom)
|
||||
}
|
||||
if len(dayTo) > 0 {
|
||||
dayTo = strings.ReplaceAll(dayTo, "-", "")
|
||||
query.Lte("day", dayTo)
|
||||
}
|
||||
if len(keyword) > 0 {
|
||||
query.Where("(tag LIKE :keyword OR description LIKE :keyword)").
|
||||
Param("keyword", "%"+keyword+"%")
|
||||
}
|
||||
if len(level) > 0 {
|
||||
query.Attr("level", level)
|
||||
}
|
||||
_, err = query.
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
Slice(&result).
|
||||
@@ -81,3 +175,33 @@ func (this *NodeLogDAO) ListNodeLogs(tx *dbs.Tx, role string, nodeId int64, offs
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateNodeLogFixed 设置节点日志为已修复
|
||||
func (this *NodeLogDAO) UpdateNodeLogFixed(tx *dbs.Tx, logId int64) error {
|
||||
if logId <= 0 {
|
||||
return errors.New("invalid logId")
|
||||
}
|
||||
|
||||
// 我们把相同内容的日志都置为已修复
|
||||
hash, err := this.Query(tx).
|
||||
Pk(logId).
|
||||
Result("hash").
|
||||
FindStringCol("")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(hash) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = this.Query(tx).
|
||||
Attr("hash", hash).
|
||||
Attr("isFixed", false).
|
||||
Set("isFixed", true).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package models
|
||||
|
||||
// 节点日志
|
||||
// NodeLog 节点日志
|
||||
type NodeLog struct {
|
||||
Id uint64 `field:"id"` // ID
|
||||
Role string `field:"role"` // 节点角色
|
||||
@@ -10,6 +10,10 @@ type NodeLog struct {
|
||||
Level string `field:"level"` // 级别
|
||||
NodeId uint32 `field:"nodeId"` // 节点ID
|
||||
Day string `field:"day"` // 日期
|
||||
ServerId uint32 `field:"serverId"` // 服务ID
|
||||
Hash string `field:"hash"` // 信息内容Hash
|
||||
Count uint32 `field:"count"` // 重复次数
|
||||
IsFixed uint8 `field:"isFixed"` // 是否已处理
|
||||
}
|
||||
|
||||
type NodeLogOperator struct {
|
||||
@@ -21,6 +25,10 @@ type NodeLogOperator struct {
|
||||
Level interface{} // 级别
|
||||
NodeId interface{} // 节点ID
|
||||
Day interface{} // 日期
|
||||
ServerId interface{} // 服务ID
|
||||
Hash interface{} // 信息内容Hash
|
||||
Count interface{} // 重复次数
|
||||
IsFixed interface{} // 是否已处理
|
||||
}
|
||||
|
||||
func NewNodeLogOperator() *NodeLogOperator {
|
||||
|
||||
@@ -99,7 +99,7 @@ Loop:
|
||||
for {
|
||||
select {
|
||||
case log := <-logChan:
|
||||
err := models.SharedNodeLogDAO.CreateLog(nil, models.NodeRoleAPI, log.NodeId, log.Level, log.Tag, log.Description, log.CreatedAt)
|
||||
err := models.SharedNodeLogDAO.CreateLog(nil, models.NodeRoleAPI, log.NodeId, 0, log.Level, log.Tag, log.Description, log.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ type HTTPCachePolicyService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 获取所有可用策略
|
||||
// FindAllEnabledHTTPCachePolicies 获取所有可用策略
|
||||
func (this *HTTPCachePolicyService) FindAllEnabledHTTPCachePolicies(ctx context.Context, req *pb.FindAllEnabledHTTPCachePoliciesRequest) (*pb.FindAllEnabledHTTPCachePoliciesResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -37,7 +37,7 @@ func (this *HTTPCachePolicyService) FindAllEnabledHTTPCachePolicies(ctx context.
|
||||
return &pb.FindAllEnabledHTTPCachePoliciesResponse{CachePolicies: result}, nil
|
||||
}
|
||||
|
||||
// 创建缓存策略
|
||||
// CreateHTTPCachePolicy 创建缓存策略
|
||||
func (this *HTTPCachePolicyService) CreateHTTPCachePolicy(ctx context.Context, req *pb.CreateHTTPCachePolicyRequest) (*pb.CreateHTTPCachePolicyResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -54,7 +54,7 @@ func (this *HTTPCachePolicyService) CreateHTTPCachePolicy(ctx context.Context, r
|
||||
return &pb.CreateHTTPCachePolicyResponse{HttpCachePolicyId: policyId}, nil
|
||||
}
|
||||
|
||||
// 修改缓存策略
|
||||
// UpdateHTTPCachePolicy 修改缓存策略
|
||||
func (this *HTTPCachePolicyService) UpdateHTTPCachePolicy(ctx context.Context, req *pb.UpdateHTTPCachePolicyRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -72,7 +72,7 @@ func (this *HTTPCachePolicyService) UpdateHTTPCachePolicy(ctx context.Context, r
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 删除缓存策略
|
||||
// DeleteHTTPCachePolicy 删除缓存策略
|
||||
func (this *HTTPCachePolicyService) DeleteHTTPCachePolicy(ctx context.Context, req *pb.DeleteHTTPCachePolicyRequest) (*pb.RPCSuccess, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -90,7 +90,7 @@ func (this *HTTPCachePolicyService) DeleteHTTPCachePolicy(ctx context.Context, r
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 计算缓存策略数量
|
||||
// CountAllEnabledHTTPCachePolicies 计算缓存策略数量
|
||||
func (this *HTTPCachePolicyService) CountAllEnabledHTTPCachePolicies(ctx context.Context, req *pb.CountAllEnabledHTTPCachePoliciesRequest) (*pb.RPCCountResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -107,7 +107,7 @@ func (this *HTTPCachePolicyService) CountAllEnabledHTTPCachePolicies(ctx context
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// 列出单页的缓存策略
|
||||
// ListEnabledHTTPCachePolicies 列出单页的缓存策略
|
||||
func (this *HTTPCachePolicyService) ListEnabledHTTPCachePolicies(ctx context.Context, req *pb.ListEnabledHTTPCachePoliciesRequest) (*pb.ListEnabledHTTPCachePoliciesResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin)
|
||||
@@ -128,7 +128,7 @@ func (this *HTTPCachePolicyService) ListEnabledHTTPCachePolicies(ctx context.Con
|
||||
return &pb.ListEnabledHTTPCachePoliciesResponse{HttpCachePoliciesJSON: cachePoliciesJSON}, nil
|
||||
}
|
||||
|
||||
// 查找单个缓存策略配置
|
||||
// FindEnabledHTTPCachePolicyConfig 查找单个缓存策略配置
|
||||
func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicyConfig(ctx context.Context, req *pb.FindEnabledHTTPCachePolicyConfigRequest) (*pb.FindEnabledHTTPCachePolicyConfigResponse, error) {
|
||||
// 校验请求
|
||||
_, _, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
||||
@@ -146,7 +146,7 @@ func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicyConfig(ctx context
|
||||
return &pb.FindEnabledHTTPCachePolicyConfigResponse{HttpCachePolicyJSON: cachePolicyJSON}, nil
|
||||
}
|
||||
|
||||
// 查找单个缓存策略信息
|
||||
// FindEnabledHTTPCachePolicy 查找单个缓存策略信息
|
||||
func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicy(ctx context.Context, req *pb.FindEnabledHTTPCachePolicyRequest) (*pb.FindEnabledHTTPCachePolicyResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
@@ -168,3 +168,18 @@ func (this *HTTPCachePolicyService) FindEnabledHTTPCachePolicy(ctx context.Conte
|
||||
IsOn: policy.IsOn == 1,
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// UpdateHTTPCachePolicyRefs 设置缓存策略的默认条件
|
||||
func (this *HTTPCachePolicyService) UpdateHTTPCachePolicyRefs(ctx context.Context, req *pb.UpdateHTTPCachePolicyRefsRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx := this.NullTx()
|
||||
err = models.SharedHTTPCachePolicyDAO.UpdatePolicyRefs(tx, req.HttpCachePolicyId, req.RefsJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ func (this *NodeGrantService) CountAllEnabledNodeGrants(ctx context.Context, req
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
count, err := models.SharedNodeGrantDAO.CountAllEnabledGrants(tx)
|
||||
count, err := models.SharedNodeGrantDAO.CountAllEnabledGrants(tx, req.Keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func (this *NodeGrantService) ListEnabledNodeGrants(ctx context.Context, req *pb
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
grants, err := models.SharedNodeGrantDAO.ListEnabledGrants(tx, req.Offset, req.Size)
|
||||
grants, err := models.SharedNodeGrantDAO.ListEnabledGrants(tx, req.Keyword, req.Offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -99,6 +99,7 @@ func (this *NodeGrantService) ListEnabledNodeGrants(ctx context.Context, req *pb
|
||||
Id: int64(grant.Id),
|
||||
Name: grant.Name,
|
||||
Method: grant.Method,
|
||||
Username: grant.Username,
|
||||
Password: grant.Password,
|
||||
Su: grant.Su == 1,
|
||||
PrivateKey: grant.PrivateKey,
|
||||
@@ -126,6 +127,7 @@ func (this *NodeGrantService) FindAllEnabledNodeGrants(ctx context.Context, req
|
||||
Id: int64(grant.Id),
|
||||
Name: grant.Name,
|
||||
Method: grant.Method,
|
||||
Username: grant.Username,
|
||||
Password: grant.Password,
|
||||
Su: grant.Su == 1,
|
||||
PrivateKey: grant.PrivateKey,
|
||||
|
||||
@@ -5,14 +5,15 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
// 节点日志相关服务
|
||||
// NodeLogService 节点日志相关服务
|
||||
type NodeLogService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 创建日志
|
||||
// CreateNodeLogs 创建日志
|
||||
func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNodeLogsRequest) (*pb.CreateNodeLogsResponse, error) {
|
||||
_, _, err := rpcutils.ValidateRequest(ctx)
|
||||
if err != nil {
|
||||
@@ -22,7 +23,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.Level, nodeLog.Tag, nodeLog.Description, nodeLog.CreatedAt)
|
||||
err := models.SharedNodeLogDAO.CreateLog(tx, nodeLog.Role, nodeLog.NodeId, nodeLog.ServerId, nodeLog.Level, nodeLog.Tag, nodeLog.Description, nodeLog.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -30,7 +31,7 @@ func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNo
|
||||
return &pb.CreateNodeLogsResponse{}, nil
|
||||
}
|
||||
|
||||
// 查询日志数量
|
||||
// CountNodeLogs 查询日志数量
|
||||
func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNodeLogsRequest) (*pb.RPCCountResponse, error) {
|
||||
_, _, err := rpcutils.ValidateRequest(ctx)
|
||||
if err != nil {
|
||||
@@ -39,14 +40,14 @@ func (this *NodeLogService) CountNodeLogs(ctx context.Context, req *pb.CountNode
|
||||
|
||||
tx := this.NullTx()
|
||||
|
||||
count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId)
|
||||
count, err := models.SharedNodeLogDAO.CountNodeLogs(tx, req.Role, req.NodeId, req.ServerId, req.DayFrom, req.DayTo, req.Keyword, req.Level)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.SuccessCount(count)
|
||||
}
|
||||
|
||||
// 列出单页日志
|
||||
// ListNodeLogs 列出单页日志
|
||||
func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLogsRequest) (*pb.ListNodeLogsResponse, error) {
|
||||
_, _, err := rpcutils.ValidateRequest(ctx)
|
||||
if err != nil {
|
||||
@@ -55,7 +56,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.Offset, req.Size)
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -63,13 +64,33 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
|
||||
result := []*pb.NodeLog{}
|
||||
for _, log := range logs {
|
||||
result = append(result, &pb.NodeLog{
|
||||
Id: int64(log.Id),
|
||||
Role: log.Role,
|
||||
Tag: log.Tag,
|
||||
Description: log.Description,
|
||||
Level: log.Level,
|
||||
NodeId: int64(log.NodeId),
|
||||
ServerId: int64(log.ServerId),
|
||||
CreatedAt: int64(log.CreatedAt),
|
||||
Count: types.Int32(log.Count),
|
||||
IsFixed: log.IsFixed == 1,
|
||||
})
|
||||
}
|
||||
return &pb.ListNodeLogsResponse{NodeLogs: result}, nil
|
||||
}
|
||||
|
||||
// FixNodeLog 设置日志为已修复
|
||||
func (this *NodeLogService) FixNodeLog(ctx context.Context, req *pb.FixNodeLogRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = models.SharedNodeLogDAO.UpdateNodeLogFixed(tx, req.NodeLogId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user