gameAction.go 11 KB

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