log_ip.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. "strconv"
  10. "time"
  11. )
  12. type ServiceIpLog struct {
  13. }
  14. //根据gameId、机器编号分组获取ip
  15. func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  16. limit := info.PageSize
  17. offset := info.PageSize * (info.Page - 1)
  18. db := global.GVA_DB.Model(&log.IpLog{})
  19. var apiList []log.IpLogResponse
  20. startDate := time.Now().Format("2006-01-02")
  21. endDate := time.Now().Format("2006-01-02")
  22. if len(api.Date) == 2 {
  23. startDate = api.Date[0]
  24. endDate = api.Date[1]
  25. }
  26. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  27. if api.GameId != 0 {
  28. db = db.Where("game_id = ?", api.GameId)
  29. }
  30. if api.PcCode != "" {
  31. db = db.Where("pc_code = ?", api.PcCode)
  32. }
  33. if api.Ip != "" {
  34. db = db.Where("ip = ?", api.Ip)
  35. }
  36. db = db.Select("create_date,pc_code,game_id,count(*) as count_total,count(distinct(ip)) as count_distinct_ip")
  37. db = db.Group("pc_code, game_id, create_date")
  38. //db.Select("count(*)").Count(&countTotal)
  39. //db.Select("count(distinct(ip))").Count(&countDistinctIp)
  40. err = db.Count(&total).Error
  41. if err != nil {
  42. return apiList, total, err
  43. } else {
  44. db = db.Limit(limit).Offset(offset)
  45. if order != "" {
  46. var OrderStr string
  47. // 设置有效排序key 防止sql注入
  48. // 感谢 Tom4t0 提交漏洞信息
  49. orderMap := make(map[string]bool, 4)
  50. orderMap["game_id"] = true
  51. orderMap["count_distinct_ip"] = true
  52. orderMap["count_total"] = true
  53. orderMap["create_date"] = true
  54. if orderMap[order] {
  55. if desc {
  56. OrderStr = order + " desc"
  57. } else {
  58. OrderStr = order
  59. }
  60. } else { // didn't matched any order key in `orderMap`
  61. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  62. return apiList, total, err
  63. }
  64. err = db.Order(OrderStr).Find(&apiList).Error
  65. } else {
  66. err = db.Order("id").Find(&apiList).Error
  67. }
  68. }
  69. //遍历更改日期格式
  70. for i, _ := range apiList {
  71. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  72. }
  73. return apiList, total, err
  74. }
  75. //根据gameId获取ip
  76. func (s *ServiceIpLog) GetGameIpList(api log.GameIpRequest, info request.PageInfo, order string, desc bool) (apiList []log.GameIpResponse, total int64, err error) {
  77. limit := info.PageSize
  78. offset := info.PageSize * (info.Page - 1)
  79. db := global.GVA_DB.Model(&log.IpLog{})
  80. startDate := time.Now().Format("2006-01-02")
  81. endDate := time.Now().Format("2006-01-02")
  82. if len(api.Date) == 2 {
  83. startDate = api.Date[0]
  84. endDate = api.Date[1]
  85. }
  86. //筛选负责人
  87. if api.User != "" {
  88. db = db.Where("user = ?", api.User)
  89. }
  90. //筛选日期
  91. db = db.Where("ip_log.create_date >= ? and ip_log.create_date <= ?", startDate, endDate)
  92. if api.GameId != 0 {
  93. db = db.Where("ip_log.game_id = ?", api.GameId)
  94. //查找总ip上报次数
  95. 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)
  96. } else {
  97. //查找总ip上报次数
  98. 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)
  99. }
  100. db = db.Where("ip_log.status = 2")
  101. db = db.Select("ip_log.create_date,ip_log.game_id,late.count as count_total, task_name, user, count(*) as success_ip , retained_complete as task_count, count(distinct(ip)) as count_distinct_ip")
  102. //拼接任务完成表
  103. 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")
  104. //拼接game_task表获取任务名称和负责人名称
  105. db = db.Joins("left join game_task as gt on gt.task_id = ip_log.game_id")
  106. db = db.Group("ip_log.game_id, ip_log.create_date")
  107. //db.Select("count(*)").Count(&countTotal)
  108. //db.Select("count(distinct(ip))").Count(&countDistinctIp)
  109. err = db.Count(&total).Error
  110. if err != nil {
  111. return apiList, total, err
  112. } else {
  113. db = db.Limit(limit).Offset(offset)
  114. if order != "" {
  115. var OrderStr string
  116. // 设置有效排序key 防止sql注入
  117. // 感谢 Tom4t0 提交漏洞信息
  118. orderMap := make(map[string]bool, 5)
  119. //orderMap["game_id"] = true
  120. orderMap["count_distinct_ip"] = true
  121. orderMap["count_total"] = true
  122. orderMap["success_ip"] = true
  123. orderMap["task_count"] = true
  124. orderMap["create_date"] = true
  125. if orderMap[order] {
  126. if desc {
  127. OrderStr = order + " desc"
  128. } else {
  129. OrderStr = order
  130. }
  131. } else { // didn't matched any order key in `orderMap`
  132. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  133. return apiList, total, err
  134. }
  135. err = db.Order(OrderStr).Find(&apiList).Error
  136. } else {
  137. err = db.Order("ip_log.id").Find(&apiList).Error
  138. }
  139. }
  140. //遍历更改日期格式
  141. for i, _ := range apiList {
  142. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  143. err = global.GVA_DB.Model(&log.IpLog{}).Select("COUNT(1) as max_count").Where("game_id = ? and create_date = ? and status = 2", apiList[i].GameId, apiList[i].CreateDate).Group("ip").Order("max_count desc").Limit(1).Find(&apiList[i]).Error
  144. global.GVA_DB.Model(&log.IpLog{}).Select("id,ip,count(*) ").Where("game_id = ? and create_date = ? and status = 2", apiList[i].GameId, apiList[i].CreateDate).Group("ip").Having("count(*) > 4").Count(&apiList[i].ExceedThree)
  145. //获取平均重复率和ip重复率 并保留小数点后两位
  146. countDistinctIp := float64(apiList[i].CountDistinctIp)
  147. successIp := float64(apiList[i].SuccessIp)
  148. taskCount := float64(apiList[i].TaskCount)
  149. apiList[i].IpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp/ successIp) *100), 64)
  150. if taskCount != 0 {
  151. apiList[i].AverageIpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp / taskCount) *100), 64)
  152. }
  153. }
  154. return apiList, total, err
  155. }
  156. //展示ip
  157. func (s *ServiceIpLog) GetIp(iplog log.IpLogResponse) (list interface{}, total int64, err error) {
  158. var ipList []log.QueryIpList
  159. 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")
  160. db.Count(&total)
  161. err = db.Order("count desc").Find(&ipList).Error
  162. return ipList, total, err
  163. }
  164. //gameID分组展示ip
  165. func (s *ServiceIpLog) GetGameIp(ip log.GameIpResponse) (list interface{}, total int64, err error) {
  166. var ipList []log.QueryIpList
  167. 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")
  168. db.Count(&total)
  169. err = db.Order("count desc").Find(&ipList).Error
  170. return ipList, total, err
  171. }
  172. //获取gameIP列表
  173. func (s *ServiceIpLog) GameIpListExcel(infoList []log.GameIpResponse, filePath string) error {
  174. excel := excelize.NewFile()
  175. excel.SetSheetRow("Sheet1", "A1", &[]string{
  176. "游戏id",
  177. "负责人",
  178. "任务名称",
  179. "上报ip次数",
  180. "任务完成",
  181. "最大ip数",
  182. "异常ip个数",
  183. "成功ip次数",
  184. "ip个数",
  185. "ip重复率",
  186. "平均重复率",
  187. "日期"})
  188. for i, statisticsLog := range infoList {
  189. axis := fmt.Sprintf("A%d", i+2)
  190. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  191. statisticsLog.GameId,
  192. statisticsLog.User,
  193. statisticsLog.TaskName,
  194. statisticsLog.CountTotal,
  195. statisticsLog.TaskCount,
  196. statisticsLog.MaxCount,
  197. statisticsLog.ExceedThree,
  198. statisticsLog.SuccessIp,
  199. statisticsLog.CountDistinctIp,
  200. statisticsLog.IpRepetitionRate,
  201. statisticsLog.AverageIpRepetitionRate,
  202. statisticsLog.CreateDate[:10],
  203. })
  204. }
  205. err := excel.SaveAs(filePath)
  206. return err
  207. }
  208. //获取异常ip数据
  209. func (s *ServiceIpLog) GetAbnormalIp() (list map[string]string, err error) {
  210. db := global.GVA_DB.Model(&log.IpLog{})
  211. startDate := time.Now().Format("2006-01-02")
  212. endDate := time.Now().Format("2006-01-02")
  213. var apiList []log.GameIpResponse
  214. //筛选日期
  215. db = db.Where("ip_log.create_date >= ? and ip_log.create_date <= ?", startDate, endDate)
  216. //查找总ip上报次数
  217. 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)
  218. db = db.Where("ip_log.status = 2")
  219. db = db.Select("ip_log.create_date,ip_log.game_id,late.count as count_total, task_name, user, count(*) as success_ip , retained_complete as task_count, count(distinct(ip)) as count_distinct_ip")
  220. //拼接任务完成表
  221. 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")
  222. //拼接game_task表获取任务名称和负责人名称
  223. db = db.Joins("left join game_task as gt on gt.task_id = ip_log.game_id")
  224. db = db.Group("ip_log.game_id, ip_log.create_date")
  225. err = db.Order("ip_log.id").Find(&apiList).Error
  226. var abnormalIpList = map[string]string{}
  227. //遍历更改日期格式
  228. for i, _ := range apiList {
  229. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  230. err = global.GVA_DB.Model(&log.IpLog{}).Select("COUNT(1) as max_count").Where("game_id = ? and create_date = ? and status = 2", apiList[i].GameId, apiList[i].CreateDate).Group("ip").Order("max_count desc").Limit(1).Find(&apiList[i]).Error
  231. global.GVA_DB.Model(&log.IpLog{}).Select("id,ip,count(*) ").Where("game_id = ? and create_date = ? and status = 2", apiList[i].GameId, apiList[i].CreateDate).Group("ip").Having("count(*) > 4").Count(&apiList[i].ExceedThree)
  232. //获取平均重复率和ip重复率 并保留小数点后两位
  233. countDistinctIp := float64(apiList[i].CountDistinctIp)
  234. successIp := float64(apiList[i].SuccessIp)
  235. taskCount := float64(apiList[i].TaskCount)
  236. apiList[i].IpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp/ successIp) *100), 64)
  237. apiList[i].AverageIpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp / taskCount) *100), 64)
  238. if apiList[i].IpRepetitionRate < 30 && apiList[i].SuccessIp > 10 {
  239. abnormalIpList[apiList[i].User] = apiList[i].TaskName
  240. }
  241. }
  242. return abnormalIpList, err
  243. }