|
@@ -50,6 +50,9 @@ func (l *CronTask) computeStatistic() {
|
|
|
lastHour := currentHour.Add(-time.Hour * 1)
|
|
|
lastHourInt, _ := strconv.Atoi(lastHour.Format("2006010215"))
|
|
|
|
|
|
+ lc := []custom_types.LabelDist{}
|
|
|
+
|
|
|
+ var allHourAiResponseInt, allHourSopRunInt, allHourFriendCountInt, allHourGroupCountInt, allHourAccountBalanceInt, allHourConsumeTokenInt, allHourActiveUserInt, allHourNewUserInt int
|
|
|
for orgID, wxinfos := range wxbotsSet {
|
|
|
var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt, orgNewUserInt int
|
|
|
for _, wxinfo := range wxinfos {
|
|
@@ -84,6 +87,7 @@ func (l *CronTask) computeStatistic() {
|
|
|
).Count(l.ctx)
|
|
|
aiResponseInt = sopresp + airesp
|
|
|
orgAiResponseInt += aiResponseInt
|
|
|
+ allHourAiResponseInt += aiResponseInt
|
|
|
|
|
|
// SOP执行次数:SOP阶段和节点的执行次数。
|
|
|
sopRunInt, _ = l.svcCtx.DB.MessageRecords.Query().Where(
|
|
@@ -95,6 +99,7 @@ func (l *CronTask) computeStatistic() {
|
|
|
messagerecords.CreatedAtLT(currentHour),
|
|
|
).Count(l.ctx)
|
|
|
orgSopRunInt += sopRunInt
|
|
|
+ allHourSopRunInt += sopRunInt
|
|
|
|
|
|
// 好友总数:contact 表中 type=1
|
|
|
friendCountInt, _ = l.svcCtx.DB.Contact.Query().Where(
|
|
@@ -102,6 +107,7 @@ func (l *CronTask) computeStatistic() {
|
|
|
contact.WxWxid(wxinfo.Wxid),
|
|
|
).Count(l.ctx)
|
|
|
orgFriendCountInt += friendCountInt
|
|
|
+ allHourFriendCountInt += friendCountInt
|
|
|
|
|
|
// 群总数:contact 表中 type=2
|
|
|
groupCountInt, _ = l.svcCtx.DB.Contact.Query().Where(
|
|
@@ -109,6 +115,7 @@ func (l *CronTask) computeStatistic() {
|
|
|
contact.WxWxid(wxinfo.Wxid),
|
|
|
).Count(l.ctx)
|
|
|
orgGroupCountInt += groupCountInt
|
|
|
+ allHourGroupCountInt += groupCountInt
|
|
|
|
|
|
// 消耗Token数:usage_detail 表
|
|
|
consumeTokenInt, _ = l.svcCtx.DB.UsageDetail.Query().Where(
|
|
@@ -118,10 +125,12 @@ func (l *CronTask) computeStatistic() {
|
|
|
usagedetail.CreatedAtLT(currentHour),
|
|
|
).Aggregate(ent.Sum("total_tokens")).Int(l.ctx)
|
|
|
orgConsumeTokenInt += consumeTokenInt
|
|
|
+ allHourConsumeTokenInt += consumeTokenInt
|
|
|
|
|
|
// 账户余额
|
|
|
accountBalanceInt = 0
|
|
|
orgAccountBalanceInt = 0
|
|
|
+ allHourAccountBalanceInt = 0
|
|
|
|
|
|
// 活跃好友:usage_detail 表 type = 1
|
|
|
activeUserInt, _ = l.svcCtx.DB.UsageDetail.Query().Where(
|
|
@@ -131,6 +140,7 @@ func (l *CronTask) computeStatistic() {
|
|
|
usagedetail.CreatedAtLT(currentHour),
|
|
|
).GroupBy(usagedetail.FieldReceiverID).Int(l.ctx)
|
|
|
orgActiveUserInt += activeUserInt
|
|
|
+ allHourActiveUserInt += activeUserInt
|
|
|
|
|
|
lastHourData, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
|
|
|
usagestatistichour.AddtimeEQ(uint64(lastHourInt)),
|
|
@@ -144,7 +154,8 @@ func (l *CronTask) computeStatistic() {
|
|
|
newUserInt = int(lastHourData.TotalFriend) - friendCountInt
|
|
|
}
|
|
|
orgNewUserInt += newUserInt
|
|
|
- var lc []custom_types.LabelDist
|
|
|
+ allHourNewUserInt += newUserInt
|
|
|
+
|
|
|
_, err := l.svcCtx.DB.UsageStatisticHour.Create().
|
|
|
SetType(1).
|
|
|
SetBotID(wxinfo.Wxid).
|
|
@@ -173,7 +184,7 @@ func (l *CronTask) computeStatistic() {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- var LabelsCount []custom_types.LabelDist
|
|
|
+ LabelsCount := []custom_types.LabelDist{}
|
|
|
err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.OrganizationIDEQ(orgID), labelrelationship.DeletedAtIsNil()).GroupBy(labelrelationship.FieldLabelID).Aggregate(ent.Count()).Scan(l.ctx, &LabelsCount)
|
|
|
l.Errorf("save hour data error:%v \n", err)
|
|
|
LabelsCountSet[orgID] = LabelsCount
|
|
@@ -195,6 +206,22 @@ func (l *CronTask) computeStatistic() {
|
|
|
l.Errorf("save hour data error:%v \n", err)
|
|
|
}
|
|
|
|
|
|
+ _, err = l.svcCtx.DB.UsageStatisticHour.Create().
|
|
|
+ SetType(1).
|
|
|
+ SetOrganizationID(0).
|
|
|
+ SetAiResponse(uint64(allHourAiResponseInt)).
|
|
|
+ SetSopRun(uint64(allHourSopRunInt)).
|
|
|
+ SetTotalFriend(uint64(allHourFriendCountInt)).
|
|
|
+ SetTotalGroup(uint64(allHourGroupCountInt)).
|
|
|
+ SetAccountBalance(uint64(allHourAccountBalanceInt)).
|
|
|
+ SetConsumeToken(uint64(allHourConsumeTokenInt)).
|
|
|
+ SetActiveUser(uint64(allHourActiveUserInt)).
|
|
|
+ SetNewUser(int64(allHourNewUserInt)).
|
|
|
+ SetAddtime(uint64(currentHourInt)).
|
|
|
+ SetLabelDist(lc).
|
|
|
+ Save(l.ctx)
|
|
|
+ l.Errorf("save hour data error:%v \n", err)
|
|
|
+
|
|
|
/*
|
|
|
计算日数据
|
|
|
----------------------------------------------------------------------------------------------------------
|
|
@@ -211,6 +238,8 @@ func (l *CronTask) computeStatistic() {
|
|
|
yesterdayLastHour := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
|
yesterdayLastHourInt, _ := strconv.Atoi(yesterdayLastHour.Format("20060102"))
|
|
|
|
|
|
+ var allDayAiResponseInt, allDaySopRunInt, allDayFriendCountInt, allDayGroupCountInt, allDayAccountBalanceInt, allDayConsumeTokenInt, allDayActiveUserInt uint64
|
|
|
+ var allDayNewUserInt int64
|
|
|
for orgID, wxinfos := range wxbotsSet {
|
|
|
var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt uint64
|
|
|
var orgNewUserInt int64
|
|
@@ -256,7 +285,15 @@ func (l *CronTask) computeStatistic() {
|
|
|
orgActiveUserInt += activeUser
|
|
|
orgNewUserInt += newUser
|
|
|
|
|
|
- var lc []custom_types.LabelDist
|
|
|
+ allDayAiResponseInt += aiResponse
|
|
|
+ allDaySopRunInt += sopRun
|
|
|
+ allDayFriendCountInt += totalFriend
|
|
|
+ allDayGroupCountInt += totalGroup
|
|
|
+ allDayAccountBalanceInt += accountBalance
|
|
|
+ allDayConsumeTokenInt += consumeToken
|
|
|
+ allDayActiveUserInt += activeUser
|
|
|
+ allDayNewUserInt += newUser
|
|
|
+
|
|
|
_, err := l.svcCtx.DB.UsageStatisticDay.Create().
|
|
|
SetAddtime(uint64(day)).
|
|
|
SetType(1).
|
|
@@ -310,6 +347,24 @@ func (l *CronTask) computeStatistic() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ _, err = l.svcCtx.DB.UsageStatisticDay.Create().
|
|
|
+ SetAddtime(uint64(day)).
|
|
|
+ SetType(1).
|
|
|
+ SetOrganizationID(0).
|
|
|
+ SetAiResponse(allDayAiResponseInt).
|
|
|
+ SetSopRun(allDaySopRunInt).
|
|
|
+ SetTotalFriend(allDayFriendCountInt).
|
|
|
+ SetTotalGroup(allDayGroupCountInt).
|
|
|
+ SetAccountBalance(allDayAccountBalanceInt).
|
|
|
+ SetConsumeToken(allDayConsumeTokenInt).
|
|
|
+ SetActiveUser(allDayActiveUserInt).
|
|
|
+ SetNewUser(allDayNewUserInt).
|
|
|
+ SetLabelDist(lc).
|
|
|
+ Save(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ l.Errorf("create day data error:%v \n", err)
|
|
|
+ }
|
|
|
+
|
|
|
/*
|
|
|
查看月表数据是否已经完成
|
|
|
1. 查询出上月里所有 usagedetail 内容
|
|
@@ -319,6 +374,8 @@ func (l *CronTask) computeStatistic() {
|
|
|
monthStr := time.Now().Format("200601")
|
|
|
month, _ := strconv.Atoi(monthStr)
|
|
|
|
|
|
+ var allMonthAiResponseInt, allMonthSopRunInt, allMonthFriendCountInt, allMonthGroupCountInt, allMonthAccountBalanceInt, allMonthConsumeTokenInt, allMonthActiveUserInt uint64
|
|
|
+ var allMonthNewUserInt int64
|
|
|
for orgID, wxinfos := range wxbotsSet {
|
|
|
var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt uint64
|
|
|
var orgNewUserInt int64
|
|
@@ -372,7 +429,16 @@ func (l *CronTask) computeStatistic() {
|
|
|
orgConsumeTokenInt += consumeToken
|
|
|
orgActiveUserInt += activeUser
|
|
|
orgNewUserInt += newUser
|
|
|
- var lc []custom_types.LabelDist
|
|
|
+
|
|
|
+ allMonthAiResponseInt += aiResponse
|
|
|
+ allMonthSopRunInt += sopRun
|
|
|
+ allMonthFriendCountInt += totalFriend
|
|
|
+ allMonthGroupCountInt += totalGroup
|
|
|
+ allMonthAccountBalanceInt += accountBalance
|
|
|
+ allMonthConsumeTokenInt += consumeToken
|
|
|
+ allMonthActiveUserInt += activeUser
|
|
|
+ allMonthNewUserInt += newUser
|
|
|
+
|
|
|
_, err := l.svcCtx.DB.UsageStatisticMonth.Create().
|
|
|
SetAddtime(uint64(month)).
|
|
|
SetType(1).
|
|
@@ -426,6 +492,24 @@ func (l *CronTask) computeStatistic() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ _, err = l.svcCtx.DB.UsageStatisticMonth.Create().
|
|
|
+ SetAddtime(uint64(month)).
|
|
|
+ SetType(1).
|
|
|
+ SetOrganizationID(0).
|
|
|
+ SetAiResponse(allMonthAiResponseInt).
|
|
|
+ SetSopRun(allMonthSopRunInt).
|
|
|
+ SetTotalFriend(allMonthFriendCountInt).
|
|
|
+ SetTotalGroup(allMonthGroupCountInt).
|
|
|
+ SetAccountBalance(allMonthAccountBalanceInt).
|
|
|
+ SetConsumeToken(allMonthConsumeTokenInt).
|
|
|
+ SetActiveUser(allMonthActiveUserInt).
|
|
|
+ SetNewUser(allMonthNewUserInt).
|
|
|
+ SetNotNilLabelDist(lc).
|
|
|
+ Save(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ l.Errorf("create month data error:%v \n", err)
|
|
|
+ }
|
|
|
+
|
|
|
finishTime := time.Now()
|
|
|
l.Logger.Infof("This process cost %v", finishTime.Sub(startTime).String())
|
|
|
return
|