소스 검색

租机ip播报提交

maker 2 년 전
부모
커밋
7d5ef9fbc6
9개의 변경된 파일278개의 추가작업 그리고 19개의 파일을 삭제
  1. 22 2
      api/v1/log/log_ip.go
  2. 1 1
      config.yaml
  3. 6 1
      initialize/timer.go
  4. 25 0
      model/log/log_ip.go
  5. 8 0
      model/log/request/log_ip.go
  6. 1 0
      router/log/log_ip.go
  7. 8 2
      service/card/id_card.go
  8. 155 13
      service/log/log_ip.go
  9. 52 0
      service/system/sys_robot.go

+ 22 - 2
api/v1/log/log_ip.go

@@ -44,8 +44,6 @@ func (s *ApiIpLog) GetIpLogList(c *gin.Context) {
 	}
 }
 
-
-
 //根据gameId分组获取ip
 func (s *ApiIpLog) GetGameIPList(c *gin.Context) {
 	var paramsInfo request.GameIpRequest
@@ -131,6 +129,28 @@ func (s *ApiIpLog) GetGameIp(c *gin.Context) {
 	}
 }
 
+//获取异常租机ip
+func (s *ApiIpLog) GetAbnormalIpLogList(c *gin.Context) {
+	var paramsInfo request.AbnormalIpLogListRequest
+	_ = c.ShouldBindJSON(&paramsInfo)
+	if err := utils.Verify(paramsInfo.PageInfo, utils.PageInfoVerify); err != nil {
+		response.FailWithMessage(err.Error(), c)
+		return
+	}
+	list, total, err := ServiceIpLog.GetAbnormalIpLogList(paramsInfo.AbnormalIpLogRequest, paramsInfo.PageInfo, paramsInfo.OrderKey, paramsInfo.Desc)
+	if err != nil {
+		global.GVA_LOG.Error("获取失败!", zap.Error(err))
+		response.FailWithMessage("获取失败", c)
+	} else {
+		response.OkWithDetailed(response.PageResult{
+			List:     list,
+			Total:    total,
+			Page:     paramsInfo.Page,
+			PageSize: paramsInfo.PageSize,
+		}, "获取成功", c)
+	}
+}
+
 //导出列表
 func (s *ApiIpLog) GameIpExport(c *gin.Context) {
 	var excelInfo request.ExcelIpInfo

+ 1 - 1
config.yaml

@@ -159,7 +159,7 @@ zap:
   show-line: true
   log-in-console: true
 send-url:
-  computer-send-url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b111"
+  computer-send-url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7d095d5b-8240-45fd-a68c-baff3628d83b"
 #  computer-send-url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=89460a48-e1a1-4181-86c0-533bad342628"
   keyword-send-url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=34cbfa5a-c31d-464f-baf8-8363d6f4ac6a111"
   send-url-one: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5ccfb180-c062-48b5-ae18-0c96f7c19f0b111" #深圳群

+ 6 - 1
initialize/timer.go

@@ -201,10 +201,15 @@ func Timer() {
 	//}
 
 	//半小时同步一次IP信息,播报是否有异常
-	_, err := global.GVA_Timer.AddTaskByFunc("SyncIPMessage", "0 */30 * * * ? ", robotService.SyncIPMessage)
+	_, err := global.GVA_Timer.AddTaskByFunc("SyncIPMessage", "0 0 * * * ? ", robotService.SyncIPMessage)
 	if err != nil {
 		fmt.Println("add SyncIPMessage timer error:", err)
 	}
+	//半小时同步一次租机IP信息,播报是否有租机异常
+	_, err = global.GVA_Timer.AddTaskByFunc("SyncMachineIPMessage", "0 0 * * * ? ", robotService.SyncMachineIPMessage)
+	if err != nil {
+		fmt.Println("update MachineIPMessage timer error:", err)
+	}
 	//定时查看延迟更新的镜像是否到期
 	_, err = global.GVA_Timer.AddTaskByFunc("CheckPushTime", "35 0/2 * * * *", serviceFileQiniu.CheckPushTime)
 	if err != nil {

+ 25 - 0
model/log/log_ip.go

@@ -73,6 +73,26 @@ type AbnormalMachineIp struct {
 	CreateDate string `json:"create_date"` // 创建日期
 }
 
+type AbnormalIpLogRequest struct {
+	Id         uint     `json:"id"`
+	Ip         string   `json:"ip"` //模拟器ip
+	Date       []string `json:"date"`
+	GameId     int      `json:"game_id"`     //游戏id
+	PcCode     string   `json:"pc_code"`     //电脑编号
+	CreateDate string   `json:"create_date"` // 创建日期
+}
+
+type TodayAbnormalMachineIp struct {
+	Id         uint   `json:"id"`
+	Ip         string `json:"ip"` //模拟器ip
+	Count      int    `json:"count"`	//ip数量
+	GameId     int    `json:"game_id"`     //游戏id
+	PcCode     string `json:"pc_code"`     //电脑编号
+	User       string `json:"user"`			//负责人名称
+	TaskName   string `json:"task_name"`	//任务名称
+	CreateDate string `json:"create_date"` // 创建日期
+}
+
 func (IpLog) TableName() string {
 	return "ip_log"
 }
@@ -80,3 +100,8 @@ func (IpLog) TableName() string {
 func (AbnormalMachineIp) TableName() string {
 	return "abnormal_machine_ip"
 }
+
+func (GameIpResponse) TableName() string {
+	return "game_ip_list"
+}
+

+ 8 - 0
model/log/request/log_ip.go

@@ -15,3 +15,11 @@ type GameIpRequest struct {
 	OrderKey string `json:"orderKey"` // 排序
 	Desc     bool   `json:"desc"`     // 排序方式:升序false(默认)|降序true
 }
+
+//异常租机ip列表获取
+type AbnormalIpLogListRequest struct {
+	log.AbnormalIpLogRequest
+	PageInfo
+	OrderKey string `json:"orderKey"` // 排序
+	Desc     bool   `json:"desc"`     // 排序方式:升序false(默认)|降序true
+}

+ 1 - 0
router/log/log_ip.go

@@ -16,6 +16,7 @@ func (e *IpLogRouter) InitIpLogRouter(Router *gin.RouterGroup) {
 		ipLogRouter.POST("getIpLogList", logApi.GetIpLogList)	//获取ip日志列表
 		ipLogRouter.POST("getIp", logApi.GetIp)	//获取ip
 		ipLogRouter.POST("getGameIpList", logApi.GetGameIPList)	//获取ip日志列表
+		ipLogRouter.POST("getAbnormalIpLogList", logApi.GetAbnormalIpLogList)	//获取异常租机ip列表
 		ipLogRouter.POST("getGameIp", logApi.GetGameIp)	//获取游戏ip
 		ipLogRouter.POST("gameIpExport", logApi.GameIpExport)	//导出游戏ip列表
 	}

+ 8 - 2
service/card/id_card.go

@@ -6,6 +6,7 @@ import (
 	"log-server/model/card"
 	cardReq "log-server/model/card/request"
 	"math/rand"
+	"strconv"
 	"time"
 )
 
@@ -22,8 +23,6 @@ func (s *IdCardService) GetIdCard(idCard cardReq.IdCardRequest) (list []card.IdC
 	//取一个0-1000的随机数作为偏移值
 	//将时间戳设置成种子数
 	rand.Seed(time.Now().UnixNano())
-	//生成10个0-99之间的随机数
-	randomOffset := rand.Intn(1000)
 	limit := idCard.Rows
 	//联合查询符合条件的身份证信息
 	//查找channel != channel 的表单信息
@@ -31,6 +30,13 @@ func (s *IdCardService) GetIdCard(idCard cardReq.IdCardRequest) (list []card.IdC
 	db = db.Select("a.id, id_name, a.id_number, flag, IFNULL(use_count, 0) as use_count, b.id as usage_id, channel")
 	db = db.Joins("left JOIN shuyou_id_card_usage as b on b.id_idCard = a.id")
 	db.Count(&total)
+
+	//将total转化为int
+	strInt64 := strconv.FormatInt(total, 10)
+	countInt ,_ := strconv.Atoi(strInt64)
+	//生成1个0-total之间的随机数
+	randomOffset := rand.Intn(countInt)
+
 	db = db.Order("use_count").Limit(limit).Offset(randomOffset)
 	err = db.Find(&unionIdCardList).Error
 	if err != nil {

+ 155 - 13
service/log/log_ip.go

@@ -80,12 +80,92 @@ func (s *ServiceIpLog) GetIpLogList(api log.IpLogRequest, info request.PageInfo,
 	return apiList, total, err
 }
 
+//获取异常租机ip列表
+func (s *ServiceIpLog) GetAbnormalIpLogList(api log.AbnormalIpLogRequest, info request.PageInfo, order string, desc bool) (list interface{}, total int64, err error) {
+	limit := info.PageSize
+	offset := info.PageSize * (info.Page - 1)
+	db := global.GVA_DB.Model(&log.AbnormalMachineIp{})
+	var apiList []log.AbnormalMachineIp
+	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("create_date >= ? and create_date <= ?", startDate, endDate)
+	if api.GameId != 0 {
+		db = db.Where("game_id = ?", api.GameId)
+	}
+	if api.PcCode != "" {
+		db = db.Where("pc_code = ?", api.PcCode)
+	}
+	if api.Ip != "" {
+		db = db.Where("ip = ?", api.Ip)
+	}
+
+	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, 3)
+			orderMap["game_id"] = true
+			orderMap["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("id desc").Find(&apiList).Error
+		}
+	}
+	//遍历更改日期格式
+	for i, _ := range apiList {
+		apiList[i].CreateDate = apiList[i].CreateDate[:10]
+	}
+	return apiList, total, err
+}
+
+//获取今日异常租机ip(ip播报使用)
+func (s *ServiceIpLog) GetTodayAbnormalIpLogList() (list []log.TodayAbnormalMachineIp, err error){
+	var total int64
+	var apiList []log.TodayAbnormalMachineIp
+	date := time.Now().Format("2006-01-02")
+	db := global.GVA_DB.Table("abnormal_machine_ip as ami")
+	db = db.Select("ami.*, gt.task_name, gt.user")
+	db = db.Joins("left join game_task as gt on gt.task_id = ami.game_id")
+	db = db.Where("create_date", date)
+	err = db.Count(&total).Error
+	if err != nil {
+		return apiList, err
+	} else {
+		err = db.Order("id").Find(&apiList).Error
+	}
+	//遍历更改日期格式
+	for i, _ := range apiList {
+		apiList[i].CreateDate = apiList[i].CreateDate[:10]
+	}
+	return apiList , err
+}
+
 //根据gameId获取ip
 func (s *ServiceIpLog) GetGameIpList(api log.GameIpRequest, 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 {
@@ -168,7 +248,6 @@ func (s *ServiceIpLog) GetGameIpList(api log.GameIpRequest, info request.PageInf
 	return apiList, total, err
 }
 
-
 //展示ip
 func (s *ServiceIpLog) GetIp(iplog log.IpLogResponse) (list interface{}, total int64, err error) {
 	var ipList []log.QueryIpList
@@ -226,7 +305,6 @@ func (s *ServiceIpLog) GameIpListExcel(infoList []log.GameIpResponse, filePath s
 
 //获取异常ip数据
 func (s *ServiceIpLog) GetAbnormalIp() (list map[string]string, err error) {
-
 	db := global.GVA_DB.Model(&log.IpLog{})
 	startDate := time.Now().Format("2006-01-02")
 	endDate := time.Now().Format("2006-01-02")
@@ -280,23 +358,87 @@ func (s *ServiceIpLog) UpdateAbnormalMachineIp() (err error) {
 	if err != nil {
 		return err
 	}
-
 	//将异常ip存储至abnormal_machine_ip数据库中
-	for k,_ := range abnormalIpList {
+	for i,_ := range abnormalIpList {
 		var entity log.AbnormalMachineIp
-		ip := abnormalIpList[k].Ip
-		gameId := abnormalIpList[k].GameId
-		pcCode := abnormalIpList[k].PcCode
-		createDate := abnormalIpList[k].CreateDate
-		if !errors.Is(global.GVA_DB.Model(&log.AbnormalMachineIp{}).Where("ip, game_id, pc_code, create_date",ip, gameId, pcCode, createDate).First(&entity).Error, gorm.ErrRecordNotFound) {
-			return nil
+		ip := abnormalIpList[i].Ip
+		gameId := abnormalIpList[i].GameId
+		pcCode := abnormalIpList[i].PcCode
+		createDate := abnormalIpList[i].CreateDate[:10]
+		count := abnormalIpList[i].Count
+
+		//根据count判断,如果count < 9, continue继续做下一条记录的处理
+		if count < 10 {
+			continue
+		}
+
+		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) {
+			//存在相同的ip、租机、gameid、创建日期记录,那么更新数量即可
+			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
+			if err != nil {
+				return errors.New("更新ip数量失败")
+			}
+			continue
 		}
 		//如果数据库中没有相同的记录,则创建相对应的记录
-		err = global.GVA_DB.Model(&log.AbnormalMachineIp{}).Create(abnormalIpList[k]).Error
+		err = global.GVA_DB.Model(&log.AbnormalMachineIp{}).Create(&abnormalIpList[i]).Error
 		if err != nil {
 			return err
 		}
 	}
-	return
+	return err
+}
 
+//定时更新ip列表数据
+func (s *ServiceIpLog) UpdateGameIpList() (err error) {
+	var total int64
+	var apiList []log.GameIpResponse
+	db := global.GVA_DB.Model(&log.IpLog{})
+	date := time.Now().Format("2006-01-02")
+	//筛选日期
+	db = db.Where("ip_log.create_date = ?", date)
+	//查找总ip上报次数
+	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)
+	db = db.Where("ip_log.status = 2")
+	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")
+	//拼接任务完成表
+	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")
+	//拼接game_task表获取任务名称和负责人名称
+	db = db.Joins("left join game_task as gt on gt.task_id = ip_log.game_id")
+	db = db.Group("ip_log.game_id, ip_log.create_date")
+	err = db.Count(&total).Error
+	if err != nil {
+		return err
+	}
+	err = db.Order("ip_log.id").Find(&apiList).Error
+	//遍历更改日期格式
+	for i, _ := range apiList {
+		var entity log.GameIpResponse
+		apiList[i].CreateDate = apiList[i].CreateDate[:10]
+		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
+		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)
+		//获取平均重复率和ip重复率 并保留小数点后两位
+		countDistinctIp := float64(apiList[i].CountDistinctIp)
+		successIp := float64(apiList[i].SuccessIp)
+		taskCount := float64(apiList[i].TaskCount)
+		apiList[i].IpRepetitionRate, _ =  strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp/ successIp) *100), 64)
+		if taskCount != 0 {
+			apiList[i].AverageIpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp / taskCount) *100), 64)
+		}
+		//查找数据库中是否有这条记录(game_id, create_date)
+		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) {
+			//如果有,更新
+			err = global.GVA_DB.Model(&log.GameIpResponse{}).Where("game_id = ? and create_date = ?",apiList[i].GameId, apiList[i].CreateDate).Updates(&apiList[i]).Error
+			if err != nil {
+				return errors.New("更新ip列表失败")
+			}
+			continue
+		}
+		//如果数据库中没有相同的记录,则创建相对应的记录
+		err = global.GVA_DB.Model(&log.GameIpResponse{}).Create(&apiList[i]).Error
+		if err != nil {
+			return err
+		}
+	}
+	return err
 }

+ 52 - 0
service/system/sys_robot.go

@@ -177,3 +177,55 @@ func (robotService *RobotService) SyncIPMessage() {
 	}
 	return
 }
+
+
+// SyncMachineIPMessage 定时判断机器ip
+func (robotService *RobotService) SyncMachineIPMessage() {
+	fmt.Println("租机ip定时检查")
+	msg := "# IP租机异常播报 \n "
+	personMsg := "# IP租机异常播报 \n "
+	var syncData task.SyncData
+	var ipService log.ServiceIpLog
+	//首先更新异常列表
+	err := ipService.UpdateAbnormalMachineIp()
+	if err != nil {
+		global.GVA_LOG.Info("更新租机异常信息失败:"  + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
+	}
+	abnormalTodayIpList, _ := ipService.GetTodayAbnormalIpLogList()
+	if len(abnormalTodayIpList) == 0 {
+		return
+	}
+	var mobile []string
+	mpsPerson, _ := syncData.Person.GetUserInfoData()
+	personMsgData := make(map[string]string, len(mpsPerson))
+	//fmt.Println(mpsPerson)
+	for _, v := range abnormalTodayIpList {
+		mobile = append(mobile, mpsPerson[v.User].MobilePhoneNumber)
+		msg += "# " + v.User + " " + "\n"
+		msg += fmt.Sprintf("<font color=\"warning\">%s</font>", v.PcCode + " " + v.TaskName) + "\n"
+		personMsgData[v.User] += fmt.Sprintf("<font color=\"warning\">%s</font>", v.PcCode + " " + v.TaskName) + "\n"
+	}
+	url := global.GVA_CONFIG.SendUrl.ComputerSendUrl
+	var sendData SendMsg
+	sendData.MsgType = "markdown"
+	sendData.Markdown.Content = msg
+	syncData.SendMsgData(url, sendData)
+
+	if len(mobile) != 0 {
+		var sendTextData task.SendTextMsg
+		sendTextData.MsgType = "text"
+		sendTextData.Text.MentionedMobileList = mobile
+		//fmt.Println(mobile)
+		//b ,_ := json.Marshal(sendTextData)
+		//global.GVA_LOG.Warn(string(b))
+		syncData.SendMsgData(url, sendTextData)
+	}
+	for person, personData := range personMsgData {
+		var sendPersonData SendMsg
+		sendPersonMsg := personMsg + personData
+		sendPersonData.MsgType = "markdown"
+		sendPersonData.Markdown.Content = sendPersonMsg
+		syncData.SendMsgData(mpsPerson[person].Url, sendPersonData)
+	}
+	return
+}