Browse Source

fix:edit compute_statistic

jimmyyem 3 months ago
parent
commit
1e159e7ede
1 changed files with 18 additions and 16 deletions
  1. 18 16
      crontask/compute_statistic.go

+ 18 - 16
crontask/compute_statistic.go

@@ -33,6 +33,7 @@ func (l *CronTask) computeStatistic() {
 
 	// 获取当前时间
 	now := time.Now()
+
 	// 获取本小时的第一分钟
 	currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
 	currentHourInt, _ := strconv.Atoi(currentHour.Format("2006010215"))
@@ -150,13 +151,14 @@ func (l *CronTask) computeStatistic() {
 	dayStr := time.Now().Format("20060102")
 	day, _ := strconv.Atoi(dayStr)
 
-	// 获取今天的第一小时
-	firstHour := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
-	firstHourInt, _ := strconv.Atoi(firstHour.Format("20060102"))
+	// 获取昨天的第一小时
+	yesterday := now.AddDate(0, 0, -1)
+	yesterdayFirstHour := time.Date(yesterday.Year(), yesterday.Month(), now.Day(), 0, 0, 0, 0, now.Location())
+	yesterdayFirstHourInt, _ := strconv.Atoi(yesterdayFirstHour.Format("20060102"))
 
-	// 获取天的最后一小时
-	lastHour = time.Date(now.Year(), now.Month(), now.Day(), 23, 0, 0, 0, now.Location())
-	lastHourInt, _ = strconv.Atoi(lastHour.Format("20060102"))
+	// 获取天的最后一小时
+	yesterdayLastHour := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
+	yesterdayLastHourInt, _ := strconv.Atoi(yesterdayLastHour.Format("20060102"))
 
 	for _, wxinfo := range wxbots {
 		l.Logger.Infof("开始计算日数据:%d\n", day)
@@ -175,8 +177,8 @@ func (l *CronTask) computeStatistic() {
 		hourDataBatch, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
 			usagestatistichour.Type(1),
 			usagestatistichour.BotID(wxinfo.Wxid),
-			usagestatistichour.AddtimeGTE(uint64(firstHourInt)),
-			usagestatistichour.AddtimeLTE(uint64(lastHourInt)),
+			usagestatistichour.AddtimeGTE(uint64(yesterdayFirstHourInt)),
+			usagestatistichour.AddtimeLT(uint64(yesterdayLastHourInt)),
 		).All(l.ctx)
 
 		var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64
@@ -224,13 +226,13 @@ func (l *CronTask) computeStatistic() {
 	for _, wxinfo := range wxbots {
 		l.Logger.Infof("开始计算月数据:%d\n", month)
 
-		// 获取月的第一天
-		firstDay := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
-		firstDayInt, _ := strconv.Atoi(firstDay.Format("20060102"))
+		// 获取月的第一天
+		monthFirstDay := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, now.Location())
+		monthFirstDayInt, _ := strconv.Atoi(monthFirstDay.Format("20060102"))
 
-		// 获取月的最后一天
-		lastDay := firstDay.AddDate(0, 1, -1)
-		lastDayInt, _ := strconv.Atoi(lastDay.Format("20060102"))
+		// 获取月的最后一天
+		monthLastDay := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
+		monthLastDayInt, _ := strconv.Atoi(monthLastDay.Format("20060102"))
 
 		// 先判断该账号是否已经统计了月数据,如果已经统计了,就不需要再统计了
 		monthDataCount, _ := l.svcCtx.DB.UsageStatisticMonth.Query().Where(
@@ -247,8 +249,8 @@ func (l *CronTask) computeStatistic() {
 		dayDataBatch, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
 			usagestatisticday.Type(1),
 			usagestatisticday.BotID(wxinfo.Wxid),
-			usagestatisticday.AddtimeGTE(uint64(firstDayInt)),
-			usagestatisticday.AddtimeLTE(uint64(lastDayInt)),
+			usagestatisticday.AddtimeGTE(uint64(monthFirstDayInt)),
+			usagestatisticday.AddtimeLT(uint64(monthLastDayInt)),
 		).All(l.ctx)
 
 		var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64