gameAction.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. Id int `json:"id"`
  72. ActionId string `json:"actionId"`
  73. ActionName string `json:"actionName"`
  74. ActionCount int `json:"actionCount"`
  75. ActionUserCount int `json:"actionUserCount"`
  76. ActiveUserRate float64 `json:"activeUserRate"`
  77. LoginActiveRate float64 `json:"loginActiveRate"`
  78. }
  79. var res []responses
  80. for _, v := range actionList {
  81. var ActiveUserRate float64
  82. var LoginActiveRate float64
  83. if userLoginCount > 0 {
  84. ActiveUserRate = float64(actionSumMap[v.ActionId] / userLoginCount)
  85. LoginActiveRate = float64(len(actionUserSumMap[v.ActionId]) / userLoginCount)
  86. }
  87. res = append(res, responses{
  88. Id: v.ID,
  89. ActionId: v.ActionId,
  90. ActionName: v.ActionName,
  91. ActionCount: actionSumMap[v.ActionId],
  92. ActionUserCount: len(actionUserSumMap[v.ActionId]),
  93. ActiveUserRate: ActiveUserRate,
  94. LoginActiveRate: LoginActiveRate,
  95. })
  96. }
  97. response.Success(c, gin.H{
  98. "data": res,
  99. "count": len(res),
  100. })
  101. }
  102. func UserActionDetail(c *gin.Context) {
  103. form := request.Check(c, &struct {
  104. Id int `form:"id" binding:"required"`
  105. Pf []string `form:"pf" binding:""`
  106. StartTime string `form:"startTime" binding:"required"`
  107. EndTime string `form:"endTime" binding:"required"`
  108. }{})
  109. //查询启动次数
  110. var action model.GameAction
  111. err := global.App.DB.Table("game_action").
  112. Where("id", form.Id).Find(&action).Error
  113. if err != nil {
  114. response.Fail(c, 1001, err.Error())
  115. return
  116. }
  117. var userAction []struct {
  118. UserId int `json:"userId" gorm:"not null;column:userId;"`
  119. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  120. }
  121. err = global.App.DB.Table("user_action").
  122. Where("gid", action.Gid).
  123. Where("actionId", action.ActionId).
  124. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  125. Scan(&userAction).Error
  126. if err != nil {
  127. response.Fail(c, 1002, err.Error())
  128. return
  129. }
  130. //查询出时间段内的活跃用户,登录次数
  131. var userLogin []struct {
  132. UserId int `json:"userId" gorm:"not null;column:userId;"`
  133. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  134. }
  135. err = global.App.DB.Table("user_login").
  136. Where("gid", action.Gid).
  137. Where("loginTime", ">=", form.StartTime).
  138. Where("loginTime", "<=", form.EndTime).
  139. Select("userId", "DATE_FORMAT(loginTime, '%Y-%m-%d') as createdAt").
  140. Scan(&userLogin).Error
  141. if err != nil {
  142. response.Fail(c, 1001, err.Error())
  143. return
  144. }
  145. //userLoginCount := len(userLogin)
  146. activeCount := make(map[string]int)
  147. activeCountUser := make(map[string]map[int]bool)
  148. //根据日期进行分组
  149. for _, v := range userAction {
  150. activeCount[v.CreatedAt]++
  151. if activeCountUser[v.CreatedAt] == nil {
  152. activeCountUser[v.CreatedAt] = make(map[int]bool)
  153. }
  154. activeCountUser[v.CreatedAt][v.UserId] = true
  155. }
  156. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  157. type responses struct {
  158. Date string `json:"date"`
  159. ActiveCount int `json:"activeCount"`
  160. ActiveUserCount int `json:"activeUserCount"`
  161. ActiveCountRate float64 `json:"activeCountRate"`
  162. ActiveCountUser float64 `json:"activeCountUser"`
  163. }
  164. var res []responses
  165. //输出格式
  166. for _, v := range days {
  167. res = append(res, responses{
  168. Date: v,
  169. ActiveCount: activeCount[v],
  170. ActiveUserCount: len(activeCountUser[v]),
  171. ActiveCountRate: float64(activeCount[v] / 1),
  172. ActiveCountUser: float64(len(activeCountUser[v]) / 1),
  173. })
  174. }
  175. response.Success(c, gin.H{
  176. "data": res,
  177. })
  178. }
  179. func UserActionDetailDistribution(c *gin.Context) {
  180. form := request.Check(c, &struct {
  181. Id int `form:"id" binding:"required"`
  182. StartTime string `form:"startTime" binding:"required"`
  183. EndTime string `form:"endTime" binding:"required"`
  184. Type int `form:"type" binding:"required"`
  185. }{})
  186. var action model.GameAction
  187. err := global.App.DB.Table("game_action").
  188. Where("id", form.Id).Find(&action).Error
  189. if err != nil {
  190. response.Fail(c, 1001, err.Error())
  191. return
  192. }
  193. res := make(map[string]interface{})
  194. if form.Type == 1 {
  195. var userAction []struct {
  196. UserId int `json:"userId" gorm:"not null;column:userId;"`
  197. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  198. }
  199. err = global.App.DB.Table("user_action").
  200. Where("gid", action.Gid).
  201. Where("actionId", action.ActionId).
  202. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  203. Scan(&userAction).Error
  204. if err != nil {
  205. response.Fail(c, 1002, err.Error())
  206. return
  207. }
  208. activeCount := make(map[string]int)
  209. for _, v := range userAction {
  210. activeCount[v.CreatedAt]++
  211. }
  212. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  213. for _, v := range days {
  214. res[v] = activeCount[v]
  215. }
  216. //fmt.Print(len(userAction) / len(days))
  217. //response.Success(c, gin.H{"data": res, "avg": fmt.Sprintf("%.2f", float64(len(userAction))/float64(len(days)))})
  218. response.Success(c, gin.H{
  219. "data": map[string]interface{}{
  220. "avg": fmt.Sprintf("%.2f", float64(len(userAction))/float64(len(days))),
  221. "list": res,
  222. },
  223. })
  224. } else if form.Type == 2 {
  225. var userAction []struct {
  226. UserId int `json:"userId" gorm:"not null;column:userId;"`
  227. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  228. }
  229. err = global.App.DB.Table("user_action").
  230. Where("gid", action.Gid).
  231. Where("actionId", action.ActionId).
  232. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  233. Scan(&userAction).Error
  234. if err != nil {
  235. response.Fail(c, 1002, err.Error())
  236. return
  237. }
  238. activeCount := make(map[string]map[int]bool)
  239. for _, v := range userAction {
  240. if activeCount[v.CreatedAt] == nil {
  241. activeCount[v.CreatedAt] = make(map[int]bool)
  242. }
  243. activeCount[v.CreatedAt][v.UserId] = true
  244. }
  245. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  246. for _, v := range days {
  247. res[v] = len(activeCount[v])
  248. }
  249. response.Success(c, gin.H{
  250. "data": map[string]interface{}{
  251. "avg": fmt.Sprintf("%.2f", float64(len(userAction))/float64(len(days))),
  252. "list": res,
  253. },
  254. })
  255. } else if form.Type == 3 {
  256. var userAction []struct {
  257. UserId int `json:"userId" gorm:"not null;column:userId;"`
  258. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  259. }
  260. err = global.App.DB.Table("user_action").
  261. Where("gid", action.Gid).
  262. Where("actionId", action.ActionId).
  263. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  264. Scan(&userAction).Error
  265. if err != nil {
  266. response.Fail(c, 1002, err.Error())
  267. return
  268. }
  269. activeCount := make(map[string]map[int]bool)
  270. for _, v := range userAction {
  271. if activeCount[v.CreatedAt] == nil {
  272. activeCount[v.CreatedAt] = make(map[int]bool)
  273. }
  274. activeCount[v.CreatedAt][v.UserId] = true
  275. }
  276. //查询活跃用户
  277. } else if form.Type == 4 {
  278. } else {
  279. response.Fail(c, 1003, errors.New("type 错误"))
  280. return
  281. }
  282. }