feat(api): статистика созданных шаблонов в БД ПАРР

This commit is contained in:
Mikhail Trubnikov
2024-09-16 10:04:45 +10:00
parent 3f447e8de6
commit 030c8f3c2f
4 changed files with 61 additions and 17 deletions

View File

@@ -208,6 +208,7 @@
public static class StatTemplate public static class StatTemplate
{ {
public const string Get = BaseStat + "/templates/"; public const string Get = BaseStat + "/templates/";
public const string GetForPeriod = BaseStat + "/templates/period";
} }
public static class StatHost public static class StatHost

View File

@@ -0,0 +1,9 @@
namespace PARR.API.Contracts.V1.Responses.Statistics
{
public class StatTemplatePeriodResponse
{
public DateOnly Date { get; set; }
public int CreatedTemplatesCount { get; set; }
}
}

View File

@@ -1,10 +1,13 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal;
using PARR.API.Contracts.V1; using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests.BaseRequests;
using PARR.API.Contracts.V1.Responses.Base; using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Contracts.V1.Responses.Statistics; using PARR.API.Contracts.V1.Responses.Statistics;
using PARR.API.Controllers.V1.Base; using PARR.API.Controllers.V1.Base;
using PARR.BLL.Services.Interfaces;
using PARR.Constants; using PARR.Constants;
using PARR.DAL.Contracts; using PARR.DAL.Contracts;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
@@ -18,12 +21,15 @@ namespace PARR.API.Controllers.V1.Statistics
public class StatTemplateController : BaseApiController public class StatTemplateController : BaseApiController
{ {
private readonly ITemplateService templateService; private readonly ITemplateService templateService;
private readonly ICalendarService calendarService;
public StatTemplateController( public StatTemplateController(
ITemplateService templateService ITemplateService templateService,
ICalendarService calendarService
) )
{ {
this.templateService = templateService; this.templateService = templateService;
this.calendarService = calendarService;
} }
/// <summary> /// <summary>
@@ -33,21 +39,6 @@ namespace PARR.API.Controllers.V1.Statistics
[HttpGet(ApiRoutes.StatTemplate.Get)] [HttpGet(ApiRoutes.StatTemplate.Get)]
public async Task<IActionResult> Get() public async Task<IActionResult> Get()
{ {
//var templates = await templateService.Get()
// .Include(t => t.ApplicationsInWork)
// .Include(t => t.RobotConfigurations)
// .ToListAsync();
//var response = new StatTemplateResponse
//{
// ActivateScheduleCount = templates.Count(x => x.IsActiveSchedule),
// ActivateTemplateCount = templates.Count(x => x.IsActiveTemplate),
// TemplateAgentCount = templates.Count(x => x.ApplicationsInWork!.IsAgent),
// TemplateCount = templates.Count(),
// SyncEsppScheduleCount = templates.Count(x => x.RobotConfigurations.Any(c => c.RobotCode == (int)RobotsEnum.ScheduleOrder && c.TaskStatusCode == (int)TaskStatusEnum.Ok)),
// SyncEsppTemplatesCount = templates.Count(x => x.RobotConfigurations.Any(c => c.RobotCode == (int)RobotsEnum.TemplateOrder && c.TaskStatusCode == (int)TaskStatusEnum.Ok))
//};
var response = new StatTemplateResponse var response = new StatTemplateResponse
{ {
ActivateScheduleCount = await templateService.Get().CountAsync(t => t.IsActiveSchedule), ActivateScheduleCount = await templateService.Get().CountAsync(t => t.IsActiveSchedule),
@@ -60,5 +51,48 @@ namespace PARR.API.Controllers.V1.Statistics
return Ok(new Response<StatTemplateResponse>(response, true)); return Ok(new Response<StatTemplateResponse>(response, true));
} }
/// <summary>
/// Получить статистику созданных шаблонов (расписаний) в БД ПАРР
/// </summary>
/// <param name="timeZoneQuery"></param>
/// <param name="startPeriodDays"></param>
/// <returns></returns>
[HttpGet(ApiRoutes.StatTemplate.GetForPeriod)]
public async Task<IActionResult> GetForPeriod([FromQuery] TimeZoneOffsetClient timeZoneQuery, [FromQuery] int startPeriodDays = 3)
{
var endPeriod = calendarService.GetDateWithTimeZoneOffset(DateTimeOffset.UtcNow, timeZoneQuery.TimeZoneOffsetHours);
var startPeriod = endPeriod.AddDays(-startPeriodDays);
var query = templateService.Get()
.Where(t =>
t.DateCreated.DateTime.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date >= startPeriod.Date
&& t.DateCreated.DateTime.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date <= endPeriod.Date
).GroupBy(t => t.DateCreated.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date)
.Select(t => new
{
Date = t.Key,
CreatedObjs = t.Count()
});
var result = await query.ToListAsync();
var daysList = calendarService.GetDatesForPeriod(startPeriod, endPeriod);
var response = new List<StatTemplatePeriodResponse>();
daysList.ForEach(date => response.Add(new StatTemplatePeriodResponse
{
Date = date,
CreatedTemplatesCount = result.FirstOrDefault(t => DateOnly.FromDateTime(t.Date) == date)?.CreatedObjs ?? 0
}));
response = response.OrderBy(t => t.Date).ToList();
return Ok(new Response<List<StatTemplatePeriodResponse>>(response, true));
}
} }
} }

View File

@@ -26,7 +26,7 @@ namespace PARR.BLL.Services.Implementations
nextStart = nextStart.Add(interval); nextStart = nextStart.Add(interval);
logger.LogInformation($"Следующая дата запуска: {nextStart}"); logger.LogInformation($"Следующая дата запуска: {nextStart}. Интервал: {interval}.");
var intervalOffset = nextStart - DateTimeOffset.UtcNow; var intervalOffset = nextStart - DateTimeOffset.UtcNow;
if (intervalOffset < TimeSpan.Zero) if (intervalOffset < TimeSpan.Zero)