base.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 static void saveClientRequestTotal(TkChannelEnum channel, string ip, string oaid)
  345. {
  346. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  347. RedisHelper.IncrBy(cacheKey);
  348. RedisHelper.Expire(cacheKey, 86400);
  349. if (!string.IsNullOrEmpty(oaid))
  350. {
  351. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  352. RedisHelper.IncrBy(cacheKey);
  353. RedisHelper.Expire(cacheKey, 86400);
  354. }
  355. }
  356. private static void saveClientRequestTotal(string channel, string ip, string oaid)
  357. {
  358. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  359. RedisHelper.IncrBy(cacheKey);
  360. RedisHelper.Expire(cacheKey, 86400);
  361. if (!string.IsNullOrEmpty(oaid))
  362. {
  363. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  364. RedisHelper.IncrBy(cacheKey);
  365. RedisHelper.Expire(cacheKey, 86400);
  366. }
  367. }
  368. public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
  369. {
  370. if (string.IsNullOrEmpty(oaid)) return 0;
  371. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  372. return RedisHelper.Get<int>(cacheKey);
  373. }
  374. public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
  375. {
  376. if (string.IsNullOrEmpty(ip)) return 0;
  377. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  378. return RedisHelper.Get<int>(cacheKey);
  379. }
  380. public static int getClientRequestTotalByOAID(string channel, string oaid)
  381. {
  382. if (string.IsNullOrEmpty(oaid)) return 0;
  383. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  384. return RedisHelper.Get<int>(cacheKey);
  385. }
  386. public static int getClientRequestTotalByIp(string channel, string ip)
  387. {
  388. if (string.IsNullOrEmpty(ip)) return 0;
  389. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  390. return RedisHelper.Get<int>(cacheKey);
  391. }
  392. private static void saveClientRequestTotal(CpsChannelEnum channel, string ip, string oaid)
  393. {
  394. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  395. RedisHelper.IncrBy(cacheKey);
  396. RedisHelper.Expire(cacheKey, 86400);
  397. if (!string.IsNullOrEmpty(oaid))
  398. {
  399. cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  400. RedisHelper.IncrBy(cacheKey);
  401. RedisHelper.Expire(cacheKey, 86400);
  402. }
  403. }
  404. public static int getClientRequestTotalByOAID(CpsChannelEnum channel, string oaid)
  405. {
  406. if (string.IsNullOrEmpty(oaid)) return 0;
  407. string cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  408. return RedisHelper.Get<int>(cacheKey);
  409. }
  410. public static int getClientRequestTotalByIp(CpsChannelEnum channel, string ip)
  411. {
  412. if (string.IsNullOrEmpty(ip)) return 0;
  413. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  414. return RedisHelper.Get<int>(cacheKey);
  415. }
  416. private static void saveParseCache(string channel, int accountId, string accountName,
  417. bool success, string message, string reason, string deeplink)
  418. {
  419. string dp_flag = deeplink switch
  420. {
  421. "" => "none",
  422. "tbopen://m.taobao.com/tbopen/index.html" or
  423. "pinduoduo://com.xunmeng.pinduoduo/" or
  424. "snssdk1128://feed?refer=web" or
  425. "bdnetdisk://n/action.EXTERNAL_ACTIVITY" or
  426. "openapp.jdmobile://virtual?params=" or "openapp.jdmobile://" => "home",
  427. _ => success ? "success" : "fail",
  428. };
  429. //关于dp的缓存
  430. saveParseAccountCache($"dp_{dp_flag}:all", success, message, reason);
  431. saveParseAccountCache($"dp_{dp_flag}:{channel}", success, message, reason);
  432. saveParseAccountCache($"dp_{dp_flag}:{accountName}", success, message, reason);
  433. if (accountId != 0)
  434. {
  435. saveParseAccountCache($"dp_{dp_flag}:{channel}_{accountId}", success, message, reason);
  436. }
  437. saveParseAccountCache("all", success, message, reason);
  438. saveParseAccountCache($"{channel}", success, message, reason);
  439. saveParseAccountCache($"{accountName}", success, message, reason);
  440. if (accountId != 0)
  441. {
  442. //todo 放着跑两天,要将读取的地方改成读取accountid
  443. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  444. }
  445. }
  446. private static void saveParseAccountCache(string accountName, bool success,
  447. string message, string reason)
  448. {
  449. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  450. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  451. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  452. string result = success ? "success" : "fail";
  453. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  454. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  455. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  456. if (!string.IsNullOrEmpty(message))
  457. {
  458. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  459. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  460. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  461. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  462. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  463. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  464. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  465. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  466. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  467. }
  468. if (!string.IsNullOrEmpty(reason))
  469. {
  470. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  471. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  472. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  473. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  474. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  475. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  476. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  477. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  478. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  479. }
  480. }
  481. public static int GetTotal(string keyname, bool all_node = true)
  482. {
  483. //:coupon_total:tb:20240706
  484. //:coupon_total:tb:success:20240706
  485. //:coupon_total:tb:放弃转链:20240706
  486. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  487. {
  488. if (!node.is_public_api) return 0;
  489. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  490. if (!all_node)
  491. {
  492. if (CenterHub.IsCenter)
  493. {
  494. if (node.is_coupon_api) { return 0; }
  495. }
  496. else
  497. {
  498. if (!node.is_coupon_api) { return 0; }
  499. }
  500. }
  501. #if DEBUG
  502. switch (node.name)
  503. {
  504. case "bj":
  505. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  506. break;
  507. case "gz":
  508. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  509. break;
  510. case "coupon1":
  511. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  512. break;
  513. default: return 0;
  514. }
  515. #endif
  516. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  517. int count = redis.Get<int>(keyname);
  518. return count;
  519. });
  520. return result.Sum();
  521. }
  522. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  523. {
  524. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  525. {
  526. if (!node.is_public_api) return [];
  527. if (string.IsNullOrEmpty(node.redis_server)) return [];
  528. if (!all_node)
  529. {
  530. if (CenterHub.IsCenter)
  531. {
  532. if (node.is_coupon_api) { return []; }
  533. }
  534. else
  535. {
  536. if (!node.is_coupon_api) { return []; }
  537. }
  538. }
  539. #if DEBUG
  540. switch (node.name)
  541. {
  542. case "bj":
  543. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  544. break;
  545. case "gz":
  546. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  547. break;
  548. case "coupon1":
  549. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  550. break;
  551. default: return [];
  552. }
  553. #endif
  554. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  555. string[] message_keys = redis.SMembers(keyname);
  556. return message_keys;
  557. });
  558. string[] message_keys = [];
  559. foreach (var arr in result)
  560. {
  561. message_keys = message_keys.Union(arr).ToArray();
  562. }
  563. return message_keys;
  564. }
  565. }
  566. }