fix: UPF流量总计7,30天累计数据查询
This commit is contained in:
@@ -326,5 +326,5 @@ func Setup(router *gin.Engine) {
|
||||
// InitLoad 初始参数
|
||||
func InitLoad() {
|
||||
// 启动时,加载UPF上下行流量
|
||||
go service.NewPerfKPI.UPFTodayFlowLoad()
|
||||
go service.NewPerfKPI.UPFTodayFlowLoad(30)
|
||||
}
|
||||
|
||||
@@ -45,12 +45,13 @@ func (r *PerfKPI) SelectGoldKPITitle(neType string) []model.GoldKPITitle {
|
||||
}
|
||||
|
||||
// UPFTodayFlowFind 查询UPF总流量 N3上行 N6下行
|
||||
// day 统计天数
|
||||
func (r PerfKPI) UPFTodayFlowFind(rmUID string, day int) (int64, int64) {
|
||||
// 获取当前日期
|
||||
now := time.Now()
|
||||
var upTotal, downTotal int64
|
||||
|
||||
// 查询最近7天的数据
|
||||
// 查询最近day天的数据
|
||||
for i := 0; i <= day; i++ {
|
||||
dateKey := now.AddDate(0, 0, -i).Format("2006-01-02")
|
||||
key := fmt.Sprintf("%sUPF_FLOW:%s:%s", cachekey.NE_DATA_KEY, rmUID, dateKey)
|
||||
@@ -73,27 +74,11 @@ func (r PerfKPI) UPFTodayFlowFind(rmUID string, day int) (int64, int64) {
|
||||
}
|
||||
|
||||
// UPFTodayFlow UPF流量今日统计
|
||||
func (r PerfKPI) UPFTodayFlowUpdate(rmUID string, upValue, downValue int64, rest bool) error {
|
||||
func (r PerfKPI) UPFTodayFlowUpdate(rmUID string, upValue, downValue int64) error {
|
||||
// 按日期存储统计数据
|
||||
dateKey := time.Now().Format("2006-01-02")
|
||||
key := fmt.Sprintf("%sUPF_FLOW:%s:%s", cachekey.NE_DATA_KEY, rmUID, dateKey)
|
||||
|
||||
// 重置数据
|
||||
if rest {
|
||||
err := redis.SetHash("", key, map[string]any{
|
||||
"up": upValue,
|
||||
"down": downValue,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 设置key的过期时间为30天,自动清理旧数据
|
||||
err = redis.Expire("", key, 30*24*time.Hour)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// 使用HIncrBy实时累加统计值
|
||||
if err := redis.IncrBy("", key, "up", upValue); err != nil {
|
||||
return err
|
||||
@@ -105,17 +90,14 @@ func (r PerfKPI) UPFTodayFlowUpdate(rmUID string, upValue, downValue int64, rest
|
||||
}
|
||||
|
||||
// UPFTodayFlowLoad UPF上下行数据到redis
|
||||
func (r PerfKPI) UPFTodayFlowLoad() {
|
||||
// day 统计天数
|
||||
func (r PerfKPI) UPFTodayFlowLoad(day int) {
|
||||
cacheKeys, _ := redis.GetKeys("", cachekey.NE_KEY+"UPF:*")
|
||||
if len(cacheKeys) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// 今日流量
|
||||
now := time.Now()
|
||||
beginTime := now.Truncate(24 * time.Hour).UnixMilli()
|
||||
endTime := beginTime + 24*60*60*1000 - 1
|
||||
|
||||
for _, key := range cacheKeys {
|
||||
var v neModel.NeInfo
|
||||
jsonStr, _ := redis.Get("", key)
|
||||
@@ -123,21 +105,40 @@ func (r PerfKPI) UPFTodayFlowLoad() {
|
||||
json.Unmarshal([]byte(jsonStr), &v)
|
||||
}
|
||||
if v.NeType == "UPF" && v.RmUID != "" {
|
||||
// 查询历史数据
|
||||
// down * 8 / 1000 / 1000 单位M
|
||||
info := r.perfKPIRepository.SelectUPFTotalFlow("UPF", v.RmUID, fmt.Sprint(beginTime), fmt.Sprint(endTime))
|
||||
if v, ok := info["up"]; ok && v == nil {
|
||||
info["up"] = 0
|
||||
}
|
||||
if v, ok := info["down"]; ok && v == nil {
|
||||
info["down"] = 0
|
||||
}
|
||||
// 查询最近day天的数据
|
||||
for i := 0; i <= day; i++ {
|
||||
dateKey := now.AddDate(0, 0, -i).Format("2006-01-02")
|
||||
key := fmt.Sprintf("%sUPF_FLOW:%s:%s", cachekey.NE_DATA_KEY, v.RmUID, dateKey)
|
||||
// 根据传入天数计算时间范围
|
||||
beginTime := now.AddDate(0, 0, -i).Truncate(24 * time.Hour).UnixMilli()
|
||||
endTime := beginTime + 24*60*60*1000 - 1
|
||||
// 查询历史数据
|
||||
// down * 8 / 1000 / 1000 单位M
|
||||
info := r.perfKPIRepository.SelectUPFTotalFlow("UPF", v.RmUID, fmt.Sprint(beginTime), fmt.Sprint(endTime))
|
||||
if v, ok := info["up"]; ok && v == nil {
|
||||
info["up"] = 0
|
||||
}
|
||||
if v, ok := info["down"]; ok && v == nil {
|
||||
info["down"] = 0
|
||||
}
|
||||
|
||||
upTotal := parse.Number(info["up"])
|
||||
downTotal := parse.Number(info["down"])
|
||||
upTotal := parse.Number(info["up"])
|
||||
downTotal := parse.Number(info["down"])
|
||||
|
||||
// 将历史数据添加到Redis
|
||||
r.UPFTodayFlowUpdate(v.RmUID, upTotal, downTotal, true)
|
||||
err := redis.SetHash("", key, map[string]any{
|
||||
"up": upTotal,
|
||||
"down": downTotal,
|
||||
})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// 设置key的过期时间为30天,自动清理旧数据
|
||||
daySub := (30 - i) * 24
|
||||
err = redis.Expire("", key, time.Duration(daySub)*time.Hour)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user