TracksController.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, [FromQuery] string scene = "", [FromQuery] int account_id = 0)
  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. string trackScene = TracksCore.ResolveMetricScene(link, scene);
  31. var dto = new TrackRequestLogDTO
  32. {
  33. track_id = track_id,
  34. account_id = Math.Max(account_id, 0),
  35. event_type = link.event_type,
  36. platform = link.platform,
  37. typename = link.typename,
  38. scene = trackScene,
  39. unique_id = unique_id,
  40. ip = _accessor.HttpContext.GetUserIp(),
  41. user_agent = Request.Headers.UserAgent.ToString(),
  42. referer = Request.Headers.Referer.ToString()
  43. };
  44. _ = TracksCore.LogTrackRequestAsync(dto);
  45. _ = TracksCore.TrackAsync(link, trackScene, account_id);
  46. return new APIResult(new { success = true, message = "ok" });
  47. }
  48. [HttpPost]
  49. public async Task<ActionResult> Create([FromQuery] string eventType, [FromQuery] string platform = "", [FromQuery] string typename = "", [FromQuery] string scene = "", [FromQuery] string uniqueId = "", [FromQuery] string description = "")
  50. {
  51. if (string.IsNullOrWhiteSpace(typename)) typename = platform;
  52. if (string.IsNullOrWhiteSpace(platform)) platform = typename;
  53. var link = await TracksCore.CreateLinkAsync(eventType, platform, typename, scene, uniqueId, description);
  54. if (link == null)
  55. {
  56. return new APIResult(new { success = false, message = "invalid params or eventType" });
  57. }
  58. //var request = _accessor.HttpContext!.Request;
  59. //string url = $"{request.Scheme}://{request.Host}{link.path}";
  60. return new APIResult(new { success = true, message = "ok", url = link.path });
  61. }
  62. [HttpGet]
  63. public async Task<ActionResult> Daily([FromQuery] int daysAgo = 0, [FromQuery] string reportDate = "", [FromQuery] bool includeToday = true, [FromQuery] bool includeYesterday = true)
  64. {
  65. var dates = new List<DateTime>();
  66. DateTime today = DateTime.Now.Date;
  67. if (!string.IsNullOrWhiteSpace(reportDate) && DateTime.TryParse(reportDate, out var parsed))
  68. {
  69. dates.Add(parsed.Date);
  70. if (includeToday && parsed.Date != today) dates.Add(today);
  71. }
  72. else
  73. {
  74. if (daysAgo > 0)
  75. {
  76. var date = today.AddDays(-daysAgo);
  77. dates.Add(date);
  78. if (includeToday && date != today) dates.Add(today);
  79. }
  80. else
  81. {
  82. dates.Add(today);
  83. if (includeYesterday) dates.Add(today.AddDays(-1));
  84. }
  85. }
  86. int rows = 0;
  87. var reportDates = new List<string>();
  88. var details = new List<object>();
  89. foreach (var date in dates.Distinct())
  90. {
  91. int dateRows = await TracksCore.FlushDailyAsync(date);
  92. rows += dateRows;
  93. reportDates.Add(date.ToString("yyyy-MM-dd"));
  94. details.Add(new { reportDate = date.ToString("yyyy-MM-dd"), rows = dateRows });
  95. }
  96. return new APIResult(new { success = true, message = "ok", rows, reportDates, details });
  97. }
  98. }
  99. }