base.cs 27 KB

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