TkLogCore.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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") || data.elapsedTime > 1000)
  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.success)
  160. {
  161. save_tk_log(data, "tk_success_logs", connection, transaction);
  162. }
  163. //if (data.success || data.message.Contains("该链接不支持转化,请更换链接尝试"))
  164. //{
  165. // if (start_comparison_tk)
  166. // {
  167. // save_tk_log(data, "comparison_tk_logs", connection, transaction);
  168. // }
  169. //}
  170. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  171. }
  172. total++;
  173. }
  174. for (int i = 0; i < limit; i++)
  175. {
  176. var data = redis.LPop<JdDataDTO>(queue_jd_key);
  177. if (data == null) break;
  178. new DBContext.Table(connection, "tk_logs")
  179. .Add("end_point", data.end_point)
  180. .Add("channel", (int)data.channel)
  181. .Add("accountId", data.accountId)
  182. .Add("accountName", data.accountName)
  183. .Add("rawContent", data.rawContent)
  184. .Add("rawContent2", data.rawContent2)
  185. .Add("success", data.success)
  186. .Add("message", data.message)
  187. .Add("reason", data.reason)
  188. .Add("shortLinkurl", data.shortLinkurl)
  189. .Add("deeplink_url", data.deeplink_url)
  190. .Add("elapsedTime", data.elapsedTime)
  191. .Add("ip", data.ip)
  192. .Add("oaid", data.oaid)
  193. .Add("create_time", data.create_time)
  194. .Create(DBContext.InsertType.NORMAL, transaction);
  195. total++;
  196. }
  197. for (int i = 0; i < limit; i++)
  198. {
  199. var data = redis.LPop<TkDataDTO>(queue_parse_tb_key);
  200. if (data == null) break;
  201. //switch (data.oaid)
  202. //{
  203. // case "test-comparison_tk_logs":
  204. // save_tk_parse_logs(data, "comparison_tk_parse_logs", connection, transaction);
  205. // break;
  206. // case "test-comparison_tk_logs_raw":
  207. // save_tk_parse_logs(data, "comparison_tk_parse_logs_raw", connection, transaction);
  208. // break;
  209. // default:
  210. // break;
  211. //}
  212. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  213. data.ip.Contains("127.0.0") || data.elapsedTime > 1000)
  214. {
  215. save_tk_parse_logs(data, "tk_parse_logs_test", connection, transaction);
  216. }
  217. else
  218. {
  219. data.id = save_tk_parse_logs(data, "tk_parse_logs", connection, transaction);
  220. if (data.success)
  221. {
  222. save_tk_parse_logs(data, "tk_success_parse_logs", connection, transaction);
  223. }
  224. if (!string.IsNullOrEmpty(data.itemId)) TkOrderTrackingCore.SaveLinkSummary(data);
  225. }
  226. if (data.reason.Contains("初步筛选2") && !data.rawContent.Contains("I:/kWqN5t623Hx"))
  227. {
  228. save_tk_parse_logs(data, "tk_parse_logs_test2", connection, transaction);
  229. }
  230. if (data.reason.Contains("初步筛选1.5"))
  231. {
  232. save_tk_parse_logs(data, "tk_parse_logs_multi_token", connection, transaction);
  233. }
  234. if (data.reason.Contains("霸下验证码"))
  235. {
  236. save_tk_parse_logs(data, "tk_parse_logs_captcha", connection, transaction);
  237. }
  238. total++;
  239. }
  240. for (int i = 0; i < limit; i++)
  241. {
  242. var data = redis.LPop<JdDataDTO>(queue_parse_jd_key);
  243. if (data == null) break;
  244. save_jd_parse_logs(data, "jd_parse_logs", connection, transaction);
  245. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  246. data.ip.Contains("127.0.0") || data.elapsedTime > 1000)
  247. {
  248. save_jd_parse_logs(data, "jd_parse_logs_test", connection, transaction);
  249. }
  250. else
  251. {
  252. if (data.success)
  253. {
  254. save_jd_parse_logs(data, "jd_parse_logs_success", connection, transaction);
  255. }
  256. }
  257. total++;
  258. }
  259. for (int i = 0; i < limit; i++)
  260. {
  261. var data = redis.LPop<DyDataDTO>(queue_parse_dy_key);
  262. if (data == null) break;
  263. new DBContext.Table(connection, "tk_parse_logs")
  264. .Add("end_point", data.end_point)
  265. .Add("channel", (int)data.channel)
  266. .Add("accountId", data.accountId)
  267. .Add("accountName", data.accountName)
  268. .Add("rawContent", data.rawContent)
  269. .Add("success", data.success)
  270. .Add("message", data.message)
  271. .Add("reason", data.reason)
  272. .Add("content", data.content)
  273. .Add("itemId", data.itemId)
  274. .Add("itemName", data.itemName)
  275. .Add("pic", data.pic)
  276. .Add("couponAmount", data.couponAmount)
  277. .Add("promotionPrice", data.promotionPrice)
  278. .Add("taoToken", data.taoToken)
  279. .Add("shortLinkurl", data.shortLinkurl)
  280. .Add("deeplink_url", data.deeplink_url)
  281. .Add("elapsedTime", data.elapsedTime)
  282. .Add("subCode", data.subCode)
  283. .Add("ip", data.ip)
  284. .Add("oaid", data.oaid)
  285. .Add("create_time", data.create_time)
  286. .Create(DBContext.InsertType.NORMAL, transaction);
  287. total++;
  288. }
  289. for (int i = 0; i < limit; i++)
  290. {
  291. var data = redis.LPop<ToolParseDataDTO>(queue_parse_tool_key);
  292. if (data == null) break;
  293. new DBContext.Table(connection, "tool_parse_logs")
  294. .Add("end_point", data.end_point)
  295. .Add("channel", (int)data.channel)
  296. .Add("rawContent", data.rawContent)
  297. .Add("success", data.success)
  298. .Add("message", data.message)
  299. .Add("reason", data.reason)
  300. .Add("content", data.content)
  301. .Add("taoToken", data.taoToken)
  302. .Add("shortLinkurl", data.shortLinkurl)
  303. .Add("deeplink_url", data.deeplink_url)
  304. .Add("elapsedTime", data.elapsedTime)
  305. .Add("ip", data.ip)
  306. .Add("oaid", data.oaid)
  307. .Add("create_time", data.create_time)
  308. .Create(DBContext.InsertType.NORMAL, transaction);
  309. total++;
  310. }
  311. for (int i = 0; i < limit; i++)
  312. {
  313. var data = redis.LPop<UnionCouponDTO>(queue_coupon_key);
  314. if (data == null) break;
  315. connection.Insert(data);
  316. if ("3JDaxNfPy83okP3kLScwCkGiuMcyC4PcyntF424979CC9C51BCAD0C245B1C7BA2".Equals(data.oaid) ||
  317. data.ip.Contains("127.0.0") || data.elapsedTime > 1000)
  318. {
  319. var test_data = data.Convert2Json().Convert2Object<TestUnionCouponDTO>();
  320. connection.Insert(test_data);
  321. }
  322. else
  323. {
  324. if (data.success)
  325. {
  326. var success_data = data.Convert2Json().Convert2Object<SuccessUnionCouponDTO>();
  327. connection.Insert(success_data);
  328. }
  329. }
  330. total++;
  331. }
  332. for (int i = 0; i < limit; i++)
  333. {
  334. var data = redis.LPop<PromotionQueryDTO>(promotion_img_key);
  335. if (data == null) break;
  336. connection.Insert(data);
  337. total++;
  338. }
  339. transaction.Commit();
  340. }
  341. catch (Exception ex)
  342. {
  343. transaction.Rollback();
  344. _ = new LoggerLibrary("database_error", "parse_log")
  345. .Info(ex.Message, ex.StackTrace)
  346. .SaveAsync();
  347. NotifyCore.Notify(new NifyMessage
  348. {
  349. message = $"【写入日志异常】\n{ex.Message}\n{ex.StackTrace}",
  350. priority = NifyMessagePriority.high,
  351. tags = ["red_circle"]
  352. });
  353. }
  354. finally
  355. {
  356. connection.Close();
  357. }
  358. return total;
  359. }
  360. public static async Task PromotionImgLogAsync(PromotionQueryDTO response)
  361. {
  362. try
  363. {
  364. var ts = DateTime.Now - response.create_time;
  365. response.elapsedTime = (int)ts.TotalMilliseconds;
  366. _ = RedisHelper.RPushAsync(promotion_img_key, response);
  367. //using var connection = DBContext.GetOpenConnection();
  368. //connection.Insert(response);
  369. savePromotionCache(response.accountId, response.accountName, response.success, response.message, response.reason);
  370. }
  371. catch (Exception ex) { }
  372. }
  373. public static async Task LogAsync(JdDataDTO response)
  374. {
  375. try
  376. {
  377. var ts = DateTime.Now - response.create_time;
  378. response.elapsedTime = (int)ts.TotalMilliseconds;
  379. _ = RedisHelper.RPushAsync(queue_jd_key, response);
  380. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  381. }
  382. catch (Exception ex) { }
  383. }
  384. public static async Task LogAsync(TkDataDTO response, AlimamaPlus? alimamaPlus = null)
  385. {
  386. try
  387. {
  388. var ts = DateTime.Now - response.create_time;
  389. response.elapsedTime = (int)ts.TotalMilliseconds;
  390. _ = RedisHelper.RPushAsync(queue_tb_key, response);
  391. saveCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  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. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  438. if (!string.IsNullOrEmpty(response.itemId)) TkOrderTrackingCore.SaveLinkSummary(response);
  439. if (!response.success && "nologin".Equals(response.message))
  440. {
  441. switch (response.channel)
  442. {
  443. case TkChannelEnum.tb:
  444. await Task.Run(() =>
  445. {
  446. if (alimamaPlus != null)
  447. {
  448. (bool success, string message) = alimamaPlus.RenewCookie();
  449. if (success) return;
  450. }
  451. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
  452. }); break;
  453. }
  454. }
  455. }
  456. catch (Exception ex)
  457. {
  458. _ = new LoggerLibrary("unionParse", "database_error")
  459. .Info(response.rawContent)
  460. .Info(response.Convert2Json())
  461. .Info(ex.Message, ex.StackTrace)
  462. .SaveAsync();
  463. }
  464. }
  465. public static async Task CouponLogAsync(UnionCouponDTO response, AlimamaPlus? alimamaPlus = null)
  466. {
  467. try
  468. {
  469. var ts = DateTime.Now - response.create_time;
  470. response.elapsedTime = (int)ts.TotalMilliseconds;
  471. _ = RedisHelper.RPushAsync(queue_coupon_key, response);
  472. saveCouponCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  473. if (!response.success && "nologin".Equals(response.message))
  474. {
  475. switch (response.channel)
  476. {
  477. case TkChannelEnum.tb:
  478. await Task.Run(() =>
  479. {
  480. if (alimamaPlus != null)
  481. {
  482. (bool success, string message) = alimamaPlus.RenewCookie();
  483. if (success) return;
  484. }
  485. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
  486. }); break;
  487. }
  488. }
  489. }
  490. catch (Exception ex)
  491. {
  492. _ = new LoggerLibrary("unionCoupon", "database_error")
  493. .Info(response.rawContent)
  494. .Info(response.Convert2Json())
  495. .Info(ex.Message, ex.StackTrace)
  496. .SaveAsync();
  497. }
  498. }
  499. public static async Task ParseLogAsync(JdDataDTO response)
  500. {
  501. try
  502. {
  503. var ts = DateTime.Now - response.create_time;
  504. response.elapsedTime = (int)ts.TotalMilliseconds;
  505. _ = RedisHelper.RPushAsync(queue_parse_jd_key, response);
  506. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  507. if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  508. if (!response.success && ("nologin".Equals(response.reason) || "未登录".Equals(response.reason)))
  509. {
  510. await Task.Run(() =>
  511. {
  512. JdPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  513. });
  514. //switch (response.channel)
  515. //{
  516. // case TkChannelEnum.tb:
  517. // //await Task.Run(() =>
  518. // //{
  519. // // if (alimamaPlus != null)
  520. // // {
  521. // // (bool success, string message) = alimamaPlus.RenewCookie();
  522. // // if (success) return;
  523. // // }
  524. // // TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  525. // //});
  526. // break;
  527. //}
  528. }
  529. if ("没有匹配账号".Equals(response.reason))
  530. {
  531. JdPoolCore.AccountExhausted();
  532. }
  533. }
  534. catch (Exception ex) { }
  535. }
  536. public static async Task ParseLogAsync(DyDataDTO response)
  537. {
  538. try
  539. {
  540. var ts = DateTime.Now - response.create_time;
  541. response.elapsedTime = (int)ts.TotalMilliseconds;
  542. _ = RedisHelper.RPushAsync(queue_parse_dy_key, response);
  543. saveParseCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  544. }
  545. catch (Exception ex) { }
  546. }
  547. public static async Task ParseLogAsync(ToolParseDataDTO response)
  548. {
  549. try
  550. {
  551. var ts = DateTime.Now - response.create_time;
  552. response.elapsedTime = (int)ts.TotalMilliseconds;
  553. _ = RedisHelper.RPushAsync(queue_parse_tool_key, response);
  554. saveParseCache(response.channel.ToString(), 0, "tool", response.success, response.message, response.reason);
  555. }
  556. catch (Exception ex) { }
  557. }
  558. private static void saveCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  559. {
  560. saveAccountCache("all", success, message, reason);
  561. saveAccountCache($"{channel}", success, message, reason);
  562. saveAccountCache($"{accountName}", success, message, reason);
  563. if (accountId != 0)
  564. {
  565. //todo 放着跑两天,要将读取的地方改成读取accountid
  566. saveAccountCache($"{channel}_{accountId}", success, message, reason);
  567. }
  568. }
  569. private static void saveAccountCache(string accountName, bool success, string message, string reason)
  570. {
  571. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
  572. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
  573. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  574. string result = success ? "success" : "fail";
  575. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  576. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  577. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  578. if (!string.IsNullOrEmpty(message))
  579. {
  580. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  581. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  582. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  583. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  584. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  585. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  586. }
  587. if (!string.IsNullOrEmpty(reason))
  588. {
  589. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  590. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  591. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  592. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  593. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  594. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  595. }
  596. }
  597. private static async Task saveUnionCouponParseCacheAsync(TkDataDTO data)
  598. {
  599. string cacheKey = $":cache:parse:{data.ip}_{data.oaid}_{data.itemId}";
  600. await EndPointCore.ProcessEndPointNodesAsync(node =>
  601. {
  602. if (!node.is_coupon_api) return Task.CompletedTask;
  603. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  604. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  605. redis.Set(cacheKey, 1, 2 * 86400);
  606. return Task.CompletedTask;
  607. });
  608. }
  609. private static void saveClientRequestTotal(TkChannelEnum channel, string ip, string oaid)
  610. {
  611. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  612. RedisHelper.IncrBy(cacheKey);
  613. RedisHelper.Expire(cacheKey, 86400);
  614. if (!string.IsNullOrEmpty(oaid))
  615. {
  616. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  617. RedisHelper.IncrBy(cacheKey);
  618. RedisHelper.Expire(cacheKey, 86400);
  619. }
  620. }
  621. public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
  622. {
  623. if (string.IsNullOrEmpty(oaid)) return 0;
  624. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  625. return RedisHelper.Get<int>(cacheKey);
  626. }
  627. public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
  628. {
  629. if (string.IsNullOrEmpty(ip)) return 0;
  630. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  631. return RedisHelper.Get<int>(cacheKey);
  632. }
  633. private static void saveParseCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  634. {
  635. saveParseAccountCache("all", success, message, reason);
  636. saveParseAccountCache($"{channel}", success, message, reason);
  637. saveParseAccountCache($"{accountName}", success, message, reason);
  638. if (accountId != 0)
  639. {
  640. //todo 放着跑两天,要将读取的地方改成读取accountid
  641. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  642. }
  643. }
  644. private static void saveParseAccountCache(string accountName, bool success, string message, string reason)
  645. {
  646. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  647. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  648. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  649. string result = success ? "success" : "fail";
  650. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  651. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  652. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  653. if (!string.IsNullOrEmpty(message))
  654. {
  655. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  656. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  657. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  658. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  659. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  660. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  661. }
  662. if (!string.IsNullOrEmpty(reason))
  663. {
  664. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  665. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  666. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  667. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  668. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  669. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  670. }
  671. }
  672. private static void savePromotionCache(int accountId, string accountName, bool success, string message, string reason)
  673. {
  674. savePromotionAccountCache("all", success, message, reason);
  675. savePromotionAccountCache($"{accountId}", success, message, reason);
  676. }
  677. private static void savePromotionAccountCache(string accountName, bool success, string message, string reason)
  678. {
  679. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMM}");
  680. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  681. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  682. string result = success ? "success" : "fail";
  683. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  684. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  685. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  686. if (!string.IsNullOrEmpty(message))
  687. {
  688. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  689. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  690. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  691. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  692. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  693. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  694. }
  695. if (!string.IsNullOrEmpty(reason))
  696. {
  697. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  698. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  699. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  700. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  701. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  702. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  703. }
  704. }
  705. private static void saveCouponCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  706. {
  707. saveAccountCouponCache("all", success, message, reason);
  708. saveAccountCouponCache($"{channel}", success, message, reason);
  709. if (accountId != 0)
  710. {
  711. saveAccountCouponCache($"{channel}_{accountId}", success, message, reason);
  712. }
  713. }
  714. private static void saveAccountCouponCache(string accountName, bool success, string message, string reason)
  715. {
  716. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMM}");
  717. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  718. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  719. string result = success ? "success" : "fail";
  720. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  721. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  722. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  723. if (!string.IsNullOrEmpty(message))
  724. {
  725. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  726. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  727. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  728. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  729. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  730. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  731. }
  732. if (!string.IsNullOrEmpty(reason))
  733. {
  734. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  735. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  736. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  737. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  738. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  739. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  740. }
  741. }
  742. public static int GetTotal(string keyname, bool all_node = true)
  743. {
  744. //:coupon_total:tb:20240706
  745. //:coupon_total:tb:success:20240706
  746. //:coupon_total:tb:放弃转链:20240706
  747. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  748. {
  749. if (!node.is_public_api) return 0;
  750. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  751. if (!all_node)
  752. {
  753. if (CenterHub.IsCenter)
  754. {
  755. if (node.is_coupon_api) { return 0; }
  756. }
  757. else
  758. {
  759. if (!node.is_coupon_api) { return 0; }
  760. }
  761. }
  762. #if DEBUG
  763. switch (node.name)
  764. {
  765. case "bj":
  766. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  767. break;
  768. case "gz":
  769. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  770. break;
  771. case "coupon1":
  772. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  773. break;
  774. default: return 0;
  775. }
  776. #endif
  777. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  778. int count = redis.Get<int>(keyname);
  779. return count;
  780. });
  781. return result.Sum();
  782. }
  783. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  784. {
  785. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  786. {
  787. if (!node.is_public_api) return [];
  788. if (string.IsNullOrEmpty(node.redis_server)) return [];
  789. if (!all_node)
  790. {
  791. if (CenterHub.IsCenter)
  792. {
  793. if (node.is_coupon_api) { return []; }
  794. }
  795. else
  796. {
  797. if (!node.is_coupon_api) { return []; }
  798. }
  799. }
  800. #if DEBUG
  801. switch (node.name)
  802. {
  803. case "bj":
  804. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  805. break;
  806. case "gz":
  807. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  808. break;
  809. case "coupon1":
  810. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  811. break;
  812. default: return [];
  813. }
  814. #endif
  815. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  816. string[] message_keys = redis.SMembers(keyname);
  817. return message_keys;
  818. });
  819. string[] message_keys = [];
  820. foreach (var arr in result)
  821. {
  822. message_keys = message_keys.Union(arr).ToArray();
  823. }
  824. return message_keys;
  825. }
  826. //if (item.total_count > 0) continue;
  827. //item.total_count = RedisHelper.Get<int>($":total:all:{item.report_date:yyyyMMdd}");
  828. //if (item.total_count == 0) continue;
  829. //item.success_count = RedisHelper.Get<int>($":total:all:success:{item.report_date:yyyyMMdd}");
  830. //item.abandon_count = RedisHelper.Get<int>($":total:all:放弃转链:{item.report_date:yyyyMMdd}");
  831. }
  832. }