gameAction.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package v1
  2. import (
  3. "designs/app/common/request"
  4. "designs/app/common/response"
  5. "designs/global"
  6. "designs/model"
  7. "designs/utils"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "github.com/pkg/errors"
  11. )
  12. func UserActionList(c *gin.Context) {
  13. form := request.Check(c, &struct {
  14. Gid string `form:"gid" binding:"required"`
  15. Pf string `form:"pf" binding:""`
  16. StartTime string `form:"startTime" binding:"required"`
  17. EndTime string `form:"endTime" binding:"required"`
  18. }{})
  19. //查询出所有的事件
  20. var actionList []model.GameAction
  21. err := global.App.DB.Table("game_action").
  22. Where("gid", form.Gid).
  23. Select("id", "actionId", "actionName").
  24. Order("id desc").Scan(&actionList).Error
  25. if err != nil {
  26. response.Fail(c, 1003, err.Error())
  27. return
  28. }
  29. //查询出时间段内的活跃用户,登录次数
  30. var userLogin []model.UserLogin
  31. err = global.App.DB.Table("user_login").
  32. Where("gid", form.Gid).
  33. Where("loginTime", ">=", form.StartTime).
  34. Where("loginTime", "<=", form.EndTime).
  35. Select("userId", "loginTime").
  36. Scan(&userLogin).Error
  37. if err != nil {
  38. response.Fail(c, 1001, err.Error())
  39. return
  40. }
  41. userLoginCount := len(userLogin)
  42. //查询出时间段内事件触发数量,以及触发人的ID
  43. var userAction []model.UserAction
  44. err = global.App.DB.Table("user_action").
  45. Where("gid", form.Gid).
  46. Where("createdAt", ">=", form.StartTime).
  47. Where("createdAt", "<=", form.EndTime).
  48. Select("id", "actionId", "userId", "createdAt").
  49. Scan(&userAction).Error
  50. if err != nil {
  51. response.Fail(c, 1002, err.Error())
  52. return
  53. }
  54. //计算事件的触发总数和触发用户数
  55. actionSumMap := make(map[string]int)
  56. actionUserSumMap := make(map[string][]int)
  57. for _, action := range userAction {
  58. actionSumMap[action.ActionId]++
  59. actionUserSumMap[action.ActionId] = append(actionUserSumMap[action.ActionId], action.UserId)
  60. }
  61. //根据事件触发和活跃用户数量进行比对得出其他数据
  62. activeUser := make(map[int]bool)
  63. var activeUserSlice []int
  64. for _, users := range userLogin {
  65. activeUser[users.UserId] = true
  66. }
  67. for k := range activeUser {
  68. activeUserSlice = append(activeUserSlice, k)
  69. }
  70. type responses struct {
  71. ActionId string `json:"actionId"`
  72. ActionName string `json:"actionName"`
  73. ActionCount int `json:"actionCount"`
  74. ActionUserCount int `json:"actionUserCount"`
  75. ActiveUserRate float64 `json:"activeUserRate"`
  76. LoginActiveRate float64 `json:"loginActiveRate"`
  77. }
  78. var res []responses
  79. for _, v := range actionList {
  80. var ActiveUserRate float64
  81. var LoginActiveRate float64
  82. if userLoginCount > 0 {
  83. ActiveUserRate = float64(actionSumMap[v.ActionId] / userLoginCount)
  84. LoginActiveRate = float64(len(actionUserSumMap[v.ActionId]) / userLoginCount)
  85. }
  86. res = append(res, responses{
  87. ActionId: v.ActionId,
  88. ActionName: v.ActionName,
  89. ActionCount: actionSumMap[v.ActionId],
  90. ActionUserCount: len(actionUserSumMap[v.ActionId]),
  91. ActiveUserRate: ActiveUserRate,
  92. LoginActiveRate: LoginActiveRate,
  93. })
  94. }
  95. response.Success(c, gin.H{
  96. "data": res,
  97. "count": len(res),
  98. })
  99. }
  100. func UserActionDetail(c *gin.Context) {
  101. form := request.Check(c, &struct {
  102. Id int `form:"id" binding:"required"`
  103. StartTime string `form:"startTime" binding:"required"`
  104. EndTime string `form:"endTime" binding:"required"`
  105. }{})
  106. //查询启动次数
  107. var action model.GameAction
  108. err := global.App.DB.Table("game_action").
  109. Where("id", form.Id).Find(&action).Error
  110. if err != nil {
  111. response.Fail(c, 1001, err.Error())
  112. return
  113. }
  114. var userAction []struct {
  115. UserId int `json:"userId" gorm:"not null;column:userId;"`
  116. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  117. }
  118. err = global.App.DB.Table("user_action").
  119. Where("gid", action.Gid).
  120. Where("actionId", action.ActionId).
  121. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  122. Scan(&userAction).Error
  123. if err != nil {
  124. response.Fail(c, 1002, err.Error())
  125. return
  126. }
  127. //查询出时间段内的活跃用户,登录次数
  128. var userLogin []struct {
  129. UserId int `json:"userId" gorm:"not null;column:userId;"`
  130. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  131. }
  132. err = global.App.DB.Table("user_login").
  133. Where("gid", action.Gid).
  134. Where("loginTime", ">=", form.StartTime).
  135. Where("loginTime", "<=", form.EndTime).
  136. Select("userId", "DATE_FORMAT(loginTime, '%Y-%m-%d') as createdAt").
  137. Scan(&userLogin).Error
  138. if err != nil {
  139. response.Fail(c, 1001, err.Error())
  140. return
  141. }
  142. //userLoginCount := len(userLogin)
  143. activeCount := make(map[string]int)
  144. activeCountUser := make(map[string]map[int]bool)
  145. //根据日期进行分组
  146. for _, v := range userAction {
  147. activeCount[v.CreatedAt]++
  148. if activeCountUser[v.CreatedAt] == nil {
  149. activeCountUser[v.CreatedAt] = make(map[int]bool)
  150. }
  151. activeCountUser[v.CreatedAt][v.UserId] = true
  152. }
  153. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  154. type responses struct {
  155. Date string `json:"date"`
  156. ActiveCount int `json:"activeCount"`
  157. ActiveUserCount int `json:"activeUserCount"`
  158. ActiveCountRate float64 `json:"activeCountRate"`
  159. ActiveCountUser float64 `json:"activeCountUser"`
  160. }
  161. var res []responses
  162. //输出格式
  163. for _, v := range days {
  164. res = append(res, responses{
  165. Date: v,
  166. ActiveCount: activeCount[v],
  167. ActiveUserCount: len(activeCountUser[v]),
  168. ActiveCountRate: float64(activeCount[v] / 1),
  169. ActiveCountUser: float64(len(activeCountUser[v]) / 1),
  170. })
  171. }
  172. response.Success(c, gin.H{
  173. "data": res,
  174. })
  175. }
  176. func UserActionDetailDistribution(c *gin.Context) {
  177. form := request.Check(c, &struct {
  178. Id int `form:"id" binding:"required"`
  179. StartTime string `form:"startTime" binding:"required"`
  180. EndTime string `form:"endTime" binding:"required"`
  181. Type int `form:"type" binding:"required"`
  182. }{})
  183. var action model.GameAction
  184. err := global.App.DB.Table("game_action").
  185. Where("id", form.Id).Find(&action).Error
  186. if err != nil {
  187. response.Fail(c, 1001, err.Error())
  188. return
  189. }
  190. res := make(map[string]interface{})
  191. if form.Type == 1 {
  192. var userAction []struct {
  193. UserId int `json:"userId" gorm:"not null;column:userId;"`
  194. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  195. }
  196. err = global.App.DB.Table("user_action").
  197. Where("gid", action.Gid).
  198. Where("actionId", action.ActionId).
  199. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  200. Scan(&userAction).Error
  201. if err != nil {
  202. response.Fail(c, 1002, err.Error())
  203. return
  204. }
  205. activeCount := make(map[string]int)
  206. for _, v := range userAction {
  207. activeCount[v.CreatedAt]++
  208. }
  209. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  210. for _, v := range days {
  211. res[v] = activeCount[v]
  212. }
  213. fmt.Print(len(userAction) / len(days))
  214. response.Success(c, gin.H{"data": res, "avg": fmt.Sprintf("%.2f", float64(len(userAction))/float64(len(days)))})
  215. } else if form.Type == 2 {
  216. var userAction []struct {
  217. UserId int `json:"userId" gorm:"not null;column:userId;"`
  218. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  219. }
  220. err = global.App.DB.Table("user_action").
  221. Where("gid", action.Gid).
  222. Where("actionId", action.ActionId).
  223. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  224. Scan(&userAction).Error
  225. if err != nil {
  226. response.Fail(c, 1002, err.Error())
  227. return
  228. }
  229. activeCount := make(map[string]map[int]bool)
  230. for _, v := range userAction {
  231. if activeCount[v.CreatedAt] == nil {
  232. activeCount[v.CreatedAt] = make(map[int]bool)
  233. }
  234. activeCount[v.CreatedAt][v.UserId] = true
  235. }
  236. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  237. for _, v := range days {
  238. res[v] = len(activeCount[v])
  239. }
  240. response.Success(c, gin.H{"data": res, "avg": fmt.Sprintf("%.2f", float64(len(userAction))/float64(len(days)))})
  241. } else if form.Type == 3 {
  242. var userAction []struct {
  243. UserId int `json:"userId" gorm:"not null;column:userId;"`
  244. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  245. }
  246. err = global.App.DB.Table("user_action").
  247. Where("gid", action.Gid).
  248. Where("actionId", action.ActionId).
  249. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  250. Scan(&userAction).Error
  251. if err != nil {
  252. response.Fail(c, 1002, err.Error())
  253. return
  254. }
  255. activeCount := make(map[string]map[int]bool)
  256. for _, v := range userAction {
  257. if activeCount[v.CreatedAt] == nil {
  258. activeCount[v.CreatedAt] = make(map[int]bool)
  259. }
  260. activeCount[v.CreatedAt][v.UserId] = true
  261. }
  262. //查询活跃用户
  263. } else if form.Type == 4 {
  264. } else {
  265. response.Fail(c, 1003, errors.New("type 错误"))
  266. return
  267. }
  268. }