gameAction.go 10 KB

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