maker пре 2 година
родитељ
комит
14dc3af2d1
4 измењених фајлова са 81 додато и 80 уклоњено
  1. 1 1
      config.yaml
  2. 6 5
      initialize/timer.go
  3. 28 63
      service/log/log_ip.go
  4. 46 11
      service/system/sys_robot.go

+ 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-baff3628d83b"
+  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=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 - 5
initialize/timer.go

@@ -200,16 +200,17 @@ func Timer() {
 	//	fmt.Println("add RemindSendOne timer error:", err)
 	//}
 
-	//小时同步一次IP信息,播报是否有异常
+	//小时同步一次IP信息,播报是否有异常
 	_, 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("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 {

+ 28 - 63
service/log/log_ip.go

@@ -165,40 +165,22 @@ func (s *ServiceIpLog) GetTodayAbnormalIpLogList() (list []log.TodayAbnormalMach
 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{})
+	db := global.GVA_DB.Model(&log.GameIpResponse{})
 	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]
 	}
-
 	//筛选负责人
 	if api.User != "" {
 		db = db.Where("user = ?", api.User)
 	}
-
 	//筛选日期
-	db = db.Where("ip_log.create_date >= ? and ip_log.create_date <= ?", startDate, endDate)
+	db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
 	if api.GameId != 0 {
-		db = db.Where("ip_log.game_id = ?", api.GameId)
-		//查找总ip上报次数
-		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 {
-		//查找总ip上报次数
-		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("game_id = ?", api.GameId)
 	}
-
-	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")
-	//db.Select("count(*)").Count(&countTotal)
-	//db.Select("count(distinct(ip))").Count(&countDistinctIp)
 	err = db.Count(&total).Error
 	if err != nil {
 		return apiList, total, err
@@ -208,13 +190,16 @@ func (s *ServiceIpLog) GetGameIpList(api log.GameIpRequest, info request.PageInf
 			var OrderStr string
 			// 设置有效排序key 防止sql注入
 			// 感谢 Tom4t0 提交漏洞信息
-			orderMap := make(map[string]bool, 5)
+			orderMap := make(map[string]bool, 8)
 			//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
+			orderMap["exceed_three"] = true
+			orderMap["ip_repetition_rate"] = true
+			orderMap["average_ip_repetition_rate"] = true
 			if orderMap[order] {
 				if desc {
 					OrderStr = order + " desc"
@@ -228,22 +213,22 @@ func (s *ServiceIpLog) GetGameIpList(api log.GameIpRequest, info request.PageInf
 
 			err = db.Order(OrderStr).Find(&apiList).Error
 		} else {
-			err = db.Order("ip_log.id").Find(&apiList).Error
+			err = db.Order("id").Find(&apiList).Error
 		}
 	}
 	//遍历更改日期格式
 	for i, _ := range apiList {
 		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)
-		}
+		//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)
+		//}
 	}
 	return apiList, total, err
 }
@@ -305,40 +290,17 @@ 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")
+	db := global.GVA_DB.Model(&log.GameIpResponse{})
+	date := time.Now().Format("2006-01-02")
 	var apiList []log.GameIpResponse
-
 	//筛选日期
-	db = db.Where("ip_log.create_date >= ? and ip_log.create_date <= ?", startDate, endDate)
-	//查找总ip上报次数
-	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("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.Order("ip_log.id").Find(&apiList).Error
-
+	db = db.Where("create_date = ?", date)
+	err = db.Order("id").Find(&apiList).Error
 	var abnormalIpList = map[string]string{}
 	//遍历更改日期格式
 	for i, _ := range apiList {
-		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)
-		apiList[i].AverageIpRepetitionRate, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", (countDistinctIp / taskCount) *100), 64)
-		if (apiList[i].IpRepetitionRate < 30 && apiList[i].SuccessIp > 10) || apiList[i].MaxCount > 50 {
+		//apiList[i].CreateDate = apiList[i].CreateDate[:10]
+		if apiList[i].IpRepetitionRate < 30 && apiList[i].CountTotal > 10 {
 			abnormalIpList[apiList[i].User] = apiList[i].TaskName
 		}
 	}
@@ -399,7 +361,9 @@ func (s *ServiceIpLog) UpdateGameIpList() (err error) {
 	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")
@@ -411,6 +375,7 @@ func (s *ServiceIpLog) UpdateGameIpList() (err error) {
 		return err
 	}
 	err = db.Order("ip_log.id").Find(&apiList).Error
+	fmt.Println(apiList)
 	//遍历更改日期格式
 	for i, _ := range apiList {
 		var entity log.GameIpResponse
@@ -428,7 +393,7 @@ func (s *ServiceIpLog) UpdateGameIpList() (err error) {
 		//查找数据库中是否有这条记录(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
+			err = global.GVA_DB.Model(&log.GameIpResponse{}).Where("game_id = ? and create_date = ?",apiList[i].GameId, apiList[i].CreateDate).Save(&apiList[i]).Error
 			if err != nil {
 				return errors.New("更新ip列表失败")
 			}

+ 46 - 11
service/system/sys_robot.go

@@ -139,26 +139,62 @@ func (robotService *RobotService) SyncIPMessage() {
 	personMsg := "# IP异常播报 \n "
 	var syncData task.SyncData
 	var ipService log.ServiceIpLog
-	abnormalIpList, _ := ipService.GetAbnormalIp()
-	if len(abnormalIpList) == 0 {
+
+	//首先更新ip列表
+	err := ipService.UpdateGameIpList()
+	if err != nil {
+		global.GVA_LOG.Info("定时更新ip列表失败:" + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
+	}
+
+	//然后更新异常列表
+	err = ipService.UpdateAbnormalMachineIp()
+	if err != nil {
+		global.GVA_LOG.Info("更新租机异常信息失败:" + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
+	}
+	//获取异常游戏名称和负责人
+	//abnormalIpList, _ := ipService.GetAbnormalIp()
+	//获取异常租机编号和负责人
+	abnormalTodayIpList, _ := ipService.GetTodayAbnormalIpLogList()
+	//如果没有异常列表,直接返回
+	//if len(abnormalIpList) == 0 && len(abnormalTodayIpList) == 0 {
+	//	return
+	//}
+	if len(abnormalTodayIpList) == 0 {
 		return
 	}
 	var mobile []string
 	mpsPerson, _ := syncData.Person.GetUserInfoData()
 	personMsgData := make(map[string]string, len(mpsPerson))
-	//fmt.Println(mpsPerson)
-	for k, _ := range abnormalIpList {
+	ipMsgData := make(map[string]string, len(mpsPerson))
+
+	//如果if len(abnormalIpList) != 0
+	//将游戏名称添加至数组
+	//if len(abnormalIpList) != 0 {
+	//	for k, _ := range abnormalIpList {
+	//		ipMsgData[k] += fmt.Sprintf("<font color=\"warning\">%s</font>", abnormalIpList[k]) + "\n"
+	//	}
+	//}
+	//if len(abnormalTodayIpList) != 0
+	//将租机编号添加至消息体
+	for k, v := range abnormalTodayIpList {
+		ipMsgData[v.User] += fmt.Sprintf("<font color=\"warning\">%s</font>", v.PcCode+" "+v.TaskName)
+		if k != len(abnormalTodayIpList) - 1 {
+			ipMsgData[v.User] +=  "\n"
+		}
+	}
+
+	for k, _ := range ipMsgData {
 		mobile = append(mobile, mpsPerson[k].MobilePhoneNumber)
 		msg += "# " + k + " " + "\n"
-		msg += fmt.Sprintf("<font color=\"warning\">%s</font>", abnormalIpList[k]) + "\n"
-		personMsgData[k] += fmt.Sprintf("<font color=\"warning\">%s</font>", abnormalIpList[k]) + "\n"
+		msg += fmt.Sprintf("<font color=\"warning\">%s</font>", ipMsgData[k]) + "\n"
+		personMsgData[k] += fmt.Sprintf("<font color=\"warning\">%s</font>", ipMsgData[k]) + "\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"
@@ -178,7 +214,6 @@ func (robotService *RobotService) SyncIPMessage() {
 	return
 }
 
-
 // SyncMachineIPMessage 定时判断机器ip
 func (robotService *RobotService) SyncMachineIPMessage() {
 	fmt.Println("租机ip定时检查")
@@ -189,7 +224,7 @@ func (robotService *RobotService) SyncMachineIPMessage() {
 	//首先更新异常列表
 	err := ipService.UpdateAbnormalMachineIp()
 	if err != nil {
-		global.GVA_LOG.Info("更新租机异常信息失败:"  + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
+		global.GVA_LOG.Info("更新租机异常信息失败:" + err.Error() + time.Now().Format("2006-01-02 15:04:05"))
 	}
 	abnormalTodayIpList, _ := ipService.GetTodayAbnormalIpLogList()
 	if len(abnormalTodayIpList) == 0 {
@@ -202,8 +237,8 @@ func (robotService *RobotService) SyncMachineIPMessage() {
 	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"
+		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