ソースを参照

租机ip异常

maker 2 年 前
コミット
f99b096858
共有2 個のファイルを変更した50 個の追加1 個の削除を含む
  1. 13 0
      model/log/log_ip.go
  2. 37 1
      service/log/log_ip.go

+ 13 - 0
model/log/log_ip.go

@@ -64,6 +64,19 @@ type QueryIpList struct {
 	Count int    `json:"count"`
 }
 
+type AbnormalMachineIp 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"`     //电脑编号
+	CreateDate string `json:"create_date"` // 创建日期
+}
+
 func (IpLog) TableName() string {
 	return "ip_log"
 }
+
+func (AbnormalMachineIp) TableName() string {
+	return "abnormal_machine_ip"
+}

+ 37 - 1
service/log/log_ip.go

@@ -1,9 +1,11 @@
 package log
 
 import (
+	"errors"
 	"fmt"
 	"github.com/xuri/excelize/v2"
 	"go.uber.org/zap"
+	"gorm.io/gorm"
 	"log-server/global"
 	"log-server/model/log"
 	"log-server/model/log/request"
@@ -258,9 +260,43 @@ func (s *ServiceIpLog) GetAbnormalIp() (list map[string]string, err error) {
 		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 {
+		if (apiList[i].IpRepetitionRate < 30 && apiList[i].SuccessIp > 10) || apiList[i].MaxCount > 50 {
 			abnormalIpList[apiList[i].User] = apiList[i].TaskName
 		}
 	}
 	return abnormalIpList, err
 }
+
+//更新租机异常ip数据
+func (s *ServiceIpLog) UpdateAbnormalMachineIp() (err error) {
+	//首先获取今天的时间
+	var abnormalIpList []log.AbnormalMachineIp
+	date := time.Now().Format("2006-01-02")
+	//然后查找租机异常ip记录,并且添加至数据库中
+	db := global.GVA_DB.Table("ip_log").Select("ip, create_date, game_id, pc_code, count(ip) as count")
+	db = db.Where("create_date", date)
+	db = db.Group("pc_code, ip, game_id")
+	err = db.Find(&abnormalIpList).Error
+	if err != nil {
+		return err
+	}
+
+	//将异常ip存储至abnormal_machine_ip数据库中
+	for k,_ := 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
+		}
+		//如果数据库中没有相同的记录,则创建相对应的记录
+		err = global.GVA_DB.Model(&log.AbnormalMachineIp{}).Create(abnormalIpList[k]).Error
+		if err != nil {
+			return err
+		}
+	}
+	return
+
+}