TkLogCore.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. namespace molilian.core
  21. {
  22. public class TkLogCore
  23. {
  24. static string queue_tb_key = "queue:logs:tb";
  25. static string queue_jd_key = "queue:logs:jd";
  26. static string queue_parse_tb_key = "queue:parse_logs:tb";
  27. static string queue_parse_jd_key = "queue:parse_logs:jd";
  28. static string queue_parse_dy_key = "queue:parse_logs:dy";
  29. static string queue_parse_tool_key = "queue:parse_logs:tool";
  30. static string queue_coupon_key = "queue:coupon_logs";
  31. static string promotion_img_key = "queue:promotion:img";
  32. public static int BatchInsertLogDB(int limit)
  33. {
  34. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  35. {
  36. if (!node.is_public_api) return 0;
  37. if (CenterHub.IsCenter)
  38. {
  39. if (node.is_coupon_api) return 0;
  40. }
  41. else
  42. {
  43. if (!node.is_coupon_api) return 0;
  44. }
  45. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  46. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  47. return BatchInsertLogDB(limit, redis);
  48. });
  49. return result.Sum();
  50. }
  51. private static int save_tk_log(TkDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  52. {
  53. return new DBContext.Table(connection, tablename)
  54. .Add("end_point", data.end_point)
  55. .Add("channel", (int)data.channel)
  56. .Add("accountId", data.accountId)
  57. .Add("accountName", data.accountName)
  58. .Add("rawContent", data.rawContent)
  59. .Add("rawContent2", data.rawContent2)
  60. .Add("success", data.success)
  61. .Add("message", data.message)
  62. .Add("reason", data.reason)
  63. .Add("content", data.content)
  64. .Add("couponAmount", data.couponAmount)
  65. .Add("itemId", data.itemId)
  66. .Add("itemName", data.itemName)
  67. .Add("pic", data.pic)
  68. .Add("promotionPrice", data.promotionPrice)
  69. .Add("taoToken", data.taoToken)
  70. .Add("shortLinkurl", data.shortLinkurl)
  71. .Add("deeplink_url", data.deeplink_url)
  72. .Add("num_iid", data.num_iid)
  73. .Add("elapsedTime", data.elapsedTime)
  74. .Add("subCode", data.subCode)
  75. .Add("ip", data.ip)
  76. .Add("oaid", data.oaid)
  77. .Add("create_time", data.create_time)
  78. .Create(DBContext.InsertType.NORMAL, transaction);
  79. }
  80. private static int save_tk_parse_logs(TkDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  81. {
  82. return new DBContext.Table(connection, tablename)
  83. .Add("end_point", data.end_point)
  84. .Add("channel", (int)data.channel)
  85. .Add("accountId", data.accountId)
  86. .Add("accountName", data.accountName)
  87. .Add("rawContent", data.rawContent)
  88. .Add("success", data.success)
  89. .Add("message", data.message)
  90. .Add("reason", data.reason)
  91. .Add("content", data.content)
  92. .Add("itemId", data.itemId)
  93. .Add("itemName", data.itemName)
  94. .Add("pic", data.pic)
  95. .Add("couponAmount", data.couponAmount)
  96. .Add("promotionPrice", data.promotionPrice)
  97. .Add("taoToken", data.taoToken)
  98. .Add("shortLinkurl", data.shortLinkurl)
  99. .Add("deeplink_url", data.deeplink_url)
  100. .Add("num_iid", data.num_iid)
  101. .Add("elapsedTime", data.elapsedTime)
  102. .Add("elapsedTime2", data.elapsedTime2)
  103. .Add("elapsedTime3", data.elapsedTime3)
  104. .Add("subCode", data.subCode)
  105. .Add("ip", data.ip)
  106. .Add("oaid", data.oaid)
  107. .Add("create_time", data.create_time)
  108. .Create(DBContext.InsertType.NORMAL, transaction);
  109. }
  110. private static int save_jd_parse_logs(JdDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  111. {
  112. return new DBContext.Table(connection, tablename)
  113. .Add("end_point", data.end_point)
  114. .Add("channel", (int)data.channel)
  115. .Add("accountId", data.accountId)
  116. .Add("accountName", data.accountName)
  117. .Add("rawContent", data.rawContent)
  118. .Add("success", data.success)
  119. .Add("message", data.message)
  120. .Add("reason", data.reason)
  121. .Add("content", data.content)
  122. .Add("itemId", data.itemId)
  123. .Add("itemName", data.itemName)
  124. .Add("pic", data.pic)
  125. .Add("couponAmount", data.couponAmount)
  126. .Add("promotionPrice", data.promotionPrice)
  127. .Add("taoToken", data.taoToken)
  128. .Add("shortLinkurl", data.shortLinkurl)
  129. .Add("deeplink_url", data.deeplink_url)
  130. .Add("elapsedTime", data.elapsedTime)
  131. .Add("subCode", data.subCode)
  132. .Add("ip", data.ip)
  133. .Add("oaid", data.oaid)
  134. .Add("create_time", data.create_time)
  135. .Create(DBContext.InsertType.NORMAL, transaction);
  136. }
  137. public static int BatchInsertLogDB(int limit, CSRedisClient redis)
  138. {
  139. int total = 0;
  140. using var connection = DBContext.GetOpenConnection();
  141. connection.Open();
  142. using var transaction = connection.BeginTransaction();
  143. string cacheKey = ":lock_key:start_comparison_tk";
  144. bool start_comparison_tk = RedisHelper.Get<int>(cacheKey) > 0;
  145. try
  146. {
  147. for (int i = 0; i < limit; i++)
  148. {
  149. var data = redis.LPop<TkDataDTO>(queue_tb_key);
  150. if (data == null) break;
  151. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  152. data.ip.Contains("127.0.0"))
  153. {
  154. save_tk_log(data, "tk_logs_test", connection, transaction);
  155. }
  156. else
  157. {
  158. data.id = save_tk_log(data, "tk_logs", connection, transaction);
  159. if (data.elapsedTime > 1000)
  160. {
  161. save_tk_log(data, "tk_logs_test", connection, transaction);
  162. }
  163. if (data.success)
  164. {
  165. save_tk_log(data, "tk_success_logs", connection, transaction);
  166. }
  167. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  168. }
  169. total++;
  170. }
  171. for (int i = 0; i < limit; i++)
  172. {
  173. var data = redis.LPop<JdDataDTO>(queue_jd_key);
  174. if (data == null) break;
  175. new DBContext.Table(connection, "tk_logs")
  176. .Add("end_point", data.end_point)
  177. .Add("channel", (int)data.channel)
  178. .Add("accountId", data.accountId)
  179. .Add("accountName", data.accountName)
  180. .Add("rawContent", data.rawContent)
  181. .Add("rawContent2", data.rawContent2)
  182. .Add("success", data.success)
  183. .Add("message", data.message)
  184. .Add("reason", data.reason)
  185. .Add("shortLinkurl", data.shortLinkurl)
  186. .Add("deeplink_url", data.deeplink_url)
  187. .Add("elapsedTime", data.elapsedTime)
  188. .Add("ip", data.ip)
  189. .Add("oaid", data.oaid)
  190. .Add("create_time", data.create_time)
  191. .Create(DBContext.InsertType.NORMAL, transaction);
  192. total++;
  193. }
  194. for (int i = 0; i < limit; i++)
  195. {
  196. var data = redis.LPop<TkDataDTO>(queue_parse_tb_key);
  197. if (data == null) break;
  198. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  199. data.ip.Contains("127.0.0"))
  200. {
  201. save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
  202. }
  203. else
  204. {
  205. data.id = save_tk_parse_logs(data, "tk_parse_logs", connection, transaction);
  206. if (data.elapsedTime > 1000)
  207. {
  208. save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
  209. }
  210. if (data.success)
  211. {
  212. save_tk_parse_logs(data, "tk_success_parse_logs", connection, transaction);
  213. }
  214. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  215. }
  216. if (data.reason.Contains("初步筛选2") && !data.rawContent.Contains("I:/kWqN5t623Hx"))
  217. {
  218. save_tk_parse_logs(data, "tk_parse_logs_test2", connection, transaction);
  219. }
  220. if (data.reason.Contains("初步筛选1.5"))
  221. {
  222. save_tk_parse_logs(data, "tk_parse_logs_multi_token", connection, transaction);
  223. }
  224. if (data.reason.Contains("霸下验证码"))
  225. {
  226. save_tk_parse_logs(data, "tk_parse_logs_captcha", connection, transaction);
  227. }
  228. total++;
  229. }
  230. for (int i = 0; i < limit; i++)
  231. {
  232. var data = redis.LPop<JdDataDTO>(queue_parse_jd_key);
  233. if (data == null) break;
  234. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  235. data.ip.Contains("127.0.0"))
  236. {
  237. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  238. }
  239. else
  240. {
  241. save_jd_parse_logs(data, "jd_parse_logs", connection, transaction);
  242. if (data.elapsedTime > 1000)
  243. {
  244. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  245. }
  246. if (data.success)
  247. {
  248. save_jd_parse_logs(data, "jd_parse_logs_success", connection, transaction);
  249. }
  250. }
  251. total++;
  252. }
  253. for (int i = 0; i < limit; i++)
  254. {
  255. var data = redis.LPop<DyDataDTO>(queue_parse_dy_key);
  256. if (data == null) break;
  257. new DBContext.Table(connection, "tk_parse_logs")
  258. .Add("end_point", data.end_point)
  259. .Add("channel", (int)data.channel)
  260. .Add("accountId", data.accountId)
  261. .Add("accountName", data.accountName)
  262. .Add("rawContent", data.rawContent)
  263. .Add("success", data.success)
  264. .Add("message", data.message)
  265. .Add("reason", data.reason)
  266. .Add("content", data.content)
  267. .Add("itemId", data.itemId)
  268. .Add("itemName", data.itemName)
  269. .Add("pic", data.pic)
  270. .Add("couponAmount", data.couponAmount)
  271. .Add("promotionPrice", data.promotionPrice)
  272. .Add("taoToken", data.taoToken)
  273. .Add("shortLinkurl", data.shortLinkurl)
  274. .Add("deeplink_url", data.deeplink_url)
  275. .Add("elapsedTime", data.elapsedTime)
  276. .Add("subCode", data.subCode)
  277. .Add("ip", data.ip)
  278. .Add("oaid", data.oaid)
  279. .Add("create_time", data.create_time)
  280. .Create(DBContext.InsertType.NORMAL, transaction);
  281. total++;
  282. }
  283. for (int i = 0; i < limit; i++)
  284. {
  285. var data = redis.LPop<ToolParseDataDTO>(queue_parse_tool_key);
  286. if (data == null) break;
  287. new DBContext.Table(connection, "tool_parse_logs")
  288. .Add("end_point", data.end_point)
  289. .Add("channel", (int)data.channel)
  290. .Add("rawContent", data.rawContent)
  291. .Add("success", data.success)
  292. .Add("message", data.message)
  293. .Add("reason", data.reason)
  294. .Add("content", data.content)
  295. .Add("taoToken", data.taoToken)
  296. .Add("shortLinkurl", data.shortLinkurl)
  297. .Add("deeplink_url", data.deeplink_url)
  298. .Add("elapsedTime", data.elapsedTime)
  299. .Add("ip", data.ip)
  300. .Add("oaid", data.oaid)
  301. .Add("create_time", data.create_time)
  302. .Create(DBContext.InsertType.NORMAL, transaction);
  303. total++;
  304. }
  305. for (int i = 0; i < limit; i++)
  306. {
  307. var data = redis.LPop<UnionCouponDTO>(queue_coupon_key);
  308. if (data == null) break;
  309. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  310. data.ip.Contains("127.0.0"))
  311. {
  312. var test_data = data.Convert2Json().Convert2Object<TestUnionCouponDTO>();
  313. connection.Insert(test_data);
  314. }
  315. else
  316. {
  317. connection.Insert(data);
  318. if (data.success)
  319. {
  320. var success_data = data.Convert2Json().Convert2Object<SuccessUnionCouponDTO>();
  321. connection.Insert(success_data);
  322. }
  323. }
  324. total++;
  325. }
  326. for (int i = 0; i < limit; i++)
  327. {
  328. var data = redis.LPop<PromotionQueryDTO>(promotion_img_key);
  329. if (data == null) break;
  330. connection.Insert(data);
  331. total++;
  332. }
  333. transaction.Commit();
  334. }
  335. catch (Exception ex)
  336. {
  337. transaction.Rollback();
  338. _ = new LoggerLibrary("database_error", "parse_log")
  339. .Info(ex.Message, ex.StackTrace)
  340. .SaveAsync();
  341. NotifyCore.Notify(new NifyMessage
  342. {
  343. message = $"【写入日志异常】\n{ex.Message}\n{ex.StackTrace}",
  344. priority = NifyMessagePriority.high,
  345. tags = ["red_circle"]
  346. });
  347. }
  348. finally
  349. {
  350. connection.Close();
  351. }
  352. return total;
  353. }
  354. public static async Task PromotionImgLogAsync(PromotionQueryDTO response)
  355. {
  356. try
  357. {
  358. var ts = DateTime.Now - response.create_time;
  359. response.elapsedTime = (int)ts.TotalMilliseconds;
  360. _ = RedisHelper.RPushAsync(promotion_img_key, response);
  361. //using var connection = DBContext.GetOpenConnection();
  362. //connection.Insert(response);
  363. savePromotionCache(response.accountId, response.accountName, response.success, response.message, response.reason);
  364. }
  365. catch (Exception ex) { }
  366. }
  367. public static async Task LogAsync(JdDataDTO response)
  368. {
  369. try
  370. {
  371. var ts = DateTime.Now - response.create_time;
  372. response.elapsedTime = (int)ts.TotalMilliseconds;
  373. _ = RedisHelper.RPushAsync(queue_jd_key, response);
  374. if (!response.ip.Contains("127.0.0"))
  375. {
  376. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  377. }
  378. }
  379. catch (Exception ex) { }
  380. }
  381. public static async Task LogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
  382. {
  383. try
  384. {
  385. var ts = DateTime.Now - response.create_time;
  386. response.elapsedTime = (int)ts.TotalMilliseconds;
  387. _ = RedisHelper.RPushAsync(queue_tb_key, response);
  388. if (!response.ip.Contains("127.0.0"))
  389. {
  390. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  391. }
  392. if (!response.success && "nologin".Equals(response.message))
  393. {
  394. switch (response.channel)
  395. {
  396. case TkChannelEnum.tb:
  397. await Task.Run(() =>
  398. {
  399. if (alimamaPlus != null)
  400. {
  401. (bool success, string message) = alimamaPlus.RenewCookie();
  402. if (success) return;
  403. }
  404. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  405. });
  406. break;
  407. }
  408. }
  409. if ("没有匹配账号".Equals(response.reason))
  410. {
  411. TkPoolCore.AccountExhausted();
  412. }
  413. }
  414. catch (Exception ex)
  415. {
  416. _ = new LoggerLibrary("unionParse", "database_error")
  417. .Info(response.rawContent, response.rawContent2)
  418. .Info(response.Convert2Json())
  419. .Info(ex.Message, ex.StackTrace)
  420. .SaveAsync();
  421. }
  422. }
  423. public static async Task ParseLogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
  424. {
  425. #if DEBUG
  426. //return;
  427. #endif
  428. try
  429. {
  430. var ts = DateTime.Now - response.create_time;
  431. response.elapsedTime = (int)ts.TotalMilliseconds;
  432. _ = RedisHelper.RPushAsync(queue_parse_tb_key, response);
  433. if (response.success)
  434. {
  435. _ = saveUnionCouponParseCacheAsync(response);
  436. }
  437. if (!response.ip.Contains("127.0.0"))
  438. {
  439. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  440. }
  441. if (!string.IsNullOrEmpty(response.itemId)) TkOrderTrackingCore.SaveLinkSummary(response);
  442. if (!response.success && "nologin".Equals(response.message))
  443. {
  444. switch (response.channel)
  445. {
  446. case TkChannelEnum.tb:
  447. await Task.Run(() =>
  448. {
  449. if (alimamaPlus != null)
  450. {
  451. (bool success, string message) = alimamaPlus.RenewCookie();
  452. if (success) return;
  453. }
  454. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
  455. }); break;
  456. }
  457. }
  458. }
  459. catch (Exception ex)
  460. {
  461. _ = new LoggerLibrary("unionParse", "database_error")
  462. .Info(response.rawContent)
  463. .Info(response.Convert2Json())
  464. .Info(ex.Message, ex.StackTrace)
  465. .SaveAsync();
  466. }
  467. }
  468. public static async Task CouponLogAsync(UnionCouponDTO response, AlimamaPlus? alimamaPlus = null)
  469. {
  470. try
  471. {
  472. var ts = DateTime.Now - response.create_time;
  473. response.elapsedTime = (int)ts.TotalMilliseconds;
  474. _ = RedisHelper.RPushAsync(queue_coupon_key, response);
  475. if (!response.ip.Contains("127.0.0"))
  476. {
  477. saveCouponCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  478. }
  479. if (!response.success && "nologin".Equals(response.message))
  480. {
  481. switch (response.channel)
  482. {
  483. case TkChannelEnum.tb:
  484. await Task.Run(() =>
  485. {
  486. if (alimamaPlus != null)
  487. {
  488. (bool success, string message) = alimamaPlus.RenewCookie();
  489. if (success) return;
  490. }
  491. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
  492. }); break;
  493. }
  494. }
  495. }
  496. catch (Exception ex)
  497. {
  498. _ = new LoggerLibrary("unionCoupon", "database_error")
  499. .Info(response.rawContent)
  500. .Info(response.Convert2Json())
  501. .Info(ex.Message, ex.StackTrace)
  502. .SaveAsync();
  503. }
  504. }
  505. public static async Task ParseLogAsync(JdDataDTO response)
  506. {
  507. try
  508. {
  509. var ts = DateTime.Now - response.create_time;
  510. response.elapsedTime = (int)ts.TotalMilliseconds;
  511. _ = RedisHelper.RPushAsync(queue_parse_jd_key, response);
  512. if (!response.ip.Contains("127.0.0"))
  513. {
  514. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  515. }
  516. if (response.success || response.message.Equals("转链失败"))
  517. {
  518. JdPoolCore.CallsIncrBy(response.accountId);
  519. }
  520. //CallsIncrBy
  521. if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  522. if (!response.success && ("nologin".Equals(response.reason) || "未登录".Equals(response.reason)))
  523. {
  524. await Task.Run(() =>
  525. {
  526. JdPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  527. });
  528. //switch (response.channel)
  529. //{
  530. // case TkChannelEnum.tb:
  531. // //await Task.Run(() =>
  532. // //{
  533. // // if (alimamaPlus != null)
  534. // // {
  535. // // (bool success, string message) = alimamaPlus.RenewCookie();
  536. // // if (success) return;
  537. // // }
  538. // // TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  539. // //});
  540. // break;
  541. //}
  542. }
  543. if ("没有匹配账号".Equals(response.reason))
  544. {
  545. JdPoolCore.AccountExhausted();
  546. }
  547. }
  548. catch (Exception ex) { }
  549. }
  550. public static async Task ParseLogAsync(DyDataDTO response)
  551. {
  552. try
  553. {
  554. var ts = DateTime.Now - response.create_time;
  555. response.elapsedTime = (int)ts.TotalMilliseconds;
  556. _ = RedisHelper.RPushAsync(queue_parse_dy_key, response);
  557. if (!response.ip.Contains("127.0.0"))
  558. {
  559. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  560. }
  561. }
  562. catch (Exception ex) { }
  563. }
  564. public static async Task ParseLogAsync(ToolParseDataDTO response)
  565. {
  566. try
  567. {
  568. var ts = DateTime.Now - response.create_time;
  569. response.elapsedTime = (int)ts.TotalMilliseconds;
  570. _ = RedisHelper.RPushAsync(queue_parse_tool_key, response);
  571. if (!response.ip.Contains("127.0.0"))
  572. {
  573. saveParseCache(response.channel.ToString(), 0, "tool", response.success, response.message, response.reason);
  574. }
  575. }
  576. catch (Exception ex) { }
  577. }
  578. private static void saveCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  579. {
  580. saveAccountCache("all", success, message, reason);
  581. saveAccountCache($"{channel}", success, message, reason);
  582. saveAccountCache($"{accountName}", success, message, reason);
  583. if (accountId != 0)
  584. {
  585. //todo 放着跑两天,要将读取的地方改成读取accountid
  586. saveAccountCache($"{channel}_{accountId}", success, message, reason);
  587. }
  588. }
  589. private static void saveAccountCache(string accountName, bool success, string message, string reason)
  590. {
  591. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
  592. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
  593. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  594. string result = success ? "success" : "fail";
  595. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  596. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  597. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  598. if (!string.IsNullOrEmpty(message))
  599. {
  600. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  601. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  602. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  603. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  604. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  605. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  606. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  607. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  608. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  609. }
  610. if (!string.IsNullOrEmpty(reason))
  611. {
  612. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  613. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  614. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  615. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  616. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  617. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  618. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  619. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  620. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  621. }
  622. }
  623. private static async Task saveUnionCouponParseCacheAsync(TkDataDTO data)
  624. {
  625. string cacheKey = $":cache:parse:{data.ip}_{data.oaid}_{data.itemId}";
  626. await EndPointCore.ProcessEndPointNodesAsync(node =>
  627. {
  628. if (!node.is_coupon_api) return Task.CompletedTask;
  629. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  630. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  631. redis.Set(cacheKey, 1, 2 * 86400);
  632. return Task.CompletedTask;
  633. });
  634. }
  635. private static void saveClientRequestTotal(TkChannelEnum channel, string ip, string oaid)
  636. {
  637. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  638. RedisHelper.IncrBy(cacheKey);
  639. RedisHelper.Expire(cacheKey, 86400);
  640. if (!string.IsNullOrEmpty(oaid))
  641. {
  642. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  643. RedisHelper.IncrBy(cacheKey);
  644. RedisHelper.Expire(cacheKey, 86400);
  645. }
  646. }
  647. public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
  648. {
  649. if (string.IsNullOrEmpty(oaid)) return 0;
  650. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  651. return RedisHelper.Get<int>(cacheKey);
  652. }
  653. public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
  654. {
  655. if (string.IsNullOrEmpty(ip)) return 0;
  656. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  657. return RedisHelper.Get<int>(cacheKey);
  658. }
  659. private static void saveParseCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  660. {
  661. saveParseAccountCache("all", success, message, reason);
  662. saveParseAccountCache($"{channel}", success, message, reason);
  663. saveParseAccountCache($"{accountName}", success, message, reason);
  664. if (accountId != 0)
  665. {
  666. //todo 放着跑两天,要将读取的地方改成读取accountid
  667. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  668. }
  669. }
  670. private static void saveParseAccountCache(string accountName, bool success, string message, string reason)
  671. {
  672. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  673. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  674. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  675. string result = success ? "success" : "fail";
  676. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  677. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  678. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  679. if (!string.IsNullOrEmpty(message))
  680. {
  681. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  682. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  683. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  684. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  685. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  686. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  687. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  688. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  689. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  690. }
  691. if (!string.IsNullOrEmpty(reason))
  692. {
  693. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  694. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  695. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  696. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  697. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  698. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  699. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  700. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  701. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  702. }
  703. }
  704. private static void savePromotionCache(int accountId, string accountName, bool success, string message, string reason)
  705. {
  706. savePromotionAccountCache("all", success, message, reason);
  707. savePromotionAccountCache($"{accountId}", success, message, reason);
  708. }
  709. private static void savePromotionAccountCache(string accountName, bool success, string message, string reason)
  710. {
  711. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMM}");
  712. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  713. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  714. string result = success ? "success" : "fail";
  715. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  716. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  717. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  718. if (!string.IsNullOrEmpty(message))
  719. {
  720. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  721. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  722. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  723. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  724. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  725. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  726. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  727. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  728. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  729. }
  730. if (!string.IsNullOrEmpty(reason))
  731. {
  732. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  733. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  734. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  735. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  736. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  737. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  738. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  739. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  740. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  741. }
  742. }
  743. private static void saveCouponCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  744. {
  745. saveAccountCouponCache("all", success, message, reason);
  746. saveAccountCouponCache($"{channel}", success, message, reason);
  747. if (accountId != 0)
  748. {
  749. saveAccountCouponCache($"{channel}_{accountId}", success, message, reason);
  750. }
  751. }
  752. private static void saveAccountCouponCache(string accountName, bool success, string message, string reason)
  753. {
  754. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMM}");
  755. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  756. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  757. string result = success ? "success" : "fail";
  758. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  759. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  760. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  761. if (!string.IsNullOrEmpty(message))
  762. {
  763. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  764. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  765. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  766. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  767. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  768. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  769. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  770. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  771. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  772. }
  773. if (!string.IsNullOrEmpty(reason))
  774. {
  775. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  776. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  777. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  778. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  779. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  780. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  781. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  782. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  783. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  784. }
  785. }
  786. public static int GetTotal(string keyname, bool all_node = true)
  787. {
  788. //:coupon_total:tb:20240706
  789. //:coupon_total:tb:success:20240706
  790. //:coupon_total:tb:放弃转链:20240706
  791. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  792. {
  793. if (!node.is_public_api) return 0;
  794. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  795. if (!all_node)
  796. {
  797. if (CenterHub.IsCenter)
  798. {
  799. if (node.is_coupon_api) { return 0; }
  800. }
  801. else
  802. {
  803. if (!node.is_coupon_api) { return 0; }
  804. }
  805. }
  806. #if DEBUG
  807. switch (node.name)
  808. {
  809. case "bj":
  810. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  811. break;
  812. case "gz":
  813. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  814. break;
  815. case "coupon1":
  816. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  817. break;
  818. default: return 0;
  819. }
  820. #endif
  821. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  822. int count = redis.Get<int>(keyname);
  823. return count;
  824. });
  825. return result.Sum();
  826. }
  827. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  828. {
  829. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  830. {
  831. if (!node.is_public_api) return [];
  832. if (string.IsNullOrEmpty(node.redis_server)) return [];
  833. if (!all_node)
  834. {
  835. if (CenterHub.IsCenter)
  836. {
  837. if (node.is_coupon_api) { return []; }
  838. }
  839. else
  840. {
  841. if (!node.is_coupon_api) { return []; }
  842. }
  843. }
  844. #if DEBUG
  845. switch (node.name)
  846. {
  847. case "bj":
  848. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  849. break;
  850. case "gz":
  851. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  852. break;
  853. case "coupon1":
  854. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  855. break;
  856. default: return [];
  857. }
  858. #endif
  859. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  860. string[] message_keys = redis.SMembers(keyname);
  861. return message_keys;
  862. });
  863. string[] message_keys = [];
  864. foreach (var arr in result)
  865. {
  866. message_keys = message_keys.Union(arr).ToArray();
  867. }
  868. return message_keys;
  869. }
  870. //if (item.total_count > 0) continue;
  871. //item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
  872. //if (item.total_count == 0) continue;
  873. //item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
  874. //item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
  875. }
  876. }