mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-05-06 13:48:52 +00:00
improve transfer record logic (#1033)
* improve transfer record logic * refactor * modernize loops * remove unused type conversions * update dependencies * script: keep .gitkeep files * fix * remove clear
This commit is contained in:
+11
-4
@@ -13,7 +13,14 @@ type arSt struct {
|
||||
exp bool
|
||||
}
|
||||
|
||||
func TestCycleRules(t *testing.T) {
|
||||
func TestAlertRules(t *testing.T) {
|
||||
t.Run("CycleRules", testCycleRules)
|
||||
t.Run("OfflineRules", testOfflineRules)
|
||||
t.Run("GeneralRules", testGeneralRules)
|
||||
t.Run("CombinedRules", testCombinedRules)
|
||||
}
|
||||
|
||||
func testCycleRules(t *testing.T) {
|
||||
cases := []arSt{
|
||||
{
|
||||
rule: &AlertRule{
|
||||
@@ -50,7 +57,7 @@ func TestCycleRules(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestOfflineRules(t *testing.T) {
|
||||
func testOfflineRules(t *testing.T) {
|
||||
cases := []arSt{
|
||||
{
|
||||
rule: &AlertRule{
|
||||
@@ -117,7 +124,7 @@ func TestOfflineRules(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeneralRules(t *testing.T) {
|
||||
func testGeneralRules(t *testing.T) {
|
||||
cases := []arSt{
|
||||
{
|
||||
rule: &AlertRule{
|
||||
@@ -193,7 +200,7 @@ func TestGeneralRules(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCombinedRules(t *testing.T) {
|
||||
func testCombinedRules(t *testing.T) {
|
||||
cases := []arSt{
|
||||
{
|
||||
rule: &AlertRule{
|
||||
|
||||
+4
-7
@@ -93,21 +93,21 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server,
|
||||
src = float64(server.LastActive.Unix())
|
||||
}
|
||||
case "transfer_in_cycle":
|
||||
src = float64(utils.Uint64SubInt64(server.State.NetInTransfer, server.PrevTransferInSnapshot))
|
||||
src = float64(utils.SubUintChecked(server.State.NetInTransfer, server.PrevTransferInSnapshot))
|
||||
if u.CycleInterval != 0 {
|
||||
var res NResult
|
||||
db.Model(&Transfer{}).Select("SUM(`in`) AS n").Where("datetime(`created_at`) >= datetime(?) AND server_id = ?", u.GetTransferDurationStart().UTC(), server.ID).Scan(&res)
|
||||
src += float64(res.N)
|
||||
}
|
||||
case "transfer_out_cycle":
|
||||
src = float64(utils.Uint64SubInt64(server.State.NetOutTransfer, server.PrevTransferOutSnapshot))
|
||||
src = float64(utils.SubUintChecked(server.State.NetOutTransfer, server.PrevTransferOutSnapshot))
|
||||
if u.CycleInterval != 0 {
|
||||
var res NResult
|
||||
db.Model(&Transfer{}).Select("SUM(`out`) AS n").Where("datetime(`created_at`) >= datetime(?) AND server_id = ?", u.GetTransferDurationStart().UTC(), server.ID).Scan(&res)
|
||||
src += float64(res.N)
|
||||
}
|
||||
case "transfer_all_cycle":
|
||||
src = float64(utils.Uint64SubInt64(server.State.NetOutTransfer, server.PrevTransferOutSnapshot) + utils.Uint64SubInt64(server.State.NetInTransfer, server.PrevTransferInSnapshot))
|
||||
src = float64(utils.SubUintChecked(server.State.NetOutTransfer, server.PrevTransferOutSnapshot) + utils.SubUintChecked(server.State.NetInTransfer, server.PrevTransferInSnapshot))
|
||||
if u.CycleInterval != 0 {
|
||||
var res NResult
|
||||
db.Model(&Transfer{}).Select("SUM(`in`+`out`) AS n").Where("datetime(`created_at`) >= datetime(?) AND server_id = ?", u.GetTransferDurationStart().UTC(), server.ID).Scan(&res)
|
||||
@@ -139,10 +139,7 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server,
|
||||
|
||||
// 循环区间流量检测 · 更新下次需要检测时间
|
||||
if u.IsTransferDurationRule() {
|
||||
seconds := 1800 * ((u.Max - src) / u.Max)
|
||||
if seconds < 180 {
|
||||
seconds = 180
|
||||
}
|
||||
seconds := max(1800*((u.Max-src)/u.Max), 180)
|
||||
if u.NextTransferAt == nil {
|
||||
u.NextTransferAt = make(map[uint64]time.Time)
|
||||
}
|
||||
|
||||
+2
-2
@@ -35,8 +35,8 @@ type Server struct {
|
||||
TaskStream pb.NezhaService_RequestTaskServer `gorm:"-" json:"-"`
|
||||
ConfigCache chan any `gorm:"-" json:"-"`
|
||||
|
||||
PrevTransferInSnapshot int64 `gorm:"-" json:"-"` // 上次数据点时的入站使用量
|
||||
PrevTransferOutSnapshot int64 `gorm:"-" json:"-"` // 上次数据点时的出站使用量
|
||||
PrevTransferInSnapshot uint64 `gorm:"-" json:"-"` // 上次数据点时的入站使用量
|
||||
PrevTransferOutSnapshot uint64 `gorm:"-" json:"-"` // 上次数据点时的出站使用量
|
||||
}
|
||||
|
||||
func InitServer(s *Server) {
|
||||
|
||||
Reference in New Issue
Block a user