log_ip.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package log
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. "go.uber.org/zap"
  6. "log-server/global"
  7. "log-server/model/log"
  8. "log-server/model/log/request"
  9. "time"
  10. )
  11. type ServiceIpLog struct {
  12. }
  13. //根据gameId、机器编号分组获取ip
  14. func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  15. limit := info.PageSize
  16. offset := info.PageSize * (info.Page - 1)
  17. db := global.GVA_DB.Model(&log.IpLog{})
  18. var apiList []log.IpLogResponse
  19. startDate := time.Now().Format("2006-01-02")
  20. endDate := time.Now().Format("2006-01-02")
  21. if len(api.Date) == 2 {
  22. startDate = api.Date[0]
  23. endDate = api.Date[1]
  24. }
  25. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  26. if api.GameId != 0 {
  27. db = db.Where("game_id = ?", api.GameId)
  28. }
  29. if api.PcCode != "" {
  30. db = db.Where("pc_code = ?", api.PcCode)
  31. }
  32. if api.Ip != "" {
  33. db = db.Where("ip = ?", api.Ip)
  34. }
  35. db = db.Select("create_date,pc_code,game_id,count(*) as count_total,count(distinct(ip)) as count_distinct_ip")
  36. db = db.Group("pc_code, game_id, create_date")
  37. //db.Select("count(*)").Count(&countTotal)
  38. //db.Select("count(distinct(ip))").Count(&countDistinctIp)
  39. err = db.Count(&total).Error
  40. if err != nil {
  41. return apiList, total, err
  42. } else {
  43. db = db.Limit(limit).Offset(offset)
  44. if order != "" {
  45. var OrderStr string
  46. // 设置有效排序key 防止sql注入
  47. // 感谢 Tom4t0 提交漏洞信息
  48. orderMap := make(map[string]bool, 4)
  49. orderMap["game_id"] = true
  50. orderMap["count_distinct_ip"] = true
  51. orderMap["count_total"] = true
  52. orderMap["create_date"] = true
  53. if orderMap[order] {
  54. if desc {
  55. OrderStr = order + " desc"
  56. } else {
  57. OrderStr = order
  58. }
  59. } else { // didn't matched any order key in `orderMap`
  60. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  61. return apiList, total, err
  62. }
  63. err = db.Order(OrderStr).Find(&apiList).Error
  64. } else {
  65. err = db.Order("id").Find(&apiList).Error
  66. }
  67. }
  68. //遍历更改日期格式
  69. for i, _ := range apiList {
  70. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  71. }
  72. return apiList, total, err
  73. }
  74. //根据gameId获取ip
  75. func (s *ServiceIpLog) GetGameIpList(api log.IpLogRequest, info request.PageInfo, order string, desc bool) (apiList []log.GameIpResponse, total int64, err error) {
  76. limit := info.PageSize
  77. offset := info.PageSize * (info.Page - 1)
  78. db := global.GVA_DB.Model(&log.IpLog{})
  79. startDate := time.Now().Format("2006-01-02")
  80. endDate := time.Now().Format("2006-01-02")
  81. if len(api.Date) == 2 {
  82. startDate = api.Date[0]
  83. endDate = api.Date[1]
  84. }
  85. db = db.Where("ip_log.create_date >= ? and ip_log.create_date <= ?", startDate, endDate)
  86. if api.GameId != 0 {
  87. db = db.Where("ip_log.game_id = ?", api.GameId)
  88. db = db.Joins("left join" + "(select count(*) as count, game_id, create_date from ip_log where game_id = ? and create_date >= ? and create_date <= ? group by game_id, create_date)" + " as late on late.game_id = ip_log.game_id and late.create_date = ip_log.create_date", api.GameId, startDate, endDate)
  89. } else {
  90. db = db.Joins("left join" + "(select count(*) as count, game_id, create_date from ip_log where create_date >= ? and create_date <= ? group by game_id, create_date)" + " as late on late.game_id = ip_log.game_id and late.create_date = ip_log.create_date", startDate, endDate)
  91. }
  92. db = db.Where("status = 2")
  93. db = db.Select("ip_log.create_date,ip_log.game_id,late.count as count_total, count(*) as success_ip , retained_complete as task_count, count(distinct(ip)) as count_distinct_ip")
  94. db = db.Joins("left join game_target_complete as gtc on gtc.task_id = ip_log.game_id and gtc.create_date = ip_log.create_date")
  95. db = db.Group("ip_log.game_id, ip_log.create_date")
  96. //db.Select("count(*)").Count(&countTotal)
  97. //db.Select("count(distinct(ip))").Count(&countDistinctIp)
  98. err = db.Count(&total).Error
  99. if err != nil {
  100. return apiList, total, err
  101. } else {
  102. db = db.Limit(limit).Offset(offset)
  103. if order != "" {
  104. var OrderStr string
  105. // 设置有效排序key 防止sql注入
  106. // 感谢 Tom4t0 提交漏洞信息
  107. orderMap := make(map[string]bool, 5)
  108. //orderMap["game_id"] = true
  109. orderMap["count_distinct_ip"] = true
  110. orderMap["count_total"] = true
  111. orderMap["success_ip"] = true
  112. orderMap["task_count"] = true
  113. orderMap["create_date"] = true
  114. if orderMap[order] {
  115. if desc {
  116. OrderStr = order + " desc"
  117. } else {
  118. OrderStr = order
  119. }
  120. } else { // didn't matched any order key in `orderMap`
  121. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  122. return apiList, total, err
  123. }
  124. err = db.Order(OrderStr).Find(&apiList).Error
  125. } else {
  126. err = db.Order("ip_log.id").Find(&apiList).Error
  127. }
  128. }
  129. //遍历更改日期格式
  130. for i, _ := range apiList {
  131. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  132. }
  133. return apiList, total, err
  134. }
  135. //展示ip
  136. func (s *ServiceIpLog) GetIp(iplog log.IpLogResponse) (list interface{}, total int64, err error) {
  137. var ipList []log.QueryIpList
  138. db := global.GVA_DB.Model(&log.IpLog{}).Select("ip, COUNT(1) as count").Where("game_id = ? and pc_code = ? and create_date = ?", iplog.GameId, iplog.PcCode, iplog.CreateDate).Group("ip")
  139. db.Count(&total)
  140. err = db.Order("count desc").Find(&ipList).Error
  141. return ipList, total, err
  142. }
  143. //gameID分组展示ip
  144. func (s *ServiceIpLog) GetGameIp(ip log.GameIpResponse) (list interface{}, total int64, err error) {
  145. var ipList []log.QueryIpList
  146. db := global.GVA_DB.Model(&log.IpLog{}).Select("ip, COUNT(1) as count").Where("game_id = ? and status = 2 and create_date = ?", ip.GameId, ip.CreateDate).Group("ip")
  147. db.Count(&total)
  148. err = db.Order("count desc").Find(&ipList).Error
  149. return ipList, total, err
  150. }
  151. //获取gameIP列表
  152. func (s *ServiceIpLog) GameIpListExcel(infoList []log.GameIpResponse, filePath string) error {
  153. excel := excelize.NewFile()
  154. excel.SetSheetRow("Sheet1", "A1", &[]string{
  155. "游戏id",
  156. "上报ip次数",
  157. "任务完成",
  158. "成功ip次数",
  159. "ip个数",
  160. "日期"})
  161. for i, statisticsLog := range infoList {
  162. axis := fmt.Sprintf("A%d", i+2)
  163. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  164. statisticsLog.GameId,
  165. statisticsLog.CountTotal,
  166. statisticsLog.TaskCount,
  167. statisticsLog.SuccessIp,
  168. statisticsLog.CountDistinctIp,
  169. statisticsLog.CreateDate[:10],
  170. })
  171. }
  172. err := excel.SaveAs(filePath)
  173. return err
  174. }