| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using molilian.core;
- using dodohold.core;
- using Microsoft.AspNetCore.Mvc;
- using System.Text.Json;
- using TencentCloud.Ecm.V20190719.Models;
- using YunhuiKit;
- namespace molilian.api.Controllers
- {
- [ApiController]
- [Route("[controller]/[action]")]
- public class TracksController : ControllerBase
- {
- protected IHttpContextAccessor _accessor;
- public TracksController(IHttpContextAccessor accessor)
- {
- _accessor = accessor;
- }
- [HttpGet]
- public async Task<ActionResult> Track([FromQuery] int track_id, [FromQuery] string unique_id, [FromQuery] string scene = "", [FromQuery] int account_id = 0)
- {
- if (track_id <= 0)
- {
- return new APIResult(new { success = false, message = "miss" });
- }
- var link = await TracksCore.GetLinkByIdAsync(track_id);
- if (link == null)
- {
- return new APIResult(new { success = false, message = "not found" });
- }
- string trackScene = TracksCore.ResolveMetricScene(link, scene);
- var dto = new TrackRequestLogDTO
- {
- track_id = track_id,
- account_id = Math.Max(account_id, 0),
- event_type = link.event_type,
- platform = link.platform,
- typename = link.typename,
- scene = trackScene,
- unique_id = unique_id,
- ip = _accessor.HttpContext.GetUserIp(),
- user_agent = Request.Headers.UserAgent.ToString(),
- referer = Request.Headers.Referer.ToString()
- };
- _ = TracksCore.LogTrackRequestAsync(dto);
- _ = TracksCore.TrackAsync(link, trackScene, account_id);
- return new APIResult(new { success = true, message = "ok" });
- }
- [HttpPost]
- public async Task<ActionResult> Create([FromQuery] string eventType, [FromQuery] string platform = "", [FromQuery] string typename = "", [FromQuery] string scene = "", [FromQuery] string uniqueId = "", [FromQuery] string description = "")
- {
- if (string.IsNullOrWhiteSpace(typename)) typename = platform;
- if (string.IsNullOrWhiteSpace(platform)) platform = typename;
- var link = await TracksCore.CreateLinkAsync(eventType, platform, typename, scene, uniqueId, description);
- if (link == null)
- {
- return new APIResult(new { success = false, message = "invalid params or eventType" });
- }
- //var request = _accessor.HttpContext!.Request;
- //string url = $"{request.Scheme}://{request.Host}{link.path}";
- return new APIResult(new { success = true, message = "ok", url = link.path });
- }
- [HttpGet]
- public async Task<ActionResult> Daily([FromQuery] int daysAgo = 0, [FromQuery] string reportDate = "", [FromQuery] bool includeToday = true, [FromQuery] bool includeYesterday = true)
- {
- var dates = new List<DateTime>();
- DateTime today = DateTime.Now.Date;
- if (!string.IsNullOrWhiteSpace(reportDate) && DateTime.TryParse(reportDate, out var parsed))
- {
- dates.Add(parsed.Date);
- if (includeToday && parsed.Date != today) dates.Add(today);
- }
- else
- {
- if (daysAgo > 0)
- {
- var date = today.AddDays(-daysAgo);
- dates.Add(date);
- if (includeToday && date != today) dates.Add(today);
- }
- else
- {
- dates.Add(today);
- if (includeYesterday) dates.Add(today.AddDays(-1));
- }
- }
- int rows = 0;
- var reportDates = new List<string>();
- var details = new List<object>();
- foreach (var date in dates.Distinct())
- {
- int dateRows = await TracksCore.FlushDailyAsync(date);
- rows += dateRows;
- reportDates.Add(date.ToString("yyyy-MM-dd"));
- details.Add(new { reportDate = date.ToString("yyyy-MM-dd"), rows = dateRows });
- }
- return new APIResult(new { success = true, message = "ok", rows, reportDates, details });
- }
- }
- }
|