compute_statistic.go 19 KB

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