cps.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using dodohold.core;
  2. using CSRedis;
  3. using System.Data;
  4. namespace molilian.core
  5. {
  6. public partial class TkLogCore
  7. {
  8. static string queue_cps_key = "queue:cps_logs";
  9. public static int task_insert_parse_cps_logs(int limit, CSRedisClient redis)
  10. {
  11. int total = 0;
  12. using var connection = DBContext.GetOpenConnection();
  13. connection.Open();
  14. using var transaction = connection.BeginTransaction();
  15. try
  16. {
  17. for (int i = 0; i < limit; i++)
  18. {
  19. var data = redis.LPop<UnionCpsDTO>(queue_cps_key);
  20. if (data == null) break;
  21. if (_test_oaid.Equals(data.oaid) ||
  22. data.ip.Contains("127.0.0"))
  23. {
  24. var test_data = data.Convert2Json().Convert2Object<TestUnionCpsDTO>();
  25. connection.Insert(test_data);
  26. }
  27. else
  28. {
  29. connection.Insert(data);
  30. if (data.success)
  31. {
  32. var success_data = data.Convert2Json().Convert2Object<SuccessUnionCpsDTO>();
  33. connection.Insert(success_data);
  34. }
  35. }
  36. total++;
  37. }
  38. transaction.Commit();
  39. }
  40. catch (Exception ex)
  41. {
  42. transaction.Rollback();
  43. new LoggerLibrary("TkLogCore_error", "task_insert_parse_cps_logs")
  44. .Info(ex.Message, ex.StackTrace)
  45. .SaveAsync();
  46. throw;
  47. }
  48. finally
  49. {
  50. connection.Close();
  51. }
  52. return total;
  53. }
  54. public static async Task CpsLogAsync(UnionCpsDTO response, AlimamaPlus? alimamaPlus = null)
  55. {
  56. try
  57. {
  58. var ts = DateTime.Now - response.create_time;
  59. response.elapsedTime = (int)ts.TotalMilliseconds;
  60. _ = RedisHelper.RPushAsync(queue_cps_key, response);
  61. if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  62. if (!response.ip.Contains("127.0.0"))
  63. {
  64. saveCpsCache(response.channel, response.accountId,
  65. response.success, response.message, response.reason);
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. _ = new LoggerLibrary("unionCps", "database_error")
  71. .Info(response.rawContent)
  72. .Info(response.Convert2Json())
  73. .Info(ex.Message, ex.StackTrace)
  74. .SaveAsync();
  75. }
  76. }
  77. private static void saveCpsCache(string channel, int accountId, bool success, string message, string reason)
  78. {
  79. saveAccountCpsCache("all", success, message, reason);
  80. saveAccountCpsCache($"{channel}", success, message, reason);
  81. if (accountId != 0)
  82. {
  83. saveAccountCpsCache($"{channel}_{accountId}", success, message, reason);
  84. }
  85. }
  86. private static void saveAccountCpsCache(string flagName, bool success, string message, string reason)
  87. {
  88. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMM}");
  89. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMdd}");
  90. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMddHH}");
  91. string result = success ? "success" : "fail";
  92. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMM}");
  93. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMdd}");
  94. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMddHH}");
  95. if (!string.IsNullOrEmpty(message))
  96. {
  97. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMM}", message);
  98. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMdd}", message);
  99. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMddHH}", message);
  100. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMM}");
  101. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMdd}");
  102. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMddHH}");
  103. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMM}");
  104. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  105. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  106. }
  107. if (!string.IsNullOrEmpty(reason))
  108. {
  109. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMM}", reason);
  110. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  111. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  112. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMM}");
  113. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMdd}");
  114. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  115. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  116. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  117. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  118. }
  119. }
  120. }
  121. }