logical_log.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. package loging
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/go-redis/redis/v8"
  8. "log-server/global"
  9. "log-server/model/log"
  10. "log-server/model/log/request"
  11. "log-server/service/cache"
  12. "log-server/utils"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. var (
  18. FailStatus = "fail"
  19. OkStatus = "ok"
  20. NoLogStatus = "notStatus"
  21. GameCacheKey = "%s:game"
  22. NodeGameCacheKey = "%s:log"
  23. reportPointsKey = "%s:logUuid:"
  24. logUuidAccountGameId = "%s:logUuid:account:gameId"
  25. ComputerCacheKey = "%s:computer:list"
  26. ComputerPullAccountCacheKey = "%s:computer:%s:pullAccount:"
  27. ComputerEnterMainCacheKey = "%s:computer:%s:enterMain:"
  28. ComputerTaskSuccessCacheKey = "%s:computer:%s:taskSuccess:"
  29. GameFeeRateCacheKey = "%s:game:gameFeeRate:%d"
  30. GameComputerRateCacheKey = "%s:Computer:Rate:%s:"
  31. GamePcFeeRateCacheKey = "%s:gamePc:gameFeeRate:%d:%s"
  32. PcGameFeeRateCacheKey = "%s:PcGame:FeeRate:%s"
  33. OnLineComputerNum = "%s:OnLineComputerNum:"
  34. )
  35. type LogicalLog struct {
  36. Status int // 状态
  37. Request request.AddLogRequest
  38. cache cache.Cache
  39. ScriptType int
  40. }
  41. func (s *LogicalLog) CurrentDate() (current string) {
  42. current = time.Now().Format("2006-01-02")
  43. return
  44. }
  45. func (s *LogicalLog) YesterdayDate() (yesterday string) {
  46. yesterday = time.Now().Add(-time.Hour * 24).Format("2006-01-02")
  47. return
  48. }
  49. func (s *LogicalLog) DataAdd() (err error) {
  50. logInfo, err := s.RepositoryData()
  51. if err != nil {
  52. return err
  53. }
  54. err = s.LogAdd(logInfo)
  55. if err != nil {
  56. return
  57. }
  58. /*if s.Status == 0 {
  59. _ = s.errLogAdd(logInfo)
  60. }*/
  61. err = s.SetGameCache(context.Background(), s.CurrentDate(), logInfo.GameId)
  62. if err != nil {
  63. return
  64. }
  65. err = s.SetAccountGameIdCache(context.Background(), s.CurrentDate(), logInfo)
  66. if err != nil {
  67. return
  68. }
  69. if logInfo.PcCode != "" {
  70. err = s.SetComputerCache(context.Background(), s.CurrentDate(), logInfo.PcCode, logInfo.Operator)
  71. _ = s.StatisticsComputerRateData(context.Background(), logInfo.PcCode, logInfo.GameId)
  72. }
  73. return
  74. }
  75. func (s *LogicalLog) NoLogStatusDataAdd(ctx context.Context) (err error) {
  76. logInfo, err := s.RepositoryData()
  77. if err != nil {
  78. return err
  79. }
  80. err = s.LogAdd(logInfo)
  81. return
  82. }
  83. func (s *LogicalLog) getLogByUuid(uuid string) (log log.Loging, err error) {
  84. db := global.GVA_DB.Table("loging")
  85. db = db.Where("log_uuid = ?", uuid)
  86. db = db.Where("coding = 4101099 or coding = 4103099")
  87. db.First(&log)
  88. return
  89. }
  90. func (s *LogicalLog) GetLogByUuidCoding(uuid string, coding int, scriptType int) (log log.Loging, err error) {
  91. db := global.GVA_DB.Table("loging")
  92. db = db.Where("log_uuid = ?", uuid)
  93. db = db.Where("coding = ?", coding)
  94. db = db.Where("script_type = ?", scriptType)
  95. db.First(&log)
  96. return
  97. }
  98. func (s *LogicalLog) RepositoryData() (*log.Loging, error) {
  99. nodeCode, typeCode, err := s.codeData()
  100. if err != nil {
  101. return nil, err
  102. }
  103. if s.Request.GameId == 0 {
  104. loging, err := s.GetAccountGameIdCache(context.Background(), s.CurrentDate(), s.Request.LogUuid)
  105. if err != nil {
  106. return nil, err
  107. }
  108. if loging.GameId == 0 {
  109. return nil, errors.New("没有找到数据" + s.Request.LogUuid)
  110. }
  111. s.Request.GameId = loging.GameId
  112. s.Request.Account = loging.Account
  113. s.Request.DeviceId = loging.DeviceId
  114. s.Request.ComputerType = loging.ComputerType
  115. s.Request.EnvCode = loging.EnvCode
  116. s.Request.Operator = loging.Operator
  117. s.Request.PcCode = loging.PcCode
  118. s.Request.PcIp = loging.PcIp
  119. s.Request.PcMac = loging.PcMac
  120. s.Request.SimulatorIp = loging.SimulatorIp
  121. s.Request.SimulatorMac = loging.SimulatorMac
  122. s.Request.AccountType = loging.AccountType
  123. s.Request.TaskType = loging.TaskType
  124. }
  125. logInfo := new(log.Loging)
  126. logInfo.Coding = s.Request.Coding
  127. logInfo.GameId = s.Request.GameId
  128. logInfo.Status = s.Status
  129. logInfo.Account = s.Request.Account
  130. logInfo.NodeCoding = nodeCode
  131. logInfo.TypeCoding = typeCode
  132. logInfo.DeviceId = s.Request.DeviceId
  133. logInfo.ComputerType = s.Request.ComputerType
  134. logInfo.EnvCode = s.Request.EnvCode
  135. logInfo.LogUuid = s.Request.LogUuid
  136. logInfo.Operator = s.Request.Operator
  137. logInfo.PcCode = s.Request.PcCode
  138. logInfo.PcIp = s.Request.PcIp
  139. logInfo.SimulatorIp = s.Request.SimulatorIp
  140. logInfo.SimulatorMac = s.Request.SimulatorMac
  141. logInfo.PcMac = s.Request.PcMac
  142. logInfo.AccountType = s.Request.AccountType
  143. logInfo.TaskType = s.Request.TaskType
  144. logInfo.ScriptType = s.ScriptType
  145. logInfo.CreateDate = time.Now().Format("2006-01-02")
  146. logInfo.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  147. return logInfo, err
  148. }
  149. func (s *LogicalLog) codeData() (nodeCode, typeCode int, err error) {
  150. code := strconv.Itoa(s.Request.Coding)
  151. nodeCodeStr := code[:3]
  152. typeCodeStr := code[:5]
  153. nodeCode, err = strconv.Atoi(nodeCodeStr)
  154. if err != nil {
  155. return
  156. }
  157. typeCode, err = strconv.Atoi(typeCodeStr)
  158. return
  159. }
  160. func (s *LogicalLog) LogAdd(l *log.Loging) error {
  161. return global.GVA_DB.Create(&l).Error
  162. }
  163. func (s *LogicalLog) errLogAdd(l *log.Loging) error {
  164. return global.GVA_DB.Table("err_log").Create(&l).Error
  165. }
  166. func (s *LogicalLog) ReportPointsLog(ctx context.Context, request1 request.AddLogRequest) (err error) {
  167. key := reportPointsKey + request1.LogUuid
  168. status, err := global.GVA_REDIS.Exists(ctx, key).Result()
  169. if err != nil {
  170. return
  171. }
  172. var reportPointsData []*request.ReportPointsData
  173. if status == 0 {
  174. reportData := new(request.ReportPointsData)
  175. reportData.Data = request1.ReportPointsData
  176. reportData.Date = time.Now().Format("2006-01-02 15:04:05")
  177. reportPointsData = append(reportPointsData, reportData)
  178. str, _ := json.Marshal(reportPointsData)
  179. global.GVA_REDIS.Set(ctx, key, str, time.Minute*20)
  180. } else {
  181. str, _ := global.GVA_REDIS.Get(ctx, key).Result()
  182. err := json.Unmarshal([]byte(str), &reportPointsData)
  183. if err != nil {
  184. return err
  185. }
  186. reportData := new(request.ReportPointsData)
  187. reportData.Data = request1.ReportPointsData
  188. reportData.Date = time.Now().Format("2006-01-02 15:04:05")
  189. reportPointsData = append(reportPointsData, reportData)
  190. str1, _ := json.Marshal(reportPointsData)
  191. global.GVA_REDIS.Set(ctx, key, str1, time.Minute*20)
  192. }
  193. return err
  194. }
  195. func (s *LogicalLog) ReportPointsLogAdd(ctx context.Context, request1 request.AddLogRequest, status int) (err error) {
  196. key := reportPointsKey + request1.LogUuid
  197. isNull, err := global.GVA_REDIS.Exists(ctx, key).Result()
  198. if err != nil {
  199. return
  200. }
  201. if isNull == 0 {
  202. return
  203. } else {
  204. str, err := global.GVA_REDIS.Get(ctx, key).Result()
  205. if err != nil {
  206. return err
  207. }
  208. logIfo, _ := s.GetAccountGameIdCache(ctx, s.CurrentDate(), request1.LogUuid)
  209. var reportPointsData []*request.ReportPointsData
  210. err = json.Unmarshal([]byte(str), &reportPointsData)
  211. if err != nil {
  212. return err
  213. }
  214. reportPoints := new(log.ReportPointsLog)
  215. reportPoints.ReportPointsData = str
  216. reportPoints.LogUuid = request1.LogUuid
  217. reportPoints.Status = status
  218. reportPoints.Account = logIfo.Account
  219. reportPoints.GameId = logIfo.GameId
  220. reportPoints.Coding = logIfo.Coding
  221. reportPoints.ReportPointsNum = len(reportPointsData)
  222. reportPoints.CreateDate = time.Now().Format("2006-01-02")
  223. reportPoints.CreateTime = time.Now().Format("2006-01-02 15:04:05")
  224. err = global.GVA_DB.Create(&reportPoints).Error
  225. if err != nil {
  226. return err
  227. }
  228. global.GVA_REDIS.Del(ctx, key)
  229. err = s.DelAccountGameIdCache(ctx, s.CurrentDate(), logIfo.LogUuid)
  230. }
  231. return
  232. }
  233. func (s *LogicalLog) ExistsKey(ctx context.Context, key string) (bool, error) {
  234. isNull, err := global.GVA_REDIS.Exists(ctx, key).Result()
  235. if err != nil {
  236. return false, err
  237. }
  238. if isNull == 0 {
  239. return false, nil
  240. } else {
  241. return true, nil
  242. }
  243. }
  244. func (s *LogicalLog) ExistsHsKey(ctx context.Context, key string, field string) (bool, error) {
  245. isNull, err := global.GVA_REDIS.HExists(ctx, key, field).Result()
  246. if err != nil {
  247. return false, err
  248. }
  249. return isNull, err
  250. }
  251. // 前3个编号
  252. func (s *LogicalLog) NodeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  253. node := code[:3]
  254. err = s.LogSetNum(ctx, date, gameId, node, status, taskType)
  255. return
  256. }
  257. // 前5个编号
  258. func (s *LogicalLog) TypeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  259. node := code[:5]
  260. err = s.LogSetNum(ctx, date, gameId, node, status, taskType)
  261. return
  262. }
  263. // 整个编号
  264. func (s *LogicalLog) CodeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  265. err = s.LogSetNum(ctx, date, gameId, code, status, taskType)
  266. return
  267. }
  268. // 前5个编号和整个编号缓存
  269. func (s *LogicalLog) PartTypeLogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  270. node := code[:5]
  271. err = s.LogSetNum(ctx, date, gameId, node, status, taskType)
  272. if err != nil {
  273. return
  274. }
  275. err = s.LogSetNum(ctx, date, gameId, code, status, taskType)
  276. return
  277. }
  278. func (s *LogicalLog) LogSetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (err error) {
  279. strGameId := strconv.Itoa(gameId)
  280. nodeGameCacheKey := fmt.Sprintf(NodeGameCacheKey, date)
  281. key := nodeGameCacheKey + ":" + strGameId + ":" + strconv.Itoa(taskType) + ":" + status + ":" + code
  282. err = s.SetCacheNum(ctx, key)
  283. return
  284. }
  285. func (s *LogicalLog) SetCacheNum(ctx context.Context, key string) (err error) {
  286. bl, err := s.ExistsKey(ctx, key)
  287. if err != nil {
  288. return err
  289. }
  290. if !bl {
  291. err = global.GVA_REDIS.Set(ctx, key, 1, time.Hour*48).Err()
  292. return err
  293. } else {
  294. err = global.GVA_REDIS.Incr(ctx, key).Err()
  295. return err
  296. }
  297. }
  298. func (s *LogicalLog) NodeLogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) {
  299. node := code[:3]
  300. num, err = s.LogGetNum(ctx, date, gameId, node, status, taskType)
  301. return
  302. }
  303. func (s *LogicalLog) TypeLogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) {
  304. node := code[:5]
  305. num, err = s.LogGetNum(ctx, date, gameId, node, status, taskType)
  306. return
  307. }
  308. func (s *LogicalLog) CodeLogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) {
  309. num, err = s.LogGetNum(ctx, date, gameId, code, status, taskType)
  310. return
  311. }
  312. func (s *LogicalLog) LogGetNum(ctx context.Context, date string, gameId int, code string, status string, taskType int) (num int, err error) {
  313. strGameId := strconv.Itoa(gameId)
  314. nodeGameCacheKey := fmt.Sprintf(NodeGameCacheKey, date)
  315. key := nodeGameCacheKey + ":" + strGameId + ":" + strconv.Itoa(taskType) + ":" + status + ":" + code
  316. num, err = s.GetCacheNum(ctx, key)
  317. return
  318. }
  319. func (s *LogicalLog) GetCacheNum(ctx context.Context, key string) (num int, err error) {
  320. num, err = global.GVA_REDIS.Get(ctx, key).Int()
  321. if err != nil {
  322. if err == redis.Nil {
  323. return 0, nil
  324. }
  325. return 0, err
  326. }
  327. return num, err
  328. }
  329. func (s *LogicalLog) SetGameCache(ctx context.Context, date string, gameId int) (err error) {
  330. key := fmt.Sprintf(GameCacheKey, date)
  331. _, err = global.GVA_REDIS.HSet(ctx, key, gameId, 1).Result()
  332. if err != nil {
  333. return err
  334. }
  335. return err
  336. }
  337. func (s *LogicalLog) GetGameCache(ctx context.Context, date string) (mps map[string]string, err error) {
  338. key := fmt.Sprintf(GameCacheKey, date)
  339. mps, err = global.GVA_REDIS.HGetAll(ctx, key).Result()
  340. if err != nil {
  341. return mps, err
  342. }
  343. return mps, err
  344. }
  345. func (s *LogicalLog) SetAccountGameIdCache(ctx context.Context, date string, logInfo *log.Loging) (err error) {
  346. key := fmt.Sprintf(logUuidAccountGameId, date)
  347. value, _ := json.Marshal(logInfo)
  348. _, err = global.GVA_REDIS.HGet(ctx, key, logInfo.LogUuid).Result()
  349. if err != nil {
  350. if err == redis.Nil {
  351. _, err = global.GVA_REDIS.HSet(ctx, key, logInfo.LogUuid, value).Result()
  352. if err != nil {
  353. return err
  354. }
  355. } else {
  356. return err
  357. }
  358. }
  359. return err
  360. }
  361. func (s *LogicalLog) GetAccountGameIdCache(ctx context.Context, date string, uuid string) (logInfo log.Loging, err error) {
  362. key := fmt.Sprintf(logUuidAccountGameId, date)
  363. data, err := global.GVA_REDIS.HGet(ctx, key, uuid).Result()
  364. if err != nil {
  365. if err == redis.Nil {
  366. logInfo, err = s.getLogByUuid(uuid)
  367. return logInfo, nil
  368. }
  369. return logInfo, err
  370. }
  371. err = json.Unmarshal([]byte(data), &logInfo)
  372. return
  373. }
  374. func (s *LogicalLog) DelAccountGameIdCache(ctx context.Context, date string, uuid string) (err error) {
  375. key := fmt.Sprintf(logUuidAccountGameId, date)
  376. _, err = global.GVA_REDIS.HDel(ctx, key, uuid).Result()
  377. return
  378. }
  379. func (s *LogicalLog) GetGameStatisticsCacheKeys(ctx context.Context, date string, gameId int, taskType int) (keys []string, err error) {
  380. strGameId := strconv.Itoa(gameId)
  381. nodeGameCacheKey := fmt.Sprintf(NodeGameCacheKey, date)
  382. key := nodeGameCacheKey + ":" + strGameId + ":" + strconv.Itoa(taskType) + ":*"
  383. keys, err = global.GVA_REDIS.Keys(ctx, key).Result()
  384. return
  385. }
  386. func (s *LogicalLog) ByCacheKeyGetEndNode(key string) (code string) {
  387. str := strings.Split(key, ":")
  388. code = str[len(str)-1]
  389. return
  390. }
  391. func (s *LogicalLog) SetUuidCodeCache(ctx context.Context, date string, uuid string, code int, gameId int) (err error) {
  392. key := fmt.Sprintf(reportPointsKey, date) + strconv.Itoa(gameId) + ":" + uuid
  393. _, err = global.GVA_REDIS.HSet(ctx, key, code, 1).Result()
  394. if err != nil {
  395. return err
  396. }
  397. return err
  398. }
  399. func (s *LogicalLog) ExistsUuidCodeCache(ctx context.Context, date string, uuid string, code int, gameId int) (b bool, err error) {
  400. key := fmt.Sprintf(reportPointsKey, date) + strconv.Itoa(gameId) + ":" + uuid
  401. b, err = s.ExistsHsKey(ctx, key, strconv.Itoa(code))
  402. if err != nil {
  403. return false, err
  404. }
  405. return
  406. }
  407. func (s *LogicalLog) GetCacheKeys(ctx context.Context, keys string) (key []string, err error) {
  408. key, err = global.GVA_REDIS.Keys(ctx, keys).Result()
  409. return
  410. }
  411. func (s *LogicalLog) DelStatisticsNumCache(ctx context.Context, date string, gameId int) (err error) {
  412. key1FailStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "1" + ":" + FailStatus + ":*"
  413. key1NoLogStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "1" + ":" + NoLogStatus + ":*"
  414. key1OkStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "1" + ":" + OkStatus + ":*"
  415. key0FailStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "0" + ":" + FailStatus + ":*"
  416. key0NoLogStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "0" + ":" + NoLogStatus + ":*"
  417. key0OkStatus := fmt.Sprintf(NodeGameCacheKey, date) + ":" + strconv.Itoa(gameId) + ":" + "0" + ":" + OkStatus + ":*"
  418. fmt.Println(key1FailStatus)
  419. fmt.Println(key1NoLogStatus)
  420. fmt.Println(key1OkStatus)
  421. fmt.Println(key0FailStatus)
  422. fmt.Println(key0NoLogStatus)
  423. fmt.Println(key0OkStatus)
  424. err = s.cache.DelBatheCache(ctx, key1FailStatus)
  425. err = s.cache.DelBatheCache(ctx, key1NoLogStatus)
  426. err = s.cache.DelBatheCache(ctx, key1OkStatus)
  427. err = s.cache.DelBatheCache(ctx, key0FailStatus)
  428. err = s.cache.DelBatheCache(ctx, key0NoLogStatus)
  429. err = s.cache.DelBatheCache(ctx, key0OkStatus)
  430. if err != nil {
  431. return err
  432. }
  433. return
  434. }
  435. func (s *LogicalLog) DelUuidCodeCache(ctx context.Context, date string, gameId int) (err error) {
  436. key := fmt.Sprintf(reportPointsKey, date) + strconv.Itoa(gameId) + ":*"
  437. keys, err := global.GVA_REDIS.Keys(ctx, key).Result()
  438. for _, v := range keys {
  439. err = s.cache.DelBatheHsCache(ctx, v)
  440. }
  441. if err != nil {
  442. return err
  443. }
  444. return err
  445. }
  446. // 统计电脑使用的记录
  447. func (s *LogicalLog) SetComputerCache(ctx context.Context, date string, pcCode, operator string) (err error) {
  448. key := fmt.Sprintf(ComputerCacheKey, date)
  449. _, err = global.GVA_REDIS.HSet(ctx, key, pcCode, operator).Result()
  450. if err != nil {
  451. return err
  452. }
  453. return err
  454. }
  455. // 获取统计电脑使用的记录
  456. func (s *LogicalLog) GetComputerCache(ctx context.Context, date string) (mps map[string]string, err error) {
  457. key := fmt.Sprintf(ComputerCacheKey, date)
  458. mps, err = global.GVA_REDIS.HGetAll(ctx, key).Result()
  459. if err != nil {
  460. return mps, err
  461. }
  462. return mps, err
  463. }
  464. // 统计电脑拉取账号的数量
  465. func (s *LogicalLog) SetComputerPullAccountNumCache(ctx context.Context, date string, pcCode string, gameId int) (err error) {
  466. key := fmt.Sprintf(ComputerPullAccountCacheKey, date, pcCode) + strconv.Itoa(gameId)
  467. err = s.cache.SetCacheNum(ctx, key)
  468. return err
  469. }
  470. // 获取统计电脑拉取账号的数量
  471. func (s *LogicalLog) GetComputerPullAccountNumCache(ctx context.Context, date string, pcCode string) (mps map[string]int, err error) {
  472. key := fmt.Sprintf(ComputerPullAccountCacheKey, date, pcCode)
  473. mps, err = s.GetByPcCodeGameNumCache(ctx, key)
  474. if len(mps) == 0 {
  475. return
  476. }
  477. return
  478. }
  479. // 统计电脑进入主线的数量
  480. func (s *LogicalLog) SetComputerEnterMainNumCache(ctx context.Context, date string, pcCode string, gameId int) (err error) {
  481. key := fmt.Sprintf(ComputerEnterMainCacheKey, date, pcCode) + strconv.Itoa(gameId)
  482. err = s.cache.SetCacheNum(ctx, key)
  483. return err
  484. }
  485. // 获取统计电脑进入主线的数量
  486. func (s *LogicalLog) GetComputerEnterMainNumCache(ctx context.Context, date string, pcCode string) (mps map[string]int, err error) {
  487. key := fmt.Sprintf(ComputerEnterMainCacheKey, date, pcCode)
  488. mps, err = s.GetByPcCodeGameNumCache(ctx, key)
  489. if len(mps) == 0 {
  490. return
  491. }
  492. return
  493. }
  494. // 统计电脑任务成功的数量
  495. func (s *LogicalLog) SetComputerTaskSuccessNumCache(ctx context.Context, date string, pcCode string, gameId int) (err error) {
  496. key := fmt.Sprintf(ComputerTaskSuccessCacheKey, date, pcCode) + strconv.Itoa(gameId)
  497. err = s.cache.SetCacheNum(ctx, key)
  498. return err
  499. }
  500. // 获取统计电脑任务成功的数量
  501. func (s *LogicalLog) GetComputerTaskSuccessNumCache(ctx context.Context, date string, pcCode string) (mps map[string]int, err error) {
  502. key := fmt.Sprintf(ComputerTaskSuccessCacheKey, date, pcCode)
  503. mps, err = s.GetByPcCodeGameNumCache(ctx, key)
  504. if len(mps) == 0 {
  505. return
  506. }
  507. return
  508. }
  509. // 通过电脑code获取游戏缓存的数量
  510. func (s *LogicalLog) GetByPcCodeGameNumCache(ctx context.Context, key string) (mps map[string]int, err error) {
  511. key += "*"
  512. data, err := s.cache.GetCacheKeys(ctx, key)
  513. if len(data) == 0 {
  514. return
  515. }
  516. mps = make(map[string]int)
  517. for _, k := range data {
  518. gameId := s.ByCacheKeyGetEndNode(k)
  519. num, _ := s.cache.GetCacheNum(ctx, k)
  520. mps[gameId] = num
  521. }
  522. return
  523. }
  524. // 统计游戏付费效率的预埋数据
  525. func (s *LogicalLog) StatisticsFeeRateData(ctx context.Context, gameId int, logUuid string) {
  526. key := fmt.Sprintf(GameFeeRateCacheKey, s.CurrentDate(), gameId)
  527. z := &redis.Z{
  528. Member: logUuid,
  529. Score: float64(time.Now().UnixNano() / 1e6),
  530. }
  531. global.GVA_REDIS.ZAdd(ctx, key, z)
  532. }
  533. // 获取统计游戏付费效率的数量
  534. func (s *LogicalLog) GetStatisticsFeeRate(ctx context.Context, gameId int) (num int) {
  535. key := fmt.Sprintf(GameFeeRateCacheKey, s.CurrentDate(), gameId)
  536. z, err := global.GVA_REDIS.ZRangeWithScores(ctx, key, -1, -1).Result()
  537. if err != nil {
  538. return
  539. }
  540. if len(z) == 0 {
  541. return
  542. }
  543. end := z[0].Score
  544. start := z[0].Score - 30*60*1000
  545. op := redis.ZRangeBy{
  546. Min: strconv.Itoa(int(start)),
  547. Max: strconv.Itoa(int(end)),
  548. }
  549. i, err := global.GVA_REDIS.ZRangeByScore(ctx, key, &op).Result()
  550. if err != nil {
  551. return
  552. }
  553. num = len(i)
  554. return
  555. }
  556. // 获取统计单台电脑单个游戏付费效率的预埋数据
  557. func (s *LogicalLog) GetStatisticsPcFeeRate(ctx context.Context, pcCode string, gameId int) (num int) {
  558. key := fmt.Sprintf(GamePcFeeRateCacheKey, s.CurrentDate(), gameId, pcCode)
  559. z, err := global.GVA_REDIS.ZRangeWithScores(ctx, key, -1, -1).Result()
  560. if err != nil {
  561. return
  562. }
  563. if len(z) == 0 {
  564. return
  565. }
  566. end := z[0].Score
  567. start := z[0].Score - 30*60*1000
  568. op := redis.ZRangeBy{
  569. Min: strconv.Itoa(int(start)),
  570. Max: strconv.Itoa(int(end)),
  571. }
  572. i, err := global.GVA_REDIS.ZRangeByScore(ctx, key, &op).Result()
  573. if err != nil {
  574. return
  575. }
  576. num = len(i)
  577. return
  578. }
  579. // 统计单台电脑单个游戏付费效率的预埋数据
  580. func (s *LogicalLog) StatisticsPcFeeRateData(ctx context.Context, pcCode string, logUuid string, gameId int) {
  581. key := fmt.Sprintf(GamePcFeeRateCacheKey, s.CurrentDate(), gameId, pcCode)
  582. z := &redis.Z{
  583. Member: logUuid,
  584. Score: float64(time.Now().UnixNano() / 1e6),
  585. }
  586. global.GVA_REDIS.ZAdd(ctx, key, z)
  587. }
  588. // 统计电脑效率的预埋数据
  589. func (s *LogicalLog) StatisticsComputerRateData(ctx context.Context, pcCode string, gameId int) (err error) {
  590. hour := time.Now().Hour()
  591. key := fmt.Sprintf(GameComputerRateCacheKey, s.CurrentDate(), pcCode)
  592. key = key + strconv.Itoa(hour)
  593. err = s.cache.SetCacheStr(ctx, key, gameId)
  594. return
  595. }
  596. // 统计电脑效率的预埋数据
  597. func (s *LogicalLog) GetStatisticsComputerRate(ctx context.Context, pcCode string) (num int) {
  598. key := fmt.Sprintf(GameComputerRateCacheKey, s.CurrentDate(), pcCode)
  599. key = key + "*"
  600. data, _ := s.cache.GetCacheKeys(ctx, key)
  601. hour := time.Now().Hour()
  602. num = hour + 1 - len(data)
  603. return
  604. }
  605. // 请求机房任务数据
  606. func (s *LogicalLog) RequestJfRoom() (result []byte, err error) {
  607. today := time.Now().Format("2006-01-02")
  608. jfurl := "http://xjf.lianyou.fun:8099/v1/task_statistics"
  609. jfparams := map[string]string{
  610. "query": "date:" + today + ",type:machine",
  611. }
  612. result, err = utils.HttpGet(jfurl, jfparams)
  613. return
  614. }
  615. // 请求机房数据接口数据
  616. func (s *LogicalLog) RequestTaskData() (result []byte, err error) {
  617. today := time.Now().Format("2006-01-02")
  618. jfurl := "http://xjf.lianyou.fun:8118/data/taskDateLog"
  619. jfparams := map[string]string{
  620. "date": today,
  621. }
  622. result, err = utils.HttpGet(jfurl, jfparams)
  623. return
  624. }
  625. // 统计在线电脑数据
  626. func (s *LogicalLog) SetOnlineComputerNumCache(ctx context.Context, date string, pcCode string, operator string) (err error) {
  627. key := fmt.Sprintf(OnLineComputerNum, date)
  628. key += pcCode
  629. err = s.cache.SetCacheStr(ctx, key, operator)
  630. return err
  631. }
  632. // 获取在线电脑数据
  633. func (s *LogicalLog) GetOnlineComputerNumCache(ctx context.Context, date string) (mps map[string]string, err error) {
  634. key := fmt.Sprintf(OnLineComputerNum, date)
  635. mps, err = s.GetAllOnlineComputerNumCache(ctx, key)
  636. if len(mps) == 0 {
  637. return
  638. }
  639. return
  640. }
  641. // 获取所有在线电脑
  642. func (s *LogicalLog) GetAllOnlineComputerNumCache(ctx context.Context, key string) (mps map[string]string, err error) {
  643. key += "*"
  644. data, err := s.cache.GetCacheKeys(ctx, key)
  645. if len(data) == 0 {
  646. return
  647. }
  648. mps = make(map[string]string)
  649. for _, k := range data {
  650. pcCode := s.ByCacheKeyGetEndNode(k)
  651. operator, _ := s.cache.GetCacheStr(ctx, k)
  652. mps[pcCode] = operator
  653. }
  654. return
  655. }