cps.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 async Task<int> InsertCpsLogAsync(int limit, YunhuiKit.RedisClient 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 = await redis.LPopAsync<UnionCpsDTO>(queue_cps_key);
  21. if (data == null) break;
  22. if (TestParseCore.InWhitelist(data.ip, data.oaid))
  23. {
  24. var test_data = data.Convert2Json().Convert2Object<TestUnionCpsDTO>();
  25. connection.Insert(test_data);
  26. }
  27. else
  28. {
  29. //按日保存数据
  30. string daily_table = $"tk_cps_logs_{data.create_time:yyyyMMdd}";
  31. save_cps_parse_logs(data, daily_table, connection, transaction);
  32. }
  33. total++;
  34. }
  35. transaction.Commit();
  36. }
  37. catch (Exception ex)
  38. {
  39. transaction.Rollback();
  40. new LoggerLibrary("TkLogCore_error", "task_insert_parse_cps_logs")
  41. .Info(ex.Message, ex.StackTrace)
  42. .SaveAsync();
  43. throw;
  44. }
  45. finally
  46. {
  47. connection.Close();
  48. }
  49. return total;
  50. }
  51. public static int task_insert_parse_cps_logs(int limit, CSRedisClient redis)
  52. {
  53. int total = 0;
  54. using var connection = DBContext.GetOpenConnection();
  55. connection.Open();
  56. using var transaction = connection.BeginTransaction();
  57. try
  58. {
  59. for (int i = 0; i < limit; i++)
  60. {
  61. var data = redis.LPop<UnionCpsDTO>(queue_cps_key);
  62. if (data == null) break;
  63. if (TestParseCore.InWhitelist(data.ip, data.oaid))
  64. {
  65. var test_data = data.Convert2Json().Convert2Object<TestUnionCpsDTO>();
  66. connection.Insert(test_data);
  67. }
  68. else
  69. {
  70. //按日保存数据
  71. string daily_table = $"tk_cps_logs_{data.create_time:yyyyMMdd}";
  72. save_cps_parse_logs(data, daily_table, connection, transaction);
  73. }
  74. total++;
  75. }
  76. transaction.Commit();
  77. }
  78. catch (Exception ex)
  79. {
  80. transaction.Rollback();
  81. new LoggerLibrary("TkLogCore_error", "task_insert_parse_cps_logs")
  82. .Info(ex.Message, ex.StackTrace)
  83. .SaveAsync();
  84. throw;
  85. }
  86. finally
  87. {
  88. connection.Close();
  89. }
  90. return total;
  91. }
  92. private static int save_cps_parse_logs(UnionCpsDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  93. {
  94. return new DBContext.Table(connection, tablename)
  95. .Add("end_point", data.end_point)
  96. .Add("channel", data.channel)
  97. .Add("accountId", data.accountId)
  98. .Add("accountName", data.accountName)
  99. .Add("proxy_node", data.proxy_node)
  100. .Add("rawContent", data.rawContent)
  101. .Add("rawContent2", data.rawContent2)
  102. .Add("success", data.success)
  103. .Add("message", data.message)
  104. .Add("reason", data.reason)
  105. .Add("deeplink_url", data.deeplink_url)
  106. .Add("create_time", data.create_time)
  107. .Add("elapsedTime", data.elapsedTime)
  108. .Add("ip", data.ip)
  109. .Add("oaid", data.oaid)
  110. .Add("token", data.token)
  111. .Add("category", data.category)
  112. .Add("pid", data.pid)
  113. .Add("title", data.title)
  114. .Add("sub_title", data.sub_title)
  115. .Add("itemId", data.itemId)
  116. .Add("couponAmount", data.couponAmount)
  117. .Create(DBContext.InsertType.NORMAL, transaction);
  118. }
  119. public static async Task CpsLogAsync(UnionCpsDTO response, AlimamaPlus? alimamaPlus = null)
  120. {
  121. try
  122. {
  123. var ts = DateTime.Now - response.create_time;
  124. response.elapsedTime = (int)ts.TotalMilliseconds;
  125. _ = RedisHelper.RPushAsync(queue_cps_key, response);
  126. if (response.success)
  127. {
  128. await RiskControlCore.CallsIncrByAsync(TkChannelEnum.cps, response.accountId);
  129. await saveClientRequestTotalAsync(response.channel, response.ip, response.oaid);
  130. }
  131. if (!TestParseCore.InWhitelist(response.ip, response.oaid))
  132. {
  133. saveCpsCache(response.channel, response.accountId,
  134. response.success, response.message, response.reason);
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. _ = new LoggerLibrary("unionCps", "database_error")
  140. .Info(response.rawContent)
  141. .Info(response.Convert2Json())
  142. .Info(ex.Message, ex.StackTrace)
  143. .SaveAsync();
  144. }
  145. }
  146. private static void saveCpsCache(string channel, int accountId, bool success, string message, string reason)
  147. {
  148. saveAccountCpsCache("all", success, message, reason);
  149. saveAccountCpsCache($"{channel}", success, message, reason);
  150. if (accountId != 0)
  151. {
  152. saveAccountCpsCache($"{channel}_{accountId}", success, message, reason);
  153. }
  154. }
  155. private static void saveAccountCpsCache(string flagName, bool success, string message, string reason)
  156. {
  157. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMM}");
  158. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMdd}");
  159. RedisHelper.IncrBy($":cps_total:{flagName}:{DateTime.Now:yyyyMMddHH}");
  160. string result = success ? "success" : "fail";
  161. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMM}");
  162. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMdd}");
  163. RedisHelper.IncrBy($":cps_total:{flagName}:{result}:{DateTime.Now:yyyyMMddHH}");
  164. if (!string.IsNullOrEmpty(message))
  165. {
  166. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMM}", message);
  167. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMdd}", message);
  168. RedisHelper.SAdd($":cps_total:{flagName}:message:{DateTime.Now:yyyyMMddHH}", message);
  169. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMM}");
  170. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMdd}");
  171. RedisHelper.IncrBy($":cps_total:{flagName}:{message}:{DateTime.Now:yyyyMMddHH}");
  172. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMM}");
  173. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  174. RedisHelper.IncrBy($":cps_total:{flagName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  175. }
  176. if (!string.IsNullOrEmpty(reason))
  177. {
  178. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMM}", reason);
  179. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  180. RedisHelper.SAdd($":cps_total:{flagName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  181. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMM}");
  182. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMdd}");
  183. RedisHelper.IncrBy($":cps_total:{flagName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  184. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  185. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  186. RedisHelper.IncrBy($":cps_total:{flagName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  187. }
  188. }
  189. }
  190. }