| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using dodohold.core;
- using Microsoft.AspNetCore.Mvc;
- using molilian.core;
- using System.Text.Json;
- namespace molilian.api.Controllers
- {
- [ApiController]
- [MyAuthorize("admin")]
- [Route("api/[controller]/[action]")]
- public class DeeplinkReportController : ControllerBase
- {
- private const int MaxRangeDays = 90;
- readonly IAuthorizationProvider provider = new AdminProvider();
- protected IHttpContextAccessor _accessor;
- public DeeplinkReportController(IHttpContextAccessor accessor)
- {
- _accessor = accessor;
- }
- [HttpPost]
- public async Task<ActionResult> daily([FromBody] JsonElement form)
- {
- _ = provider.Get(_accessor.HttpContext);
- var queryDate = form.PathReadArray<string>("query_date[]");
- var channelName = form.Read("channel_name", string.Empty).Trim();
- DateTime start = DateTime.Now.Date.AddDays(-6);
- DateTime end = DateTime.Now.Date;
- if (queryDate.Count == 2)
- {
- if (DateTime.TryParse(queryDate[0], out var parsedStart))
- {
- start = parsedStart.Date;
- }
- if (DateTime.TryParse(queryDate[1], out var parsedEnd))
- {
- end = parsedEnd.Date;
- }
- }
- if (end < start)
- {
- (start, end) = (end, start);
- }
- if ((end - start).TotalDays >= MaxRangeDays)
- {
- end = start.AddDays(MaxRangeDays - 1);
- }
- var list = new List<DeeplinkDailyReportRow>();
- var accountName = string.IsNullOrWhiteSpace(channelName)
- ? "tool"
- : channelName;
- for (var date = start; date <= end; date = date.AddDays(1))
- {
- var dateKey = date.ToString("yyyyMMdd");
- var row = new DeeplinkDailyReportRow
- {
- report_date = date.ToString("yyyy-MM-dd"),
- total_count = await TkLogCore.GetTotalAsync($":parse_total:{accountName}:{dateKey}"),
- success_count = await TkLogCore.GetTotalAsync($":parse_total:{accountName}:success:{dateKey}"),
- fail_count = await TkLogCore.GetTotalAsync($":parse_total:{accountName}:fail:{dateKey}")
- };
- list.Add(row);
- }
- list = list.OrderByDescending(item => item.report_date).ToList();
- var summary = new DeeplinkDailyReportSummary
- {
- total_count = list.Sum(item => item.total_count),
- success_count = list.Sum(item => item.success_count),
- fail_count = list.Sum(item => item.fail_count)
- };
- return new APIResult(new
- {
- data = new
- {
- list,
- count = list.Count,
- summary,
- maxRangeDays = MaxRangeDays
- }
- });
- }
- }
- public class DeeplinkDailyReportRow
- {
- public string report_date { get; set; } = string.Empty;
- public long total_count { get; set; } = 0;
- public long success_count { get; set; } = 0;
- public long fail_count { get; set; } = 0;
- }
- public class DeeplinkDailyReportSummary
- {
- public long total_count { get; set; } = 0;
- public long success_count { get; set; } = 0;
- public long fail_count { get; set; } = 0;
- }
- }
|