|
@@ -11,7 +11,6 @@ import (
|
|
|
"wechat-api/ent/usagedetail"
|
|
|
"wechat-api/ent/usagestatisticday"
|
|
|
"wechat-api/ent/usagestatistichour"
|
|
|
- "wechat-api/ent/usagestatisticmonth"
|
|
|
"wechat-api/ent/wx"
|
|
|
)
|
|
|
|
|
@@ -177,7 +176,7 @@ func (l *CronTask) computeStatistic() {
|
|
|
hourDataCount, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
|
|
|
usagestatistichour.Type(1),
|
|
|
usagestatistichour.OrganizationID(orgID),
|
|
|
- usagestatistichour.BotIDIsNil(),
|
|
|
+ usagestatistichour.BotID(""),
|
|
|
usagestatistichour.Addtime(uint64(currentHourInt)),
|
|
|
).Count(l.ctx)
|
|
|
if hourDataCount > 0 {
|
|
@@ -206,21 +205,30 @@ 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)
|
|
|
+ // 先判断该租户是否已经统计了小时数据,如果已经统计了,就不需要再统计了
|
|
|
+ hourDataCount, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
|
|
|
+ usagestatistichour.Type(1),
|
|
|
+ usagestatistichour.OrganizationID(0),
|
|
|
+ usagestatistichour.BotID(""),
|
|
|
+ usagestatistichour.Addtime(uint64(currentHourInt)),
|
|
|
+ ).Count(l.ctx)
|
|
|
+ if hourDataCount == 0 {
|
|
|
+ _, 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)
|
|
|
+ }
|
|
|
|
|
|
/*
|
|
|
计算日数据
|
|
@@ -232,11 +240,12 @@ func (l *CronTask) computeStatistic() {
|
|
|
// 获取昨天的第一小时
|
|
|
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"))
|
|
|
+ yesterdayInt, _ := strconv.Atoi(yesterdayFirstHour.Format("20060102"))
|
|
|
+ yesterdayFirstHourInt, _ := strconv.Atoi(yesterdayFirstHour.Format("2006010215"))
|
|
|
|
|
|
// 获取昨天的最后一小时
|
|
|
yesterdayLastHour := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
|
- yesterdayLastHourInt, _ := strconv.Atoi(yesterdayLastHour.Format("20060102"))
|
|
|
+ yesterdayLastHourInt, _ := strconv.Atoi(yesterdayLastHour.Format("2006010215"))
|
|
|
|
|
|
var allDayAiResponseInt, allDaySopRunInt, allDayFriendCountInt, allDayGroupCountInt, allDayAccountBalanceInt, allDayConsumeTokenInt, allDayActiveUserInt uint64
|
|
|
var allDayNewUserInt int64
|
|
@@ -244,18 +253,7 @@ func (l *CronTask) computeStatistic() {
|
|
|
var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt uint64
|
|
|
var orgNewUserInt int64
|
|
|
for _, wxinfo := range wxinfos {
|
|
|
- l.Logger.Infof("开始计算日数据:%d\n", day)
|
|
|
- // 先判断该账号是否已经统计了日数据,如果已经统计了,就不需要再统计了
|
|
|
- dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
|
|
|
- usagestatisticday.Type(1),
|
|
|
- usagestatisticday.BotID(wxinfo.Wxid),
|
|
|
- usagestatisticday.Addtime(uint64(day)),
|
|
|
- ).Count(l.ctx)
|
|
|
-
|
|
|
- // 如果添加过了就略过
|
|
|
- if dayDataCount > 0 {
|
|
|
- continue
|
|
|
- }
|
|
|
+ l.Logger.Infof("开始计算日数据:%d\n", yesterdayInt)
|
|
|
|
|
|
hourDataBatch, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
|
|
|
usagestatistichour.Type(1),
|
|
@@ -264,6 +262,9 @@ func (l *CronTask) computeStatistic() {
|
|
|
usagestatistichour.AddtimeLT(uint64(yesterdayLastHourInt)),
|
|
|
).All(l.ctx)
|
|
|
|
|
|
+ if hourDataBatch == nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64
|
|
|
var newUser int64
|
|
|
for _, hourData := range hourDataBatch {
|
|
@@ -294,221 +295,304 @@ func (l *CronTask) computeStatistic() {
|
|
|
allDayActiveUserInt += activeUser
|
|
|
allDayNewUserInt += newUser
|
|
|
|
|
|
- _, err := l.svcCtx.DB.UsageStatisticDay.Create().
|
|
|
- SetAddtime(uint64(day)).
|
|
|
- SetType(1).
|
|
|
- SetBotID(wxinfo.Wxid).
|
|
|
- SetOrganizationID(wxinfo.OrganizationID).
|
|
|
- SetAiResponse(aiResponse).
|
|
|
- SetSopRun(sopRun).
|
|
|
- SetTotalFriend(totalFriend).
|
|
|
- SetTotalGroup(totalGroup).
|
|
|
- SetAccountBalance(accountBalance).
|
|
|
- SetConsumeToken(consumeToken).
|
|
|
- SetActiveUser(activeUser).
|
|
|
- SetNewUser(newUser).
|
|
|
- SetLabelDist(lc).
|
|
|
- Save(l.ctx)
|
|
|
- if err != nil {
|
|
|
- l.Errorf("create day data error:%v \n", err)
|
|
|
- continue
|
|
|
+ // 先判断该账号是否已经统计了日数据,如果已经统计了,就不需要再统计了
|
|
|
+ dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
|
|
|
+ usagestatisticday.Type(1),
|
|
|
+ usagestatisticday.BotID(wxinfo.Wxid),
|
|
|
+ usagestatisticday.Addtime(uint64(yesterdayInt)),
|
|
|
+ ).Count(l.ctx)
|
|
|
+
|
|
|
+ // 如果添加过了就略过
|
|
|
+ if dayDataCount > 0 {
|
|
|
+ _, err := l.svcCtx.DB.UsageStatisticDay.Update().
|
|
|
+ Where(
|
|
|
+ usagestatisticday.Type(1),
|
|
|
+ usagestatisticday.BotID(wxinfo.Wxid),
|
|
|
+ usagestatisticday.Addtime(uint64(yesterdayInt)),
|
|
|
+ ).
|
|
|
+ SetAiResponse(aiResponse).
|
|
|
+ SetSopRun(sopRun).
|
|
|
+ SetTotalFriend(totalFriend).
|
|
|
+ SetTotalGroup(totalGroup).
|
|
|
+ SetAccountBalance(accountBalance).
|
|
|
+ SetConsumeToken(consumeToken).
|
|
|
+ SetActiveUser(activeUser).
|
|
|
+ SetNewUser(newUser).
|
|
|
+ SetLabelDist(lc).
|
|
|
+ Save(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ l.Errorf("create day data error:%v \n", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ _, err := l.svcCtx.DB.UsageStatisticDay.Create().
|
|
|
+ SetAddtime(uint64(yesterdayInt)).
|
|
|
+ SetType(1).
|
|
|
+ SetBotID(wxinfo.Wxid).
|
|
|
+ SetOrganizationID(wxinfo.OrganizationID).
|
|
|
+ SetAiResponse(aiResponse).
|
|
|
+ SetSopRun(sopRun).
|
|
|
+ SetTotalFriend(totalFriend).
|
|
|
+ SetTotalGroup(totalGroup).
|
|
|
+ SetAccountBalance(accountBalance).
|
|
|
+ SetConsumeToken(consumeToken).
|
|
|
+ SetActiveUser(activeUser).
|
|
|
+ SetNewUser(newUser).
|
|
|
+ SetLabelDist(lc).
|
|
|
+ Save(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ l.Errorf("create day data error:%v \n", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
// 先判断该租户是否已经统计了日数据,如果已经统计了,就不需要再统计了
|
|
|
dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
|
|
|
usagestatisticday.Type(1),
|
|
|
usagestatisticday.OrganizationID(orgID),
|
|
|
- usagestatisticday.BotIDIsNil(),
|
|
|
- usagestatisticday.Addtime(uint64(day)),
|
|
|
+ usagestatisticday.BotID(""),
|
|
|
+ usagestatisticday.Addtime(uint64(yesterdayInt)),
|
|
|
).Count(l.ctx)
|
|
|
|
|
|
// 如果添加过了就略过
|
|
|
if dayDataCount > 0 {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- _, err := l.svcCtx.DB.UsageStatisticDay.Create().
|
|
|
- SetAddtime(uint64(day)).
|
|
|
- SetType(1).
|
|
|
- SetOrganizationID(orgID).
|
|
|
- SetAiResponse(orgAiResponseInt).
|
|
|
- SetSopRun(orgSopRunInt).
|
|
|
- SetTotalFriend(orgFriendCountInt).
|
|
|
- SetTotalGroup(orgGroupCountInt).
|
|
|
- SetAccountBalance(orgAccountBalanceInt).
|
|
|
- SetConsumeToken(orgConsumeTokenInt).
|
|
|
- SetActiveUser(orgActiveUserInt).
|
|
|
- SetNewUser(orgNewUserInt).
|
|
|
- SetNotNilLabelDist(LabelsCountSet[orgID]).
|
|
|
- Save(l.ctx)
|
|
|
- if err != nil {
|
|
|
- l.Errorf("create day data error:%v \n", err)
|
|
|
- continue
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- _, 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 内容
|
|
|
- 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
|
|
|
- ----------------------------------------------------------------------------------------------------------
|
|
|
- */
|
|
|
- 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
|
|
|
- for _, wxinfo := range wxinfos {
|
|
|
- l.Logger.Infof("开始计算月数据:%d\n", month)
|
|
|
-
|
|
|
- // 获取上月的第一天
|
|
|
- monthFirstDay := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, now.Location())
|
|
|
- monthFirstDayInt, _ := strconv.Atoi(monthFirstDay.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(
|
|
|
- usagestatisticmonth.Type(1),
|
|
|
- usagestatisticmonth.BotID(wxinfo.Wxid),
|
|
|
- usagestatisticmonth.Addtime(uint64(month)),
|
|
|
- ).Count(l.ctx)
|
|
|
-
|
|
|
- // 如果添加过了就略过
|
|
|
- if monthDataCount > 0 {
|
|
|
+ _, err := l.svcCtx.DB.UsageStatisticDay.Update().
|
|
|
+ Where(
|
|
|
+ usagestatisticday.Type(1),
|
|
|
+ usagestatisticday.OrganizationID(orgID),
|
|
|
+ usagestatisticday.BotID(""),
|
|
|
+ usagestatisticday.Addtime(uint64(yesterdayInt)),
|
|
|
+ ).
|
|
|
+ SetAiResponse(orgAiResponseInt).
|
|
|
+ SetSopRun(orgSopRunInt).
|
|
|
+ SetTotalFriend(orgFriendCountInt).
|
|
|
+ SetTotalGroup(orgGroupCountInt).
|
|
|
+ SetAccountBalance(orgAccountBalanceInt).
|
|
|
+ SetConsumeToken(orgConsumeTokenInt).
|
|
|
+ SetActiveUser(orgActiveUserInt).
|
|
|
+ SetNewUser(orgNewUserInt).
|
|
|
+ SetNotNilLabelDist(LabelsCountSet[orgID]).
|
|
|
+ Save(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ l.Errorf("create day data error:%v \n", err)
|
|
|
continue
|
|
|
}
|
|
|
-
|
|
|
- dayDataBatch, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
|
|
|
- usagestatisticday.Type(1),
|
|
|
- usagestatisticday.BotID(wxinfo.Wxid),
|
|
|
- usagestatisticday.AddtimeGTE(uint64(monthFirstDayInt)),
|
|
|
- usagestatisticday.AddtimeLT(uint64(monthLastDayInt)),
|
|
|
- ).All(l.ctx)
|
|
|
-
|
|
|
- var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64
|
|
|
- var newUser int64
|
|
|
- for _, dayData := range dayDataBatch {
|
|
|
- aiResponse += dayData.AiResponse
|
|
|
- sopRun += dayData.SopRun
|
|
|
- totalFriend += dayData.TotalFriend
|
|
|
- totalGroup += dayData.TotalGroup
|
|
|
- accountBalance += dayData.AccountBalance
|
|
|
- consumeToken += dayData.ConsumeToken
|
|
|
- activeUser += dayData.ActiveUser
|
|
|
- newUser += dayData.NewUser
|
|
|
- }
|
|
|
- orgAiResponseInt += aiResponse
|
|
|
- orgSopRunInt += sopRun
|
|
|
- orgFriendCountInt += totalFriend
|
|
|
- orgGroupCountInt += totalGroup
|
|
|
- orgAccountBalanceInt += accountBalance
|
|
|
- orgConsumeTokenInt += consumeToken
|
|
|
- orgActiveUserInt += activeUser
|
|
|
- orgNewUserInt += newUser
|
|
|
-
|
|
|
- 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)).
|
|
|
+ } else {
|
|
|
+ _, err := l.svcCtx.DB.UsageStatisticDay.Create().
|
|
|
+ SetAddtime(uint64(yesterdayInt)).
|
|
|
SetType(1).
|
|
|
- SetBotID(wxinfo.Wxid).
|
|
|
- SetOrganizationID(wxinfo.OrganizationID).
|
|
|
- SetAiResponse(aiResponse).
|
|
|
- SetSopRun(sopRun).
|
|
|
- SetTotalFriend(totalFriend).
|
|
|
- SetTotalGroup(totalGroup).
|
|
|
- SetAccountBalance(accountBalance).
|
|
|
- SetConsumeToken(consumeToken).
|
|
|
- SetActiveUser(activeUser).
|
|
|
- SetNewUser(newUser).
|
|
|
- SetLabelDist(lc).
|
|
|
+ SetOrganizationID(orgID).
|
|
|
+ SetAiResponse(orgAiResponseInt).
|
|
|
+ SetSopRun(orgSopRunInt).
|
|
|
+ SetTotalFriend(orgFriendCountInt).
|
|
|
+ SetTotalGroup(orgGroupCountInt).
|
|
|
+ SetAccountBalance(orgAccountBalanceInt).
|
|
|
+ SetConsumeToken(orgConsumeTokenInt).
|
|
|
+ SetActiveUser(orgActiveUserInt).
|
|
|
+ SetNewUser(orgNewUserInt).
|
|
|
+ SetNotNilLabelDist(LabelsCountSet[orgID]).
|
|
|
Save(l.ctx)
|
|
|
if err != nil {
|
|
|
- l.Errorf("create month data error:%v \n", err)
|
|
|
+ l.Errorf("create day data error:%v \n", err)
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
- // 先判断该租户是否已经统计了月数据,如果已经统计了,就不需要再统计了
|
|
|
- monthDataCount, _ := l.svcCtx.DB.UsageStatisticMonth.Query().Where(
|
|
|
- usagestatisticmonth.Type(1),
|
|
|
- usagestatisticmonth.OrganizationID(orgID),
|
|
|
- usagestatisticmonth.BotIDIsNil(),
|
|
|
- usagestatisticmonth.Addtime(uint64(month)),
|
|
|
- ).Count(l.ctx)
|
|
|
+ }
|
|
|
|
|
|
- // 如果添加过了就略过
|
|
|
- if monthDataCount > 0 {
|
|
|
- continue
|
|
|
+ // 先判断该租户是否已经统计了日数据,如果已经统计了,就不需要再统计了
|
|
|
+ dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
|
|
|
+ usagestatisticday.Type(1),
|
|
|
+ usagestatisticday.OrganizationID(0),
|
|
|
+ usagestatisticday.BotID(""),
|
|
|
+ usagestatisticday.Addtime(uint64(yesterdayInt)),
|
|
|
+ ).Count(l.ctx)
|
|
|
+
|
|
|
+ // 如果添加过了就略过
|
|
|
+ if dayDataCount > 0 {
|
|
|
+ _, err = l.svcCtx.DB.UsageStatisticDay.Update().
|
|
|
+ Where(
|
|
|
+ usagestatisticday.Type(1),
|
|
|
+ usagestatisticday.OrganizationID(0),
|
|
|
+ usagestatisticday.BotID(""),
|
|
|
+ usagestatisticday.Addtime(uint64(yesterdayInt)),
|
|
|
+ ).
|
|
|
+ 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)
|
|
|
}
|
|
|
-
|
|
|
- _, err := l.svcCtx.DB.UsageStatisticMonth.Create().
|
|
|
- SetAddtime(uint64(month)).
|
|
|
+ } else {
|
|
|
+ _, err = l.svcCtx.DB.UsageStatisticDay.Create().
|
|
|
+ SetAddtime(uint64(day)).
|
|
|
SetType(1).
|
|
|
- SetOrganizationID(orgID).
|
|
|
- SetAiResponse(orgAiResponseInt).
|
|
|
- SetSopRun(orgSopRunInt).
|
|
|
- SetTotalFriend(orgFriendCountInt).
|
|
|
- SetTotalGroup(orgGroupCountInt).
|
|
|
- SetAccountBalance(orgAccountBalanceInt).
|
|
|
- SetConsumeToken(orgConsumeTokenInt).
|
|
|
- SetActiveUser(orgActiveUserInt).
|
|
|
- SetNewUser(orgNewUserInt).
|
|
|
- SetNotNilLabelDist(LabelsCountSet[orgID]).
|
|
|
+ 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 month data error:%v \n", err)
|
|
|
- continue
|
|
|
+ l.Errorf("create day data error:%v \n", err)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- _, 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)
|
|
|
- }
|
|
|
+ /*
|
|
|
+ 查看月表数据是否已经完成
|
|
|
+ 1. 查询出上月里所有 usagedetail 内容
|
|
|
+ 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
|
|
|
+ ----------------------------------------------------------------------------------------------------------
|
|
|
+ */
|
|
|
+ //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
|
|
|
+ // for _, wxinfo := range wxinfos {
|
|
|
+ // l.Logger.Infof("开始计算月数据:%d\n", month)
|
|
|
+ //
|
|
|
+ // // 获取上月的第一天
|
|
|
+ // monthFirstDay := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, now.Location())
|
|
|
+ // monthFirstDayInt, _ := strconv.Atoi(monthFirstDay.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(
|
|
|
+ // usagestatisticmonth.Type(1),
|
|
|
+ // usagestatisticmonth.BotID(wxinfo.Wxid),
|
|
|
+ // usagestatisticmonth.Addtime(uint64(month)),
|
|
|
+ // ).Count(l.ctx)
|
|
|
+ //
|
|
|
+ // // 如果添加过了就略过
|
|
|
+ // if monthDataCount > 0 {
|
|
|
+ // continue
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // dayDataBatch, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
|
|
|
+ // usagestatisticday.Type(1),
|
|
|
+ // usagestatisticday.BotID(wxinfo.Wxid),
|
|
|
+ // usagestatisticday.AddtimeGTE(uint64(monthFirstDayInt)),
|
|
|
+ // usagestatisticday.AddtimeLT(uint64(monthLastDayInt)),
|
|
|
+ // ).All(l.ctx)
|
|
|
+ //
|
|
|
+ // var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64
|
|
|
+ // var newUser int64
|
|
|
+ // for _, dayData := range dayDataBatch {
|
|
|
+ // aiResponse += dayData.AiResponse
|
|
|
+ // sopRun += dayData.SopRun
|
|
|
+ // totalFriend += dayData.TotalFriend
|
|
|
+ // totalGroup += dayData.TotalGroup
|
|
|
+ // accountBalance += dayData.AccountBalance
|
|
|
+ // consumeToken += dayData.ConsumeToken
|
|
|
+ // activeUser += dayData.ActiveUser
|
|
|
+ // newUser += dayData.NewUser
|
|
|
+ // }
|
|
|
+ // orgAiResponseInt += aiResponse
|
|
|
+ // orgSopRunInt += sopRun
|
|
|
+ // orgFriendCountInt += totalFriend
|
|
|
+ // orgGroupCountInt += totalGroup
|
|
|
+ // orgAccountBalanceInt += accountBalance
|
|
|
+ // orgConsumeTokenInt += consumeToken
|
|
|
+ // orgActiveUserInt += activeUser
|
|
|
+ // orgNewUserInt += newUser
|
|
|
+ //
|
|
|
+ // 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).
|
|
|
+ // SetBotID(wxinfo.Wxid).
|
|
|
+ // SetOrganizationID(wxinfo.OrganizationID).
|
|
|
+ // SetAiResponse(aiResponse).
|
|
|
+ // SetSopRun(sopRun).
|
|
|
+ // SetTotalFriend(totalFriend).
|
|
|
+ // SetTotalGroup(totalGroup).
|
|
|
+ // SetAccountBalance(accountBalance).
|
|
|
+ // SetConsumeToken(consumeToken).
|
|
|
+ // SetActiveUser(activeUser).
|
|
|
+ // SetNewUser(newUser).
|
|
|
+ // SetLabelDist(lc).
|
|
|
+ // Save(l.ctx)
|
|
|
+ // if err != nil {
|
|
|
+ // l.Errorf("create month data error:%v \n", err)
|
|
|
+ // continue
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // // 先判断该租户是否已经统计了月数据,如果已经统计了,就不需要再统计了
|
|
|
+ // monthDataCount, _ := l.svcCtx.DB.UsageStatisticMonth.Query().Where(
|
|
|
+ // usagestatisticmonth.Type(1),
|
|
|
+ // usagestatisticmonth.OrganizationID(orgID),
|
|
|
+ // usagestatisticmonth.BotIDIsNil(),
|
|
|
+ // usagestatisticmonth.Addtime(uint64(month)),
|
|
|
+ // ).Count(l.ctx)
|
|
|
+ //
|
|
|
+ // // 如果添加过了就略过
|
|
|
+ // if monthDataCount > 0 {
|
|
|
+ // continue
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // _, err := l.svcCtx.DB.UsageStatisticMonth.Create().
|
|
|
+ // SetAddtime(uint64(month)).
|
|
|
+ // SetType(1).
|
|
|
+ // SetOrganizationID(orgID).
|
|
|
+ // SetAiResponse(orgAiResponseInt).
|
|
|
+ // SetSopRun(orgSopRunInt).
|
|
|
+ // SetTotalFriend(orgFriendCountInt).
|
|
|
+ // SetTotalGroup(orgGroupCountInt).
|
|
|
+ // SetAccountBalance(orgAccountBalanceInt).
|
|
|
+ // SetConsumeToken(orgConsumeTokenInt).
|
|
|
+ // SetActiveUser(orgActiveUserInt).
|
|
|
+ // SetNewUser(orgNewUserInt).
|
|
|
+ // SetNotNilLabelDist(LabelsCountSet[orgID]).
|
|
|
+ // Save(l.ctx)
|
|
|
+ // if err != nil {
|
|
|
+ // l.Errorf("create month data error:%v \n", err)
|
|
|
+ // continue
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ //
|
|
|
+ //_, 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())
|