Merge branch 'main-v2' into lite-ba

This commit is contained in:
TsMask
2025-09-19 15:10:55 +08:00
56 changed files with 2929 additions and 515 deletions

View File

@@ -280,3 +280,222 @@ func (r KpiReport) UPFTodayFlowLoad(day int) {
}
}
}
// IMSBusyHour IMS忙时流量统计
// SCSCF.06呼叫尝试次数 SCSCF.09呼叫成功次数
func (r KpiReport) IMSBusyHour(rmUID string, timestamp int64) []map[string]any {
t := time.UnixMilli(timestamp)
beginTime := t
endTime := t
// 检查时分秒是否都为零
if t.Hour() == 0 && t.Minute() == 0 && t.Second() == 0 {
// 获取当天起始时间00:00:00
beginTime = t.Truncate(time.Hour)
// 计算当天结束时间23:59:59
endTime = beginTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
} else {
// 起始时间:当前小时的 00 分 00 秒
beginTime = t.Truncate(time.Hour)
// 结束时间:当前小时的 59 分 59 秒 999 毫秒
endTime = beginTime.Add(time.Hour - time.Millisecond)
}
// 转换为毫秒级时间戳
rows := r.kpiReportRepository.SelectIMS(rmUID, beginTime.UnixMilli(), endTime.UnixMilli())
// 创建一个map来存储按时间段合并后的数据
timeGroup := make(map[int64]map[string]int64)
// 遍历每个数据项
for _, row := range rows {
// 将毫秒时间戳转换为小时级时间戳(保留到小时的起始毫秒)
timeHour := row.CreatedAt / 3600000 * 3600000 // 1小时 = 3600000毫秒
// 解析 JSON 字符串为 map
var kpiValues []map[string]any
err := json.Unmarshal([]byte(row.KpiValues), &kpiValues)
if err != nil {
continue
}
var callAttempts, callCompletions int64
for _, v := range kpiValues {
if k, ok := v["kpiId"]; ok {
if k == "SCSCF.06" {
callAttempts = parse.Number(v["value"])
}
if k == "SCSCF.09" {
callCompletions = parse.Number(v["value"])
}
}
}
// 合并到对应的小时段
if _, exists := timeGroup[timeHour]; !exists {
timeGroup[timeHour] = map[string]int64{
"callAttempts": 0,
"callCompletions": 0,
}
}
timeGroup[timeHour]["callAttempts"] += callAttempts
timeGroup[timeHour]["callCompletions"] += callCompletions
}
// 时间组合输出
data := make([]map[string]any, 0, len(timeGroup))
for hour, sums := range timeGroup {
data = append(data, map[string]any{
"timeGroup": fmt.Sprintf("%d", hour),
"callAttempts": sums["callAttempts"],
"callCompletions": sums["callCompletions"],
})
}
return data
}
// 定义结构体用于存储话务量值和对应的时间
type TrafficData struct {
Time int64 `json:"time"` // 时间戳(毫秒)
Value float64 `json:"value"` // 话务量值
}
// IMSBusyWeek IMS忙时流量统计 周
func (r KpiReport) IMSBusyWeek(rmUID string, weekStart, weekEnd int64) map[string]any {
weekStartTime := time.UnixMilli(weekStart)
weekEndTime := time.UnixMilli(weekEnd)
// 1. 获取一周内每小时的呼叫数据
// 转换为毫秒级时间戳
rows := r.kpiReportRepository.SelectIMS(rmUID, weekStartTime.UnixMilli(), weekEndTime.UnixMilli())
// 创建一个map来存储按时间段合并后的数据
timeGroup := make(map[int64]map[string]int64)
// 遍历每个数据项
for _, row := range rows {
// 将毫秒时间戳转换为小时级时间戳(保留到小时的起始毫秒)
timeHour := row.CreatedAt / 3600000 * 3600000 // 1小时 = 3600000毫秒
// 解析 JSON 字符串为 map
var kpiValues []map[string]any
err := json.Unmarshal([]byte(row.KpiValues), &kpiValues)
if err != nil {
continue
}
var callAttempts, callCompletions int64
for _, v := range kpiValues {
if k, ok := v["kpiId"]; ok {
if k == "SCSCF.06" {
callAttempts = parse.Number(v["value"])
}
if k == "SCSCF.09" {
callCompletions = parse.Number(v["value"])
}
}
}
// 合并到对应的小时段
if _, exists := timeGroup[timeHour]; !exists {
timeGroup[timeHour] = map[string]int64{
"callAttempts": 0,
"callCompletions": 0,
}
}
timeGroup[timeHour]["callAttempts"] += callAttempts
timeGroup[timeHour]["callCompletions"] += callCompletions
}
// 时间组合输出
data := make([]map[string]any, 0, len(timeGroup))
for hour, sums := range timeGroup {
data = append(data, map[string]any{
"timeGroup": fmt.Sprintf("%d", hour),
"callAttempts": sums["callAttempts"],
"callCompletions": sums["callCompletions"],
})
}
if len(data) == 0 {
return map[string]any{
"busyHourAverageBHCA": 0,
"busyHourAverageBHCC": 0,
"topFourHoursBHCA": []float64{},
"topFourHoursBHCC": []float64{},
"totalHours": 0,
}
}
// 2. 分离BHCA和BHCC数据并按降序排序
var bhcaData []TrafficData
var bhccData []TrafficData
for _, row := range data {
// 获取时间戳
timeValue := int64(0)
if t, ok := row["timeGroup"]; ok {
timeValue = parse.Number(t)
}
// 处理BHCA数据
if value, ok := row["callAttempts"]; ok {
bhcaVal := parse.Number(value)
bhcaData = append(bhcaData, TrafficData{
Time: timeValue,
Value: float64(bhcaVal),
})
}
// 处理BHCC数据
if value, ok := row["callCompletions"]; ok {
bhccVal := parse.Number(value)
bhccData = append(bhccData, TrafficData{
Time: timeValue,
Value: float64(bhccVal),
})
}
}
// 按降序排序(值大的在前)
sort.Slice(bhcaData, func(i, j int) bool { return bhcaData[i].Value > bhcaData[j].Value })
sort.Slice(bhccData, func(i, j int) bool { return bhccData[i].Value > bhccData[j].Value })
// 3. 取前四个最高值并计算平均值
topFourBHCA := getTopFourTrafficData(bhcaData)
topFourBHCC := getTopFourTrafficData(bhccData)
avgBHCA := calculateTrafficDataAverage(topFourBHCA)
avgBHCC := calculateTrafficDataAverage(topFourBHCC)
// 4. 返回结果
return map[string]any{
"busyHourAverageBHCA": avgBHCA,
"busyHourAverageBHCC": avgBHCC,
"topFourHoursBHCA": topFourBHCA,
"topFourHoursBHCC": topFourBHCC,
"totalHours": len(data),
}
}
// 辅助函数获取前四个最高值的TrafficData
func getTopFourTrafficData(data []TrafficData) []TrafficData {
if len(data) == 0 {
return []TrafficData{}
}
// 最多取前四个值
maxCount := 4
if len(data) < maxCount {
maxCount = len(data)
}
return data[:maxCount]
}
// 辅助函数计算TrafficData的平均值
func calculateTrafficDataAverage(data []TrafficData) float64 {
if len(data) == 0 {
return 0
}
var sum float64 = 0
for _, v := range data {
sum += v.Value
}
return sum / float64(len(data))
}