base.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  10. using System.Net;
  11. using System.Security.Cryptography;
  12. using Spire.Pdf.Exporting.XPS.Schema;
  13. using System.Xml.Linq;
  14. using static QRCoder.PayloadGenerator;
  15. using TencentCloud.Ssl.V20191205.Models;
  16. using CSRedis;
  17. using System.Data;
  18. using TencentCloud.Omics.V20221128.Models;
  19. using TencentCloud.Csip.V20221121.Models;
  20. using COSXML.Network;
  21. using System.Security.Policy;
  22. using System.Diagnostics;
  23. using TencentCloud.Ecm.V20190719.Models;
  24. using YunhuiKit;
  25. namespace molilian.core
  26. {
  27. public partial class TkLogCore
  28. {
  29. public static bool save_dailys_log = false;
  30. public static string _test_oaid = "3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2";
  31. static TkLogCore()
  32. {
  33. int flag = RedisHelper.Get<int>("turn:save_dailys_log");
  34. if (flag == 1) save_dailys_log = true;
  35. }
  36. private static readonly SemaphoreSlim semaphore = new SemaphoreSlim(10, 10);
  37. public static async Task<int> BatchInsertLogDBAsync(int limit)
  38. {
  39. await semaphore.WaitAsync(); // 等待获取锁
  40. try
  41. {
  42. var result = await Task.Run(() =>
  43. {
  44. return EndPointCore.ProcessEndPointNodes<int>(node =>
  45. {
  46. if (!node.is_public_api) return 0;
  47. if (CenterHub.IsCenter)
  48. {
  49. if (node.is_coupon_api) return 0;
  50. }
  51. else
  52. {
  53. if (!node.is_coupon_api) return 0;
  54. }
  55. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  56. #if DEBUG
  57. switch (node.name)
  58. {
  59. case "bj":
  60. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  61. break;
  62. case "gz":
  63. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  64. break;
  65. case "coupon1":
  66. node.redis_server = "123.56.185.166:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  67. break;
  68. default: return 0;
  69. }
  70. #endif
  71. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  72. return BatchInsertLogDB(limit, redis);
  73. });
  74. });
  75. return result.Sum();
  76. }
  77. finally
  78. {
  79. semaphore.Release(); // 释放锁,允许下一个任务执行
  80. }
  81. }
  82. public static int BatchInsertLogDB2(int limit, CSRedisClient redis)
  83. {
  84. int total = 0;
  85. using var connection = DBContext.GetOpenConnection();
  86. connection.Open();
  87. using var transaction = connection.BeginTransaction();
  88. try
  89. {
  90. Stopwatch stopwatch = new Stopwatch(); // 创建一个计时器
  91. LoggerLibrary log = new LoggerLibrary("debug", "BatchInsertLogDB"); // 创建日志对象
  92. //第三方接口写入日志
  93. stopwatch.Start();
  94. int taskTotal = task_insert_tk_logs(limit, redis);
  95. total += taskTotal;
  96. stopwatch.Stop();
  97. log.Info($"task_insert_tk_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  98. log.SaveAsync();
  99. stopwatch.Reset();
  100. stopwatch.Start();
  101. taskTotal = task_insert_parse_tb_logs(limit, redis);
  102. total += taskTotal;
  103. stopwatch.Stop();
  104. log.Info($"task_insert_parse_tb_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  105. log.SaveAsync();
  106. stopwatch.Reset();
  107. stopwatch.Start();
  108. taskTotal = task_insert_parse_jd_logs(limit, redis);
  109. total += taskTotal;
  110. stopwatch.Stop();
  111. log.Info($"task_insert_parse_jd_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  112. log.SaveAsync();
  113. stopwatch.Reset();
  114. stopwatch.Start();
  115. taskTotal = task_insert_parse_pdd_logs(limit, redis);
  116. total += taskTotal;
  117. stopwatch.Stop();
  118. log.Info($"task_insert_parse_pdd_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  119. log.SaveAsync();
  120. stopwatch.Reset();
  121. stopwatch.Start();
  122. taskTotal = task_insert_parse_dy_logs(limit, redis);
  123. total += taskTotal;
  124. stopwatch.Stop();
  125. log.Info($"task_insert_parse_dy_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  126. log.SaveAsync();
  127. stopwatch.Reset();
  128. stopwatch.Start();
  129. taskTotal = task_insert_parse_tool_logs(limit, redis);
  130. total += taskTotal;
  131. stopwatch.Stop();
  132. log.Info($"task_insert_parse_tool_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  133. log.SaveAsync();
  134. stopwatch.Reset();
  135. stopwatch.Start();
  136. taskTotal = task_insert_parse_deeplink_logs(limit, redis);
  137. total += taskTotal;
  138. stopwatch.Stop();
  139. log.Info($"task_insert_parse_deeplink_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  140. log.SaveAsync();
  141. stopwatch.Reset();
  142. stopwatch.Start();
  143. taskTotal = task_insert_parse_coupon_logs(limit, redis);
  144. total += taskTotal;
  145. stopwatch.Stop();
  146. log.Info($"task_insert_parse_coupon_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  147. log.SaveAsync();
  148. stopwatch.Reset();
  149. stopwatch.Start();
  150. taskTotal = task_insert_parse_cps_logs(limit, redis);
  151. total += taskTotal;
  152. stopwatch.Stop();
  153. log.Info($"task_insert_parse_cps_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  154. log.SaveAsync();
  155. stopwatch.Reset();
  156. stopwatch.Start();
  157. taskTotal = task_insert_promotion_img_logs(limit, redis);
  158. total += taskTotal;
  159. stopwatch.Stop();
  160. log.Info($"task_insert_promotion_img_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  161. log.SaveAsync();
  162. stopwatch.Reset();
  163. stopwatch.Start();
  164. taskTotal = task_insert_parse_ks_logs(limit, redis);
  165. total += taskTotal;
  166. stopwatch.Stop();
  167. log.Info($"task_insert_parse_ks_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  168. log.SaveAsync();
  169. stopwatch.Reset();
  170. stopwatch.Start();
  171. taskTotal = task_insert_parse_dy_logs(limit, redis);
  172. total += taskTotal;
  173. stopwatch.Stop();
  174. log.Info($"task_insert_parse_dy_logs 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  175. log.SaveAsync();
  176. stopwatch.Reset();
  177. transaction.Commit();
  178. }
  179. catch (Exception ex)
  180. {
  181. transaction.Rollback();
  182. _ = new LoggerLibrary("database_error", "parse_log")
  183. .Info(ex.Message, ex.StackTrace)
  184. .SaveAsync();
  185. NotifyCore.Notify(new NifyMessage
  186. {
  187. message = $"【写入日志异常】\n{ex.Message}\n{ex.StackTrace}",
  188. priority = NifyMessagePriority.high,
  189. tags = ["red_circle"]
  190. });
  191. }
  192. finally
  193. {
  194. connection.Close();
  195. }
  196. return total;
  197. }
  198. public static int BatchInsertLogDB(int limit, CSRedisClient redis)
  199. {
  200. int total = 0;
  201. //using var connection = DBContext.GetOpenConnection();
  202. //connection.Open();
  203. //using var transaction = connection.BeginTransaction();
  204. using IDbTransaction transaction = null;
  205. using IDbConnection connection = null;
  206. try
  207. {
  208. Stopwatch stopwatch = new Stopwatch(); // 创建一个计时器
  209. var tasks = new List<Task<int>>
  210. {
  211. Task.Run(() => RunTaskWithLogging(() => task_insert_tk_logs(limit, redis), "task_insert_tk_logs")),
  212. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_tb_logs(limit, redis), "task_insert_parse_tb_logs")),
  213. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_jd_logs(limit, redis), "task_insert_parse_jd_logs")),
  214. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_pdd_logs(limit, redis), "task_insert_parse_pdd_logs")),
  215. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_dy_logs(limit, redis), "task_insert_parse_dy_logs")),
  216. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_tool_logs(limit, redis), "task_insert_parse_tool_logs")),
  217. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_deeplink_logs(limit, redis), "task_insert_parse_deeplink_logs")),
  218. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_coupon_logs(limit, redis), "task_insert_parse_coupon_logs")),
  219. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_cps_logs(limit, redis), "task_insert_parse_cps_logs")),
  220. Task.Run(() => RunTaskWithLogging(() => task_insert_promotion_img_logs(limit, redis), "task_insert_promotion_img_logs")),
  221. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_ks_logs(limit, redis), "task_insert_parse_ks_logs")),
  222. Task.Run(() => RunTaskWithLogging(() => task_insert_parse_dy_logs(limit, redis), "task_insert_parse_dy_logs"))
  223. };
  224. // 等待所有任务完成
  225. Task.WhenAll(tasks).Wait();//252行
  226. // 计算所有任务的结果总和
  227. total = tasks.Select(t => t.Result).Sum();
  228. //transaction.Commit();
  229. }
  230. catch (Exception ex)
  231. {
  232. //transaction.Rollback();
  233. // 构建异常详细信息字符串
  234. string detailedError = $"{ex.Message}\n" +
  235. $"堆栈跟踪: {ex.StackTrace}\n";
  236. if (ex.InnerException != null)
  237. {
  238. detailedError += $"内部异常: {ex.InnerException.Message}\n" +
  239. $"内部堆栈跟踪: {ex.InnerException.StackTrace}\n";
  240. }
  241. // 如果异常包含其他数据,也可以记录下来
  242. if (ex.Data != null && ex.Data.Count > 0)
  243. {
  244. detailedError += "附加数据:\n";
  245. foreach (var key in ex.Data.Keys)
  246. {
  247. detailedError += $"{key}: {ex.Data[key]}\n";
  248. }
  249. }
  250. _ = new LoggerLibrary("database_error", "parse_log")
  251. .Info(detailedError)
  252. .SaveAsync();
  253. NotifyCore.Notify(new NifyMessage
  254. {
  255. message = $"【Exception】\n{detailedError}",
  256. priority = NifyMessagePriority.high,
  257. tags = ["red_circle"]
  258. });
  259. }
  260. finally
  261. {
  262. //connection.Close();
  263. }
  264. return total;
  265. }
  266. // 新增一个辅助方法来处理日志记录和任务执行
  267. private static int RunTaskWithLogging(Func<int> taskFunc, string taskName)
  268. {
  269. LoggerLibrary log = new LoggerLibrary("debug", "BatchInsertLogDB"); // 创建日志对象
  270. Stopwatch stopwatch = new Stopwatch();
  271. stopwatch.Start();
  272. int taskTotal = taskFunc();
  273. stopwatch.Stop();
  274. log.Info($"{taskName} 耗时: {stopwatch.ElapsedMilliseconds} ms,\t插入记录数: {taskTotal}");
  275. log.SaveAsync();
  276. return taskTotal;
  277. }
  278. public static int BatchInsertLogDB(int limit)
  279. {
  280. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  281. {
  282. if (!node.is_public_api) return 0;
  283. if (CenterHub.IsCenter)
  284. {
  285. if (node.is_coupon_api) return 0;
  286. }
  287. else
  288. {
  289. if (!node.is_coupon_api) return 0;
  290. }
  291. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  292. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  293. return BatchInsertLogDB(limit, redis);
  294. });
  295. return result.Sum();
  296. }
  297. private static void saveCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  298. {
  299. saveAccountCache("all", success, message, reason);
  300. saveAccountCache($"{channel}", success, message, reason);
  301. saveAccountCache($"{accountName}", success, message, reason);
  302. if (accountId != 0)
  303. {
  304. //todo 放着跑两天,要将读取的地方改成读取accountid
  305. saveAccountCache($"{channel}_{accountId}", success, message, reason);
  306. }
  307. }
  308. private static void saveAccountCache(string accountName, bool success, string message, string reason)
  309. {
  310. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
  311. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
  312. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  313. string result = success ? "success" : "fail";
  314. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  315. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  316. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  317. if (!string.IsNullOrEmpty(message))
  318. {
  319. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  320. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  321. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  322. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  323. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  324. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  325. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  326. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  327. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  328. }
  329. if (!string.IsNullOrEmpty(reason))
  330. {
  331. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  332. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  333. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  334. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  335. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  336. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  337. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  338. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  339. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  340. }
  341. }
  342. private static async Task saveUnionCouponParseCacheAsync(TkDataDTO data)
  343. {
  344. string cacheKey = $":cache:parse:{data.ip}_{data.oaid}_{data.itemId}";
  345. await EndPointCore.ProcessEndPointNodesAsync(node =>
  346. {
  347. if (!node.is_coupon_api) return Task.CompletedTask;
  348. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  349. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  350. redis.Set(cacheKey, 1, 2 * 86400);
  351. return Task.CompletedTask;
  352. });
  353. }
  354. private async static Task saveClientRequestTotal(TkChannelEnum channel, string ip, string oaid)
  355. {
  356. await saveClientRequestTotal(channel.ToString(), ip, oaid);
  357. }
  358. private static async Task saveClientRequestTotal(string channel, string ip, string oaid)
  359. {
  360. await EndPointCore.ProcessEndPointNodesAsync(node =>
  361. {
  362. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  363. if (!node.is_public_api) return Task.CompletedTask;
  364. #if DEBUG
  365. switch (node.name)
  366. {
  367. case "bj":
  368. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  369. break;
  370. case "gz":
  371. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  372. break;
  373. case "coupon1":
  374. node.redis_server = "123.56.185.166:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  375. break;
  376. default: return Task.CompletedTask;
  377. }
  378. #endif
  379. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  380. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  381. redis.IncrBy(cacheKey);
  382. redis.Expire(cacheKey, 86400);
  383. if (!string.IsNullOrEmpty(oaid))
  384. {
  385. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  386. redis.IncrBy(cacheKey);
  387. redis.Expire(cacheKey, 86400);
  388. }
  389. return Task.CompletedTask;
  390. });
  391. }
  392. public static bool InBlacklist(string blacklist, string oaid)
  393. {
  394. if (string.IsNullOrEmpty(blacklist)) return false;
  395. var arr = blacklist.Split(new[] { "\r\n" }, StringSplitOptions.None)
  396. .Select(s => s.Trim()).ToArray();
  397. return arr.Contains(oaid);
  398. }
  399. public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
  400. {
  401. if (string.IsNullOrEmpty(oaid)) return 0;
  402. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  403. return RedisHelper.Get<int>(cacheKey);
  404. }
  405. public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
  406. {
  407. if (string.IsNullOrEmpty(ip)) return 0;
  408. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  409. return RedisHelper.Get<int>(cacheKey);
  410. }
  411. public static int getClientRequestTotalByOAID(string channel, string oaid)
  412. {
  413. if (string.IsNullOrEmpty(oaid)) return 0;
  414. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  415. return RedisHelper.Get<int>(cacheKey);
  416. }
  417. public static int getClientRequestTotalByIp(string channel, string ip)
  418. {
  419. if (string.IsNullOrEmpty(ip)) return 0;
  420. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  421. return RedisHelper.Get<int>(cacheKey);
  422. }
  423. private static void saveClientRequestTotal(CpsChannelEnum channel, string ip, string oaid)
  424. {
  425. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  426. RedisHelper.IncrBy(cacheKey);
  427. RedisHelper.Expire(cacheKey, 86400);
  428. if (!string.IsNullOrEmpty(oaid))
  429. {
  430. cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  431. RedisHelper.IncrBy(cacheKey);
  432. RedisHelper.Expire(cacheKey, 86400);
  433. }
  434. }
  435. public static int getClientRequestTotalByOAID(CpsChannelEnum channel, string oaid)
  436. {
  437. if (string.IsNullOrEmpty(oaid)) return 0;
  438. string cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  439. return RedisHelper.Get<int>(cacheKey);
  440. }
  441. public static int getClientRequestTotalByIp(CpsChannelEnum channel, string ip)
  442. {
  443. if (string.IsNullOrEmpty(ip)) return 0;
  444. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  445. return RedisHelper.Get<int>(cacheKey);
  446. }
  447. private static void saveParseCache(string channel, int accountId, string accountName,
  448. bool success, string message, string reason, string deeplink)
  449. {
  450. string dp_flag = deeplink switch
  451. {
  452. "" => "none",
  453. "tbopen://m.taobao.com/tbopen/index.html" or
  454. "pinduoduo://com.xunmeng.pinduoduo/" or
  455. "snssdk1128://feed?refer=web" or
  456. "bdnetdisk://n/action.EXTERNAL_ACTIVITY" or
  457. "openapp.jdmobile://virtual?params=" or "openapp.jdmobile://" => "home",
  458. _ => success ? "success" : "fail",
  459. };
  460. //关于dp的缓存
  461. saveParseAccountCache($"dp_{dp_flag}:all", success, message, reason);
  462. saveParseAccountCache($"dp_{dp_flag}:{channel}", success, message, reason);
  463. saveParseAccountCache($"dp_{dp_flag}:{accountName}", success, message, reason);
  464. if (accountId != 0)
  465. {
  466. saveParseAccountCache($"dp_{dp_flag}:{channel}_{accountId}", success, message, reason);
  467. }
  468. saveParseAccountCache("all", success, message, reason);
  469. saveParseAccountCache($"{channel}", success, message, reason);
  470. saveParseAccountCache($"{accountName}", success, message, reason);
  471. if (accountId != 0)
  472. {
  473. //todo 放着跑两天,要将读取的地方改成读取accountid
  474. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  475. }
  476. }
  477. private static void saveParseAccountCache(string accountName, bool success,
  478. string message, string reason)
  479. {
  480. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  481. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  482. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  483. string result = success ? "success" : "fail";
  484. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  485. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  486. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  487. if (!string.IsNullOrEmpty(message))
  488. {
  489. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  490. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  491. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  492. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  493. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  494. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  495. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  496. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  497. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  498. }
  499. if (!string.IsNullOrEmpty(reason))
  500. {
  501. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  502. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  503. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  504. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  505. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  506. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  507. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  508. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  509. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  510. }
  511. }
  512. public static int GetTotal(string keyname, bool all_node = true)
  513. {
  514. //:coupon_total:tb:20240706
  515. //:coupon_total:tb:success:20240706
  516. //:coupon_total:tb:放弃转链:20240706
  517. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  518. {
  519. if (!node.is_public_api) return 0;
  520. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  521. if (!all_node)
  522. {
  523. if (CenterHub.IsCenter)
  524. {
  525. if (node.is_coupon_api) { return 0; }
  526. }
  527. else
  528. {
  529. if (!node.is_coupon_api) { return 0; }
  530. }
  531. }
  532. #if DEBUG
  533. switch (node.name)
  534. {
  535. case "bj":
  536. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  537. break;
  538. case "gz":
  539. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  540. break;
  541. case "coupon1":
  542. node.redis_server = "123.56.185.166:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  543. break;
  544. default: return 0;
  545. }
  546. #endif
  547. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  548. int count = redis.Get<int>(keyname);
  549. return count;
  550. });
  551. return result.Sum();
  552. }
  553. public static async Task<int> GetTotalAsync(string keyname, bool all_node = true)
  554. {
  555. async Task<int> ProcessNode(EndPointDTO node)
  556. {
  557. if (!IsValidNode(node, all_node)) return 0;
  558. var redisServer = GetRedisServer(node);
  559. if (string.IsNullOrEmpty(redisServer)) return 0;
  560. try
  561. {
  562. //var redis = RedisClientManager.GetRedisClient(redisServer);
  563. await using var scope = RedisClientFactory.CreateScope(redisServer);
  564. var client = scope.Client;
  565. return await client.GetAsync<int>(keyname);
  566. }
  567. catch (Exception ex)
  568. {
  569. // 可以添加日志记录
  570. return 0;
  571. }
  572. }
  573. var result = await EndPointCore.ProcessEndPointNodesTaskAsync<int>(ProcessNode);
  574. return result.Sum();
  575. }
  576. private static bool IsValidNode(EndPointDTO node, bool all_node)
  577. {
  578. if (!node.is_public_api || string.IsNullOrEmpty(node.redis_server))
  579. return false;
  580. if (!all_node)
  581. {
  582. if (CenterHub.IsCenter)
  583. return !node.is_coupon_api;
  584. else
  585. return node.is_coupon_api;
  586. }
  587. return true;
  588. }
  589. private static string GetRedisServer(EndPointDTO node)
  590. {
  591. #if DEBUG
  592. return node.name switch
  593. {
  594. "bj" => "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook",
  595. "gz" => "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook",
  596. "coupon1" => "123.56.185.166:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon",
  597. _ => string.Empty
  598. };
  599. #else
  600. return node.redis_server;
  601. #endif
  602. }
  603. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  604. {
  605. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  606. {
  607. if (!node.is_public_api) return [];
  608. if (string.IsNullOrEmpty(node.redis_server)) return [];
  609. if (!all_node)
  610. {
  611. if (CenterHub.IsCenter)
  612. {
  613. if (node.is_coupon_api) { return []; }
  614. }
  615. else
  616. {
  617. if (!node.is_coupon_api) { return []; }
  618. }
  619. }
  620. #if DEBUG
  621. switch (node.name)
  622. {
  623. case "bj":
  624. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  625. break;
  626. case "gz":
  627. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  628. break;
  629. case "coupon1":
  630. node.redis_server = "123.56.185.166:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  631. break;
  632. default: return [];
  633. }
  634. #endif
  635. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  636. string[] message_keys = redis.SMembers(keyname);
  637. return message_keys;
  638. });
  639. string[] message_keys = [];
  640. foreach (var arr in result)
  641. {
  642. message_keys = message_keys.Union(arr).ToArray();
  643. }
  644. return message_keys;
  645. }
  646. }
  647. }