mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	网站列表增加QPS和攻击QPS信息
This commit is contained in:
		@@ -903,6 +903,22 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
 | 
				
			|||||||
		query.Desc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
 | 
							query.Desc("IF(FIND_IN_SET(bandwidthTime, :times), bandwidthBytes, 0)")
 | 
				
			||||||
		query.Param("times", strings.Join(times, ","))
 | 
							query.Param("times", strings.Join(times, ","))
 | 
				
			||||||
		query.DescPk()
 | 
							query.DescPk()
 | 
				
			||||||
 | 
						case "requestsAsc":
 | 
				
			||||||
 | 
							query.Asc("IF(FIND_IN_SET(bandwidthTime, :times), countRequests, 0)")
 | 
				
			||||||
 | 
							query.Param("times", strings.Join(times, ","))
 | 
				
			||||||
 | 
							query.DescPk()
 | 
				
			||||||
 | 
						case "requestsDesc":
 | 
				
			||||||
 | 
							query.Desc("IF(FIND_IN_SET(bandwidthTime, :times), countRequests, 0)")
 | 
				
			||||||
 | 
							query.Param("times", strings.Join(times, ","))
 | 
				
			||||||
 | 
							query.DescPk()
 | 
				
			||||||
 | 
						case "attackRequestsAsc":
 | 
				
			||||||
 | 
							query.Asc("IF(FIND_IN_SET(bandwidthTime, :times), countAttackRequests, 0)")
 | 
				
			||||||
 | 
							query.Param("times", strings.Join(times, ","))
 | 
				
			||||||
 | 
							query.DescPk()
 | 
				
			||||||
 | 
						case "attackRequestsDesc":
 | 
				
			||||||
 | 
							query.Desc("IF(FIND_IN_SET(bandwidthTime, :times), countAttackRequests, 0)")
 | 
				
			||||||
 | 
							query.Param("times", strings.Join(times, ","))
 | 
				
			||||||
 | 
							query.DescPk()
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		query.DescPk()
 | 
							query.DescPk()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -913,6 +929,8 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in
 | 
				
			|||||||
	for _, server := range result {
 | 
						for _, server := range result {
 | 
				
			||||||
		if len(server.BandwidthTime) > 0 && !lists.ContainsString(times, server.BandwidthTime) {
 | 
							if len(server.BandwidthTime) > 0 && !lists.ContainsString(times, server.BandwidthTime) {
 | 
				
			||||||
			server.BandwidthBytes = 0
 | 
								server.BandwidthBytes = 0
 | 
				
			||||||
 | 
								server.CountRequests = 0
 | 
				
			||||||
 | 
								server.CountAttackRequests = 0
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -2737,7 +2755,7 @@ func (this *ServerDAO) FindUserServerClusterIds(tx *dbs.Tx, userId int64) ([]int
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// UpdateServerBandwidth 更新服务带宽
 | 
					// UpdateServerBandwidth 更新服务带宽
 | 
				
			||||||
// fullTime YYYYMMDDHHII
 | 
					// fullTime YYYYMMDDHHII
 | 
				
			||||||
func (this *ServerDAO) UpdateServerBandwidth(tx *dbs.Tx, serverId int64, fullTime string, bandwidthBytes int64) error {
 | 
					func (this *ServerDAO) UpdateServerBandwidth(tx *dbs.Tx, serverId int64, fullTime string, bandwidthBytes int64, countRequests int64, countAttackRequests int64) error {
 | 
				
			||||||
	if serverId <= 0 {
 | 
						if serverId <= 0 {
 | 
				
			||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -2758,13 +2776,19 @@ func (this *ServerDAO) UpdateServerBandwidth(tx *dbs.Tx, serverId int64, fullTim
 | 
				
			|||||||
			Pk(serverId).
 | 
								Pk(serverId).
 | 
				
			||||||
			Set("bandwidthTime", fullTime).
 | 
								Set("bandwidthTime", fullTime).
 | 
				
			||||||
			Set("bandwidthBytes", bandwidthBytes).
 | 
								Set("bandwidthBytes", bandwidthBytes).
 | 
				
			||||||
 | 
								Set("countRequests", countRequests).
 | 
				
			||||||
 | 
								Set("countAttackRequests", countAttackRequests).
 | 
				
			||||||
			UpdateQuickly()
 | 
								UpdateQuickly()
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		return this.Query(tx).
 | 
							return this.Query(tx).
 | 
				
			||||||
			Pk(serverId).
 | 
								Pk(serverId).
 | 
				
			||||||
			Set("bandwidthTime", fullTime).
 | 
								Set("bandwidthTime", fullTime).
 | 
				
			||||||
			Set("bandwidthBytes", dbs.SQL("bandwidthBytes+:bytes")).
 | 
								Set("bandwidthBytes", dbs.SQL("bandwidthBytes+:bytes")).
 | 
				
			||||||
 | 
								Set("countRequests", dbs.SQL("countRequests+:countRequests")).
 | 
				
			||||||
 | 
								Set("countAttackRequests", dbs.SQL("countAttackRequests+:countAttackRequests")).
 | 
				
			||||||
			Param("bytes", bandwidthBytes).
 | 
								Param("bytes", bandwidthBytes).
 | 
				
			||||||
 | 
								Param("countRequests", countRequests).
 | 
				
			||||||
 | 
								Param("countAttackRequests", countAttackRequests).
 | 
				
			||||||
			UpdateQuickly()
 | 
								UpdateQuickly()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -326,7 +326,7 @@ func TestServerDAO_FindBool(t *testing.T) {
 | 
				
			|||||||
func TestServerDAO_UpdateServerBandwidth(t *testing.T) {
 | 
					func TestServerDAO_UpdateServerBandwidth(t *testing.T) {
 | 
				
			||||||
	var dao = models.NewServerDAO()
 | 
						var dao = models.NewServerDAO()
 | 
				
			||||||
	var tx *dbs.Tx
 | 
						var tx *dbs.Tx
 | 
				
			||||||
	err := dao.UpdateServerBandwidth(tx, 1, timeutil.FormatTime("YmdHi", time.Now().Unix()/300*300), 1024)
 | 
						err := dao.UpdateServerBandwidth(tx, 1, timeutil.FormatTime("YmdHi", time.Now().Unix()/300*300), 1024, 1, 0)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		t.Fatal(err)
 | 
							t.Fatal(err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,6 +2,57 @@ package models
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import "github.com/iwind/TeaGo/dbs"
 | 
					import "github.com/iwind/TeaGo/dbs"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						ServerField_Id                  dbs.FieldName = "id"                  // ID
 | 
				
			||||||
 | 
						ServerField_IsOn                dbs.FieldName = "isOn"                // 是否启用
 | 
				
			||||||
 | 
						ServerField_UserId              dbs.FieldName = "userId"              // 用户ID
 | 
				
			||||||
 | 
						ServerField_AdminId             dbs.FieldName = "adminId"             // 管理员ID
 | 
				
			||||||
 | 
						ServerField_Type                dbs.FieldName = "type"                // 服务类型
 | 
				
			||||||
 | 
						ServerField_Name                dbs.FieldName = "name"                // 名称
 | 
				
			||||||
 | 
						ServerField_Description         dbs.FieldName = "description"         // 描述
 | 
				
			||||||
 | 
						ServerField_PlainServerNames    dbs.FieldName = "plainServerNames"    // 扁平化域名列表
 | 
				
			||||||
 | 
						ServerField_ServerNames         dbs.FieldName = "serverNames"         // 域名列表
 | 
				
			||||||
 | 
						ServerField_AuditingAt          dbs.FieldName = "auditingAt"          // 审核提交时间
 | 
				
			||||||
 | 
						ServerField_AuditingServerNames dbs.FieldName = "auditingServerNames" // 审核中的域名
 | 
				
			||||||
 | 
						ServerField_IsAuditing          dbs.FieldName = "isAuditing"          // 是否正在审核
 | 
				
			||||||
 | 
						ServerField_AuditingResult      dbs.FieldName = "auditingResult"      // 审核结果
 | 
				
			||||||
 | 
						ServerField_Http                dbs.FieldName = "http"                // HTTP配置
 | 
				
			||||||
 | 
						ServerField_Https               dbs.FieldName = "https"               // HTTPS配置
 | 
				
			||||||
 | 
						ServerField_Tcp                 dbs.FieldName = "tcp"                 // TCP配置
 | 
				
			||||||
 | 
						ServerField_Tls                 dbs.FieldName = "tls"                 // TLS配置
 | 
				
			||||||
 | 
						ServerField_Unix                dbs.FieldName = "unix"                // Unix配置
 | 
				
			||||||
 | 
						ServerField_Udp                 dbs.FieldName = "udp"                 // UDP配置
 | 
				
			||||||
 | 
						ServerField_WebId               dbs.FieldName = "webId"               // WEB配置
 | 
				
			||||||
 | 
						ServerField_ReverseProxy        dbs.FieldName = "reverseProxy"        // 反向代理配置
 | 
				
			||||||
 | 
						ServerField_GroupIds            dbs.FieldName = "groupIds"            // 分组ID列表
 | 
				
			||||||
 | 
						ServerField_Config              dbs.FieldName = "config"              // 服务配置,自动生成
 | 
				
			||||||
 | 
						ServerField_ConfigMd5           dbs.FieldName = "configMd5"           // Md5
 | 
				
			||||||
 | 
						ServerField_ClusterId           dbs.FieldName = "clusterId"           // 集群ID
 | 
				
			||||||
 | 
						ServerField_IncludeNodes        dbs.FieldName = "includeNodes"        // 部署条件
 | 
				
			||||||
 | 
						ServerField_ExcludeNodes        dbs.FieldName = "excludeNodes"        // 节点排除条件
 | 
				
			||||||
 | 
						ServerField_Version             dbs.FieldName = "version"             // 版本号
 | 
				
			||||||
 | 
						ServerField_CreatedAt           dbs.FieldName = "createdAt"           // 创建时间
 | 
				
			||||||
 | 
						ServerField_State               dbs.FieldName = "state"               // 状态
 | 
				
			||||||
 | 
						ServerField_DnsName             dbs.FieldName = "dnsName"             // DNS名称
 | 
				
			||||||
 | 
						ServerField_TcpPorts            dbs.FieldName = "tcpPorts"            // 所包含TCP端口
 | 
				
			||||||
 | 
						ServerField_UdpPorts            dbs.FieldName = "udpPorts"            // 所包含UDP端口
 | 
				
			||||||
 | 
						ServerField_SupportCNAME        dbs.FieldName = "supportCNAME"        // 允许CNAME不在域名名单
 | 
				
			||||||
 | 
						ServerField_TrafficLimit        dbs.FieldName = "trafficLimit"        // 流量限制
 | 
				
			||||||
 | 
						ServerField_TrafficDay          dbs.FieldName = "trafficDay"          // YYYYMMDD
 | 
				
			||||||
 | 
						ServerField_TrafficMonth        dbs.FieldName = "trafficMonth"        // YYYYMM
 | 
				
			||||||
 | 
						ServerField_TotalDailyTraffic   dbs.FieldName = "totalDailyTraffic"   // 日流量
 | 
				
			||||||
 | 
						ServerField_TotalMonthlyTraffic dbs.FieldName = "totalMonthlyTraffic" // 月流量
 | 
				
			||||||
 | 
						ServerField_TrafficLimitStatus  dbs.FieldName = "trafficLimitStatus"  // 流量限制状态
 | 
				
			||||||
 | 
						ServerField_TotalTraffic        dbs.FieldName = "totalTraffic"        // 总流量
 | 
				
			||||||
 | 
						ServerField_UserPlanId          dbs.FieldName = "userPlanId"          // 所属套餐ID
 | 
				
			||||||
 | 
						ServerField_LastUserPlanId      dbs.FieldName = "lastUserPlanId"      // 上一次使用的套餐
 | 
				
			||||||
 | 
						ServerField_Uam                 dbs.FieldName = "uam"                 // UAM设置
 | 
				
			||||||
 | 
						ServerField_BandwidthTime       dbs.FieldName = "bandwidthTime"       // 带宽更新时间,YYYYMMDDHHII
 | 
				
			||||||
 | 
						ServerField_BandwidthBytes      dbs.FieldName = "bandwidthBytes"      // 最近带宽峰值
 | 
				
			||||||
 | 
						ServerField_CountAttackRequests dbs.FieldName = "countAttackRequests" // 最近攻击请求数
 | 
				
			||||||
 | 
						ServerField_CountRequests       dbs.FieldName = "countRequests"       // 最近总请求数
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Server 服务
 | 
					// Server 服务
 | 
				
			||||||
type Server struct {
 | 
					type Server struct {
 | 
				
			||||||
	Id                  uint32   `field:"id"`                  // ID
 | 
						Id                  uint32   `field:"id"`                  // ID
 | 
				
			||||||
@@ -50,6 +101,8 @@ type Server struct {
 | 
				
			|||||||
	Uam                 dbs.JSON `field:"uam"`                 // UAM设置
 | 
						Uam                 dbs.JSON `field:"uam"`                 // UAM设置
 | 
				
			||||||
	BandwidthTime       string   `field:"bandwidthTime"`       // 带宽更新时间,YYYYMMDDHHII
 | 
						BandwidthTime       string   `field:"bandwidthTime"`       // 带宽更新时间,YYYYMMDDHHII
 | 
				
			||||||
	BandwidthBytes      uint64   `field:"bandwidthBytes"`      // 最近带宽峰值
 | 
						BandwidthBytes      uint64   `field:"bandwidthBytes"`      // 最近带宽峰值
 | 
				
			||||||
 | 
						CountAttackRequests uint64   `field:"countAttackRequests"` // 最近攻击请求数
 | 
				
			||||||
 | 
						CountRequests       uint64   `field:"countRequests"`       // 最近总请求数
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ServerOperator struct {
 | 
					type ServerOperator struct {
 | 
				
			||||||
@@ -99,6 +152,8 @@ type ServerOperator struct {
 | 
				
			|||||||
	Uam                 any // UAM设置
 | 
						Uam                 any // UAM设置
 | 
				
			||||||
	BandwidthTime       any // 带宽更新时间,YYYYMMDDHHII
 | 
						BandwidthTime       any // 带宽更新时间,YYYYMMDDHHII
 | 
				
			||||||
	BandwidthBytes      any // 最近带宽峰值
 | 
						BandwidthBytes      any // 最近带宽峰值
 | 
				
			||||||
 | 
						CountAttackRequests any // 最近攻击请求数
 | 
				
			||||||
 | 
						CountRequests       any // 最近总请求数
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewServerOperator() *ServerOperator {
 | 
					func NewServerOperator() *ServerOperator {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1301,6 +1301,14 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
 | 
				
			|||||||
		order = "trafficOutAsc"
 | 
							order = "trafficOutAsc"
 | 
				
			||||||
	} else if req.TrafficOutDesc {
 | 
						} else if req.TrafficOutDesc {
 | 
				
			||||||
		order = "trafficOutDesc"
 | 
							order = "trafficOutDesc"
 | 
				
			||||||
 | 
						} else if req.RequestsAsc {
 | 
				
			||||||
 | 
							order = "requestsAsc"
 | 
				
			||||||
 | 
						} else if req.RequestsDesc {
 | 
				
			||||||
 | 
							order = "requestsDesc"
 | 
				
			||||||
 | 
						} else if req.AttackRequestsAsc {
 | 
				
			||||||
 | 
							order = "attackRequestsAsc"
 | 
				
			||||||
 | 
						} else if req.AttackRequestsDesc {
 | 
				
			||||||
 | 
							order = "attackRequestsDesc"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	servers, err := models.SharedServerDAO.ListEnabledServersMatch(tx, req.Offset, req.Size, req.ServerGroupId, req.Keyword, req.UserId, req.NodeClusterId, req.AuditingFlag, utils.SplitStrings(req.ProtocolFamily, ","), order)
 | 
						servers, err := models.SharedServerDAO.ListEnabledServersMatch(tx, req.Offset, req.Size, req.ServerGroupId, req.Keyword, req.UserId, req.NodeClusterId, req.AuditingFlag, utils.SplitStrings(req.ProtocolFamily, ","), order)
 | 
				
			||||||
@@ -1423,6 +1431,8 @@ func (this *ServerService) ListEnabledServersMatch(ctx context.Context, req *pb.
 | 
				
			|||||||
			User:                pbUser,
 | 
								User:                pbUser,
 | 
				
			||||||
			BandwidthTime:       server.BandwidthTime,
 | 
								BandwidthTime:       server.BandwidthTime,
 | 
				
			||||||
			BandwidthBytes:      int64(server.BandwidthBytes),
 | 
								BandwidthBytes:      int64(server.BandwidthBytes),
 | 
				
			||||||
 | 
								CountRequests:       int64(server.CountRequests),
 | 
				
			||||||
 | 
								CountAttackRequests: int64(server.CountAttackRequests),
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -65,12 +65,12 @@ func init() {
 | 
				
			|||||||
					for _, stat := range m {
 | 
										for _, stat := range m {
 | 
				
			||||||
						// 更新服务的带宽峰值
 | 
											// 更新服务的带宽峰值
 | 
				
			||||||
						if stat.ServerId > 0 {
 | 
											if stat.ServerId > 0 {
 | 
				
			||||||
							err := models.SharedServerBandwidthStatDAO.UpdateServerBandwidth(tx, stat.UserId, stat.ServerId, stat.NodeRegionId, stat.Day, stat.TimeAt, stat.Bytes, stat.TotalBytes, stat.CachedBytes, stat.AttackBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests)
 | 
												err = models.SharedServerBandwidthStatDAO.UpdateServerBandwidth(tx, stat.UserId, stat.ServerId, stat.NodeRegionId, stat.Day, stat.TimeAt, stat.Bytes, stat.TotalBytes, stat.CachedBytes, stat.AttackBytes, stat.CountRequests, stat.CountCachedRequests, stat.CountAttackRequests)
 | 
				
			||||||
							if err != nil {
 | 
												if err != nil {
 | 
				
			||||||
								remotelogs.Error("ServerBandwidthStatService", "dump bandwidth stats failed: "+err.Error())
 | 
													remotelogs.Error("ServerBandwidthStatService", "dump bandwidth stats failed: "+err.Error())
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							err = models.SharedServerDAO.UpdateServerBandwidth(tx, stat.ServerId, stat.Day+stat.TimeAt, stat.Bytes)
 | 
												err = models.SharedServerDAO.UpdateServerBandwidth(tx, stat.ServerId, stat.Day+stat.TimeAt, stat.Bytes, stat.CountRequests, stat.CountAttackRequests)
 | 
				
			||||||
							if err != nil {
 | 
												if err != nil {
 | 
				
			||||||
								remotelogs.Error("ServerBandwidthStatService", "update server bandwidth failed: "+err.Error())
 | 
													remotelogs.Error("ServerBandwidthStatService", "update server bandwidth failed: "+err.Error())
 | 
				
			||||||
							}
 | 
												}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user