国家/地区统计时上传流量、攻击量等信息/多个统计增加自动清理程序

This commit is contained in:
刘祥超
2021-12-05 18:58:14 +08:00
parent 12f816cb5e
commit 364636ea61
13 changed files with 388 additions and 29 deletions

View File

@@ -6,6 +6,7 @@ import (
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/authority"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/regions"
"github.com/TeaOSLab/EdgeAPI/internal/db/models/stats"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
@@ -735,6 +736,34 @@ func (this *AdminService) ComposeAdminDashboard(ctx context.Context, req *pb.Com
}
}
// 流量排行
if isPlus {
totalBytes, err := stats.SharedServerRegionCountryDailyStatDAO.SumDailyTotalBytes(tx, timeutil.Format("Ymd"))
if err != nil {
return nil, err
}
if totalBytes > 0 {
topCountryStats, err := stats.SharedServerRegionCountryDailyStatDAO.ListSumStats(tx, timeutil.Format("Ymd"), "bytes", 0, 100)
if err != nil {
return nil, err
}
for _, stat := range topCountryStats {
countryName, err := regions.SharedRegionCountryDAO.FindRegionCountryName(tx, int64(stat.CountryId))
if err != nil {
return nil, err
}
result.TopCountryStats = append(result.TopCountryStats, &pb.ComposeAdminDashboardResponse_CountryStat{
CountryName: countryName,
Bytes: int64(stat.Bytes),
CountRequests: int64(stat.CountRequests),
Percent: float32(stat.Bytes*100) / float32(totalBytes),
})
}
}
}
// 指标数据
pbCharts, err := this.findMetricDataCharts(tx)
if err != nil {

View File

@@ -1358,9 +1358,17 @@ func (this *ServerService) UploadServerHTTPRequestStat(ctx context.Context, req
return err
}
if countryId > 0 {
key := fmt.Sprintf("%d@%d@%s", result.ServerId, countryId, month)
countryKey := fmt.Sprintf("%d@%d@%s", result.ServerId, countryId, day)
serverStatLocker.Lock()
serverHTTPCountryStatMap[key] += result.Count
stat, ok := serverHTTPCountryStatMap[countryKey]
if !ok {
stat = &TrafficStat{}
serverHTTPCountryStatMap[countryKey] = stat
}
stat.CountRequests += result.CountRequests
stat.Bytes += result.Bytes
stat.CountAttackRequests += result.CountAttackRequests
stat.AttackBytes += result.AttackBytes
serverStatLocker.Unlock()
// 省份
@@ -1372,7 +1380,7 @@ func (this *ServerService) UploadServerHTTPRequestStat(ctx context.Context, req
if provinceId > 0 {
key := fmt.Sprintf("%d@%d@%s", result.ServerId, provinceId, month)
serverStatLocker.Lock()
serverHTTPProvinceStatMap[key] += result.Count
serverHTTPProvinceStatMap[key] += result.CountRequests
serverStatLocker.Unlock()
// 城市
@@ -1384,7 +1392,7 @@ func (this *ServerService) UploadServerHTTPRequestStat(ctx context.Context, req
if cityId > 0 {
key := fmt.Sprintf("%d@%d@%s", result.ServerId, cityId, month)
serverStatLocker.Lock()
serverHTTPCityStatMap[key] += result.Count
serverHTTPCityStatMap[key] += result.CountRequests
serverStatLocker.Unlock()
}
}

View File

@@ -2,6 +2,7 @@ package services
import (
"github.com/TeaOSLab/EdgeAPI/internal/db/models/stats"
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
@@ -12,8 +13,19 @@ import (
"time"
)
type TrafficStat struct {
Bytes int64
CachedBytes int64
CountRequests int64
CountCachedRequests int64
CountAttackRequests int64
AttackBytes int64
PlanId int64
CheckingTrafficLimit bool
}
// HTTP请求统计缓存队列
var serverHTTPCountryStatMap = map[string]int64{} // serverId@countryId@month => count
var serverHTTPCountryStatMap = map[string]*TrafficStat{} // serverId@countryId@day => *TrafficStat
var serverHTTPProvinceStatMap = map[string]int64{} // serverId@provinceId@month => count
var serverHTTPCityStatMap = map[string]int64{} // serverId@cityId@month => count
var serverHTTPProviderStatMap = map[string]int64{} // serverId@providerId@month => count
@@ -49,14 +61,26 @@ func (this *ServerService) dumpServerHTTPStats() error {
{
serverStatLocker.Lock()
m := serverHTTPCountryStatMap
serverHTTPCountryStatMap = map[string]int64{}
serverHTTPCountryStatMap = map[string]*TrafficStat{}
serverStatLocker.Unlock()
for k, count := range m {
for k, stat := range m {
pieces := strings.Split(k, "@")
if len(pieces) != 3 {
continue
}
err := stats.SharedServerRegionCountryMonthlyStatDAO.IncreaseMonthlyCount(nil, types.Int64(pieces[0]), types.Int64(pieces[1]), pieces[2], count)
// Monthly
var day = pieces[2]
if len(day) != 8 {
return errors.New("invalid day '" + day + "'")
}
err := stats.SharedServerRegionCountryMonthlyStatDAO.IncreaseMonthlyCount(nil, types.Int64(pieces[0]), types.Int64(pieces[1]), day[:6], stat.CountRequests)
if err != nil {
return err
}
// Daily
err = stats.SharedServerRegionCountryDailyStatDAO.IncreaseDailyStat(nil, types.Int64(pieces[0]), types.Int64(pieces[1]), day, stat.Bytes, stat.CountRequests, stat.AttackBytes, stat.CountAttackRequests)
if err != nil {
return err
}
@@ -87,12 +111,12 @@ func (this *ServerService) dumpServerHTTPStats() error {
m := serverHTTPCityStatMap
serverHTTPCityStatMap = map[string]int64{}
serverStatLocker.Unlock()
for k, count := range m {
for k, countRequests := range m {
pieces := strings.Split(k, "@")
if len(pieces) != 3 {
continue
}
err := stats.SharedServerRegionCityMonthlyStatDAO.IncreaseMonthlyCount(nil, types.Int64(pieces[0]), types.Int64(pieces[1]), pieces[2], count)
err := stats.SharedServerRegionCityMonthlyStatDAO.IncreaseMonthlyCount(nil, types.Int64(pieces[0]), types.Int64(pieces[1]), pieces[2], countRequests)
if err != nil {
return err
}
@@ -172,13 +196,12 @@ func (this *ServerService) dumpServerHTTPStats() error {
}
// 按小时统计
err = stats.SharedServerHTTPFirewallHourlyStatDAO.IncreaseHourlyCount(nil, types.Int64(pieces[0]), types.Int64(pieces[1]), pieces[2], pieces[3] + timeutil.Format("H"), count)
err = stats.SharedServerHTTPFirewallHourlyStatDAO.IncreaseHourlyCount(nil, types.Int64(pieces[0]), types.Int64(pieces[1]), pieces[2], pieces[3]+timeutil.Format("H"), count)
if err != nil {
return err
}
}
}
return nil
}