|
|
@@ -1,6 +1,8 @@
|
|
|
package log
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
+ "github.com/xuri/excelize/v2"
|
|
|
"go.uber.org/zap"
|
|
|
"log-server/global"
|
|
|
"log-server/model/log"
|
|
|
@@ -11,6 +13,7 @@ import (
|
|
|
type ServiceIpLog struct {
|
|
|
}
|
|
|
|
|
|
+//根据gameId、机器编号分组获取ip
|
|
|
func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
|
|
|
limit := info.PageSize
|
|
|
offset := info.PageSize * (info.Page - 1)
|
|
|
@@ -33,7 +36,7 @@ func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo,
|
|
|
db = db.Where("ip = ?", api.Ip)
|
|
|
}
|
|
|
|
|
|
- db = db.Select("create_date,ip,pc_code,game_id,count(*) as count_total,count(distinct(ip)) as count_distinct_ip")
|
|
|
+ db = db.Select("create_date,pc_code,game_id,count(*) as count_total,count(distinct(ip)) as count_distinct_ip")
|
|
|
db = db.Group("pc_code, game_id, create_date")
|
|
|
//db.Select("count(*)").Count(&countTotal)
|
|
|
//db.Select("count(distinct(ip))").Count(&countDistinctIp)
|
|
|
@@ -46,10 +49,11 @@ func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo,
|
|
|
var OrderStr string
|
|
|
// 设置有效排序key 防止sql注入
|
|
|
// 感谢 Tom4t0 提交漏洞信息
|
|
|
- orderMap := make(map[string]bool, 3)
|
|
|
+ orderMap := make(map[string]bool, 4)
|
|
|
orderMap["game_id"] = true
|
|
|
orderMap["count_distinct_ip"] = true
|
|
|
orderMap["count_total"] = true
|
|
|
+ orderMap["create_date"] = true
|
|
|
if orderMap[order] {
|
|
|
if desc {
|
|
|
OrderStr = order + " desc"
|
|
|
@@ -73,10 +77,113 @@ func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo,
|
|
|
return apiList, total, err
|
|
|
}
|
|
|
|
|
|
+//根据gameId获取ip
|
|
|
+func (s *ServiceIpLog) GetGameIpList(api log.IpLogRequest, info request.PageInfo, order string, desc bool) (apiList []log.GameIpResponse, total int64, err error) {
|
|
|
+ limit := info.PageSize
|
|
|
+ offset := info.PageSize * (info.Page - 1)
|
|
|
+ db := global.GVA_DB.Model(&log.IpLog{})
|
|
|
+
|
|
|
+ startDate := time.Now().Format("2006-01-02")
|
|
|
+ endDate := time.Now().Format("2006-01-02")
|
|
|
+ if len(api.Date) == 2 {
|
|
|
+ startDate = api.Date[0]
|
|
|
+ endDate = api.Date[1]
|
|
|
+ }
|
|
|
+ db = db.Where("ip_log.create_date >= ? and ip_log.create_date <= ?", startDate, endDate)
|
|
|
+ if api.GameId != 0 {
|
|
|
+ db = db.Where("ip_log.game_id = ?", api.GameId)
|
|
|
+ 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)
|
|
|
+ } else {
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ db = db.Where("status = 2")
|
|
|
+
|
|
|
+ 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")
|
|
|
+ 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")
|
|
|
+
|
|
|
+ db = db.Group("ip_log.game_id, ip_log.create_date")
|
|
|
+ //db.Select("count(*)").Count(&countTotal)
|
|
|
+ //db.Select("count(distinct(ip))").Count(&countDistinctIp)
|
|
|
+ err = db.Count(&total).Error
|
|
|
+ if err != nil {
|
|
|
+ return apiList, total, err
|
|
|
+ } else {
|
|
|
+ db = db.Limit(limit).Offset(offset)
|
|
|
+ if order != "" {
|
|
|
+ var OrderStr string
|
|
|
+ // 设置有效排序key 防止sql注入
|
|
|
+ // 感谢 Tom4t0 提交漏洞信息
|
|
|
+ orderMap := make(map[string]bool, 5)
|
|
|
+ //orderMap["game_id"] = true
|
|
|
+ orderMap["count_distinct_ip"] = true
|
|
|
+ orderMap["count_total"] = true
|
|
|
+ orderMap["success_ip"] = true
|
|
|
+ orderMap["task_count"] = true
|
|
|
+ orderMap["create_date"] = true
|
|
|
+ if orderMap[order] {
|
|
|
+ if desc {
|
|
|
+ OrderStr = order + " desc"
|
|
|
+ } else {
|
|
|
+ OrderStr = order
|
|
|
+ }
|
|
|
+ } else { // didn't matched any order key in `orderMap`
|
|
|
+ global.GVA_LOG.Error("获取失败!", zap.Error(err))
|
|
|
+ return apiList, total, err
|
|
|
+ }
|
|
|
+
|
|
|
+ err = db.Order(OrderStr).Find(&apiList).Error
|
|
|
+ } else {
|
|
|
+ err = db.Order("ip_log.id").Find(&apiList).Error
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //遍历更改日期格式
|
|
|
+ for i, _ := range apiList {
|
|
|
+ apiList[i].CreateDate = apiList[i].CreateDate[:10]
|
|
|
+ }
|
|
|
+ return apiList, total, err
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//展示ip
|
|
|
func (s *ServiceIpLog) GetIp(iplog log.IpLogResponse) (list interface{}, total int64, err error) {
|
|
|
var ipList []log.QueryIpList
|
|
|
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")
|
|
|
- //db.Group("ip").Count(&total)
|
|
|
- err = db.Find(&ipList).Count(&total).Error
|
|
|
+ db.Count(&total)
|
|
|
+ err = db.Order("count desc").Find(&ipList).Error
|
|
|
return ipList, total, err
|
|
|
}
|
|
|
+
|
|
|
+//gameID分组展示ip
|
|
|
+func (s *ServiceIpLog) GetGameIp(ip log.GameIpResponse) (list interface{}, total int64, err error) {
|
|
|
+ var ipList []log.QueryIpList
|
|
|
+ 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")
|
|
|
+ db.Count(&total)
|
|
|
+ err = db.Order("count desc").Find(&ipList).Error
|
|
|
+ return ipList, total, err
|
|
|
+}
|
|
|
+
|
|
|
+//获取gameIP列表
|
|
|
+func (s *ServiceIpLog) GameIpListExcel(infoList []log.GameIpResponse, filePath string) error {
|
|
|
+ excel := excelize.NewFile()
|
|
|
+ excel.SetSheetRow("Sheet1", "A1", &[]string{
|
|
|
+ "游戏id",
|
|
|
+ "上报ip次数",
|
|
|
+ "任务完成",
|
|
|
+ "成功ip次数",
|
|
|
+ "ip个数",
|
|
|
+ "日期"})
|
|
|
+ for i, statisticsLog := range infoList {
|
|
|
+ axis := fmt.Sprintf("A%d", i+2)
|
|
|
+ excel.SetSheetRow("Sheet1", axis, &[]interface{}{
|
|
|
+ statisticsLog.GameId,
|
|
|
+ statisticsLog.CountTotal,
|
|
|
+ statisticsLog.TaskCount,
|
|
|
+ statisticsLog.SuccessIp,
|
|
|
+ statisticsLog.CountDistinctIp,
|
|
|
+ statisticsLog.CreateDate[:10],
|
|
|
+ })
|
|
|
+ }
|
|
|
+ err := excel.SaveAs(filePath)
|
|
|
+ return err
|
|
|
+}
|