base.cs 30 KB

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