compute_statistic.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. activeUserInt, _ = 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).Int(l.ctx)
  123. orgActiveUserInt += activeUserInt
  124. allHourActiveUserInt += activeUserInt
  125. lastHourData, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
  126. usagestatistichour.AddtimeEQ(uint64(lastHourInt)),
  127. usagestatistichour.Type(1),
  128. usagestatistichour.BotID(wxinfo.Wxid),
  129. ).First(l.ctx)
  130. if lastHourData == nil {
  131. newUserInt = friendCountInt
  132. } else {
  133. newUserInt = int(lastHourData.TotalFriend) - friendCountInt
  134. }
  135. orgNewUserInt += newUserInt
  136. allHourNewUserInt += newUserInt
  137. _, err := l.svcCtx.DB.UsageStatisticHour.Create().
  138. SetType(1).
  139. SetBotID(wxinfo.Wxid).
  140. SetOrganizationID(wxinfo.OrganizationID).
  141. SetAiResponse(uint64(aiResponseInt)).
  142. SetSopRun(uint64(sopRunInt)).
  143. SetTotalFriend(uint64(friendCountInt)).
  144. SetTotalGroup(uint64(groupCountInt)).
  145. SetAccountBalance(uint64(accountBalanceInt)).
  146. SetConsumeToken(uint64(consumeTokenInt)).
  147. SetActiveUser(uint64(activeUserInt)).
  148. SetNewUser(int64(newUserInt)).
  149. SetAddtime(uint64(currentHourInt)).
  150. SetLabelDist(lc).
  151. Save(l.ctx)
  152. l.Errorf("save hour data error:%v \n", err)
  153. }
  154. // 先判断该租户是否已经统计了小时数据,如果已经统计了,就不需要再统计了
  155. hourDataCount, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
  156. usagestatistichour.Type(1),
  157. usagestatistichour.OrganizationID(orgID),
  158. usagestatistichour.BotID(""),
  159. usagestatistichour.Addtime(uint64(currentHourInt)),
  160. ).Count(l.ctx)
  161. if hourDataCount > 0 {
  162. continue
  163. }
  164. LabelsCount := []custom_types.LabelDist{}
  165. err := l.svcCtx.DB.LabelRelationship.Query().Where(labelrelationship.OrganizationIDEQ(orgID), labelrelationship.DeletedAtIsNil()).GroupBy(labelrelationship.FieldLabelID).Aggregate(ent.Count()).Scan(l.ctx, &LabelsCount)
  166. l.Errorf("save hour data error:%v \n", err)
  167. LabelsCountSet[orgID] = LabelsCount
  168. _, err = l.svcCtx.DB.UsageStatisticHour.Create().
  169. SetType(1).
  170. SetOrganizationID(orgID).
  171. SetAiResponse(uint64(orgAiResponseInt)).
  172. SetSopRun(uint64(orgSopRunInt)).
  173. SetTotalFriend(uint64(orgFriendCountInt)).
  174. SetTotalGroup(uint64(orgGroupCountInt)).
  175. SetAccountBalance(uint64(orgAccountBalanceInt)).
  176. SetConsumeToken(uint64(orgConsumeTokenInt)).
  177. SetActiveUser(uint64(orgActiveUserInt)).
  178. SetNewUser(int64(orgNewUserInt)).
  179. SetAddtime(uint64(currentHourInt)).
  180. SetNotNilLabelDist(LabelsCount).
  181. Save(l.ctx)
  182. l.Errorf("save hour data error:%v \n", err)
  183. }
  184. // 先判断该租户是否已经统计了小时数据,如果已经统计了,就不需要再统计了
  185. hourDataCount, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
  186. usagestatistichour.Type(1),
  187. usagestatistichour.OrganizationID(0),
  188. usagestatistichour.BotID(""),
  189. usagestatistichour.Addtime(uint64(currentHourInt)),
  190. ).Count(l.ctx)
  191. if hourDataCount == 0 {
  192. _, err = l.svcCtx.DB.UsageStatisticHour.Create().
  193. SetType(1).
  194. SetOrganizationID(0).
  195. SetAiResponse(uint64(allHourAiResponseInt)).
  196. SetSopRun(uint64(allHourSopRunInt)).
  197. SetTotalFriend(uint64(allHourFriendCountInt)).
  198. SetTotalGroup(uint64(allHourGroupCountInt)).
  199. SetAccountBalance(uint64(allHourAccountBalanceInt)).
  200. SetConsumeToken(uint64(allHourConsumeTokenInt)).
  201. SetActiveUser(uint64(allHourActiveUserInt)).
  202. SetNewUser(int64(allHourNewUserInt)).
  203. SetAddtime(uint64(currentHourInt)).
  204. SetLabelDist(lc).
  205. Save(l.ctx)
  206. l.Errorf("save hour data error:%v \n", err)
  207. }
  208. /*
  209. 计算日数据
  210. ----------------------------------------------------------------------------------------------------------
  211. */
  212. dayStr := time.Now().Format("20060102")
  213. day, _ := strconv.Atoi(dayStr)
  214. // 获取昨天的第一小时
  215. yesterday := now.AddDate(0, 0, -1)
  216. yesterdayFirstHour := time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, now.Location())
  217. yesterdayInt, _ := strconv.Atoi(yesterdayFirstHour.Format("20060102"))
  218. yesterdayFirstHourInt, _ := strconv.Atoi(yesterdayFirstHour.Format("2006010215"))
  219. // 获取昨天的最后一小时
  220. yesterdayLastHour := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  221. yesterdayLastHourInt, _ := strconv.Atoi(yesterdayLastHour.Format("2006010215"))
  222. var allDayAiResponseInt, allDaySopRunInt, allDayFriendCountInt, allDayGroupCountInt, allDayAccountBalanceInt, allDayConsumeTokenInt, allDayActiveUserInt uint64
  223. var allDayNewUserInt int64
  224. for orgID, wxinfos := range wxbotsSet {
  225. var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt uint64
  226. var orgNewUserInt int64
  227. for _, wxinfo := range wxinfos {
  228. l.Logger.Infof("开始计算日数据:%d\n", yesterdayInt)
  229. hourDataBatch, _ := l.svcCtx.DB.UsageStatisticHour.Query().Where(
  230. usagestatistichour.Type(1),
  231. usagestatistichour.BotID(wxinfo.Wxid),
  232. usagestatistichour.AddtimeGTE(uint64(yesterdayFirstHourInt)),
  233. usagestatistichour.AddtimeLT(uint64(yesterdayLastHourInt)),
  234. ).All(l.ctx)
  235. if hourDataBatch == nil {
  236. continue
  237. }
  238. var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64
  239. var newUser int64
  240. for _, hourData := range hourDataBatch {
  241. aiResponse += hourData.AiResponse
  242. sopRun += hourData.SopRun
  243. totalFriend += hourData.TotalFriend
  244. totalGroup += hourData.TotalGroup
  245. accountBalance += hourData.AccountBalance
  246. consumeToken += hourData.ConsumeToken
  247. activeUser += hourData.ActiveUser
  248. newUser += hourData.NewUser
  249. }
  250. orgAiResponseInt += aiResponse
  251. orgSopRunInt += sopRun
  252. orgFriendCountInt += totalFriend
  253. orgGroupCountInt += totalGroup
  254. orgAccountBalanceInt += accountBalance
  255. orgConsumeTokenInt += consumeToken
  256. orgActiveUserInt += activeUser
  257. orgNewUserInt += newUser
  258. allDayAiResponseInt += aiResponse
  259. allDaySopRunInt += sopRun
  260. allDayFriendCountInt += totalFriend
  261. allDayGroupCountInt += totalGroup
  262. allDayAccountBalanceInt += accountBalance
  263. allDayConsumeTokenInt += consumeToken
  264. allDayActiveUserInt += activeUser
  265. allDayNewUserInt += newUser
  266. // 先判断该账号是否已经统计了日数据,如果已经统计了,就不需要再统计了
  267. dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  268. usagestatisticday.Type(1),
  269. usagestatisticday.BotID(wxinfo.Wxid),
  270. usagestatisticday.Addtime(uint64(yesterdayInt)),
  271. ).Count(l.ctx)
  272. // 如果添加过了就略过
  273. if dayDataCount == 0 && yesterdayLastHourInt <= currentHourInt {
  274. _, err := l.svcCtx.DB.UsageStatisticDay.Create().
  275. SetAddtime(uint64(yesterdayInt)).
  276. SetType(1).
  277. SetBotID(wxinfo.Wxid).
  278. SetOrganizationID(wxinfo.OrganizationID).
  279. SetAiResponse(aiResponse).
  280. SetSopRun(sopRun).
  281. SetTotalFriend(totalFriend).
  282. SetTotalGroup(totalGroup).
  283. SetAccountBalance(accountBalance).
  284. SetConsumeToken(consumeToken).
  285. SetActiveUser(activeUser).
  286. SetNewUser(newUser).
  287. SetLabelDist(lc).
  288. Save(l.ctx)
  289. if err != nil {
  290. l.Errorf("create day data error:%v \n", err)
  291. continue
  292. }
  293. }
  294. }
  295. // 先判断该租户是否已经统计了日数据,如果已经统计了,就不需要再统计了
  296. dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  297. usagestatisticday.Type(1),
  298. usagestatisticday.OrganizationID(orgID),
  299. usagestatisticday.BotID(""),
  300. usagestatisticday.Addtime(uint64(yesterdayInt)),
  301. ).Count(l.ctx)
  302. // 如果添加过了就略过
  303. if dayDataCount == 0 && yesterdayLastHourInt <= currentHourInt {
  304. _, err := l.svcCtx.DB.UsageStatisticDay.Create().
  305. SetAddtime(uint64(yesterdayInt)).
  306. SetType(1).
  307. SetOrganizationID(orgID).
  308. SetAiResponse(orgAiResponseInt).
  309. SetSopRun(orgSopRunInt).
  310. SetTotalFriend(orgFriendCountInt).
  311. SetTotalGroup(orgGroupCountInt).
  312. SetAccountBalance(orgAccountBalanceInt).
  313. SetConsumeToken(orgConsumeTokenInt).
  314. SetActiveUser(orgActiveUserInt).
  315. SetNewUser(orgNewUserInt).
  316. SetNotNilLabelDist(LabelsCountSet[orgID]).
  317. Save(l.ctx)
  318. if err != nil {
  319. l.Errorf("create day data error:%v \n", err)
  320. continue
  321. }
  322. }
  323. }
  324. // 先判断该租户是否已经统计了日数据,如果已经统计了,就不需要再统计了
  325. dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  326. usagestatisticday.Type(1),
  327. usagestatisticday.OrganizationID(0),
  328. usagestatisticday.BotID(""),
  329. usagestatisticday.Addtime(uint64(yesterdayInt)),
  330. ).Count(l.ctx)
  331. // 如果添加过了就略过
  332. if dayDataCount == 0 && yesterdayLastHourInt <= currentHourInt {
  333. _, err = l.svcCtx.DB.UsageStatisticDay.Create().
  334. SetAddtime(uint64(yesterdayInt)).
  335. SetType(1).
  336. SetOrganizationID(0).
  337. SetAiResponse(allDayAiResponseInt).
  338. SetSopRun(allDaySopRunInt).
  339. SetTotalFriend(allDayFriendCountInt).
  340. SetTotalGroup(allDayGroupCountInt).
  341. SetAccountBalance(allDayAccountBalanceInt).
  342. SetConsumeToken(allDayConsumeTokenInt).
  343. SetActiveUser(allDayActiveUserInt).
  344. SetNewUser(allDayNewUserInt).
  345. SetLabelDist(lc).
  346. Save(l.ctx)
  347. if err != nil {
  348. l.Errorf("create day data error:%v \n", err)
  349. }
  350. }
  351. /*
  352. 查看月表数据是否已经完成
  353. 1. 查询出上月里所有 usagedetail 内容
  354. 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
  355. ----------------------------------------------------------------------------------------------------------
  356. */
  357. //monthStr := time.Now().Format("200601")
  358. //month, _ := strconv.Atoi(monthStr)
  359. //
  360. //var allMonthAiResponseInt, allMonthSopRunInt, allMonthFriendCountInt, allMonthGroupCountInt, allMonthAccountBalanceInt, allMonthConsumeTokenInt, allMonthActiveUserInt uint64
  361. //var allMonthNewUserInt int64
  362. //for orgID, wxinfos := range wxbotsSet {
  363. // var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt uint64
  364. // var orgNewUserInt int64
  365. // for _, wxinfo := range wxinfos {
  366. // l.Logger.Infof("开始计算月数据:%d\n", month)
  367. //
  368. // // 获取上月的第一天
  369. // monthFirstDay := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, now.Location())
  370. // monthFirstDayInt, _ := strconv.Atoi(monthFirstDay.Format("20060102"))
  371. //
  372. // // 获取上月的最后一天
  373. // monthLastDay := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  374. // monthLastDayInt, _ := strconv.Atoi(monthLastDay.Format("20060102"))
  375. //
  376. // // 先判断该账号是否已经统计了月数据,如果已经统计了,就不需要再统计了
  377. // monthDataCount, _ := l.svcCtx.DB.UsageStatisticMonth.Query().Where(
  378. // usagestatisticmonth.Type(1),
  379. // usagestatisticmonth.BotID(wxinfo.Wxid),
  380. // usagestatisticmonth.Addtime(uint64(month)),
  381. // ).Count(l.ctx)
  382. //
  383. // // 如果添加过了就略过
  384. // if monthDataCount > 0 {
  385. // continue
  386. // }
  387. //
  388. // dayDataBatch, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  389. // usagestatisticday.Type(1),
  390. // usagestatisticday.BotID(wxinfo.Wxid),
  391. // usagestatisticday.AddtimeGTE(uint64(monthFirstDayInt)),
  392. // usagestatisticday.AddtimeLT(uint64(monthLastDayInt)),
  393. // ).All(l.ctx)
  394. //
  395. // var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64
  396. // var newUser int64
  397. // for _, dayData := range dayDataBatch {
  398. // aiResponse += dayData.AiResponse
  399. // sopRun += dayData.SopRun
  400. // totalFriend += dayData.TotalFriend
  401. // totalGroup += dayData.TotalGroup
  402. // accountBalance += dayData.AccountBalance
  403. // consumeToken += dayData.ConsumeToken
  404. // activeUser += dayData.ActiveUser
  405. // newUser += dayData.NewUser
  406. // }
  407. // orgAiResponseInt += aiResponse
  408. // orgSopRunInt += sopRun
  409. // orgFriendCountInt += totalFriend
  410. // orgGroupCountInt += totalGroup
  411. // orgAccountBalanceInt += accountBalance
  412. // orgConsumeTokenInt += consumeToken
  413. // orgActiveUserInt += activeUser
  414. // orgNewUserInt += newUser
  415. //
  416. // allMonthAiResponseInt += aiResponse
  417. // allMonthSopRunInt += sopRun
  418. // allMonthFriendCountInt += totalFriend
  419. // allMonthGroupCountInt += totalGroup
  420. // allMonthAccountBalanceInt += accountBalance
  421. // allMonthConsumeTokenInt += consumeToken
  422. // allMonthActiveUserInt += activeUser
  423. // allMonthNewUserInt += newUser
  424. //
  425. // _, err := l.svcCtx.DB.UsageStatisticMonth.Create().
  426. // SetAddtime(uint64(month)).
  427. // SetType(1).
  428. // SetBotID(wxinfo.Wxid).
  429. // SetOrganizationID(wxinfo.OrganizationID).
  430. // SetAiResponse(aiResponse).
  431. // SetSopRun(sopRun).
  432. // SetTotalFriend(totalFriend).
  433. // SetTotalGroup(totalGroup).
  434. // SetAccountBalance(accountBalance).
  435. // SetConsumeToken(consumeToken).
  436. // SetActiveUser(activeUser).
  437. // SetNewUser(newUser).
  438. // SetLabelDist(lc).
  439. // Save(l.ctx)
  440. // if err != nil {
  441. // l.Errorf("create month data error:%v \n", err)
  442. // continue
  443. // }
  444. // }
  445. // // 先判断该租户是否已经统计了月数据,如果已经统计了,就不需要再统计了
  446. // monthDataCount, _ := l.svcCtx.DB.UsageStatisticMonth.Query().Where(
  447. // usagestatisticmonth.Type(1),
  448. // usagestatisticmonth.OrganizationID(orgID),
  449. // usagestatisticmonth.BotIDIsNil(),
  450. // usagestatisticmonth.Addtime(uint64(month)),
  451. // ).Count(l.ctx)
  452. //
  453. // // 如果添加过了就略过
  454. // if monthDataCount > 0 {
  455. // continue
  456. // }
  457. //
  458. // _, err := l.svcCtx.DB.UsageStatisticMonth.Create().
  459. // SetAddtime(uint64(month)).
  460. // SetType(1).
  461. // SetOrganizationID(orgID).
  462. // SetAiResponse(orgAiResponseInt).
  463. // SetSopRun(orgSopRunInt).
  464. // SetTotalFriend(orgFriendCountInt).
  465. // SetTotalGroup(orgGroupCountInt).
  466. // SetAccountBalance(orgAccountBalanceInt).
  467. // SetConsumeToken(orgConsumeTokenInt).
  468. // SetActiveUser(orgActiveUserInt).
  469. // SetNewUser(orgNewUserInt).
  470. // SetNotNilLabelDist(LabelsCountSet[orgID]).
  471. // Save(l.ctx)
  472. // if err != nil {
  473. // l.Errorf("create month data error:%v \n", err)
  474. // continue
  475. // }
  476. //}
  477. //
  478. //_, err = l.svcCtx.DB.UsageStatisticMonth.Create().
  479. // SetAddtime(uint64(month)).
  480. // SetType(1).
  481. // SetOrganizationID(0).
  482. // SetAiResponse(allMonthAiResponseInt).
  483. // SetSopRun(allMonthSopRunInt).
  484. // SetTotalFriend(allMonthFriendCountInt).
  485. // SetTotalGroup(allMonthGroupCountInt).
  486. // SetAccountBalance(allMonthAccountBalanceInt).
  487. // SetConsumeToken(allMonthConsumeTokenInt).
  488. // SetActiveUser(allMonthActiveUserInt).
  489. // SetNewUser(allMonthNewUserInt).
  490. // SetNotNilLabelDist(lc).
  491. // Save(l.ctx)
  492. //if err != nil {
  493. // l.Errorf("create month data error:%v \n", err)
  494. //}
  495. finishTime := time.Now()
  496. l.Logger.Infof("This process cost %v", finishTime.Sub(startTime).String())
  497. return
  498. }