ChartDataCore.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 System.Threading.Channels;
  10. namespace molilian.core
  11. {
  12. public partial class ChartDataCore
  13. {
  14. public class ChartDataItem
  15. {
  16. public string name { get; set; } = string.Empty;
  17. public int value { get; set; } = 0;
  18. public decimal increases { get; set; } = 0;
  19. }
  20. public class ChartDataRes
  21. {
  22. public List<ChartDataItem> data { get; set; } = [];
  23. public List<ChartDataItem> data2 { get; set; } = [];
  24. public List<ChartDataItem> data3 { get; set; } = [];
  25. public int total { get; set; } = 0;
  26. }
  27. public static ChartDataRes GetJdData(string total_key, string accountName, string report_date)
  28. {
  29. ChartDataRes result = new();
  30. total_key = "parse_total";
  31. if (string.IsNullOrEmpty(accountName) || "all".Equals(accountName)) accountName = "jd";
  32. string cacheKey = $":{total_key}:{accountName}:{report_date}";
  33. result.total = TkLogCore.GetTotal(cacheKey);
  34. if (result.total > 0)
  35. {
  36. string[] keys = [];
  37. cacheKey = $":{total_key}:{accountName}:message:{report_date}";
  38. string[] message_keys = TkLogCore.GetTotalKeys(cacheKey);
  39. var combinedKeys = keys.Union(message_keys).ToList();
  40. combinedKeys.RemoveAll(item => item == "fail");
  41. combinedKeys.RemoveAll(item => item == "OK");
  42. foreach (var keyname in combinedKeys)
  43. {
  44. cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
  45. int value = TkLogCore.GetTotal(cacheKey);
  46. if (value == 0) continue;
  47. result.data.Add(new ChartDataItem
  48. {
  49. name = keyname,
  50. value = value,
  51. increases = Math.Round((decimal)value / result.total * 100, 2)
  52. });
  53. }
  54. result.data = result.data.OrderByDescending(item => item.value).ToList();
  55. cacheKey = $":{total_key}:{accountName}:reason:{report_date}";
  56. string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);
  57. foreach (var keyname in reason_keys)
  58. {
  59. if ("没有优惠券".Equals(keyname)) continue;
  60. cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
  61. int value = TkLogCore.GetTotal(cacheKey);
  62. //if (value < 50) continue;
  63. result.data2.Add(new ChartDataItem
  64. {
  65. name = keyname,
  66. value = value,
  67. increases = Math.Round((decimal)value / result.total * 100, 2)
  68. });
  69. }
  70. result.data2 = result.data2.OrderByDescending(item => item.value).ToList();
  71. string[] deeplink_keys = ["none", "home", "success", "fail"];
  72. foreach (var keyname in deeplink_keys)
  73. {
  74. cacheKey = $":{total_key}:dp_{keyname}:{accountName}:{report_date}";
  75. int value = TkLogCore.GetTotal(cacheKey);
  76. if (value == 0) continue;
  77. result.data3.Add(new ChartDataItem
  78. {
  79. name = keyname,
  80. value = value,
  81. increases = Math.Round((decimal)value / result.total * 100, 2)
  82. });
  83. }
  84. result.data3 = result.data3.OrderByDescending(item => item.value).ToList();
  85. }
  86. return result;
  87. }
  88. public static ChartDataRes GetTaobaoData(string total_key, string accountName, string report_date)
  89. {
  90. ChartDataRes result = new();
  91. if (string.IsNullOrEmpty(accountName) || "all".Equals(accountName)) accountName = "tb";
  92. string cacheKey = $":{total_key}:{accountName}:{report_date}";
  93. result.total = TkLogCore.GetTotal(cacheKey);
  94. if (result.total > 0)
  95. {
  96. string[] keys = ["success"];
  97. cacheKey = $":{total_key}:{accountName}:message:{report_date}";
  98. string[] message_keys = TkLogCore.GetTotalKeys(cacheKey);
  99. var combinedKeys = keys.Union(message_keys).ToList();
  100. combinedKeys.RemoveAll(item => item == "fail");
  101. combinedKeys.RemoveAll(item => item == "OK");
  102. foreach (var keyname in combinedKeys)
  103. {
  104. cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
  105. int value = TkLogCore.GetTotal(cacheKey);
  106. if (value == 0) continue;
  107. result.data.Add(new ChartDataItem
  108. {
  109. name = keyname,
  110. value = value,
  111. increases = Math.Round((decimal)value / result.total * 100, 2)
  112. });
  113. }
  114. result.data = result.data.OrderByDescending(item => item.value).ToList();
  115. cacheKey = $":{total_key}:{accountName}:reason:{report_date}";
  116. string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);
  117. foreach (var keyname in reason_keys)
  118. {
  119. if ("没有优惠券".Equals(keyname)) continue;
  120. cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
  121. int value = TkLogCore.GetTotal(cacheKey);
  122. //if (value < 50) continue;
  123. result.data2.Add(new ChartDataItem
  124. {
  125. name = keyname,
  126. value = value,
  127. increases = Math.Round((decimal)value / result.total * 100, 2)
  128. });
  129. }
  130. result.data2 = result.data2.OrderByDescending(item => item.value).ToList();
  131. string[] deeplink_keys = ["none", "home", "success", "fail"];
  132. foreach (var keyname in deeplink_keys)
  133. {
  134. cacheKey = $":{total_key}:dp_{keyname}:{accountName}:{report_date}";
  135. int value = TkLogCore.GetTotal(cacheKey);
  136. if (value == 0) continue;
  137. result.data3.Add(new ChartDataItem
  138. {
  139. name = keyname,
  140. value = value,
  141. increases = Math.Round((decimal)value / result.total * 100, 2)
  142. });
  143. }
  144. result.data3 = result.data3.OrderByDescending(item => item.value).ToList();
  145. }
  146. return result;
  147. }
  148. public static ChartDataRes GetTaobaoCouponData(string total_key, string accountName, string report_date)
  149. {
  150. ChartDataRes result = new();
  151. if (string.IsNullOrEmpty(accountName) || "all".Equals(accountName)) accountName = "tb";
  152. string cacheKey = $":{total_key}:{accountName}:{report_date}";
  153. result.total = TkLogCore.GetTotal(cacheKey, false);
  154. if (result.total > 0)
  155. {
  156. string[] keys = ["success"];
  157. cacheKey = $":{total_key}:{accountName}:message:{report_date}";
  158. string[] message_keys = TkLogCore.GetTotalKeys(cacheKey, false);
  159. var combinedKeys = keys.Union(message_keys).ToList();
  160. combinedKeys.RemoveAll(item => item == "fail");
  161. combinedKeys.RemoveAll(item => item == "OK");
  162. foreach (var keyname in combinedKeys)
  163. {
  164. cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
  165. int value = TkLogCore.GetTotal(cacheKey, false);
  166. if (value == 0) continue;
  167. result.data.Add(new ChartDataItem
  168. {
  169. name = keyname,
  170. value = value,
  171. increases = Math.Round((decimal)value / result.total * 100, 2)
  172. });
  173. }
  174. result.data = result.data.OrderByDescending(item => item.value).ToList();
  175. cacheKey = $":{total_key}:{accountName}:reason:{report_date}";
  176. string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey, false);
  177. foreach (var keyname in reason_keys)
  178. {
  179. if ("没有优惠券".Equals(keyname)) continue;
  180. cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
  181. int value = TkLogCore.GetTotal(cacheKey, false);
  182. //if (value < 50) continue;
  183. result.data2.Add(new ChartDataItem
  184. {
  185. name = keyname,
  186. value = value,
  187. increases = Math.Round((decimal)value / result.total * 100, 2)
  188. });
  189. }
  190. result.data2 = result.data2.OrderByDescending(item => item.value).ToList();
  191. }
  192. return result;
  193. }
  194. public static ChartDataRes GetToolData(string total_key, string accountName, string report_date)
  195. {
  196. ChartDataRes result = new();
  197. accountName = "tool";
  198. total_key = "parse_total";
  199. string cacheKey = $":{total_key}:{accountName}:{report_date}";
  200. result.total = TkLogCore.GetTotal(cacheKey);
  201. if (result.total > 0)
  202. {
  203. string[] keys = ["bdpan", "wemeet"];
  204. foreach (var keyname in keys)
  205. {
  206. cacheKey = $":{total_key}:{keyname}:{report_date}";
  207. var total = TkLogCore.GetTotal(cacheKey);
  208. cacheKey = $":{total_key}:{keyname}:success:{report_date}";
  209. int value = TkLogCore.GetTotal(cacheKey);
  210. if (value == 0) continue;
  211. result.data.Add(new ChartDataItem
  212. {
  213. name = $"{keyname}-成功",
  214. value = value,
  215. increases = Math.Round((decimal)value / total * 100, 2)
  216. });
  217. cacheKey = $":{total_key}:{keyname}:fail:{report_date}";
  218. value = TkLogCore.GetTotal(cacheKey);
  219. if (value == 0) continue;
  220. result.data.Add(new ChartDataItem
  221. {
  222. name = $"{keyname}-失败",
  223. value = value,
  224. increases = Math.Round((decimal)value / total * 100, 2)
  225. });
  226. }
  227. cacheKey = $":{total_key}:{accountName}:reason:{report_date}";
  228. string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);
  229. foreach (var keyname in reason_keys)
  230. {
  231. cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
  232. int value = TkLogCore.GetTotal(cacheKey);
  233. if (value == 0) continue;
  234. result.data2.Add(new ChartDataItem
  235. {
  236. name = keyname,
  237. value = value,
  238. increases = Math.Round((decimal)value / result.total * 100, 2)
  239. });
  240. }
  241. result.data2 = result.data2.OrderByDescending(item => item.value).ToList();
  242. string[] deeplink_keys = ["none", "home", "success", "fail"];
  243. foreach (var keyname in deeplink_keys)
  244. {
  245. cacheKey = $":{total_key}:dp_{keyname}:{accountName}:{report_date}";
  246. int value = TkLogCore.GetTotal(cacheKey);
  247. if (value == 0) continue;
  248. result.data3.Add(new ChartDataItem
  249. {
  250. name = keyname,
  251. value = value,
  252. increases = Math.Round((decimal)value / result.total * 100, 2)
  253. });
  254. }
  255. result.data3 = result.data3.OrderByDescending(item => item.value).ToList();
  256. }
  257. //string dp_flag = deeplink switch
  258. //{
  259. // "" => "none",
  260. // "tbopen://m.taobao.com/tbopen/index.html" or
  261. // "pinduoduo://com.xunmeng.pinduoduo/" or
  262. // "snssdk1128://feed?refer=web" or
  263. // "bdnetdisk://n/action.EXTERNAL_ACTIVITY" or
  264. // "openapp.jdmobile://virtual?params=" => "home",
  265. // _ => success ? "success" : "fail",
  266. //};
  267. ////关于dp的缓存
  268. //saveParseAccountCache($"dp_{dp_flag}:all", success, message, reason);
  269. //saveParseAccountCache($"dp_{dp_flag}:{channel}", success, message, reason);
  270. //if (accountId != 0)
  271. //{
  272. // saveParseAccountCache($"dp_{dp_flag}:{channel}_{accountId}", success, message, reason);
  273. //}
  274. return result;
  275. }
  276. //public static void GetTaobaoData(string accountName, string report_date)
  277. //{
  278. // if (string.IsNullOrEmpty(accountName)) accountName = "tb";
  279. // if (total_key == "tool_total")
  280. // {
  281. // accountName = "tool";
  282. // total_key = "parse_total";
  283. // string cacheKey = $":{total_key}:{accountName}:{_report_date}";
  284. // double total = TkLogCore.GetTotal(cacheKey);
  285. // if (total > 0)
  286. // {
  287. // string[] keys = ["bdpan", "wemeet"];
  288. // foreach (var keyname in keys)
  289. // {
  290. // cacheKey = $":{total_key}:{keyname}:{_report_date}";
  291. // total = TkLogCore.GetTotal(cacheKey);
  292. // cacheKey = $":{total_key}:{keyname}:success:{_report_date}";
  293. // int value = TkLogCore.GetTotal(cacheKey);
  294. // if (value == 0) continue;
  295. // result.Add(new { name = $"{keyname}-成功", value, increases = Math.Round(value / total * 100, 2) });
  296. // cacheKey = $":{total_key}:{keyname}:fail:{_report_date}";
  297. // value = TkLogCore.GetTotal(cacheKey);
  298. // if (value == 0) continue;
  299. // result.Add(new { name = $"{keyname}-失败", value, increases = Math.Round(value / total * 100, 2) });
  300. // }
  301. // cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
  302. // string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);
  303. // foreach (var keyname in reason_keys)
  304. // {
  305. // cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  306. // int value = TkLogCore.GetTotal(cacheKey);
  307. // if (value == 0) continue;
  308. // reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  309. // }
  310. // reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  311. // }
  312. // return new APIResult(new { total, data = result, data2 = reason_result });
  313. // }
  314. // else
  315. // {
  316. // string cacheKey = $":{total_key}:{accountName}:{_report_date}";
  317. // double total = TkLogCore.GetTotal(cacheKey);
  318. // if (total > 0)
  319. // {
  320. // string[] keys = ["success"];
  321. // cacheKey = $":{total_key}:{accountName}:message:{_report_date}";
  322. // string[] message_keys = TkLogCore.GetTotalKeys(cacheKey);
  323. // var combinedKeys = keys.Union(message_keys).ToList();
  324. // combinedKeys.RemoveAll(item => item == "fail");
  325. // foreach (var keyname in combinedKeys)
  326. // {
  327. // cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  328. // int value = TkLogCore.GetTotal(cacheKey);
  329. // if (value == 0) continue;
  330. // result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  331. // }
  332. // result = result.OrderByDescending(item => item.value).ToList();
  333. // cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
  334. // string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);
  335. // foreach (var keyname in reason_keys)
  336. // {
  337. // cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
  338. // int value = TkLogCore.GetTotal(cacheKey);
  339. // if (value < 50) continue;
  340. // reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
  341. // }
  342. // reason_result = reason_result.OrderByDescending(item => item.value).ToList();
  343. // }
  344. // return new APIResult(new { total, data = result, data2 = reason_result });
  345. // }
  346. //}
  347. }
  348. }