compute_statistic.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. package crontask
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "wechat-api/ent"
  7. "wechat-api/ent/contact"
  8. "wechat-api/ent/custom_types"
  9. "wechat-api/ent/labelrelationship"
  10. "wechat-api/ent/messagerecords"
  11. "wechat-api/ent/usagedetail"
  12. "wechat-api/ent/usagestatisticday"
  13. "wechat-api/ent/usagestatistichour"
  14. "wechat-api/ent/wx"
  15. )
  16. func (l *CronTask) computeStatistic() {
  17. startTime := time.Now()
  18. // 获取所有机器人信息
  19. wxbots, err := l.svcCtx.DB.Wx.Query().Select(wx.FieldWxid, wx.FieldID, wx.FieldOrganizationID).All(l.ctx)
  20. if err != nil {
  21. l.Errorf("fetch wxids error:%v\n", err)
  22. return
  23. }
  24. wxbotsSet := make(map[uint64][]*ent.Wx)
  25. for _, bot := range wxbots {
  26. if !strings.HasPrefix(bot.Wxid, "temp-") {
  27. wxbotsSet[bot.OrganizationID] = append(wxbotsSet[bot.OrganizationID], bot)
  28. }
  29. }
  30. LabelsCountSet := make(map[uint64][]custom_types.LabelDist)
  31. /*
  32. 计算本小时的数据
  33. 1. 查询出上小时里所有 usagedetail 内容
  34. 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
  35. 3. 遍历的时候可能有重复,所以要先检查是否生成了数据,如果有就忽略,没有再生成
  36. ----------------------------------------------------------------------------------------------------------
  37. */
  38. // 获取当前时间
  39. now := time.Now()
  40. // 获取本小时的第一分钟
  41. currentHour := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location())
  42. currentHourInt, _ := strconv.Atoi(currentHour.Format("2006010215"))
  43. // 上一个小时的起始时间
  44. lastHour := currentHour.Add(-time.Hour * 1)
  45. lastHourInt, _ := strconv.Atoi(lastHour.Format("2006010215"))
  46. lc := []custom_types.LabelDist{}
  47. var allHourAiResponseInt, allHourSopRunInt, allHourFriendCountInt, allHourGroupCountInt, allHourAccountBalanceInt, allHourConsumeTokenInt, allHourActiveUserInt, allHourNewUserInt int
  48. for orgID, wxinfos := range wxbotsSet {
  49. var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt, orgNewUserInt int
  50. for _, wxinfo := range wxinfos {
  51. l.Logger.Infof("开始计算小时数据:%d\n", lastHourInt)
  52. // 先判断该账号是否已经统计了小时数据,如果已经统计了,就不需要再统计了
  53. var aiResponseInt, sopRunInt, friendCountInt, groupCountInt, accountBalanceInt, consumeTokenInt, activeUserInt, newUserInt int
  54. hourDataCount, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
  55. usagestatistichour.Type(1),
  56. usagestatistichour.BotID(wxinfo.Wxid),
  57. usagestatistichour.Addtime(uint64(lastHourInt)),
  58. ).Count(l.ctx)
  59. if hourDataCount > 0 {
  60. continue
  61. }
  62. // AI回复包括:SOP次数+AI次数
  63. // SOP次数:content 非空,source_type = 3 或 4,sub_source_id = 0
  64. // AI次数:app = 1 或 3
  65. sopresp, _ := l.svcCtx.DB.MessageRecords.Query().Where(
  66. messagerecords.SubSourceID(0),
  67. messagerecords.SourceTypeIn(3, 4),
  68. messagerecords.BotWxid(wxinfo.Wxid),
  69. messagerecords.CreatedAtGTE(lastHour),
  70. messagerecords.CreatedAtLT(currentHour),
  71. ).Count(l.ctx)
  72. airesp, _ := l.svcCtx.DB.UsageDetail.Query().Where(
  73. usagedetail.AppIn(1, 3),
  74. usagedetail.BotID(wxinfo.Wxid),
  75. usagedetail.CreatedAtGTE(lastHour),
  76. usagedetail.CreatedAtLT(currentHour),
  77. ).Count(l.ctx)
  78. aiResponseInt = sopresp + airesp
  79. orgAiResponseInt += aiResponseInt
  80. allHourAiResponseInt += aiResponseInt
  81. // SOP执行次数:SOP阶段和节点的执行次数。
  82. sopRunInt, _ = l.svcCtx.DB.MessageRecords.Query().Where(
  83. messagerecords.BotWxid(wxinfo.Wxid),
  84. messagerecords.SubSourceIDEQ(0),
  85. messagerecords.SourceTypeIn(3, 4),
  86. messagerecords.BotWxid(wxinfo.Wxid),
  87. messagerecords.CreatedAtGTE(lastHour),
  88. messagerecords.CreatedAtLT(currentHour),
  89. ).Count(l.ctx)
  90. orgSopRunInt += sopRunInt
  91. allHourSopRunInt += sopRunInt
  92. // 好友总数:contact 表中 type=1
  93. friendCountInt, _ = l.svcCtx.DB.Contact.Query().Where(
  94. contact.Type(1),
  95. contact.WxWxid(wxinfo.Wxid),
  96. contact.Ctype(1),
  97. ).Count(l.ctx)
  98. orgFriendCountInt += friendCountInt
  99. allHourFriendCountInt += friendCountInt
  100. // 群总数:contact 表中 type=2
  101. groupCountInt, _ = l.svcCtx.DB.Contact.Query().Where(
  102. contact.Type(2),
  103. contact.WxWxid(wxinfo.Wxid),
  104. contact.Ctype(1),
  105. ).Count(l.ctx)
  106. orgGroupCountInt += groupCountInt
  107. allHourGroupCountInt += groupCountInt
  108. // 消耗Token数:usage_detail 表
  109. consumeTokenInt, _ = l.svcCtx.DB.UsageDetail.Query().Where(
  110. usagedetail.TypeEQ(1),
  111. usagedetail.BotID(wxinfo.Wxid),
  112. usagedetail.CreatedAtGTE(lastHour),
  113. usagedetail.CreatedAtLT(currentHour),
  114. ).Aggregate(ent.Sum("total_tokens")).Int(l.ctx)
  115. orgConsumeTokenInt += consumeTokenInt
  116. allHourConsumeTokenInt += consumeTokenInt
  117. // 账户余额
  118. accountBalanceInt = 0
  119. orgAccountBalanceInt = 0
  120. allHourAccountBalanceInt = 0
  121. // 活跃好友:usage_detail 表 type = 1
  122. activeUsers, _ := l.svcCtx.DB.UsageDetail.Query().Where(
  123. usagedetail.Type(1),
  124. usagedetail.BotID(wxinfo.Wxid),
  125. usagedetail.CreatedAtGTE(lastHour),
  126. usagedetail.CreatedAtLT(currentHour),
  127. ).GroupBy(usagedetail.FieldReceiverID).Strings(l.ctx)
  128. activeUserInt = len(activeUsers)
  129. orgActiveUserInt += activeUserInt
  130. allHourActiveUserInt += activeUserInt
  131. lastHourData, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
  132. usagestatistichour.AddtimeEQ(uint64(lastHourInt)),
  133. usagestatistichour.Type(1),
  134. usagestatistichour.BotID(wxinfo.Wxid),
  135. ).First(l.ctx)
  136. if lastHourData == nil {
  137. newUserInt = 0
  138. } else {
  139. newUserInt = friendCountInt - int(lastHourData.TotalFriend)
  140. }
  141. orgNewUserInt += newUserInt
  142. allHourNewUserInt += newUserInt
  143. _, err := l.svcCtx.DB.UsageStatisticHour.Create().
  144. SetType(1).
  145. SetBotID(wxinfo.Wxid).
  146. SetOrganizationID(wxinfo.OrganizationID).
  147. SetAiResponse(uint64(aiResponseInt)).
  148. SetSopRun(uint64(sopRunInt)).
  149. SetTotalFriend(uint64(friendCountInt)).
  150. SetTotalGroup(uint64(groupCountInt)).
  151. SetAccountBalance(uint64(accountBalanceInt)).
  152. SetConsumeToken(uint64(consumeTokenInt)).
  153. SetActiveUser(uint64(activeUserInt)).
  154. SetNewUser(int64(newUserInt)).
  155. SetAddtime(uint64(lastHourInt)).
  156. SetLabelDist(lc).
  157. Save(l.ctx)
  158. l.Errorf("save hour data error:%v \n", err)
  159. }
  160. // 先判断该租户是否已经统计了小时数据,如果已经统计了,就不需要再统计了
  161. hourDataCount, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
  162. usagestatistichour.Type(1),
  163. usagestatistichour.OrganizationID(orgID),
  164. usagestatistichour.BotID(""),
  165. usagestatistichour.Addtime(uint64(lastHourInt)),
  166. ).Count(l.ctx)
  167. if hourDataCount > 0 {
  168. continue
  169. }
  170. LabelsCount := []custom_types.LabelDist{}
  171. err := l.svcCtx.DB.LabelRelationship.Query().Where(
  172. labelrelationship.OrganizationIDEQ(orgID),
  173. labelrelationship.Ctype(1),
  174. ).GroupBy(labelrelationship.FieldLabelID).Aggregate(ent.Count()).Scan(l.ctx, &LabelsCount)
  175. l.Errorf("save hour data error:%v \n", err)
  176. LabelsCountSet[orgID] = LabelsCount
  177. _, err = l.svcCtx.DB.UsageStatisticHour.Create().
  178. SetType(1).
  179. SetOrganizationID(orgID).
  180. SetAiResponse(uint64(orgAiResponseInt)).
  181. SetSopRun(uint64(orgSopRunInt)).
  182. SetTotalFriend(uint64(orgFriendCountInt)).
  183. SetTotalGroup(uint64(orgGroupCountInt)).
  184. SetAccountBalance(uint64(orgAccountBalanceInt)).
  185. SetConsumeToken(uint64(orgConsumeTokenInt)).
  186. SetActiveUser(uint64(orgActiveUserInt)).
  187. SetNewUser(int64(orgNewUserInt)).
  188. SetAddtime(uint64(lastHourInt)).
  189. SetNotNilLabelDist(LabelsCount).
  190. Save(l.ctx)
  191. l.Errorf("save hour data error:%v \n", err)
  192. }
  193. // 先判断该租户是否已经统计了小时数据,如果已经统计了,就不需要再统计了
  194. hourDataCount, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
  195. usagestatistichour.Type(1),
  196. usagestatistichour.OrganizationID(0),
  197. usagestatistichour.BotID(""),
  198. usagestatistichour.Addtime(uint64(lastHourInt)),
  199. ).Count(l.ctx)
  200. if hourDataCount == 0 {
  201. _, err = l.svcCtx.DB.UsageStatisticHour.Create().
  202. SetType(1).
  203. SetOrganizationID(0).
  204. SetAiResponse(uint64(allHourAiResponseInt)).
  205. SetSopRun(uint64(allHourSopRunInt)).
  206. SetTotalFriend(uint64(allHourFriendCountInt)).
  207. SetTotalGroup(uint64(allHourGroupCountInt)).
  208. SetAccountBalance(uint64(allHourAccountBalanceInt)).
  209. SetConsumeToken(uint64(allHourConsumeTokenInt)).
  210. SetActiveUser(uint64(allHourActiveUserInt)).
  211. SetNewUser(int64(allHourNewUserInt)).
  212. SetAddtime(uint64(lastHourInt)).
  213. SetLabelDist(lc).
  214. Save(l.ctx)
  215. l.Errorf("save hour data error:%v \n", err)
  216. }
  217. /*
  218. 计算日数据
  219. ----------------------------------------------------------------------------------------------------------
  220. */
  221. //dayStr := time.Now().Format("20060102")
  222. //day, _ := strconv.Atoi(dayStr)
  223. // 获取昨天的第一小时
  224. yesterday := now.AddDate(0, 0, -1)
  225. yesterdayFirstHour := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, now.Location())
  226. yesterdayInt, _ := strconv.Atoi(yesterdayFirstHour.Format("20060102"))
  227. yesterdayFirstHourInt, _ := strconv.Atoi(yesterdayFirstHour.Format("2006010215"))
  228. // 获取昨天的最后一小时
  229. yesterdayLastHour := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  230. yesterdayLastHourInt, _ := strconv.Atoi(yesterdayLastHour.Format("2006010215"))
  231. var allDayAiResponseInt, allDaySopRunInt, allDayFriendCountInt, allDayGroupCountInt, allDayAccountBalanceInt, allDayConsumeTokenInt, allDayActiveUserInt uint64
  232. var allDayNewUserInt int64
  233. for orgID, wxinfos := range wxbotsSet {
  234. var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt uint64
  235. var orgNewUserInt int64
  236. for _, wxinfo := range wxinfos {
  237. l.Logger.Infof("开始计算日数据:%d\n", yesterdayInt)
  238. hourDataBatch, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
  239. usagestatistichour.Type(1),
  240. usagestatistichour.BotID(wxinfo.Wxid),
  241. usagestatistichour.AddtimeGTE(uint64(yesterdayFirstHourInt)),
  242. usagestatistichour.AddtimeLT(uint64(yesterdayLastHourInt)),
  243. ).All(l.ctx)
  244. if hourDataBatch == nil {
  245. continue
  246. }
  247. var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64
  248. var newUser int64
  249. for _, hourData := range hourDataBatch {
  250. aiResponse += hourData.AiResponse
  251. sopRun += hourData.SopRun
  252. totalFriend = hourData.TotalFriend
  253. totalGroup = hourData.TotalGroup
  254. accountBalance = hourData.AccountBalance
  255. consumeToken += hourData.ConsumeToken
  256. //activeUser += hourData.ActiveUser
  257. newUser += hourData.NewUser
  258. }
  259. // 活跃好友:usage_detail 表 type = 1
  260. activeUsers, _ := l.svcCtx.DB.UsageDetail.Query().Where(
  261. usagedetail.Type(1),
  262. usagedetail.BotID(wxinfo.Wxid),
  263. usagedetail.CreatedAtGTE(yesterdayFirstHour),
  264. usagedetail.CreatedAtLT(yesterdayLastHour),
  265. ).GroupBy(usagedetail.FieldReceiverID).Strings(l.ctx)
  266. activeUser = uint64(len(activeUsers))
  267. orgAiResponseInt += aiResponse
  268. orgSopRunInt += sopRun
  269. orgFriendCountInt += totalFriend
  270. orgGroupCountInt += totalGroup
  271. orgAccountBalanceInt += accountBalance
  272. orgConsumeTokenInt += consumeToken
  273. orgActiveUserInt += activeUser
  274. orgNewUserInt += newUser
  275. allDayAiResponseInt += aiResponse
  276. allDaySopRunInt += sopRun
  277. allDayFriendCountInt += totalFriend
  278. allDayGroupCountInt += totalGroup
  279. allDayAccountBalanceInt += accountBalance
  280. allDayConsumeTokenInt += consumeToken
  281. allDayActiveUserInt += activeUser
  282. allDayNewUserInt += newUser
  283. // 先判断该账号是否已经统计了日数据,如果已经统计了,就不需要再统计了
  284. dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  285. usagestatisticday.Type(1),
  286. usagestatisticday.BotID(wxinfo.Wxid),
  287. usagestatisticday.Addtime(uint64(yesterdayInt)),
  288. ).Count(l.ctx)
  289. // 如果添加过了就略过
  290. if dayDataCount == 0 && yesterdayLastHourInt <= currentHourInt {
  291. _, err := l.svcCtx.DB.UsageStatisticDay.Create().
  292. SetAddtime(uint64(yesterdayInt)).
  293. SetType(1).
  294. SetBotID(wxinfo.Wxid).
  295. SetOrganizationID(wxinfo.OrganizationID).
  296. SetAiResponse(aiResponse).
  297. SetSopRun(sopRun).
  298. SetTotalFriend(totalFriend).
  299. SetTotalGroup(totalGroup).
  300. SetAccountBalance(accountBalance).
  301. SetConsumeToken(consumeToken).
  302. SetActiveUser(activeUser).
  303. SetNewUser(newUser).
  304. SetLabelDist(lc).
  305. Save(l.ctx)
  306. if err != nil {
  307. l.Errorf("create day data error:%v \n", err)
  308. continue
  309. }
  310. }
  311. }
  312. // 先判断该租户是否已经统计了日数据,如果已经统计了,就不需要再统计了
  313. dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  314. usagestatisticday.Type(1),
  315. usagestatisticday.OrganizationID(orgID),
  316. usagestatisticday.BotID(""),
  317. usagestatisticday.Addtime(uint64(yesterdayInt)),
  318. ).Count(l.ctx)
  319. // 如果添加过了就略过
  320. if dayDataCount == 0 && yesterdayLastHourInt <= currentHourInt {
  321. _, err := l.svcCtx.DB.UsageStatisticDay.Create().
  322. SetAddtime(uint64(yesterdayInt)).
  323. SetType(1).
  324. SetOrganizationID(orgID).
  325. SetAiResponse(orgAiResponseInt).
  326. SetSopRun(orgSopRunInt).
  327. SetTotalFriend(orgFriendCountInt).
  328. SetTotalGroup(orgGroupCountInt).
  329. SetAccountBalance(orgAccountBalanceInt).
  330. SetConsumeToken(orgConsumeTokenInt).
  331. SetActiveUser(orgActiveUserInt).
  332. SetNewUser(orgNewUserInt).
  333. SetNotNilLabelDist(LabelsCountSet[orgID]).
  334. Save(l.ctx)
  335. if err != nil {
  336. l.Errorf("create day data error:%v \n", err)
  337. continue
  338. }
  339. }
  340. }
  341. // 先判断该租户是否已经统计了日数据,如果已经统计了,就不需要再统计了
  342. dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  343. usagestatisticday.Type(1),
  344. usagestatisticday.OrganizationID(0),
  345. usagestatisticday.BotID(""),
  346. usagestatisticday.Addtime(uint64(yesterdayInt)),
  347. ).Count(l.ctx)
  348. // 如果添加过了就略过
  349. if dayDataCount == 0 && yesterdayLastHourInt <= currentHourInt {
  350. _, err = l.svcCtx.DB.UsageStatisticDay.Create().
  351. SetAddtime(uint64(yesterdayInt)).
  352. SetType(1).
  353. SetOrganizationID(0).
  354. SetAiResponse(allDayAiResponseInt).
  355. SetSopRun(allDaySopRunInt).
  356. SetTotalFriend(allDayFriendCountInt).
  357. SetTotalGroup(allDayGroupCountInt).
  358. SetAccountBalance(allDayAccountBalanceInt).
  359. SetConsumeToken(allDayConsumeTokenInt).
  360. SetActiveUser(allDayActiveUserInt).
  361. SetNewUser(allDayNewUserInt).
  362. SetLabelDist(lc).
  363. Save(l.ctx)
  364. if err != nil {
  365. l.Errorf("create day data error:%v \n", err)
  366. }
  367. }
  368. /*
  369. 查看月表数据是否已经完成
  370. 1. 查询出上月里所有 usagedetail 内容
  371. 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
  372. ----------------------------------------------------------------------------------------------------------
  373. */
  374. //monthStr := time.Now().Format("200601")
  375. //month, _ := strconv.Atoi(monthStr)
  376. //
  377. //var allMonthAiResponseInt, allMonthSopRunInt, allMonthFriendCountInt, allMonthGroupCountInt, allMonthAccountBalanceInt, allMonthConsumeTokenInt, allMonthActiveUserInt uint64
  378. //var allMonthNewUserInt int64
  379. //for orgID, wxinfos := range wxbotsSet {
  380. // var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt uint64
  381. // var orgNewUserInt int64
  382. // for _, wxinfo := range wxinfos {
  383. // l.Logger.Infof("开始计算月数据:%d\n", month)
  384. //
  385. // // 获取上月的第一天
  386. // monthFirstDay := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, now.Location())
  387. // monthFirstDayInt, _ := strconv.Atoi(monthFirstDay.Format("20060102"))
  388. //
  389. // // 获取上月的最后一天
  390. // monthLastDay := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  391. // monthLastDayInt, _ := strconv.Atoi(monthLastDay.Format("20060102"))
  392. //
  393. // // 先判断该账号是否已经统计了月数据,如果已经统计了,就不需要再统计了
  394. // monthDataCount, _ := l.svcCtx.DB.UsageStatisticMonth.Query().Where(
  395. // usagestatisticmonth.Type(1),
  396. // usagestatisticmonth.BotID(wxinfo.Wxid),
  397. // usagestatisticmonth.Addtime(uint64(month)),
  398. // ).Count(l.ctx)
  399. //
  400. // // 如果添加过了就略过
  401. // if monthDataCount > 0 {
  402. // continue
  403. // }
  404. //
  405. // dayDataBatch, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  406. // usagestatisticday.Type(1),
  407. // usagestatisticday.BotID(wxinfo.Wxid),
  408. // usagestatisticday.AddtimeGTE(uint64(monthFirstDayInt)),
  409. // usagestatisticday.AddtimeLT(uint64(monthLastDayInt)),
  410. // ).All(l.ctx)
  411. //
  412. // var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64
  413. // var newUser int64
  414. // for _, dayData := range dayDataBatch {
  415. // aiResponse += dayData.AiResponse
  416. // sopRun += dayData.SopRun
  417. // totalFriend += dayData.TotalFriend
  418. // totalGroup += dayData.TotalGroup
  419. // accountBalance += dayData.AccountBalance
  420. // consumeToken += dayData.ConsumeToken
  421. // activeUser += dayData.ActiveUser
  422. // newUser += dayData.NewUser
  423. // }
  424. // orgAiResponseInt += aiResponse
  425. // orgSopRunInt += sopRun
  426. // orgFriendCountInt += totalFriend
  427. // orgGroupCountInt += totalGroup
  428. // orgAccountBalanceInt += accountBalance
  429. // orgConsumeTokenInt += consumeToken
  430. // orgActiveUserInt += activeUser
  431. // orgNewUserInt += newUser
  432. //
  433. // allMonthAiResponseInt += aiResponse
  434. // allMonthSopRunInt += sopRun
  435. // allMonthFriendCountInt += totalFriend
  436. // allMonthGroupCountInt += totalGroup
  437. // allMonthAccountBalanceInt += accountBalance
  438. // allMonthConsumeTokenInt += consumeToken
  439. // allMonthActiveUserInt += activeUser
  440. // allMonthNewUserInt += newUser
  441. //
  442. // _, err := l.svcCtx.DB.UsageStatisticMonth.Create().
  443. // SetAddtime(uint64(month)).
  444. // SetType(1).
  445. // SetBotID(wxinfo.Wxid).
  446. // SetOrganizationID(wxinfo.OrganizationID).
  447. // SetAiResponse(aiResponse).
  448. // SetSopRun(sopRun).
  449. // SetTotalFriend(totalFriend).
  450. // SetTotalGroup(totalGroup).
  451. // SetAccountBalance(accountBalance).
  452. // SetConsumeToken(consumeToken).
  453. // SetActiveUser(activeUser).
  454. // SetNewUser(newUser).
  455. // SetLabelDist(lc).
  456. // Save(l.ctx)
  457. // if err != nil {
  458. // l.Errorf("create month data error:%v \n", err)
  459. // continue
  460. // }
  461. // }
  462. // // 先判断该租户是否已经统计了月数据,如果已经统计了,就不需要再统计了
  463. // monthDataCount, _ := l.svcCtx.DB.UsageStatisticMonth.Query().Where(
  464. // usagestatisticmonth.Type(1),
  465. // usagestatisticmonth.OrganizationID(orgID),
  466. // usagestatisticmonth.BotIDIsNil(),
  467. // usagestatisticmonth.Addtime(uint64(month)),
  468. // ).Count(l.ctx)
  469. //
  470. // // 如果添加过了就略过
  471. // if monthDataCount > 0 {
  472. // continue
  473. // }
  474. //
  475. // _, err := l.svcCtx.DB.UsageStatisticMonth.Create().
  476. // SetAddtime(uint64(month)).
  477. // SetType(1).
  478. // SetOrganizationID(orgID).
  479. // SetAiResponse(orgAiResponseInt).
  480. // SetSopRun(orgSopRunInt).
  481. // SetTotalFriend(orgFriendCountInt).
  482. // SetTotalGroup(orgGroupCountInt).
  483. // SetAccountBalance(orgAccountBalanceInt).
  484. // SetConsumeToken(orgConsumeTokenInt).
  485. // SetActiveUser(orgActiveUserInt).
  486. // SetNewUser(orgNewUserInt).
  487. // SetNotNilLabelDist(LabelsCountSet[orgID]).
  488. // Save(l.ctx)
  489. // if err != nil {
  490. // l.Errorf("create month data error:%v \n", err)
  491. // continue
  492. // }
  493. //}
  494. //
  495. //_, err = l.svcCtx.DB.UsageStatisticMonth.Create().
  496. // SetAddtime(uint64(month)).
  497. // SetType(1).
  498. // SetOrganizationID(0).
  499. // SetAiResponse(allMonthAiResponseInt).
  500. // SetSopRun(allMonthSopRunInt).
  501. // SetTotalFriend(allMonthFriendCountInt).
  502. // SetTotalGroup(allMonthGroupCountInt).
  503. // SetAccountBalance(allMonthAccountBalanceInt).
  504. // SetConsumeToken(allMonthConsumeTokenInt).
  505. // SetActiveUser(allMonthActiveUserInt).
  506. // SetNewUser(allMonthNewUserInt).
  507. // SetNotNilLabelDist(lc).
  508. // Save(l.ctx)
  509. //if err != nil {
  510. // l.Errorf("create month data error:%v \n", err)
  511. //}
  512. finishTime := time.Now()
  513. l.Logger.Infof("This process cost %v", finishTime.Sub(startTime).String())
  514. return
  515. }