fix: UPF流量总计7,30天累计数据查询

This commit is contained in:
TsMask
2025-03-21 15:03:29 +08:00
parent 07718f1e0c
commit cf48cd8854
4 changed files with 46 additions and 46 deletions

View File

@@ -36,7 +36,7 @@ func (k *KpiCReport) Get(c *gin.Context) {
dbg := db.DB("").Model(&KpiCReport{}).Table(tableName) dbg := db.DB("").Model(&KpiCReport{}).Table(tableName)
if querys.NeID != "" { if querys.NeID != "" {
conditions = append(conditions, "rm_uid = (select n.rm_uid from ne_info n where n.ne_type=? and n.ne_id=? and n.status=1)") conditions = append(conditions, "rm_uid = (select n.rm_uid from ne_info n where n.ne_type=? and n.ne_id=?)")
params = append(params, strings.ToUpper(querys.NeType), querys.NeID) params = append(params, strings.ToUpper(querys.NeType), querys.NeID)
} else { } else {
c.JSON(http.StatusBadRequest, services.ErrResp("Not found required parameter NE ID")) c.JSON(http.StatusBadRequest, services.ErrResp("Not found required parameter NE ID"))

View File

@@ -202,7 +202,7 @@ func saveKPIData(kpiReport KpiReport, index int64) int64 {
// 更新UPF总流量 // 更新UPF总流量
upValue := parse.Number(kpiEvent["UPF.03"]) upValue := parse.Number(kpiEvent["UPF.03"])
downValue := parse.Number(kpiEvent["UPF.06"]) downValue := parse.Number(kpiEvent["UPF.06"])
neDataService.NewKpiReport.UPFTodayFlowUpdate(neInfo.RmUID, upValue, downValue, false) neDataService.NewKpiReport.UPFTodayFlowUpdate(neInfo.RmUID, upValue, downValue)
} }
} }
} }

View File

@@ -357,5 +357,5 @@ func Setup(router *gin.Engine) {
// InitLoad 初始参数 // InitLoad 初始参数
func InitLoad() { func InitLoad() {
// 启动时加载UPF上下行流量 // 启动时加载UPF上下行流量
go service.NewKpiReport.UPFTodayFlowLoad() go service.NewKpiReport.UPFTodayFlowLoad(30)
} }

View File

@@ -142,13 +142,14 @@ func (r KpiReport) FindTitle(neType string) []model.KpiTitle {
} }
// UPFTodayFlowFind 查询UPF总流量 N3上行 N6下行 // UPFTodayFlowFind 查询UPF总流量 N3上行 N6下行
// day 统计天数
// down * 8 / 1000 / 1000 单位M // down * 8 / 1000 / 1000 单位M
func (r KpiReport) UPFTodayFlowFind(rmUID string, day int) (int64, int64) { func (r KpiReport) UPFTodayFlowFind(rmUID string, day int) (int64, int64) {
// 获取当前日期 // 获取当前日期
now := time.Now() now := time.Now()
var upTotal, downTotal int64 var upTotal, downTotal int64
// 查询最近7天的数据 // 查询最近day天的数据
for i := 0; i <= day; i++ { for i := 0; i <= day; i++ {
dateKey := now.AddDate(0, 0, -i).Format("2006-01-02") dateKey := now.AddDate(0, 0, -i).Format("2006-01-02")
key := fmt.Sprintf("%s:UPF_FLOW:%s:%s", constants.CACHE_NE_DATA, rmUID, dateKey) key := fmt.Sprintf("%s:UPF_FLOW:%s:%s", constants.CACHE_NE_DATA, rmUID, dateKey)
@@ -171,28 +172,11 @@ func (r KpiReport) UPFTodayFlowFind(rmUID string, day int) (int64, int64) {
} }
// UPFTodayFlow UPF流量今日统计 // UPFTodayFlow UPF流量今日统计
func (r KpiReport) UPFTodayFlowUpdate(rmUID string, upValue, downValue int64, rest bool) error { func (r KpiReport) UPFTodayFlowUpdate(rmUID string, upValue, downValue int64) error {
// 按日期存储统计数据 // 按日期存储统计数据
dateKey := time.Now().Format("2006-01-02") dateKey := time.Now().Format("2006-01-02")
key := fmt.Sprintf("%s:UPF_FLOW:%s:%s", constants.CACHE_NE_DATA, rmUID, dateKey) key := fmt.Sprintf("%s:UPF_FLOW:%s:%s", constants.CACHE_NE_DATA, 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
}
return nil
}
// 使用HIncrBy实时累加统计值 // 使用HIncrBy实时累加统计值
if err := redis.IncrBy("", key, "up", upValue); err != nil { if err := redis.IncrBy("", key, "up", upValue); err != nil {
return err return err
@@ -204,17 +188,14 @@ func (r KpiReport) UPFTodayFlowUpdate(rmUID string, upValue, downValue int64, re
} }
// UPFTodayFlowLoad UPF上下行数据到redis // UPFTodayFlowLoad UPF上下行数据到redis
func (r KpiReport) UPFTodayFlowLoad() { // day 统计天数
func (r KpiReport) UPFTodayFlowLoad(day int) {
cacheKeys, _ := redis.GetKeys("", constants.CACHE_NE_INFO+":UPF:*") cacheKeys, _ := redis.GetKeys("", constants.CACHE_NE_INFO+":UPF:*")
if len(cacheKeys) == 0 { if len(cacheKeys) == 0 {
return return
} }
// 今日流量
now := time.Now() now := time.Now()
beginTime := now.Truncate(24 * time.Hour).UnixMilli()
endTime := beginTime + 24*60*60*1000 - 1
for _, key := range cacheKeys { for _, key := range cacheKeys {
var v neModel.NeInfo var v neModel.NeInfo
jsonStr, _ := redis.Get("", key) jsonStr, _ := redis.Get("", key)
@@ -222,6 +203,13 @@ func (r KpiReport) UPFTodayFlowLoad() {
json.Unmarshal([]byte(jsonStr), &v) json.Unmarshal([]byte(jsonStr), &v)
} }
if v.NeType == "UPF" && v.RmUID != "" { if v.NeType == "UPF" && v.RmUID != "" {
// 查询最近day天的数据
for i := 0; i <= day; i++ {
dateKey := now.AddDate(0, 0, -i).Format("2006-01-02")
key := fmt.Sprintf("%s:UPF_FLOW:%s:%s", constants.CACHE_NE_INFO, v.RmUID, dateKey)
// 根据传入天数计算时间范围
beginTime := now.AddDate(0, 0, -i).Truncate(24 * time.Hour).UnixMilli()
endTime := beginTime + 24*60*60*1000 - 1
// 查询历史数据 // 查询历史数据
rows := r.kpiReportRepository.SelectUPF(v.RmUID, beginTime, endTime) rows := r.kpiReportRepository.SelectUPF(v.RmUID, beginTime, endTime)
var upTotal, downTotal int64 var upTotal, downTotal int64
@@ -245,8 +233,20 @@ func (r KpiReport) UPFTodayFlowLoad() {
} }
} }
// 将历史数据添加到Redis err := redis.SetHash("", key, map[string]any{
r.UPFTodayFlowUpdate(v.RmUID, upTotal, downTotal, true) "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
}
}
} }
} }
} }