base.cs 30 KB

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