log_list.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package log
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "go.uber.org/zap"
  7. "log-server/global"
  8. "log-server/model/log"
  9. "log-server/model/log/request"
  10. "log-server/model/log/response"
  11. loging2 "log-server/service/log/loging"
  12. "strconv"
  13. "time"
  14. )
  15. type ServiceLogList struct {
  16. ServiceLogCoding
  17. logical loging2.LogicalLog
  18. }
  19. func (s *ServiceLogList) GetCodeLogList(ctx context.Context, api log.Loging, info request.PageInfo, order string, desc bool, code int) (list interface{}, total int64, err error) {
  20. limit := info.PageSize
  21. offset := info.PageSize * (info.Page - 1)
  22. db := global.GVA_DB.Model(&log.Loging{})
  23. var apiList []log.Loging
  24. var createDate string
  25. if api.CreateDate == "" {
  26. createDate = time.Now().Format("2006-01-02")
  27. } else {
  28. createDate = api.CreateDate
  29. }
  30. db = db.Where("create_date = ?", createDate)
  31. if api.Coding != 0 {
  32. db = db.Where("coding = ?", api.Coding)
  33. }
  34. if api.GameId != 0 {
  35. db = db.Where("game_id = ?", api.GameId)
  36. } else {
  37. return
  38. }
  39. if api.PcCode != "" {
  40. db = db.Where("pc_code = ?", api.PcCode)
  41. }
  42. if api.Account != "" {
  43. db = db.Where("account = ?", api.Account)
  44. }
  45. if api.LogUuid != "" {
  46. db = db.Where("log_uuid = ?", api.LogUuid)
  47. }
  48. if code != 0 {
  49. db = db.Where("type_coding = ?", code)
  50. }
  51. if api.Status != 0 {
  52. if api.Status == 2 {
  53. db = db.Where("status = ?", 0)
  54. } else {
  55. db = db.Where("status = ?", 1)
  56. }
  57. }
  58. err = db.Count(&total).Error
  59. if err != nil {
  60. return apiList, total, err
  61. } else {
  62. db = db.Limit(limit).Offset(offset)
  63. if order != "" {
  64. var OrderStr string
  65. // 设置有效排序key 防止sql注入
  66. // 感谢 Tom4t0 提交漏洞信息
  67. orderMap := make(map[string]bool, 4)
  68. orderMap["id"] = true
  69. orderMap["log_uuid"] = true
  70. orderMap["account"] = true
  71. orderMap["game_id"] = true
  72. if orderMap[order] {
  73. if desc {
  74. OrderStr = order + " desc"
  75. } else {
  76. OrderStr = order
  77. }
  78. } else { // didn't matched any order key in `orderMap`
  79. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  80. return apiList, total, err
  81. }
  82. err = db.Order(OrderStr).Find(&apiList).Error
  83. } else {
  84. err = db.Order("id").Find(&apiList).Error
  85. }
  86. }
  87. fmt.Println(time.Now().Unix())
  88. var apisReply []*response.GetLogReply
  89. if total != 0 {
  90. for _, apiInfo := range apiList {
  91. apiReply := new(response.GetLogReply)
  92. apiReply.Id = apiInfo.Id
  93. apiReply.Coding = strconv.Itoa(apiInfo.Coding)
  94. apiReply.GameId = apiInfo.GameId
  95. apiReply.Status = apiInfo.Status
  96. apiReply.Account = apiInfo.Account
  97. apiReply.DeviceId = apiInfo.DeviceId
  98. apiReply.ComputerType = apiInfo.ComputerType
  99. apiReply.EnvCode = apiInfo.EnvCode
  100. apiReply.LogUuid = apiInfo.LogUuid
  101. apiReply.Operator = apiInfo.Operator
  102. apiReply.PcCode = apiInfo.PcCode
  103. apiReply.PcIp = apiInfo.PcIp
  104. apiReply.SimulatorIp = apiInfo.SimulatorIp
  105. apiReply.SimulatorMac = apiInfo.SimulatorMac
  106. apiReply.PcMac = apiInfo.PcMac
  107. apiReply.AccountType = apiInfo.AccountType
  108. apiReply.CreateDate = apiInfo.CreateDate[:10]
  109. apiReply.CreateTime = apiInfo.CreateTime
  110. apiReply.Remarks = apiInfo.Remarks
  111. apiReply.TaskType = apiInfo.TaskType
  112. describe, _ := s.CodeListCache(ctx, apiReply.Coding)
  113. apiReply.Describe = describe
  114. apiReply.ScriptType = apiInfo.ScriptType
  115. apisReply = append(apisReply, apiReply)
  116. }
  117. }
  118. fmt.Println(time.Now().Unix())
  119. return apisReply, total, err
  120. }
  121. /*var loging1 = map[string]ServiceLoging{
  122. "410":new(loging2.PullAccountLog),
  123. "430":new(loging2.SimulatorStartLog),
  124. //"450":new(loging2.ScriptStartLog),
  125. "450":new(loging2.GameStartLog),
  126. "460":new(loging2.LoginLog),
  127. //"470":new(loging2.EnterGameLog),
  128. "470":new(loging2.EnterMainLog),
  129. "480":new(loging2.FeeLog),
  130. "490":new(loging2.TaskEndLog),
  131. "other":new(loging2.OtherLog),
  132. }*/
  133. var failCacheKey = "%s:fail:list"
  134. func (s *ServiceLogList) CreateLog(c context.Context, request request.AddLogRequest, logical ServiceLoging, status, noLogStatus string) {
  135. var err error
  136. if status == "99" {
  137. err = logical.SuccessLog(c, request)
  138. } else if noLogStatus == "99" {
  139. err = logical.NoLogStatusData(c, request)
  140. } else {
  141. err = logical.FailLog(c, request)
  142. }
  143. if err != nil {
  144. body, _ := json.Marshal(request)
  145. current := time.Now().Format("2006-01-02")
  146. key := fmt.Sprintf(failCacheKey, current)
  147. global.GVA_REDIS.LPush(c, key, body)
  148. global.GVA_LOG.Error("创建失败!: "+string(body), zap.Error(err))
  149. return
  150. }
  151. global.GVA_LOG.Info("创建成功!: " + request.LogUuid + " code: " + strconv.Itoa(request.Coding))
  152. }
  153. func (s *ServiceLogList) CreateFailLog() {
  154. c := context.Background()
  155. current := time.Now().Format("2006-01-02")
  156. key := fmt.Sprintf(failCacheKey, current)
  157. n, err := global.GVA_REDIS.LLen(c, key).Result()
  158. if err != nil {
  159. global.GVA_LOG.Error("corn 获取队列长度失败!"+key, zap.Error(err))
  160. return
  161. }
  162. if n == 0 {
  163. global.GVA_LOG.Info("corn 获取队列长度为0!")
  164. return
  165. }
  166. if n > 300 {
  167. n = 300
  168. }
  169. for i := 0; i < int(n); i++ {
  170. str, err := global.GVA_REDIS.RPop(c, key).Result()
  171. if err != nil {
  172. global.GVA_LOG.Error("rpop获取数据失败!", zap.Error(err))
  173. return
  174. }
  175. var data request.AddLogRequest
  176. err = json.Unmarshal([]byte(str), &data)
  177. if err != nil {
  178. global.GVA_LOG.Error("rpop获取数据失败!", zap.Error(err))
  179. continue
  180. }
  181. l, err := s.logical.GetLogByUuidCoding(data.LogUuid, data.Coding, data.ScriptType)
  182. if err != nil {
  183. global.GVA_LOG.Error("GetLogByUuidCoding获取数据失败!", zap.Error(err))
  184. continue
  185. }
  186. if l.GameId != 0 {
  187. global.GVA_LOG.Info("数据已存在!: " + data.LogUuid + " code: " + strconv.Itoa(data.Coding))
  188. continue
  189. }
  190. coding := strconv.Itoa(data.Coding)
  191. nodeCoding := coding[:3]
  192. status := coding[5:]
  193. noLogStatus := coding[3:5]
  194. var logical ServiceLoging
  195. switch nodeCoding {
  196. case "410":
  197. logical = new(loging2.PullAccountLog)
  198. break
  199. case "430":
  200. logical = new(loging2.SimulatorStartLog)
  201. break
  202. case "450":
  203. logical = new(loging2.GameStartLog)
  204. break
  205. case "460":
  206. logical = new(loging2.LoginLog)
  207. break
  208. case "470":
  209. logical = new(loging2.EnterMainLog)
  210. break
  211. case "480":
  212. logical = new(loging2.FeeLog)
  213. break
  214. default:
  215. logical = new(loging2.OtherLog)
  216. break
  217. }
  218. s.CreateLog(c, data, logical, status, noLogStatus)
  219. global.GVA_LOG.Info("重试!: " + data.LogUuid + " code: " + strconv.Itoa(data.Coding))
  220. }
  221. }