DeeplinkReportController.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using dodohold.core;
  2. using Microsoft.AspNetCore.Mvc;
  3. using molilian.core;
  4. using System.Text.Json;
  5. namespace molilian.api.Controllers
  6. {
  7. [ApiController]
  8. [MyAuthorize("admin")]
  9. [Route("api/[controller]/[action]")]
  10. public class DeeplinkReportController : ControllerBase
  11. {
  12. private const int MaxRangeDays = 90;
  13. readonly IAuthorizationProvider provider = new AdminProvider();
  14. protected IHttpContextAccessor _accessor;
  15. public DeeplinkReportController(IHttpContextAccessor accessor)
  16. {
  17. _accessor = accessor;
  18. }
  19. [HttpPost]
  20. public async Task<ActionResult> daily([FromBody] JsonElement form)
  21. {
  22. _ = provider.Get(_accessor.HttpContext);
  23. var queryDate = form.PathReadArray<string>("query_date[]");
  24. var channelName = form.Read("channel_name", string.Empty).Trim();
  25. DateTime start = DateTime.Now.Date.AddDays(-6);
  26. DateTime end = DateTime.Now.Date;
  27. if (queryDate.Count == 2)
  28. {
  29. if (DateTime.TryParse(queryDate[0], out var parsedStart))
  30. {
  31. start = parsedStart.Date;
  32. }
  33. if (DateTime.TryParse(queryDate[1], out var parsedEnd))
  34. {
  35. end = parsedEnd.Date;
  36. }
  37. }
  38. if (end < start)
  39. {
  40. (start, end) = (end, start);
  41. }
  42. if ((end - start).TotalDays >= MaxRangeDays)
  43. {
  44. end = start.AddDays(MaxRangeDays - 1);
  45. }
  46. var list = new List<DeeplinkDailyReportRow>();
  47. var accountName = string.IsNullOrWhiteSpace(channelName)
  48. ? "tool"
  49. : channelName;
  50. for (var date = start; date <= end; date = date.AddDays(1))
  51. {
  52. var dateKey = date.ToString("yyyyMMdd");
  53. var row = new DeeplinkDailyReportRow
  54. {
  55. report_date = date.ToString("yyyy-MM-dd"),
  56. total_count = await TkLogCore.GetTotalAsync($":parse_total:{accountName}:{dateKey}"),
  57. success_count = await TkLogCore.GetTotalAsync($":parse_total:{accountName}:success:{dateKey}"),
  58. fail_count = await TkLogCore.GetTotalAsync($":parse_total:{accountName}:fail:{dateKey}")
  59. };
  60. list.Add(row);
  61. }
  62. list = list.OrderByDescending(item => item.report_date).ToList();
  63. var summary = new DeeplinkDailyReportSummary
  64. {
  65. total_count = list.Sum(item => item.total_count),
  66. success_count = list.Sum(item => item.success_count),
  67. fail_count = list.Sum(item => item.fail_count)
  68. };
  69. return new APIResult(new
  70. {
  71. data = new
  72. {
  73. list,
  74. count = list.Count,
  75. summary,
  76. maxRangeDays = MaxRangeDays
  77. }
  78. });
  79. }
  80. }
  81. public class DeeplinkDailyReportRow
  82. {
  83. public string report_date { get; set; } = string.Empty;
  84. public long total_count { get; set; } = 0;
  85. public long success_count { get; set; } = 0;
  86. public long fail_count { get; set; } = 0;
  87. }
  88. public class DeeplinkDailyReportSummary
  89. {
  90. public long total_count { get; set; } = 0;
  91. public long success_count { get; set; } = 0;
  92. public long fail_count { get; set; } = 0;
  93. }
  94. }