base.cs 29 KB

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