Ver código fonte

Merge branch 'feature/dashboard' into debug

* feature/dashboard:
  优化统计结果
boweniac 3 meses atrás
pai
commit
d339c72deb

+ 10 - 0
crontask/compute_statistic.go

@@ -281,6 +281,16 @@ func (l *CronTask) computeStatistic() {
 				activeUser += hourData.ActiveUser
 				newUser += hourData.NewUser
 			}
+
+			// 活跃好友:usage_detail 表 type = 1
+			activeUsers, _ := l.svcCtx.DB.UsageDetail.Query().Where(
+				usagedetail.Type(1),
+				usagedetail.BotID(wxinfo.Wxid),
+				usagedetail.CreatedAtGTE(yesterdayFirstHour),
+				usagedetail.CreatedAtLT(yesterdayLastHour),
+			).GroupBy(usagedetail.FieldReceiverID).Strings(l.ctx)
+			activeUser = uint64(len(activeUsers))
+
 			orgAiResponseInt += aiResponse
 			orgSopRunInt += sopRun
 			orgFriendCountInt += totalFriend

+ 64 - 32
internal/logic/dashboard/get_charts_logic.go

@@ -9,6 +9,7 @@ import (
 	"wechat-api/ent/custom_types"
 	"wechat-api/ent/label"
 	"wechat-api/ent/predicate"
+	"wechat-api/ent/usagedetail"
 	"wechat-api/ent/usagestatisticday"
 	"wechat-api/ent/usagestatistichour"
 	"wechat-api/internal/svc"
@@ -155,55 +156,70 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 			addtimeLastTwoDigits := hourData.Addtime % 100
 			aiResponse.Count += hourData.AiResponse
 			aiResponse.Val = append(aiResponse.Val, hourData.AiResponse)
-			aiResponse.Label = append(aiResponse.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			aiResponse.Label = append(aiResponse.Label, fmt.Sprintf("%02d-%02d点", addtimeLastTwoDigits, addtimeLastTwoDigits+1))
 			aiResponse.LabelText = "次"
 
 			sopRun.Count += hourData.SopRun
 			sopRun.Val = append(sopRun.Val, hourData.SopRun)
-			sopRun.Label = append(sopRun.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			sopRun.Label = append(sopRun.Label, fmt.Sprintf("%02d-%02d点", addtimeLastTwoDigits, addtimeLastTwoDigits+1))
 			sopRun.LabelText = "次"
 
 			totalFriend.Count = hourData.TotalFriend
 			totalFriend.Val = append(totalFriend.Val, hourData.TotalFriend)
-			totalFriend.Label = append(totalFriend.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			totalFriend.Label = append(totalFriend.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits+1))
 			totalFriend.LabelText = "个"
 
 			totalGroup.Count = hourData.TotalGroup
 			totalGroup.Val = append(totalGroup.Val, hourData.TotalGroup)
-			totalGroup.Label = append(totalGroup.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			totalGroup.Label = append(totalGroup.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits+1))
 			totalGroup.LabelText = "个"
 
 			consumeToken.Count += hourData.ConsumeToken
 			consumeToken.Val = append(consumeToken.Val, hourData.ConsumeToken)
-			consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%02d-%02d点", addtimeLastTwoDigits, addtimeLastTwoDigits+1))
 			consumeToken.LabelText = "token"
 
-			activeUser.Count = hourData.ActiveUser
+			//activeUser.Count = hourData.ActiveUser
 			activeUser.Val = append(activeUser.Val, hourData.ActiveUser)
-			activeUser.Label = append(activeUser.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			activeUser.Label = append(activeUser.Label, fmt.Sprintf("%02d-%02d点", addtimeLastTwoDigits, addtimeLastTwoDigits+1))
 			activeUser.LabelText = "个"
 
 			newUser.Count += hourData.NewUser
 			newUser.Val = append(newUser.Val, hourData.NewUser)
-			newUser.Label = append(newUser.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
+			newUser.Label = append(newUser.Label, fmt.Sprintf("%02d-%02d点", addtimeLastTwoDigits, addtimeLastTwoDigits+1))
 			newUser.LabelText = "个"
 		}
+
+		// 活跃好友:usage_detail 表 type = 1
+		var usageDetailPredicates []predicate.UsageDetail
+		if organizationId != 0 {
+			usageDetailPredicates = append(usageDetailPredicates, usagedetail.OrganizationID(organizationId))
+		}
+		if req.Wxid != nil {
+			usageDetailPredicates = append(usageDetailPredicates, usagedetail.BotID(*req.Wxid))
+		}
+		usageDetailPredicates = append(usageDetailPredicates, usagedetail.Type(1))
+		usageDetailPredicates = append(usageDetailPredicates, usagedetail.CreatedAtGTE(startTime))
+		usageDetailPredicates = append(usageDetailPredicates, usagedetail.CreatedAtLT(nextDayMidnight))
+		activeUsers, _ := l.svcCtx.DB.UsageDetail.Query().Where(usageDetailPredicates...).GroupBy(usagedetail.FieldReceiverID).Strings(l.ctx)
+		activeUser.Count = uint64(len(activeUsers))
+
 		hourLen := len(usageStatisticHour)
 		if hourLen > 0 {
 			if usageStatisticHour[0].TotalFriend > 0 {
-				totalFriendRate := float32((usageStatisticHour[hourLen-1].TotalFriend - usageStatisticHour[0].TotalFriend) / usageStatisticHour[0].TotalFriend)
+				totalFriendRate := float32((float64(usageStatisticHour[hourLen-1].TotalFriend) - float64(usageStatisticHour[0].TotalFriend)) / float64(usageStatisticHour[0].TotalFriend))
 				totalFriend.Rate = &totalFriendRate
 			}
 			if usageStatisticHour[0].TotalGroup > 0 {
-				totalGroupRate := float32((usageStatisticHour[hourLen-1].TotalGroup - usageStatisticHour[0].TotalGroup) / usageStatisticHour[0].TotalGroup)
+				totalGroupRate := float32((float64(usageStatisticHour[hourLen-1].TotalGroup) - float64(usageStatisticHour[0].TotalGroup)) / float64(usageStatisticHour[0].TotalGroup))
 				totalGroup.Rate = &totalGroupRate
 			}
-			//if usageStatisticHour[0].ActiveUser > 0 {
-			//	activeUserRate := float32((usageStatisticHour[hourLen-1].ActiveUser - usageStatisticHour[0].ActiveUser) / usageStatisticHour[0].ActiveUser)
-			//	activeUser.Rate = &activeUserRate
-			//}
+			if usageStatisticHour[0].ActiveUser > 0 {
+				activeUserRate := float32((float64(usageStatisticHour[hourLen-1].ActiveUser) - float64(usageStatisticHour[0].ActiveUser)) / float64(usageStatisticHour[0].ActiveUser))
+				activeUser.Rate = &activeUserRate
+			}
 			if usageStatisticHour[0].NewUser > 0 {
-				newUserRate := float32((usageStatisticHour[hourLen-1].NewUser - usageStatisticHour[0].NewUser) / usageStatisticHour[0].NewUser)
+				newUserRate := float32((float64(usageStatisticHour[hourLen-1].NewUser) - float64(usageStatisticHour[0].NewUser)) / float64(usageStatisticHour[0].NewUser))
 				newUser.Rate = &newUserRate
 			}
 			labelDists = usageStatisticHour[hourLen-1].LabelDist
@@ -218,6 +234,8 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 		}
 		endAddTimeString := endTime.Format("20060102")
 		endAddTime, err := strconv.ParseUint(endAddTimeString, 10, 64)
+
+		nextDayMidnight := time.Date(endTime.Year(), endTime.Month(), endTime.Day()+1, 0, 0, 0, 0, endTime.Location())
 		if err != nil {
 			fmt.Println("转换截止时间失败:", err)
 			return nil, err
@@ -265,7 +283,7 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 			consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%d", dayData.Addtime))
 			consumeToken.LabelText = "token"
 
-			activeUser.Count = dayData.ActiveUser
+			//activeUser.Count = dayData.ActiveUser
 			activeUser.Val = append(activeUser.Val, dayData.ActiveUser)
 			activeUser.Label = append(activeUser.Label, fmt.Sprintf("%d", dayData.Addtime))
 			activeUser.LabelText = "个"
@@ -278,19 +296,19 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 		dayLen := len(usageStatisticDay)
 		if dayLen > 0 {
 			if usageStatisticDay[0].TotalFriend > 0 {
-				totalFriendRate := float32((usageStatisticDay[dayLen-1].TotalFriend - usageStatisticDay[0].TotalFriend) / usageStatisticDay[0].TotalFriend)
+				totalFriendRate := float32((float64(usageStatisticDay[dayLen-1].TotalFriend) - float64(usageStatisticDay[0].TotalFriend)) / float64(usageStatisticDay[0].TotalFriend))
 				totalFriend.Rate = &totalFriendRate
 			}
 			if usageStatisticDay[0].TotalGroup > 0 {
-				totalGroupRate := float32((usageStatisticDay[dayLen-1].TotalGroup - usageStatisticDay[0].TotalGroup) / usageStatisticDay[0].TotalGroup)
+				totalGroupRate := float32((float64(usageStatisticDay[dayLen-1].TotalGroup) - float64(usageStatisticDay[0].TotalGroup)) / float64(usageStatisticDay[0].TotalGroup))
 				totalGroup.Rate = &totalGroupRate
 			}
-			//if usageStatisticDay[0].ActiveUser > 0 {
-			//	activeUserRate := float32((usageStatisticDay[dayLen-1].ActiveUser - usageStatisticDay[0].ActiveUser) / usageStatisticDay[0].ActiveUser)
-			//	activeUser.Rate = &activeUserRate
-			//}
+			if usageStatisticDay[0].ActiveUser > 0 {
+				activeUserRate := float32((float64(usageStatisticDay[dayLen-1].ActiveUser) - float64(usageStatisticDay[0].ActiveUser)) / float64(usageStatisticDay[0].ActiveUser))
+				activeUser.Rate = &activeUserRate
+			}
 			if usageStatisticDay[0].NewUser > 0 {
-				newUserRate := float32((usageStatisticDay[dayLen-1].NewUser - usageStatisticDay[0].NewUser) / usageStatisticDay[0].NewUser)
+				newUserRate := float32((float64(usageStatisticDay[dayLen-1].NewUser) - float64(usageStatisticDay[0].NewUser)) / float64(usageStatisticDay[0].NewUser))
 				newUser.Rate = &newUserRate
 			}
 			labelDists = usageStatisticDay[dayLen-1].LabelDist
@@ -356,7 +374,7 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				totalFriend.Label = append(totalFriend.Label, "今日")
 				tfLen := len(totalFriend.Val)
 				if tfLen > 0 && totalFriend.Val[0] > 0 {
-					totalFriendRate := float32((totalFriend.Val[tfLen-1] - totalFriend.Val[0]) / totalFriend.Val[0])
+					totalFriendRate := float32((float64(totalFriend.Val[tfLen-1]) - float64(totalFriend.Val[0])) / float64(totalFriend.Val[0]))
 					totalFriend.Rate = &totalFriendRate
 				}
 				totalFriend.LabelText = "个"
@@ -366,7 +384,7 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				totalGroup.Label = append(totalGroup.Label, "今日")
 				tgLen := len(totalGroup.Val)
 				if tgLen > 0 && totalGroup.Val[0] > 0 {
-					totalGroupRate := float32((totalGroup.Val[tgLen-1] - totalGroup.Val[0]) / totalGroup.Val[0])
+					totalGroupRate := float32((float64(totalGroup.Val[tgLen-1]) - float64(totalGroup.Val[0])) / float64(totalGroup.Val[0]))
 					totalGroup.Rate = &totalGroupRate
 				}
 				totalGroup.LabelText = "个"
@@ -376,14 +394,14 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				consumeToken.Label = append(consumeToken.Label, "今日")
 				consumeToken.LabelText = "token"
 
-				activeUser.Count = activeUserOfDay
+				//activeUser.Count = activeUserOfDay
 				activeUser.Val = append(activeUser.Val, activeUserOfDay)
 				activeUser.Label = append(activeUser.Label, "今日")
-				//auLen := len(activeUser.Val)
-				//if auLen > 0 && activeUser.Val[0] > 0 {
-				//	activeUserRate := float32((activeUser.Val[auLen-1] - activeUser.Val[0]) / activeUser.Val[0])
-				//	activeUser.Rate = &activeUserRate
-				//}
+				auLen := len(activeUser.Val)
+				if auLen > 0 && activeUser.Val[0] > 0 {
+					activeUserRate := float32((float64(activeUser.Val[auLen-1]) - float64(activeUser.Val[0])) / float64(activeUser.Val[0]))
+					activeUser.Rate = &activeUserRate
+				}
 				activeUser.LabelText = "个"
 
 				newUser.Count = newUserOfDay
@@ -391,7 +409,7 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				newUser.Label = append(newUser.Label, "今日")
 				nuLen := len(newUser.Val)
 				if nuLen > 0 && newUser.Val[0] > 0 {
-					newUserRate := float32((newUser.Val[nuLen-1] - newUser.Val[0]) / newUser.Val[0])
+					newUserRate := float32((float64(newUser.Val[nuLen-1]) - float64(newUser.Val[0])) / float64(newUser.Val[0]))
 					newUser.Rate = &newUserRate
 				}
 				newUser.LabelText = "个"
@@ -399,6 +417,20 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				labelDists = usageStatisticHour[hourLen-1].LabelDist
 			}
 		}
+
+		// 活跃好友:usage_detail 表 type = 1
+		var usageDetailPredicates []predicate.UsageDetail
+		if organizationId != 0 {
+			usageDetailPredicates = append(usageDetailPredicates, usagedetail.OrganizationID(organizationId))
+		}
+		if req.Wxid != nil {
+			usageDetailPredicates = append(usageDetailPredicates, usagedetail.BotID(*req.Wxid))
+		}
+		usageDetailPredicates = append(usageDetailPredicates, usagedetail.Type(1))
+		usageDetailPredicates = append(usageDetailPredicates, usagedetail.CreatedAtGTE(startTime))
+		usageDetailPredicates = append(usageDetailPredicates, usagedetail.CreatedAtLT(nextDayMidnight))
+		activeUsers, _ := l.svcCtx.DB.UsageDetail.Query().Where(usageDetailPredicates...).GroupBy(usagedetail.FieldReceiverID).Strings(l.ctx)
+		activeUser.Count = uint64(len(activeUsers))
 	}
 
 	var labelIds []uint64