TkLogCore.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. }
  607. if (!string.IsNullOrEmpty(reason))
  608. {
  609. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  610. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  611. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  612. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  613. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  614. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  615. }
  616. }
  617. private static async Task saveUnionCouponParseCacheAsync(TkDataDTO data)
  618. {
  619. string cacheKey = $":cache:parse:{data.ip}_{data.oaid}_{data.itemId}";
  620. await EndPointCore.ProcessEndPointNodesAsync(node =>
  621. {
  622. if (!node.is_coupon_api) return Task.CompletedTask;
  623. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  624. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  625. redis.Set(cacheKey, 1, 2 * 86400);
  626. return Task.CompletedTask;
  627. });
  628. }
  629. private static void saveClientRequestTotal(TkChannelEnum channel, string ip, string oaid)
  630. {
  631. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  632. RedisHelper.IncrBy(cacheKey);
  633. RedisHelper.Expire(cacheKey, 86400);
  634. if (!string.IsNullOrEmpty(oaid))
  635. {
  636. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  637. RedisHelper.IncrBy(cacheKey);
  638. RedisHelper.Expire(cacheKey, 86400);
  639. }
  640. }
  641. public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
  642. {
  643. if (string.IsNullOrEmpty(oaid)) return 0;
  644. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  645. return RedisHelper.Get<int>(cacheKey);
  646. }
  647. public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
  648. {
  649. if (string.IsNullOrEmpty(ip)) return 0;
  650. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  651. return RedisHelper.Get<int>(cacheKey);
  652. }
  653. private static void saveParseCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  654. {
  655. saveParseAccountCache("all", success, message, reason);
  656. saveParseAccountCache($"{channel}", success, message, reason);
  657. saveParseAccountCache($"{accountName}", success, message, reason);
  658. if (accountId != 0)
  659. {
  660. //todo 放着跑两天,要将读取的地方改成读取accountid
  661. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  662. }
  663. }
  664. private static void saveParseAccountCache(string accountName, bool success, string message, string reason)
  665. {
  666. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  667. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  668. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  669. string result = success ? "success" : "fail";
  670. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  671. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  672. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  673. if (!string.IsNullOrEmpty(message))
  674. {
  675. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  676. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  677. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  678. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  679. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  680. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  681. }
  682. if (!string.IsNullOrEmpty(reason))
  683. {
  684. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  685. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  686. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  687. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  688. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  689. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  690. }
  691. }
  692. private static void savePromotionCache(int accountId, string accountName, bool success, string message, string reason)
  693. {
  694. savePromotionAccountCache("all", success, message, reason);
  695. savePromotionAccountCache($"{accountId}", success, message, reason);
  696. }
  697. private static void savePromotionAccountCache(string accountName, bool success, string message, string reason)
  698. {
  699. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMM}");
  700. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  701. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  702. string result = success ? "success" : "fail";
  703. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  704. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  705. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  706. if (!string.IsNullOrEmpty(message))
  707. {
  708. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  709. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  710. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  711. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  712. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  713. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  714. }
  715. if (!string.IsNullOrEmpty(reason))
  716. {
  717. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  718. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  719. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  720. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  721. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  722. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  723. }
  724. }
  725. private static void saveCouponCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  726. {
  727. saveAccountCouponCache("all", success, message, reason);
  728. saveAccountCouponCache($"{channel}", success, message, reason);
  729. if (accountId != 0)
  730. {
  731. saveAccountCouponCache($"{channel}_{accountId}", success, message, reason);
  732. }
  733. }
  734. private static void saveAccountCouponCache(string accountName, bool success, string message, string reason)
  735. {
  736. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMM}");
  737. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  738. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  739. string result = success ? "success" : "fail";
  740. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  741. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  742. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  743. if (!string.IsNullOrEmpty(message))
  744. {
  745. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  746. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  747. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  748. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  749. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  750. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  751. }
  752. if (!string.IsNullOrEmpty(reason))
  753. {
  754. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  755. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  756. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  757. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  758. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  759. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  760. }
  761. }
  762. public static int GetTotal(string keyname, bool all_node = true)
  763. {
  764. //:coupon_total:tb:20240706
  765. //:coupon_total:tb:success:20240706
  766. //:coupon_total:tb:放弃转链:20240706
  767. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  768. {
  769. if (!node.is_public_api) return 0;
  770. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  771. if (!all_node)
  772. {
  773. if (CenterHub.IsCenter)
  774. {
  775. if (node.is_coupon_api) { return 0; }
  776. }
  777. else
  778. {
  779. if (!node.is_coupon_api) { return 0; }
  780. }
  781. }
  782. #if DEBUG
  783. switch (node.name)
  784. {
  785. case "bj":
  786. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  787. break;
  788. case "gz":
  789. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  790. break;
  791. case "coupon1":
  792. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  793. break;
  794. default: return 0;
  795. }
  796. #endif
  797. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  798. int count = redis.Get<int>(keyname);
  799. return count;
  800. });
  801. return result.Sum();
  802. }
  803. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  804. {
  805. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  806. {
  807. if (!node.is_public_api) return [];
  808. if (string.IsNullOrEmpty(node.redis_server)) return [];
  809. if (!all_node)
  810. {
  811. if (CenterHub.IsCenter)
  812. {
  813. if (node.is_coupon_api) { return []; }
  814. }
  815. else
  816. {
  817. if (!node.is_coupon_api) { return []; }
  818. }
  819. }
  820. #if DEBUG
  821. switch (node.name)
  822. {
  823. case "bj":
  824. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  825. break;
  826. case "gz":
  827. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  828. break;
  829. case "coupon1":
  830. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  831. break;
  832. default: return [];
  833. }
  834. #endif
  835. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  836. string[] message_keys = redis.SMembers(keyname);
  837. return message_keys;
  838. });
  839. string[] message_keys = [];
  840. foreach (var arr in result)
  841. {
  842. message_keys = message_keys.Union(arr).ToArray();
  843. }
  844. return message_keys;
  845. }
  846. //if (item.total_count > 0) continue;
  847. //item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
  848. //if (item.total_count == 0) continue;
  849. //item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
  850. //item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
  851. }
  852. }