gameAction.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. form.EndTime = form.EndTime + " 23:59:59"
  20. //查询出所有的事件
  21. var actionList []model.GameAction
  22. query := global.App.DB.Table("game_action")
  23. err := query.
  24. Where("gid", form.Gid).
  25. Select("id", "actionId", "actionName").
  26. Order("id desc").Scan(&actionList).Error
  27. if err != nil {
  28. response.Fail(c, 1003, err.Error())
  29. return
  30. }
  31. //查询出时间段内的活跃用户,登录次数
  32. var userLogin []model.UserLogin
  33. query1 := global.App.DB.Table("user_login")
  34. if len(form.Pf) > 0 {
  35. query1 = query1.WhereIn("pf", form.Pf)
  36. }
  37. err = query1.
  38. Where("gid", form.Gid).
  39. Where("loginTime", ">=", form.StartTime).
  40. Where("loginTime", "<=", form.EndTime).
  41. Select("userId", "loginTime").
  42. Scan(&userLogin).Error
  43. if err != nil {
  44. response.Fail(c, 1001, err.Error())
  45. return
  46. }
  47. userLoginCount := len(userLogin)
  48. //查询出时间段内事件触发数量,以及触发人的ID
  49. var userAction []model.UserAction
  50. query2 := global.App.DB.Table("user_action")
  51. if len(form.Pf) > 0 {
  52. query2 = query2.WhereIn("pf", form.Pf)
  53. }
  54. err = query2.
  55. Where("gid", form.Gid).
  56. Where("createdAt", ">=", form.StartTime).
  57. Where("createdAt", "<=", form.EndTime).
  58. Select("id", "actionId", "userId", "createdAt").
  59. Scan(&userAction).Error
  60. if err != nil {
  61. response.Fail(c, 1002, err.Error())
  62. return
  63. }
  64. //计算事件的触发总数和触发用户数
  65. actionSumMap := make(map[string]int)
  66. actionUserSumMap := make(map[string][]int)
  67. //fmt.Println(userAction)
  68. for _, action := range userAction {
  69. actionSumMap[action.ActionId]++
  70. actionUserSumMap[action.ActionId] = append(actionUserSumMap[action.ActionId], action.UserId)
  71. }
  72. //根据事件触发和活跃用户数量进行比对得出其他数据
  73. activeUser := make(map[int]bool)
  74. var activeUserSlice []int
  75. for _, users := range userLogin {
  76. activeUser[users.UserId] = true
  77. }
  78. for k := range activeUser {
  79. activeUserSlice = append(activeUserSlice, k)
  80. }
  81. type responses struct {
  82. Id int `json:"id"`
  83. ActionId string `json:"actionId"`
  84. ActionName string `json:"actionName"`
  85. ActionCount int `json:"actionCount"`
  86. ActionUserCount int `json:"actionUserCount"`
  87. ActiveUserRate float64 `json:"activeUserRate"`
  88. LoginActiveRate float64 `json:"loginActiveRate"`
  89. }
  90. var res []responses
  91. for _, v := range actionList {
  92. var ActiveUserRate float64
  93. var LoginActiveRate float64
  94. if userLoginCount > 0 {
  95. ActiveUserRate = float64(actionSumMap[v.ActionId] / userLoginCount)
  96. LoginActiveRate = float64(len(actionUserSumMap[v.ActionId]) / userLoginCount)
  97. }
  98. res = append(res, responses{
  99. Id: v.ID,
  100. ActionId: v.ActionId,
  101. ActionName: v.ActionName,
  102. ActionCount: actionSumMap[v.ActionId],
  103. ActionUserCount: len(actionUserSumMap[v.ActionId]),
  104. ActiveUserRate: ActiveUserRate,
  105. LoginActiveRate: LoginActiveRate,
  106. })
  107. }
  108. response.Success(c, gin.H{
  109. "data": res,
  110. "count": len(res),
  111. })
  112. }
  113. func UserActionDetail(c *gin.Context) {
  114. form := request.Check(c, &struct {
  115. Id int `form:"id" binding:"required"`
  116. Pf []string `form:"pf" binding:""`
  117. StartTime string `form:"startTime" binding:"required"`
  118. EndTime string `form:"endTime" binding:"required"`
  119. }{})
  120. //查询启动次数
  121. var action model.GameAction
  122. err := global.App.DB.Table("game_action").
  123. Where("id", form.Id).Find(&action).Error
  124. if err != nil {
  125. response.Fail(c, 1001, err.Error())
  126. return
  127. }
  128. var userAction []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_action").
  133. Where("gid", action.Gid).
  134. Where("actionId", action.ActionId).
  135. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  136. Scan(&userAction).Error
  137. if err != nil {
  138. response.Fail(c, 1002, err.Error())
  139. return
  140. }
  141. //查询出时间段内的活跃用户,登录次数
  142. var userLogin []struct {
  143. UserId int `json:"userId" gorm:"not null;column:userId;"`
  144. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  145. }
  146. err = global.App.DB.Table("user_login").
  147. Where("gid", action.Gid).
  148. Where("loginTime", ">=", form.StartTime).
  149. Where("loginTime", "<=", form.EndTime).
  150. Select("userId", "DATE_FORMAT(loginTime, '%Y-%m-%d') as createdAt").
  151. Scan(&userLogin).Error
  152. if err != nil {
  153. response.Fail(c, 1001, err.Error())
  154. return
  155. }
  156. //userLoginCount := len(userLogin)
  157. activeCount := make(map[string]int)
  158. activeCountUser := make(map[string]map[int]bool)
  159. //根据日期进行分组
  160. for _, v := range userAction {
  161. activeCount[v.CreatedAt]++
  162. if activeCountUser[v.CreatedAt] == nil {
  163. activeCountUser[v.CreatedAt] = make(map[int]bool)
  164. }
  165. activeCountUser[v.CreatedAt][v.UserId] = true
  166. }
  167. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  168. type responses struct {
  169. Date string `json:"date"`
  170. ActiveCount int `json:"activeCount"`
  171. ActiveUserCount int `json:"activeUserCount"`
  172. ActiveCountRate float64 `json:"activeCountRate"`
  173. ActiveCountUser float64 `json:"activeCountUser"`
  174. }
  175. var res []responses
  176. //输出格式
  177. for _, v := range days {
  178. res = append(res, responses{
  179. Date: v,
  180. ActiveCount: activeCount[v],
  181. ActiveUserCount: len(activeCountUser[v]),
  182. ActiveCountRate: float64(activeCount[v] / 1),
  183. ActiveCountUser: float64(len(activeCountUser[v]) / 1),
  184. })
  185. }
  186. response.Success(c, gin.H{
  187. "data": res,
  188. })
  189. }
  190. func UserActionDetailDistribution(c *gin.Context) {
  191. form := request.Check(c, &struct {
  192. Id int `form:"id" binding:"required"`
  193. Pf []string `form:"pf" binding:""`
  194. StartTime string `form:"startTime" binding:"required"`
  195. EndTime string `form:"endTime" binding:"required"`
  196. Type int `form:"type" binding:"required"`
  197. }{})
  198. var action model.GameAction
  199. err := global.App.DB.Table("game_action").
  200. Where("id", form.Id).Find(&action).Error
  201. if err != nil {
  202. response.Fail(c, 1001, err.Error())
  203. return
  204. }
  205. res := make(map[string]interface{})
  206. if form.Type == 1 {
  207. var userAction []struct {
  208. UserId int `json:"userId" gorm:"not null;column:userId;"`
  209. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  210. }
  211. query := global.App.DB.Table("user_action")
  212. if len(form.Pf) > 0 {
  213. query = query.WhereIn("pf", form.Pf)
  214. }
  215. err = query.
  216. Where("gid", action.Gid).
  217. Where("actionId", action.ActionId).
  218. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  219. Scan(&userAction).Error
  220. if err != nil {
  221. response.Fail(c, 1002, err.Error())
  222. return
  223. }
  224. activeCount := make(map[string]int)
  225. for _, v := range userAction {
  226. activeCount[v.CreatedAt]++
  227. }
  228. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  229. for _, v := range days {
  230. res[v] = activeCount[v]
  231. }
  232. response.Success(c, gin.H{
  233. "data": map[string]interface{}{
  234. "avg": fmt.Sprintf("%.2f", float64(len(userAction))/float64(len(days))),
  235. "list": res,
  236. },
  237. })
  238. } else if form.Type == 2 {
  239. var userAction []struct {
  240. UserId int `json:"userId" gorm:"not null;column:userId;"`
  241. CreatedAt string `json:"createdAt" gorm:"not null;column:createdAt;"`
  242. }
  243. query := global.App.DB.Table("user_action")
  244. if len(form.Pf) > 0 {
  245. query = query.WhereIn("gid", form.Pf)
  246. }
  247. err = query.
  248. Where("gid", action.Gid).
  249. Where("actionId", action.ActionId).
  250. Select("userId", "DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt").
  251. Scan(&userAction).Error
  252. if err != nil {
  253. response.Fail(c, 1002, err.Error())
  254. return
  255. }
  256. activeCount := make(map[string]map[int]bool)
  257. for _, v := range userAction {
  258. if activeCount[v.CreatedAt] == nil {
  259. activeCount[v.CreatedAt] = make(map[int]bool)
  260. }
  261. activeCount[v.CreatedAt][v.UserId] = true
  262. }
  263. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  264. for _, v := range days {
  265. res[v] = len(activeCount[v])
  266. }
  267. response.Success(c, gin.H{
  268. "data": map[string]interface{}{
  269. "avg": fmt.Sprintf("%.2f", float64(len(userAction))/float64(len(days))),
  270. "list": res,
  271. },
  272. })
  273. } else if form.Type == 3 {
  274. //活跃设备发生率
  275. var userAction []string
  276. query := global.App.DB.Table("user_action")
  277. if len(form.Pf) > 0 {
  278. query = query.WhereIn("pf", form.Pf)
  279. }
  280. err = query.
  281. Where("gid", action.Gid).
  282. Where("actionId", action.ActionId).
  283. Pluck("DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt", &userAction).Error
  284. if err != nil {
  285. response.Fail(c, 1002, err.Error())
  286. return
  287. }
  288. actionCount := make(map[string]int)
  289. for _, v := range userAction {
  290. actionCount[v]++
  291. }
  292. //var userOnline model.UserOnline
  293. //query = global.App.DB.Table("user_online")
  294. //if len(form.Pf) > 0 {
  295. // query = query.WhereIn("gid", form.Pf)
  296. //}
  297. //err = query.
  298. // Where("gid", action.Gid).
  299. // Error
  300. //if err != nil {
  301. // response.Fail(c, 1002, err.Error())
  302. // return
  303. //}
  304. //userCount := make(map[string]map[int]bool)
  305. //for _, v := range userOnline {
  306. //
  307. //}
  308. } else if form.Type == 4 {
  309. //每次启动发生率
  310. var userLogin []string
  311. query := global.App.DB.Table("user_login")
  312. if len(form.Pf) > 0 {
  313. query = query.WhereIn("pf", form.Pf)
  314. }
  315. err = query.
  316. Where("gid", action.Gid).
  317. Pluck("DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt", &userLogin).Error
  318. if err != nil {
  319. response.Fail(c, 1001, err.Error())
  320. return
  321. }
  322. loginCount := make(map[string]int)
  323. for _, v := range userLogin {
  324. loginCount[v]++
  325. }
  326. var userAction []string
  327. query = global.App.DB.Table("user_action")
  328. if len(form.Pf) > 0 {
  329. query = query.WhereIn("pf", form.Pf)
  330. }
  331. err = query.
  332. Where("gid", action.Gid).
  333. Where("actionId", action.ActionId).
  334. Pluck("DATE_FORMAT(createdAt, '%Y-%m-%d') as createdAt", &userAction).Error
  335. if err != nil {
  336. response.Fail(c, 1002, err.Error())
  337. return
  338. }
  339. actionCount := make(map[string]int)
  340. for _, v := range userAction {
  341. actionCount[v]++
  342. }
  343. days := utils.GetTimeDayDateFormat(form.StartTime, form.EndTime)
  344. for _, v := range days {
  345. if loginCount[v] > 0 {
  346. res[v] = float64(actionCount[v]) / float64(loginCount[v])
  347. } else {
  348. res[v] = 0
  349. }
  350. }
  351. } else {
  352. response.Fail(c, 1003, errors.New("type 错误"))
  353. return
  354. }
  355. }