get_charts_logic.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package dashboard
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  6. "strconv"
  7. "time"
  8. "wechat-api/ent/custom_types"
  9. "wechat-api/ent/label"
  10. "wechat-api/ent/predicate"
  11. "wechat-api/ent/usagestatisticday"
  12. "wechat-api/ent/usagestatistichour"
  13. "wechat-api/internal/svc"
  14. "wechat-api/internal/types"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type GetChartsLogic struct {
  18. logx.Logger
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. }
  22. func NewGetChartsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetChartsLogic {
  23. return &GetChartsLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx}
  27. }
  28. type SumUint struct {
  29. Addtime uint64 `json:"addtime"`
  30. Value uint64 `json:"value"`
  31. }
  32. type SumInt struct {
  33. Addtime uint64 `json:"addtime"`
  34. Value int64 `json:"value"`
  35. }
  36. func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp, err error) {
  37. // 获取组织id
  38. var organizationId uint64 = 0
  39. isAdmin := l.ctx.Value("isAdmin").(bool)
  40. if isAdmin && req.OrganizationId != nil && *req.OrganizationId != 0 {
  41. if req.OrganizationId != nil && *req.OrganizationId != 0 {
  42. organizationId = *req.OrganizationId
  43. }
  44. } else {
  45. organizationId = l.ctx.Value("organizationId").(uint64)
  46. }
  47. // 解析起始和截止时间
  48. layouts := []string{
  49. "2006-01", // 对应 "2024-01"
  50. "2006-01-02", // 对应 "2024-01-01"
  51. }
  52. var layoutsType int
  53. var startTime time.Time
  54. for i, layout := range layouts {
  55. startTime, err = time.Parse(layout, *req.StartDate)
  56. layoutsType = i
  57. if err == nil {
  58. break
  59. }
  60. }
  61. if err != nil {
  62. fmt.Println("解析开始时间失败:", err)
  63. return
  64. }
  65. var endTime time.Time
  66. for _, layout := range layouts {
  67. endTime, err = time.Parse(layout, *req.EndDate)
  68. if err == nil {
  69. break
  70. }
  71. }
  72. if err != nil {
  73. fmt.Println("解析结束时间失败:", err)
  74. return
  75. }
  76. // 判断截止日期是否包含当前日
  77. var isCurrentDay bool
  78. now := time.Now()
  79. if layoutsType == 0 {
  80. isCurrentDay = endTime.Year() == now.Year() && endTime.Month() == now.Month()
  81. } else {
  82. isCurrentDay = endTime.Year() == now.Year() && endTime.Month() == now.Month() && endTime.Day() == now.Day()
  83. }
  84. // 定义变量
  85. aiResponse := types.ChartsUint{}
  86. sopRun := types.ChartsUint{}
  87. totalFriend := types.ChartsUint{}
  88. totalGroup := types.ChartsUint{}
  89. accountBalance := types.ChartsUint{}
  90. consumeToken := types.ChartsUint{}
  91. activeUser := types.ChartsUint{}
  92. newUser := types.ChartsInt{}
  93. var labelDists []custom_types.LabelDist
  94. if isCurrentDay && layoutsType == 1 && *req.StartDate == *req.EndDate {
  95. // 返回当日每小时的数据
  96. startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
  97. startAddTimeString := startOfDay.Format("2006010200")
  98. startAddTime, err := strconv.ParseUint(startAddTimeString, 10, 64)
  99. if err != nil {
  100. fmt.Println("转换开始时间失败:", err)
  101. return nil, err
  102. }
  103. var predicates []predicate.UsageStatisticHour
  104. predicates = append(predicates, usagestatistichour.OrganizationID(organizationId))
  105. predicates = append(predicates, usagestatistichour.BotID(""))
  106. predicates = append(predicates, usagestatistichour.AddtimeGTE(startAddTime))
  107. usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(predicates...).All(l.ctx)
  108. if err != nil {
  109. return nil, err
  110. }
  111. for _, hourData := range usageStatisticHour {
  112. addtimeLastTwoDigits := hourData.Addtime % 100
  113. aiResponse.Count += hourData.AiResponse
  114. aiResponse.Val = append(aiResponse.Val, hourData.AiResponse)
  115. aiResponse.Label = append(aiResponse.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
  116. sopRun.Count += hourData.SopRun
  117. sopRun.Val = append(sopRun.Val, hourData.SopRun)
  118. sopRun.Label = append(sopRun.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
  119. totalFriend.Count = hourData.TotalFriend
  120. totalFriend.Val = append(totalFriend.Val, hourData.TotalFriend)
  121. totalFriend.Label = append(totalFriend.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
  122. totalGroup.Count = hourData.TotalGroup
  123. totalGroup.Val = append(totalGroup.Val, hourData.TotalGroup)
  124. totalGroup.Label = append(totalGroup.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
  125. consumeToken.Count += hourData.ConsumeToken
  126. consumeToken.Val = append(consumeToken.Val, hourData.ConsumeToken)
  127. consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
  128. activeUser.Count = hourData.ActiveUser
  129. activeUser.Val = append(activeUser.Val, hourData.ActiveUser)
  130. activeUser.Label = append(activeUser.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
  131. newUser.Count += hourData.NewUser
  132. newUser.Val = append(newUser.Val, hourData.NewUser)
  133. newUser.Label = append(newUser.Label, fmt.Sprintf("%02d点", addtimeLastTwoDigits))
  134. }
  135. hourLen := len(usageStatisticHour)
  136. if hourLen > 0 {
  137. if usageStatisticHour[0].TotalFriend > 0 {
  138. totalFriend.Rate = float32((usageStatisticHour[hourLen-1].TotalFriend - usageStatisticHour[0].TotalFriend) / usageStatisticHour[0].TotalFriend)
  139. }
  140. if usageStatisticHour[0].TotalGroup > 0 {
  141. totalGroup.Rate = float32((usageStatisticHour[hourLen-1].TotalGroup - usageStatisticHour[0].TotalGroup) / usageStatisticHour[0].TotalGroup)
  142. }
  143. if usageStatisticHour[0].ActiveUser > 0 {
  144. activeUser.Rate = float32((usageStatisticHour[hourLen-1].ActiveUser - usageStatisticHour[0].ActiveUser) / usageStatisticHour[0].ActiveUser)
  145. }
  146. if usageStatisticHour[0].NewUser > 0 {
  147. newUser.Rate = float32((usageStatisticHour[hourLen-1].NewUser - usageStatisticHour[0].NewUser) / usageStatisticHour[0].NewUser)
  148. }
  149. labelDists = usageStatisticHour[hourLen-1].LabelDist
  150. }
  151. } else {
  152. // 从天表统计数据
  153. startAddTimeString := startTime.Format("20060102")
  154. startAddTime, err := strconv.ParseUint(startAddTimeString, 10, 64)
  155. if err != nil {
  156. fmt.Println("转换开始时间失败:", err)
  157. return nil, err
  158. }
  159. endAddTimeString := endTime.Format("20060102")
  160. endAddTime, err := strconv.ParseUint(endAddTimeString, 10, 64)
  161. if err != nil {
  162. fmt.Println("转换截止时间失败:", err)
  163. return nil, err
  164. }
  165. var predicatesDay []predicate.UsageStatisticDay
  166. predicatesDay = append(predicatesDay, usagestatisticday.OrganizationID(organizationId))
  167. predicatesDay = append(predicatesDay, usagestatisticday.BotID(""))
  168. predicatesDay = append(predicatesDay, usagestatisticday.AddtimeGTE(startAddTime))
  169. predicatesDay = append(predicatesDay, usagestatisticday.AddtimeLTE(endAddTime))
  170. usageStatisticDay, err := l.svcCtx.DB.UsageStatisticDay.Query().Where(predicatesDay...).All(l.ctx)
  171. if err != nil {
  172. return nil, err
  173. }
  174. for _, dayData := range usageStatisticDay {
  175. aiResponse.Count = aiResponse.Count + dayData.AiResponse
  176. aiResponse.Val = append(aiResponse.Val, dayData.AiResponse)
  177. aiResponse.Label = append(aiResponse.Label, fmt.Sprintf("%d", dayData.Addtime))
  178. sopRun.Count += dayData.SopRun
  179. sopRun.Val = append(sopRun.Val, dayData.SopRun)
  180. sopRun.Label = append(sopRun.Label, fmt.Sprintf("%d", dayData.Addtime))
  181. totalFriend.Count = dayData.TotalFriend
  182. totalFriend.Val = append(totalFriend.Val, dayData.TotalFriend)
  183. totalFriend.Label = append(totalFriend.Label, fmt.Sprintf("%d", dayData.Addtime))
  184. totalGroup.Count = dayData.TotalGroup
  185. totalGroup.Val = append(totalGroup.Val, dayData.TotalGroup)
  186. totalGroup.Label = append(totalGroup.Label, fmt.Sprintf("%d", dayData.Addtime))
  187. consumeToken.Count += dayData.ConsumeToken
  188. consumeToken.Val = append(consumeToken.Val, dayData.ConsumeToken)
  189. consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%d", dayData.Addtime))
  190. activeUser.Count = dayData.ActiveUser
  191. activeUser.Val = append(activeUser.Val, dayData.ActiveUser)
  192. activeUser.Label = append(activeUser.Label, fmt.Sprintf("%d", dayData.Addtime))
  193. newUser.Count += dayData.NewUser
  194. newUser.Val = append(newUser.Val, dayData.NewUser)
  195. newUser.Label = append(newUser.Label, fmt.Sprintf("%d", dayData.Addtime))
  196. }
  197. dayLen := len(usageStatisticDay)
  198. if dayLen > 0 {
  199. if usageStatisticDay[0].TotalFriend > 0 {
  200. totalFriend.Rate = float32((usageStatisticDay[dayLen-1].TotalFriend - usageStatisticDay[0].TotalFriend) / usageStatisticDay[0].TotalFriend)
  201. }
  202. if usageStatisticDay[0].TotalGroup > 0 {
  203. totalGroup.Rate = float32((usageStatisticDay[dayLen-1].TotalGroup - usageStatisticDay[0].TotalGroup) / usageStatisticDay[0].TotalGroup)
  204. }
  205. if usageStatisticDay[0].ActiveUser > 0 {
  206. activeUser.Rate = float32((usageStatisticDay[dayLen-1].ActiveUser - usageStatisticDay[0].ActiveUser) / usageStatisticDay[0].ActiveUser)
  207. }
  208. if usageStatisticDay[0].NewUser > 0 {
  209. newUser.Rate = float32((usageStatisticDay[dayLen-1].NewUser - usageStatisticDay[0].NewUser) / usageStatisticDay[0].NewUser)
  210. }
  211. labelDists = usageStatisticDay[dayLen-1].LabelDist
  212. }
  213. if isCurrentDay {
  214. // 从小时表统计当天数据
  215. startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
  216. startOfDayAddTimeString := startOfDay.Format("2006010200")
  217. startOfDayAddTime, err := strconv.ParseUint(startOfDayAddTimeString, 10, 64)
  218. if err != nil {
  219. fmt.Println("转换开始时间失败:", err)
  220. return nil, err
  221. }
  222. var predicates []predicate.UsageStatisticHour
  223. predicates = append(predicates, usagestatistichour.OrganizationID(organizationId))
  224. predicates = append(predicates, usagestatistichour.BotID(""))
  225. predicates = append(predicates, usagestatistichour.AddtimeGTE(startOfDayAddTime))
  226. usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(predicates...).All(l.ctx)
  227. if err != nil {
  228. return nil, err
  229. }
  230. hourLen := len(usageStatisticHour)
  231. if hourLen > 0 {
  232. var aiResponseOfDay uint64
  233. var sopRunOfDay uint64
  234. var totalFriendOfDay uint64
  235. var totalGroupOfDay uint64
  236. var consumeTokenOfDay uint64
  237. var activeUserOfDay uint64
  238. var newUserOfDay int64
  239. for _, hourData := range usageStatisticHour {
  240. aiResponseOfDay += hourData.AiResponse
  241. sopRunOfDay += hourData.SopRun
  242. consumeTokenOfDay += hourData.ConsumeToken
  243. }
  244. totalFriendOfDay = usageStatisticHour[hourLen-1].TotalFriend
  245. totalGroupOfDay = usageStatisticHour[hourLen-1].TotalGroup
  246. activeUserOfDay = usageStatisticHour[hourLen-1].ActiveUser
  247. newUserOfDay = usageStatisticHour[hourLen-1].NewUser
  248. aiResponse.Count = aiResponse.Count + aiResponseOfDay
  249. aiResponse.Val = append(aiResponse.Val, aiResponseOfDay)
  250. aiResponse.Label = append(aiResponse.Label, "今日")
  251. sopRun.Count = sopRun.Count + sopRunOfDay
  252. sopRun.Val = append(sopRun.Val, sopRunOfDay)
  253. sopRun.Label = append(sopRun.Label, "今日")
  254. totalFriend.Count = totalFriendOfDay
  255. totalFriend.Val = append(totalFriend.Val, totalFriendOfDay)
  256. totalFriend.Label = append(totalFriend.Label, "今日")
  257. tfLen := len(totalFriend.Val)
  258. if tfLen > 0 && totalFriend.Val[0] > 0 {
  259. totalFriend.Rate = float32((totalFriend.Val[tfLen-1] - totalFriend.Val[0]) / totalFriend.Val[0])
  260. }
  261. totalGroup.Count = totalGroupOfDay
  262. totalGroup.Val = append(totalGroup.Val, totalGroupOfDay)
  263. totalGroup.Label = append(totalGroup.Label, "今日")
  264. tgLen := len(totalGroup.Val)
  265. if tgLen > 0 && totalGroup.Val[0] > 0 {
  266. totalGroup.Rate = float32((totalGroup.Val[tgLen-1] - totalGroup.Val[0]) / totalGroup.Val[0])
  267. }
  268. consumeToken.Count = consumeToken.Count + consumeTokenOfDay
  269. consumeToken.Val = append(consumeToken.Val, consumeTokenOfDay)
  270. consumeToken.Label = append(consumeToken.Label, "今日")
  271. activeUser.Count = activeUserOfDay
  272. activeUser.Val = append(activeUser.Val, activeUserOfDay)
  273. activeUser.Label = append(activeUser.Label, "今日")
  274. auLen := len(activeUser.Val)
  275. if auLen > 0 && activeUser.Val[0] > 0 {
  276. activeUser.Rate = float32((activeUser.Val[auLen-1] - activeUser.Val[0]) / activeUser.Val[0])
  277. }
  278. newUser.Count = newUserOfDay
  279. newUser.Val = append(newUser.Val, newUserOfDay)
  280. newUser.Label = append(newUser.Label, "今日")
  281. nuLen := len(newUser.Val)
  282. if nuLen > 0 && newUser.Val[0] > 0 {
  283. newUser.Rate = float32((newUser.Val[nuLen-1] - newUser.Val[0]) / newUser.Val[0])
  284. }
  285. labelDists = usageStatisticHour[hourLen-1].LabelDist
  286. }
  287. }
  288. }
  289. var labelIds []uint64
  290. var labelsData []types.LabelsData
  291. labelSet := make(map[uint64]uint64)
  292. for _, labelDist := range labelDists {
  293. if labelDist.Count != 0 {
  294. labelIds = append(labelIds, labelDist.LabelID)
  295. labelSet[labelDist.LabelID] = labelDist.Count
  296. }
  297. }
  298. labelInfos, err := l.svcCtx.DB.Label.Query().Where(label.IDIn(labelIds...)).All(l.ctx)
  299. for _, labelInfo := range labelInfos {
  300. labelsData = append(labelsData, types.LabelsData{
  301. Value: labelSet[labelInfo.ID],
  302. Name: labelInfo.Name,
  303. })
  304. }
  305. chartsData := types.ChartsData{
  306. AiResponse: &aiResponse,
  307. SopRun: &sopRun,
  308. TotalFriend: &totalFriend,
  309. TotalGroup: &totalGroup,
  310. AccountBalance: &accountBalance,
  311. ConsumeToken: &consumeToken,
  312. ActiveUser: &activeUser,
  313. NewUser: &newUser,
  314. LabelDist: labelsData,
  315. }
  316. return &types.ChartsResp{BaseDataInfo: types.BaseDataInfo{Msg: errormsg.UpdateSuccess}, Data: &chartsData}, nil
  317. }