TracksController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using molilian.core;
  2. using dodohold.core;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text.Json;
  5. using TencentCloud.Ecm.V20190719.Models;
  6. using YunhuiKit;
  7. namespace molilian.api.Controllers
  8. {
  9. [ApiController]
  10. [Route("[controller]/[action]")]
  11. public class TracksController : ControllerBase
  12. {
  13. protected IHttpContextAccessor _accessor;
  14. public TracksController(IHttpContextAccessor accessor)
  15. {
  16. _accessor = accessor;
  17. }
  18. [HttpGet]
  19. public async Task<ActionResult> Track([FromQuery] int track_id, [FromQuery] string unique_id)
  20. {
  21. if (track_id <= 0)
  22. {
  23. return new APIResult(new { success = false, message = "miss" });
  24. }
  25. var link = await TracksCore.GetLinkByIdAsync(track_id);
  26. if (link == null)
  27. {
  28. return new APIResult(new { success = false, message = "not found" });
  29. }
  30. var dto = new TrackRequestLogDTO
  31. {
  32. track_id = track_id,
  33. event_type = link.event_type,
  34. platform = link.platform,
  35. typename = link.typename,
  36. scene = link.scene,
  37. unique_id = unique_id,
  38. ip = _accessor.HttpContext.GetUserIp(),
  39. user_agent = Request.Headers.UserAgent.ToString(),
  40. referer = Request.Headers.Referer.ToString()
  41. };
  42. _ = TracksCore.LogTrackRequestAsync(dto);
  43. _ = TracksCore.TrackAsync(link);
  44. return new APIResult(new { success = true, message = "ok" });
  45. }
  46. [HttpPost]
  47. public async Task<ActionResult> Create([FromQuery] string eventType, [FromQuery] string platform = "", [FromQuery] string typename = "", [FromQuery] string scene = "", [FromQuery] string uniqueId = "", [FromQuery] string description = "")
  48. {
  49. if (string.IsNullOrWhiteSpace(typename)) typename = platform;
  50. if (string.IsNullOrWhiteSpace(platform)) platform = typename;
  51. var link = await TracksCore.CreateLinkAsync(eventType, platform, typename, scene, uniqueId, description);
  52. if (link == null)
  53. {
  54. return new APIResult(new { success = false, message = "invalid params or eventType" });
  55. }
  56. //var request = _accessor.HttpContext!.Request;
  57. //string url = $"{request.Scheme}://{request.Host}{link.path}";
  58. return new APIResult(new { success = true, message = "ok", url = link.path });
  59. }
  60. [HttpGet]
  61. public async Task<ActionResult> Daily([FromQuery] int daysAgo = 0, [FromQuery] string reportDate = "", [FromQuery] bool includeToday = false, [FromQuery] bool includeYesterday = true)
  62. {
  63. var dates = new List<DateTime>();
  64. DateTime today = DateTime.Now.Date;
  65. if (!string.IsNullOrWhiteSpace(reportDate) && DateTime.TryParse(reportDate, out var parsed))
  66. {
  67. dates.Add(parsed.Date);
  68. if (includeToday && parsed.Date != today) dates.Add(today);
  69. }
  70. else
  71. {
  72. if (daysAgo > 0)
  73. {
  74. var date = today.AddDays(-daysAgo);
  75. dates.Add(date);
  76. if (includeToday && date != today) dates.Add(today);
  77. }
  78. else
  79. {
  80. dates.Add(today);
  81. if (includeYesterday) dates.Add(today.AddDays(-1));
  82. }
  83. }
  84. int rows = 0;
  85. var reportDates = new List<string>();
  86. foreach (var date in dates.Distinct())
  87. {
  88. rows += await TracksCore.FlushDailyAsync(date);
  89. reportDates.Add(date.ToString("yyyy-MM-dd"));
  90. }
  91. return new APIResult(new { success = true, message = "ok", rows, reportDates });
  92. }
  93. }
  94. }