mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	优化代码
This commit is contained in:
		@@ -120,8 +120,8 @@ func (this *NodeRegionDAO) FindAllEnabledRegionPrices(tx *dbs.Tx) (result []*Nod
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindAllEnabledAndOnRegions 列出所有启用的区域
 | 
			
		||||
func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
 | 
			
		||||
// FindAllAvailableRegions 列出所有启用的区域
 | 
			
		||||
func (this *NodeRegionDAO) FindAllAvailableRegions(tx *dbs.Tx) (result []*NodeRegion, err error) {
 | 
			
		||||
	_, err = this.Query(tx).
 | 
			
		||||
		State(NodeRegionStateEnabled).
 | 
			
		||||
		Attr("isOn", true).
 | 
			
		||||
@@ -132,6 +132,25 @@ func (this *NodeRegionDAO) FindAllEnabledAndOnRegions(tx *dbs.Tx) (result []*Nod
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindAllRegionIds 查找所有区域ID
 | 
			
		||||
func (this *NodeRegionDAO) FindAllRegionIds(tx *dbs.Tx) ([]int64, error) {
 | 
			
		||||
	ones, err := this.Query(tx).
 | 
			
		||||
		ResultPk().
 | 
			
		||||
		State(NodeRegionStateEnabled).
 | 
			
		||||
		Desc("order").
 | 
			
		||||
		AscPk().
 | 
			
		||||
		FindAll()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var regionIds = []int64{}
 | 
			
		||||
	for _, one := range ones {
 | 
			
		||||
		regionIds = append(regionIds, int64(one.(*NodeRegion).Id))
 | 
			
		||||
	}
 | 
			
		||||
	return regionIds, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateRegionOrders 排序
 | 
			
		||||
func (this *NodeRegionDAO) UpdateRegionOrders(tx *dbs.Tx, regionIds []int64) error {
 | 
			
		||||
	order := len(regionIds)
 | 
			
		||||
 
 | 
			
		||||
@@ -86,7 +86,7 @@ func (this *ServerDailyStatDAO) SaveStats(tx *dbs.Tx, stats []*pb.ServerDailySta
 | 
			
		||||
			InsertOrUpdate(maps.Map{
 | 
			
		||||
				"userId":              serverUserId,
 | 
			
		||||
				"serverId":            stat.ServerId,
 | 
			
		||||
				"regionId":            stat.RegionId,
 | 
			
		||||
				"regionId":            stat.NodeRegionId,
 | 
			
		||||
				"bytes":               stat.Bytes,
 | 
			
		||||
				"cachedBytes":         stat.CachedBytes,
 | 
			
		||||
				"countRequests":       stat.CountRequests,
 | 
			
		||||
 
 | 
			
		||||
@@ -2,27 +2,29 @@ package models
 | 
			
		||||
 | 
			
		||||
// UserTrafficBill 用户流量/带宽账单
 | 
			
		||||
type UserTrafficBill struct {
 | 
			
		||||
	Id           uint64  `field:"id"`           // ID
 | 
			
		||||
	BillId       uint64  `field:"billId"`       // 主账单ID
 | 
			
		||||
	RegionId     uint32  `field:"regionId"`     // 区域ID
 | 
			
		||||
	Amount       float64 `field:"amount"`       // 金额
 | 
			
		||||
	BandwidthMB  float64 `field:"bandwidthMB"`  // 带宽MB
 | 
			
		||||
	TrafficGB    float64 `field:"trafficGB"`    // 流量GB
 | 
			
		||||
	PricePerUnit float64 `field:"pricePerUnit"` // 单位价格
 | 
			
		||||
	PriceType    string  `field:"priceType"`    // 计费方式:traffic|bandwidth
 | 
			
		||||
	State        uint8   `field:"state"`        // 状态
 | 
			
		||||
	Id                  uint64  `field:"id"`                  // ID
 | 
			
		||||
	BillId              uint64  `field:"billId"`              // 主账单ID
 | 
			
		||||
	RegionId            uint32  `field:"regionId"`            // 区域ID
 | 
			
		||||
	Amount              float64 `field:"amount"`              // 金额
 | 
			
		||||
	BandwidthMB         float64 `field:"bandwidthMB"`         // 带宽MB
 | 
			
		||||
	BandwidthPercentile uint8   `field:"bandwidthPercentile"` // 带宽百分位
 | 
			
		||||
	TrafficGB           float64 `field:"trafficGB"`           // 流量GB
 | 
			
		||||
	PricePerUnit        float64 `field:"pricePerUnit"`        // 单位价格
 | 
			
		||||
	PriceType           string  `field:"priceType"`           // 计费方式:traffic|bandwidth
 | 
			
		||||
	State               uint8   `field:"state"`               // 状态
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type UserTrafficBillOperator struct {
 | 
			
		||||
	Id           any // ID
 | 
			
		||||
	BillId       any // 主账单ID
 | 
			
		||||
	RegionId     any // 区域ID
 | 
			
		||||
	Amount       any // 金额
 | 
			
		||||
	BandwidthMB  any // 带宽MB
 | 
			
		||||
	TrafficGB    any // 流量GB
 | 
			
		||||
	PricePerUnit any // 单位价格
 | 
			
		||||
	PriceType    any // 计费方式:traffic|bandwidth
 | 
			
		||||
	State        any // 状态
 | 
			
		||||
	Id                  any // ID
 | 
			
		||||
	BillId              any // 主账单ID
 | 
			
		||||
	RegionId            any // 区域ID
 | 
			
		||||
	Amount              any // 金额
 | 
			
		||||
	BandwidthMB         any // 带宽MB
 | 
			
		||||
	BandwidthPercentile any // 带宽百分位
 | 
			
		||||
	TrafficGB           any // 流量GB
 | 
			
		||||
	PricePerUnit        any // 单位价格
 | 
			
		||||
	PriceType           any // 计费方式:traffic|bandwidth
 | 
			
		||||
	State               any // 状态
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewUserTrafficBillOperator() *UserTrafficBillOperator {
 | 
			
		||||
 
 | 
			
		||||
@@ -94,11 +94,11 @@ func (this *NodeRegionService) FindAllAvailableNodeRegions(ctx context.Context,
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
 | 
			
		||||
	regions, err := models.SharedNodeRegionDAO.FindAllEnabledAndOnRegions(tx)
 | 
			
		||||
	regions, err := models.SharedNodeRegionDAO.FindAllAvailableRegions(tx)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	result := []*pb.NodeRegion{}
 | 
			
		||||
	var result = []*pb.NodeRegion{}
 | 
			
		||||
	for _, region := range regions {
 | 
			
		||||
		result = append(result, &pb.NodeRegion{
 | 
			
		||||
			Id:          int64(region.Id),
 | 
			
		||||
 
 | 
			
		||||
@@ -76,7 +76,7 @@ func init() {
 | 
			
		||||
 | 
			
		||||
						// 更新用户的带宽峰值
 | 
			
		||||
						if stat.UserId > 0 {
 | 
			
		||||
							err = models.SharedUserBandwidthStatDAO.UpdateUserBandwidth(tx, stat.UserId, stat.RegionId, stat.Day, stat.TimeAt, stat.Bytes)
 | 
			
		||||
							err = models.SharedUserBandwidthStatDAO.UpdateUserBandwidth(tx, stat.UserId, stat.NodeRegionId, stat.Day, stat.TimeAt, stat.Bytes)
 | 
			
		||||
							if err != nil {
 | 
			
		||||
								remotelogs.Error("SharedUserBandwidthStatDAO", "dump bandwidth stats failed: "+err.Error())
 | 
			
		||||
							}
 | 
			
		||||
@@ -119,20 +119,20 @@ func (this *ServerBandwidthStatService) UploadServerBandwidthStats(ctx context.C
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, stat := range req.ServerBandwidthStats {
 | 
			
		||||
		var key = ServerBandwidthCacheKey(stat.ServerId, stat.RegionId, stat.Day, stat.TimeAt)
 | 
			
		||||
		var key = ServerBandwidthCacheKey(stat.ServerId, stat.NodeRegionId, stat.Day, stat.TimeAt)
 | 
			
		||||
		serverBandwidthStatsLocker.Lock()
 | 
			
		||||
		oldStat, ok := serverBandwidthStatsMap[key]
 | 
			
		||||
		if ok {
 | 
			
		||||
			oldStat.Bytes += stat.Bytes
 | 
			
		||||
		} else {
 | 
			
		||||
			serverBandwidthStatsMap[key] = &pb.ServerBandwidthStat{
 | 
			
		||||
				Id:       0,
 | 
			
		||||
				RegionId: stat.RegionId,
 | 
			
		||||
				UserId:   stat.UserId,
 | 
			
		||||
				ServerId: stat.ServerId,
 | 
			
		||||
				Day:      stat.Day,
 | 
			
		||||
				TimeAt:   stat.TimeAt,
 | 
			
		||||
				Bytes:    stat.Bytes,
 | 
			
		||||
				Id:           0,
 | 
			
		||||
				NodeRegionId: stat.NodeRegionId,
 | 
			
		||||
				UserId:       stat.UserId,
 | 
			
		||||
				ServerId:     stat.ServerId,
 | 
			
		||||
				Day:          stat.Day,
 | 
			
		||||
				TimeAt:       stat.TimeAt,
 | 
			
		||||
				Bytes:        stat.Bytes,
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		serverBandwidthStatsLocker.Unlock()
 | 
			
		||||
@@ -271,10 +271,10 @@ func (this *ServerBandwidthStatService) FindDailyServerBandwidthStatsBetweenDays
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	} else { // 用户统计
 | 
			
		||||
		pbStats, err = models.SharedUserBandwidthStatDAO.FindUserBandwidthStatsBetweenDays(tx, req.UserId, req.RegionId, req.DayFrom, req.DayTo)
 | 
			
		||||
		pbStats, err = models.SharedUserBandwidthStatDAO.FindUserBandwidthStatsBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo)
 | 
			
		||||
 | 
			
		||||
		// nth
 | 
			
		||||
		stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, req.RegionId, req.DayFrom, req.DayTo, req.Percentile)
 | 
			
		||||
		stat, err := models.SharedUserBandwidthStatDAO.FindPercentileBetweenDays(tx, req.UserId, req.NodeRegionId, req.DayFrom, req.DayTo, req.Percentile)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -239,7 +239,7 @@ func (this *ServerDailyStatService) FindLatestServerDailyStats(ctx context.Conte
 | 
			
		||||
	if req.Days > 0 {
 | 
			
		||||
		for i := int32(0); i < req.Days; i++ {
 | 
			
		||||
			dayString := timeutil.Format("Ymd", time.Now().AddDate(0, 0, -int(i)))
 | 
			
		||||
			stat, err := models.SharedServerDailyStatDAO.SumDailyStat(tx, 0, req.ServerId, req.RegionId, dayString, dayString)
 | 
			
		||||
			stat, err := models.SharedServerDailyStatDAO.SumDailyStat(tx, 0, req.ServerId, req.NodeRegionId, dayString, dayString)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
@@ -286,7 +286,7 @@ func (this *ServerDailyStatService) FindServerDailyStatsBetweenDays(ctx context.
 | 
			
		||||
		return nil, errors.New("invalid dayTo '" + req.DayTo + "'")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dailyStats, err := models.SharedServerDailyStatDAO.FindStatsBetweenDays(tx, req.UserId, req.ServerId, req.RegionId, req.DayFrom, req.DayTo)
 | 
			
		||||
	dailyStats, err := models.SharedServerDailyStatDAO.FindStatsBetweenDays(tx, req.UserId, req.ServerId, req.NodeRegionId, req.DayFrom, req.DayTo)
 | 
			
		||||
	var pbStats = []*pb.FindServerDailyStatsBetweenDaysResponse_Stat{}
 | 
			
		||||
	for _, stat := range dailyStats {
 | 
			
		||||
		// 防止数据出错
 | 
			
		||||
@@ -396,7 +396,7 @@ func (this *ServerDailyStatService) SumServerDailyStats(ctx context.Context, req
 | 
			
		||||
		req.DayTo = req.DayFrom
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	stat, err := models.SharedServerDailyStatDAO.SumDailyStat(tx, req.UserId, req.ServerId, req.RegionId, req.DayFrom, req.DayTo)
 | 
			
		||||
	stat, err := models.SharedServerDailyStatDAO.SumDailyStat(tx, req.UserId, req.ServerId, req.NodeRegionId, req.DayFrom, req.DayTo)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user