log_ip.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. package log
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/xuri/excelize/v2"
  6. "go.uber.org/zap"
  7. "gorm.io/gorm"
  8. "log-server/global"
  9. "log-server/model/log"
  10. "log-server/model/log/request"
  11. "strconv"
  12. "time"
  13. )
  14. type ServiceIpLog struct {
  15. }
  16. //根据gameId、机器编号分组获取ip
  17. func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  18. limit := info.PageSize
  19. offset := info.PageSize * (info.Page - 1)
  20. db := global.GVA_DB.Model(&log.IpLog{})
  21. var apiList []log.IpLogResponse
  22. startDate := time.Now().Format("2006-01-02")
  23. endDate := time.Now().Format("2006-01-02")
  24. if len(api.Date) == 2 {
  25. startDate = api.Date[0]
  26. endDate = api.Date[1]
  27. }
  28. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  29. if api.GameId != 0 {
  30. db = db.Where("game_id = ?", api.GameId)
  31. }
  32. if api.PcCode != "" {
  33. db = db.Where("pc_code = ?", api.PcCode)
  34. }
  35. if api.Ip != "" {
  36. db = db.Where("ip = ?", api.Ip)
  37. }
  38. db = db.Select("create_date,pc_code,game_id,count(*) as count_total,count(distinct(ip)) as count_distinct_ip")
  39. db = db.Group("pc_code, game_id, create_date")
  40. //db.Select("count(*)").Count(&countTotal)
  41. //db.Select("count(distinct(ip))").Count(&countDistinctIp)
  42. err = db.Count(&total).Error
  43. if err != nil {
  44. return apiList, total, err
  45. } else {
  46. db = db.Limit(limit).Offset(offset)
  47. if order != "" {
  48. var OrderStr string
  49. // 设置有效排序key 防止sql注入
  50. // 感谢 Tom4t0 提交漏洞信息
  51. orderMap := make(map[string]bool, 4)
  52. orderMap["game_id"] = true
  53. orderMap["count_distinct_ip"] = true
  54. orderMap["count_total"] = true
  55. orderMap["create_date"] = true
  56. if orderMap[order] {
  57. if desc {
  58. OrderStr = order + " desc"
  59. } else {
  60. OrderStr = order
  61. }
  62. } else { // didn't matched any order key in `orderMap`
  63. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  64. return apiList, total, err
  65. }
  66. err = db.Order(OrderStr).Find(&apiList).Error
  67. } else {
  68. err = db.Order("id").Find(&apiList).Error
  69. }
  70. }
  71. //遍历更改日期格式
  72. for i, _ := range apiList {
  73. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  74. }
  75. return apiList, total, err
  76. }
  77. //获取异常租机ip列表
  78. func (s *ServiceIpLog) GetAbnormalIpLogList(api log.AbnormalIpLogRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
  79. limit := info.PageSize
  80. offset := info.PageSize * (info.Page - 1)
  81. db := global.GVA_DB.Model(&log.AbnormalMachineIp{})
  82. var apiList []log.AbnormalMachineIp
  83. startDate := time.Now().Format("2006-01-02")
  84. endDate := time.Now().Format("2006-01-02")
  85. if len(api.Date) == 2 {
  86. startDate = api.Date[0]
  87. endDate = api.Date[1]
  88. }
  89. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  90. if api.GameId != 0 {
  91. db = db.Where("game_id = ?", api.GameId)
  92. }
  93. if api.PcCode != "" {
  94. db = db.Where("pc_code = ?", api.PcCode)
  95. }
  96. if api.Ip != "" {
  97. db = db.Where("ip = ?", api.Ip)
  98. }
  99. err = db.Count(&total).Error
  100. if err != nil {
  101. return apiList, total, err
  102. } else {
  103. db = db.Limit(limit).Offset(offset)
  104. if order != "" {
  105. var OrderStr string
  106. // 设置有效排序key 防止sql注入
  107. // 感谢 Tom4t0 提交漏洞信息
  108. orderMap := make(map[string]bool, 3)
  109. orderMap["game_id"] = true
  110. orderMap["count"] = true
  111. orderMap["create_date"] = true
  112. if orderMap[order] {
  113. if desc {
  114. OrderStr = order + " desc"
  115. } else {
  116. OrderStr = order
  117. }
  118. } else { // didn't matched any order key in `orderMap`
  119. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  120. return apiList, total, err
  121. }
  122. err = db.Order(OrderStr).Find(&apiList).Error
  123. } else {
  124. err = db.Order("id desc").Find(&apiList).Error
  125. }
  126. }
  127. //遍历更改日期格式
  128. for i, _ := range apiList {
  129. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  130. }
  131. return apiList, total, err
  132. }
  133. //获取今日异常租机ip(ip播报使用)
  134. func (s *ServiceIpLog) GetTodayAbnormalIpLogList() (list []log.TodayAbnormalMachineIp, err error){
  135. var total int64
  136. var apiList []log.TodayAbnormalMachineIp
  137. date := time.Now().Format("2006-01-02")
  138. db := global.GVA_DB.Table("abnormal_machine_ip as ami")
  139. db = db.Select("ami.*, gt.task_name, gt.user")
  140. db = db.Joins("left join game_task as gt on gt.task_id = ami.game_id")
  141. db = db.Where("create_date", date)
  142. err = db.Count(&total).Error
  143. if err != nil {
  144. return apiList, err
  145. } else {
  146. err = db.Order("id").Find(&apiList).Error
  147. }
  148. //遍历更改日期格式
  149. for i, _ := range apiList {
  150. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  151. }
  152. return apiList , err
  153. }
  154. //根据gameId获取ip
  155. func (s *ServiceIpLog) GetGameIpList(api log.GameIpRequest, info request.PageInfo, order string, desc bool) (apiList []log.GameIpResponse, total int64, err error) {
  156. limit := info.PageSize
  157. offset := info.PageSize * (info.Page - 1)
  158. db := global.GVA_DB.Model(&log.GameIpResponse{})
  159. startDate := time.Now().Format("2006-01-02")
  160. endDate := time.Now().Format("2006-01-02")
  161. if len(api.Date) == 2 {
  162. startDate = api.Date[0]
  163. endDate = api.Date[1]
  164. }
  165. //筛选负责人
  166. if api.User != "" {
  167. db = db.Where("user = ?", api.User)
  168. }
  169. //筛选日期
  170. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  171. if api.GameId != 0 {
  172. db = db.Where("game_id = ?", api.GameId)
  173. }
  174. err = db.Count(&total).Error
  175. if err != nil {
  176. return apiList, total, err
  177. } else {
  178. db = db.Limit(limit).Offset(offset)
  179. if order != "" {
  180. var OrderStr string
  181. // 设置有效排序key 防止sql注入
  182. // 感谢 Tom4t0 提交漏洞信息
  183. orderMap := make(map[string]bool, 8)
  184. //orderMap["game_id"] = true
  185. orderMap["count_distinct_ip"] = true
  186. orderMap["count_total"] = true
  187. orderMap["success_ip"] = true
  188. orderMap["task_count"] = true
  189. orderMap["create_date"] = true
  190. orderMap["exceed_three"] = true
  191. orderMap["ip_repetition_rate"] = true
  192. orderMap["average_ip_repetition_rate"] = true
  193. if orderMap[order] {
  194. if desc {
  195. OrderStr = order + " desc"
  196. } else {
  197. OrderStr = order
  198. }
  199. } else { // didn't matched any order key in `orderMap`
  200. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  201. return apiList, total, err
  202. }
  203. err = db.Order(OrderStr).Find(&apiList).Error
  204. } else {
  205. err = db.Order("id").Find(&apiList).Error
  206. }
  207. }
  208. //遍历更改日期格式
  209. for i, _ := range apiList {
  210. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  211. //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
  212. //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)
  213. ////获取平均重复率和ip重复率 并保留小数点后两位
  214. //countDistinctIp := float64(apiList[i].CountDistinctIp)
  215. //successIp := float64(apiList[i].SuccessIp)
  216. //taskCount := float64(apiList[i].TaskCount)
  217. //apiList[i].IpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp/ successIp) *100), 64)
  218. //if taskCount != 0 {
  219. // apiList[i].AverageIpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp / taskCount) *100), 64)
  220. //}
  221. }
  222. return apiList, total, err
  223. }
  224. //展示ip
  225. func (s *ServiceIpLog) GetIp(iplog log.IpLogResponse) (list interface{}, total int64, err error) {
  226. var ipList []log.QueryIpList
  227. 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")
  228. db.Count(&total)
  229. err = db.Order("count desc").Find(&ipList).Error
  230. return ipList, total, err
  231. }
  232. //gameID分组展示ip
  233. func (s *ServiceIpLog) GetGameIp(ip log.GameIpResponse) (list interface{}, total int64, err error) {
  234. var ipList []log.QueryIpList
  235. 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")
  236. db.Count(&total)
  237. err = db.Order("count desc").Find(&ipList).Error
  238. return ipList, total, err
  239. }
  240. //获取gameIP列表
  241. func (s *ServiceIpLog) GameIpListExcel(infoList []log.GameIpResponse, filePath string) error {
  242. excel := excelize.NewFile()
  243. excel.SetSheetRow("Sheet1", "A1", &[]string{
  244. "游戏id",
  245. "负责人",
  246. "任务名称",
  247. "上报ip次数",
  248. "任务完成",
  249. "最大ip数",
  250. "异常ip个数",
  251. "成功ip次数",
  252. "ip个数",
  253. "ip重复率",
  254. "平均重复率",
  255. "日期"})
  256. for i, statisticsLog := range infoList {
  257. axis := fmt.Sprintf("A%d", i+2)
  258. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  259. statisticsLog.GameId,
  260. statisticsLog.User,
  261. statisticsLog.TaskName,
  262. statisticsLog.CountTotal,
  263. statisticsLog.TaskCount,
  264. statisticsLog.MaxCount,
  265. statisticsLog.ExceedThree,
  266. statisticsLog.SuccessIp,
  267. statisticsLog.CountDistinctIp,
  268. statisticsLog.IpRepetitionRate,
  269. statisticsLog.AverageIpRepetitionRate,
  270. statisticsLog.CreateDate[:10],
  271. })
  272. }
  273. err := excel.SaveAs(filePath)
  274. return err
  275. }
  276. //获取异常ip数据
  277. func (s *ServiceIpLog) GetAbnormalIp() (list map[string]string, err error) {
  278. db := global.GVA_DB.Model(&log.GameIpResponse{})
  279. date := time.Now().Format("2006-01-02")
  280. var apiList []log.GameIpResponse
  281. //筛选日期
  282. db = db.Where("create_date = ?", date)
  283. err = db.Order("id").Find(&apiList).Error
  284. var abnormalIpList = map[string]string{}
  285. //遍历更改日期格式
  286. for i, _ := range apiList {
  287. //apiList[i].CreateDate = apiList[i].CreateDate[:10]
  288. if apiList[i].IpRepetitionRate < 30 && apiList[i].CountTotal > 10 {
  289. abnormalIpList[apiList[i].User] = apiList[i].TaskName
  290. }
  291. }
  292. return abnormalIpList, err
  293. }
  294. //更新租机异常ip数据
  295. func (s *ServiceIpLog) UpdateAbnormalMachineIp() (err error) {
  296. //首先获取今天的时间
  297. var abnormalIpList []log.AbnormalMachineIp
  298. date := time.Now().Format("2006-01-02")
  299. //然后查找租机异常ip记录,并且添加至数据库中
  300. db := global.GVA_DB.Table("ip_log").Select("ip, create_date, game_id, pc_code, count(ip) as count")
  301. db = db.Where("create_date", date)
  302. db = db.Group("pc_code, ip, game_id")
  303. err = db.Find(&abnormalIpList).Error
  304. if err != nil {
  305. return err
  306. }
  307. //将异常ip存储至abnormal_machine_ip数据库中
  308. for i,_ := range abnormalIpList {
  309. var entity log.AbnormalMachineIp
  310. ip := abnormalIpList[i].Ip
  311. gameId := abnormalIpList[i].GameId
  312. pcCode := abnormalIpList[i].PcCode
  313. createDate := abnormalIpList[i].CreateDate[:10]
  314. count := abnormalIpList[i].Count
  315. //根据count判断,如果count < 9, continue继续做下一条记录的处理
  316. if count < 10 {
  317. continue
  318. }
  319. if !errors.Is(global.GVA_DB.Model(&log.AbnormalMachineIp{}).Where("ip = ? and game_id = ? and pc_code = ? and create_date = ?",ip, gameId, pcCode, createDate).First(&entity).Error, gorm.ErrRecordNotFound) {
  320. //存在相同的ip、租机、gameid、创建日期记录,那么更新数量即可
  321. err = global.GVA_DB.Model(&log.AbnormalMachineIp{}).Where("ip = ? and game_id = ? and pc_code = ? and create_date = ?", ip, gameId, pcCode, createDate).Update("count", count).Error
  322. if err != nil {
  323. return errors.New("更新ip数量失败")
  324. }
  325. continue
  326. }
  327. //如果数据库中没有相同的记录,则创建相对应的记录
  328. err = global.GVA_DB.Model(&log.AbnormalMachineIp{}).Create(&abnormalIpList[i]).Error
  329. if err != nil {
  330. return err
  331. }
  332. }
  333. return err
  334. }
  335. //定时更新ip列表数据
  336. func (s *ServiceIpLog) UpdateGameIpList() (err error) {
  337. var total int64
  338. var apiList []log.GameIpResponse
  339. db := global.GVA_DB.Model(&log.IpLog{})
  340. date := time.Now().Format("2006-01-02")
  341. //筛选日期
  342. db = db.Where("ip_log.create_date = ?", date)
  343. //查找总ip上报次数
  344. db = db.Joins("left join" + "(select count(*) as count, game_id, create_date from ip_log where 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", date)
  345. db = db.Where("ip_log.status = 2")
  346. 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")
  347. //拼接任务完成表
  348. 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")
  349. //拼接game_task表获取任务名称和负责人名称
  350. db = db.Joins("left join game_task as gt on gt.task_id = ip_log.game_id")
  351. db = db.Group("ip_log.game_id, ip_log.create_date")
  352. err = db.Count(&total).Error
  353. if err != nil {
  354. return err
  355. }
  356. err = db.Order("ip_log.id").Find(&apiList).Error
  357. fmt.Println(apiList)
  358. //遍历更改日期格式
  359. for i, _ := range apiList {
  360. var entity log.GameIpResponse
  361. apiList[i].CreateDate = apiList[i].CreateDate[:10]
  362. 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
  363. 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)
  364. //获取平均重复率和ip重复率 并保留小数点后两位
  365. countDistinctIp := float64(apiList[i].CountDistinctIp)
  366. successIp := float64(apiList[i].SuccessIp)
  367. taskCount := float64(apiList[i].TaskCount)
  368. apiList[i].IpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp/ successIp) *100), 64)
  369. if taskCount != 0 {
  370. apiList[i].AverageIpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp / taskCount) *100), 64)
  371. }
  372. //查找数据库中是否有这条记录(game_id, create_date)
  373. if !errors.Is(global.GVA_DB.Model(&log.GameIpResponse{}).Where("game_id = ? and create_date = ?",apiList[i].GameId, apiList[i].CreateDate).First(&entity).Error, gorm.ErrRecordNotFound) {
  374. //如果有,更新
  375. err = global.GVA_DB.Model(&log.GameIpResponse{}).Where("game_id = ? and create_date = ?",apiList[i].GameId, apiList[i].CreateDate).Save(&apiList[i]).Error
  376. if err != nil {
  377. return errors.New("更新ip列表失败")
  378. }
  379. continue
  380. }
  381. //如果数据库中没有相同的记录,则创建相对应的记录
  382. err = global.GVA_DB.Model(&log.GameIpResponse{}).Create(&apiList[i]).Error
  383. if err != nil {
  384. return err
  385. }
  386. }
  387. return err
  388. }