cps.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using dodohold.core;
  2. using CSRedis;
  3. using System.Data;
  4. using System.Threading.Channels;
  5. namespace molilian.core
  6. {
  7. public partial class TkLogCore
  8. {
  9. static string queue_cps_key = "queue:cps_logs";
  10. public static int task_insert_parse_cps_logs(int limit, CSRedisClient redis)
  11. {
  12. int total = 0;
  13. using var connection = DBContext.GetOpenConnection();
  14. connection.Open();
  15. using var transaction = connection.BeginTransaction();
  16. try
  17. {
  18. for (int i = 0; i < limit; i++)
  19. {
  20. var data = redis.LPop<UnionCpsDTO>(queue_cps_key);
  21. if (data == null) break;
  22. if (_test_oaid.Equals(data.oaid) ||
  23. data.ip.StartsWith("127.0.0"))
  24. {
  25. var test_data = data.Convert2Json().Convert2Object<TestUnionCpsDTO>();
  26. connection.Insert(test_data);
  27. }
  28. else
  29. {
  30. //按日保存数据
  31. string daily_table = $"tk_cps_logs_{data.create_time:yyyyMMdd}";
  32. save_cps_parse_logs(data, daily_table, connection, transaction);
  33. }
  34. total++;
  35. }
  36. transaction.Commit();
  37. }
  38. catch (Exception ex)
  39. {
  40. transaction.Rollback();
  41. new LoggerLibrary("TkLogCore_error", "task_insert_parse_cps_logs")
  42. .Info(ex.Message, ex.StackTrace)
  43. .SaveAsync();
  44. throw;
  45. }
  46. finally
  47. {
  48. connection.Close();
  49. }
  50. return total;
  51. }
  52. private static int save_cps_parse_logs(UnionCpsDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  53. {
  54. return new DBContext.Table(connection, tablename)
  55. .Add("end_point", data.end_point)
  56. .Add("channel", data.channel)
  57. .Add("accountId", data.accountId)
  58. .Add("accountName", data.accountName)
  59. .Add("proxy_node", data.proxy_node)
  60. .Add("rawContent", data.rawContent)
  61. .Add("rawContent2", data.rawContent2)
  62. .Add("success", data.success)
  63. .Add("message", data.message)
  64. .Add("reason", data.reason)
  65. .Add("deeplink_url", data.deeplink_url)
  66. .Add("create_time", data.create_time)
  67. .Add("elapsedTime", data.elapsedTime)
  68. .Add("ip", data.ip)
  69. .Add("oaid", data.oaid)
  70. .Add("token", data.token)
  71. .Add("category", data.category)
  72. .Add("pid", data.pid)
  73. .Add("title", data.title)
  74. .Add("itemId", data.itemId)
  75. .Add("couponAmount", data.couponAmount)
  76. .Create(DBContext.InsertType.NORMAL, transaction);
  77. }
  78. public static async Task CpsLogAsync(UnionCpsDTO response, AlimamaPlus? alimamaPlus = null)
  79. {
  80. try
  81. {
  82. var ts = DateTime.Now - response.create_time;
  83. response.elapsedTime = (int)ts.TotalMilliseconds;
  84. _ = RedisHelper.RPushAsync(queue_cps_key, response);
  85. if (response.success)
  86. {
  87. RiskControlCore.CallsIncrBy(TkChannelEnum.cps, response.accountId);
  88. saveClientRequestTotal(response.channel, response.ip, response.oaid);
  89. }
  90. if (!response.ip.StartsWith("127.0.0"))
  91. {
  92. saveCpsCache(response.channel, response.accountId,
  93. response.success, response.message, response.reason);
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. _ = new LoggerLibrary("unionCps", "database_error")
  99. .Info(response.rawContent)
  100. .Info(response.Convert2Json())
  101. .Info(ex.Message, ex.StackTrace)
  102. .SaveAsync();
  103. }
  104. }
  105. private static void saveCpsCache(string channel, int accountId, bool success, string message, string reason)
  106. {
  107. saveAccountCpsCache("all", success, message, reason);
  108. saveAccountCpsCache($"{channel}", success, message, reason);
  109. if (accountId != 0)
  110. {
  111. saveAccountCpsCache($"{channel}_{accountId}", success, message, reason);
  112. }
  113. }
  114. private static void saveAccountCpsCache(string flagName, bool success, string message, string reason)
  115. {
  116. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMM}");
  117. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMdd}");
  118. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMddHH}");
  119. string result = success ? "success" : "fail";
  120. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMM}");
  121. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMdd}");
  122. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMddHH}");
  123. if (!string.IsNullOrEmpty(message))
  124. {
  125. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMM}", message);
  126. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMdd}", message);
  127. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMddHH}", message);
  128. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMM}");
  129. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMdd}");
  130. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMddHH}");
  131. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMM}");
  132. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  133. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  134. }
  135. if (!string.IsNullOrEmpty(reason))
  136. {
  137. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMM}", reason);
  138. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  139. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  140. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMM}");
  141. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMdd}");
  142. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  143. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  144. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  145. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  146. }
  147. }
  148. }
  149. }