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

This commit is contained in:
TsMask
2025-03-21 15:03:38 +08:00
parent ad64e90301
commit bd3910a217
4 changed files with 41 additions and 41 deletions

View File

@@ -34,7 +34,7 @@ func (k *KpiCReport) Get(c *gin.Context) {
dbg := dborm.DefaultDB().Table(tableName)
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)
} else {
c.JSON(http.StatusBadRequest, services.ErrResp("Not found required parameter NE ID"))
@@ -101,7 +101,7 @@ func (k *KpiCReport) GetReport2FE(c *gin.Context) {
dbg := dborm.DefaultDB().Table(tableName)
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, querys.NeType, querys.NeID)
} else {
c.JSON(http.StatusBadRequest, services.ErrResp("Not found required parameter NE ID"))

View File

@@ -342,7 +342,7 @@ func PostKPIReportFromNF(w http.ResponseWriter, r *http.Request) {
// 更新UPF总流量
upValue := parse.Number(kpiEvent["UPF.03"])
downValue := parse.Number(kpiEvent["UPF.06"])
neDataService.NewPerfKPI.UPFTodayFlowUpdate(neInfo.RmUID, upValue, downValue, false)
neDataService.NewPerfKPI.UPFTodayFlowUpdate(neInfo.RmUID, upValue, downValue)
}
}

View File

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

View File

@@ -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,28 +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
}
return nil
}
// 使用HIncrBy实时累加统计值
if err := redis.IncrBy("", key, "up", upValue); err != nil {
return err
@@ -106,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)
@@ -124,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
}
}
}
}
}