compute_statistic.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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(), now.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 {
  274. _, err := l.svcCtx.DB.UsageStatisticDay.Update().
  275. Where(
  276. usagestatisticday.Type(1),
  277. usagestatisticday.BotID(wxinfo.Wxid),
  278. usagestatisticday.Addtime(uint64(yesterdayInt)),
  279. ).
  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. } else {
  295. _, err := l.svcCtx.DB.UsageStatisticDay.Create().
  296. SetAddtime(uint64(yesterdayInt)).
  297. SetType(1).
  298. SetBotID(wxinfo.Wxid).
  299. SetOrganizationID(wxinfo.OrganizationID).
  300. SetAiResponse(aiResponse).
  301. SetSopRun(sopRun).
  302. SetTotalFriend(totalFriend).
  303. SetTotalGroup(totalGroup).
  304. SetAccountBalance(accountBalance).
  305. SetConsumeToken(consumeToken).
  306. SetActiveUser(activeUser).
  307. SetNewUser(newUser).
  308. SetLabelDist(lc).
  309. Save(l.ctx)
  310. if err != nil {
  311. l.Errorf("create day data error:%v \n", err)
  312. continue
  313. }
  314. }
  315. }
  316. // 先判断该租户是否已经统计了日数据,如果已经统计了,就不需要再统计了
  317. dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  318. usagestatisticday.Type(1),
  319. usagestatisticday.OrganizationID(orgID),
  320. usagestatisticday.BotID(""),
  321. usagestatisticday.Addtime(uint64(yesterdayInt)),
  322. ).Count(l.ctx)
  323. // 如果添加过了就略过
  324. if dayDataCount > 0 {
  325. _, err := l.svcCtx.DB.UsageStatisticDay.Update().
  326. Where(
  327. usagestatisticday.Type(1),
  328. usagestatisticday.OrganizationID(orgID),
  329. usagestatisticday.BotID(""),
  330. usagestatisticday.Addtime(uint64(yesterdayInt)),
  331. ).
  332. SetAiResponse(orgAiResponseInt).
  333. SetSopRun(orgSopRunInt).
  334. SetTotalFriend(orgFriendCountInt).
  335. SetTotalGroup(orgGroupCountInt).
  336. SetAccountBalance(orgAccountBalanceInt).
  337. SetConsumeToken(orgConsumeTokenInt).
  338. SetActiveUser(orgActiveUserInt).
  339. SetNewUser(orgNewUserInt).
  340. SetNotNilLabelDist(LabelsCountSet[orgID]).
  341. Save(l.ctx)
  342. if err != nil {
  343. l.Errorf("create day data error:%v \n", err)
  344. continue
  345. }
  346. } else {
  347. _, err := l.svcCtx.DB.UsageStatisticDay.Create().
  348. SetAddtime(uint64(yesterdayInt)).
  349. SetType(1).
  350. SetOrganizationID(orgID).
  351. SetAiResponse(orgAiResponseInt).
  352. SetSopRun(orgSopRunInt).
  353. SetTotalFriend(orgFriendCountInt).
  354. SetTotalGroup(orgGroupCountInt).
  355. SetAccountBalance(orgAccountBalanceInt).
  356. SetConsumeToken(orgConsumeTokenInt).
  357. SetActiveUser(orgActiveUserInt).
  358. SetNewUser(orgNewUserInt).
  359. SetNotNilLabelDist(LabelsCountSet[orgID]).
  360. Save(l.ctx)
  361. if err != nil {
  362. l.Errorf("create day data error:%v \n", err)
  363. continue
  364. }
  365. }
  366. }
  367. // 先判断该租户是否已经统计了日数据,如果已经统计了,就不需要再统计了
  368. dayDataCount, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  369. usagestatisticday.Type(1),
  370. usagestatisticday.OrganizationID(0),
  371. usagestatisticday.BotID(""),
  372. usagestatisticday.Addtime(uint64(yesterdayInt)),
  373. ).Count(l.ctx)
  374. // 如果添加过了就略过
  375. if dayDataCount > 0 {
  376. _, err = l.svcCtx.DB.UsageStatisticDay.Update().
  377. Where(
  378. usagestatisticday.Type(1),
  379. usagestatisticday.OrganizationID(0),
  380. usagestatisticday.BotID(""),
  381. usagestatisticday.Addtime(uint64(yesterdayInt)),
  382. ).
  383. SetAiResponse(allDayAiResponseInt).
  384. SetSopRun(allDaySopRunInt).
  385. SetTotalFriend(allDayFriendCountInt).
  386. SetTotalGroup(allDayGroupCountInt).
  387. SetAccountBalance(allDayAccountBalanceInt).
  388. SetConsumeToken(allDayConsumeTokenInt).
  389. SetActiveUser(allDayActiveUserInt).
  390. SetNewUser(allDayNewUserInt).
  391. SetLabelDist(lc).
  392. Save(l.ctx)
  393. if err != nil {
  394. l.Errorf("create day data error:%v \n", err)
  395. }
  396. } else {
  397. _, err = l.svcCtx.DB.UsageStatisticDay.Create().
  398. SetAddtime(uint64(day)).
  399. SetType(1).
  400. SetOrganizationID(0).
  401. SetAiResponse(allDayAiResponseInt).
  402. SetSopRun(allDaySopRunInt).
  403. SetTotalFriend(allDayFriendCountInt).
  404. SetTotalGroup(allDayGroupCountInt).
  405. SetAccountBalance(allDayAccountBalanceInt).
  406. SetConsumeToken(allDayConsumeTokenInt).
  407. SetActiveUser(allDayActiveUserInt).
  408. SetNewUser(allDayNewUserInt).
  409. SetLabelDist(lc).
  410. Save(l.ctx)
  411. if err != nil {
  412. l.Errorf("create day data error:%v \n", err)
  413. }
  414. }
  415. /*
  416. 查看月表数据是否已经完成
  417. 1. 查询出上月里所有 usagedetail 内容
  418. 2. 挨个遍历他的 bot_id ,再查询他的 bot_id 相关的参数
  419. ----------------------------------------------------------------------------------------------------------
  420. */
  421. //monthStr := time.Now().Format("200601")
  422. //month, _ := strconv.Atoi(monthStr)
  423. //
  424. //var allMonthAiResponseInt, allMonthSopRunInt, allMonthFriendCountInt, allMonthGroupCountInt, allMonthAccountBalanceInt, allMonthConsumeTokenInt, allMonthActiveUserInt uint64
  425. //var allMonthNewUserInt int64
  426. //for orgID, wxinfos := range wxbotsSet {
  427. // var orgAiResponseInt, orgSopRunInt, orgFriendCountInt, orgGroupCountInt, orgAccountBalanceInt, orgConsumeTokenInt, orgActiveUserInt uint64
  428. // var orgNewUserInt int64
  429. // for _, wxinfo := range wxinfos {
  430. // l.Logger.Infof("开始计算月数据:%d\n", month)
  431. //
  432. // // 获取上月的第一天
  433. // monthFirstDay := time.Date(now.Year(), now.Month()-1, 1, 0, 0, 0, 0, now.Location())
  434. // monthFirstDayInt, _ := strconv.Atoi(monthFirstDay.Format("20060102"))
  435. //
  436. // // 获取上月的最后一天
  437. // monthLastDay := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
  438. // monthLastDayInt, _ := strconv.Atoi(monthLastDay.Format("20060102"))
  439. //
  440. // // 先判断该账号是否已经统计了月数据,如果已经统计了,就不需要再统计了
  441. // monthDataCount, _ := l.svcCtx.DB.UsageStatisticMonth.Query().Where(
  442. // usagestatisticmonth.Type(1),
  443. // usagestatisticmonth.BotID(wxinfo.Wxid),
  444. // usagestatisticmonth.Addtime(uint64(month)),
  445. // ).Count(l.ctx)
  446. //
  447. // // 如果添加过了就略过
  448. // if monthDataCount > 0 {
  449. // continue
  450. // }
  451. //
  452. // dayDataBatch, _ := l.svcCtx.DB.UsageStatisticDay.Query().Where(
  453. // usagestatisticday.Type(1),
  454. // usagestatisticday.BotID(wxinfo.Wxid),
  455. // usagestatisticday.AddtimeGTE(uint64(monthFirstDayInt)),
  456. // usagestatisticday.AddtimeLT(uint64(monthLastDayInt)),
  457. // ).All(l.ctx)
  458. //
  459. // var aiResponse, sopRun, totalFriend, totalGroup, accountBalance, consumeToken, activeUser uint64
  460. // var newUser int64
  461. // for _, dayData := range dayDataBatch {
  462. // aiResponse += dayData.AiResponse
  463. // sopRun += dayData.SopRun
  464. // totalFriend += dayData.TotalFriend
  465. // totalGroup += dayData.TotalGroup
  466. // accountBalance += dayData.AccountBalance
  467. // consumeToken += dayData.ConsumeToken
  468. // activeUser += dayData.ActiveUser
  469. // newUser += dayData.NewUser
  470. // }
  471. // orgAiResponseInt += aiResponse
  472. // orgSopRunInt += sopRun
  473. // orgFriendCountInt += totalFriend
  474. // orgGroupCountInt += totalGroup
  475. // orgAccountBalanceInt += accountBalance
  476. // orgConsumeTokenInt += consumeToken
  477. // orgActiveUserInt += activeUser
  478. // orgNewUserInt += newUser
  479. //
  480. // allMonthAiResponseInt += aiResponse
  481. // allMonthSopRunInt += sopRun
  482. // allMonthFriendCountInt += totalFriend
  483. // allMonthGroupCountInt += totalGroup
  484. // allMonthAccountBalanceInt += accountBalance
  485. // allMonthConsumeTokenInt += consumeToken
  486. // allMonthActiveUserInt += activeUser
  487. // allMonthNewUserInt += newUser
  488. //
  489. // _, err := l.svcCtx.DB.UsageStatisticMonth.Create().
  490. // SetAddtime(uint64(month)).
  491. // SetType(1).
  492. // SetBotID(wxinfo.Wxid).
  493. // SetOrganizationID(wxinfo.OrganizationID).
  494. // SetAiResponse(aiResponse).
  495. // SetSopRun(sopRun).
  496. // SetTotalFriend(totalFriend).
  497. // SetTotalGroup(totalGroup).
  498. // SetAccountBalance(accountBalance).
  499. // SetConsumeToken(consumeToken).
  500. // SetActiveUser(activeUser).
  501. // SetNewUser(newUser).
  502. // SetLabelDist(lc).
  503. // Save(l.ctx)
  504. // if err != nil {
  505. // l.Errorf("create month data error:%v \n", err)
  506. // continue
  507. // }
  508. // }
  509. // // 先判断该租户是否已经统计了月数据,如果已经统计了,就不需要再统计了
  510. // monthDataCount, _ := l.svcCtx.DB.UsageStatisticMonth.Query().Where(
  511. // usagestatisticmonth.Type(1),
  512. // usagestatisticmonth.OrganizationID(orgID),
  513. // usagestatisticmonth.BotIDIsNil(),
  514. // usagestatisticmonth.Addtime(uint64(month)),
  515. // ).Count(l.ctx)
  516. //
  517. // // 如果添加过了就略过
  518. // if monthDataCount > 0 {
  519. // continue
  520. // }
  521. //
  522. // _, err := l.svcCtx.DB.UsageStatisticMonth.Create().
  523. // SetAddtime(uint64(month)).
  524. // SetType(1).
  525. // SetOrganizationID(orgID).
  526. // SetAiResponse(orgAiResponseInt).
  527. // SetSopRun(orgSopRunInt).
  528. // SetTotalFriend(orgFriendCountInt).
  529. // SetTotalGroup(orgGroupCountInt).
  530. // SetAccountBalance(orgAccountBalanceInt).
  531. // SetConsumeToken(orgConsumeTokenInt).
  532. // SetActiveUser(orgActiveUserInt).
  533. // SetNewUser(orgNewUserInt).
  534. // SetNotNilLabelDist(LabelsCountSet[orgID]).
  535. // Save(l.ctx)
  536. // if err != nil {
  537. // l.Errorf("create month data error:%v \n", err)
  538. // continue
  539. // }
  540. //}
  541. //
  542. //_, err = l.svcCtx.DB.UsageStatisticMonth.Create().
  543. // SetAddtime(uint64(month)).
  544. // SetType(1).
  545. // SetOrganizationID(0).
  546. // SetAiResponse(allMonthAiResponseInt).
  547. // SetSopRun(allMonthSopRunInt).
  548. // SetTotalFriend(allMonthFriendCountInt).
  549. // SetTotalGroup(allMonthGroupCountInt).
  550. // SetAccountBalance(allMonthAccountBalanceInt).
  551. // SetConsumeToken(allMonthConsumeTokenInt).
  552. // SetActiveUser(allMonthActiveUserInt).
  553. // SetNewUser(allMonthNewUserInt).
  554. // SetNotNilLabelDist(lc).
  555. // Save(l.ctx)
  556. //if err != nil {
  557. // l.Errorf("create month data error:%v \n", err)
  558. //}
  559. finishTime := time.Now()
  560. l.Logger.Infof("This process cost %v", finishTime.Sub(startTime).String())
  561. return
  562. }