diff --git a/PARR.API/Contracts/V1/ApiRoutes.cs b/PARR.API/Contracts/V1/ApiRoutes.cs index c15f3ca9..d41ca1d5 100644 --- a/PARR.API/Contracts/V1/ApiRoutes.cs +++ b/PARR.API/Contracts/V1/ApiRoutes.cs @@ -208,6 +208,7 @@ public static class StatTemplate { public const string Get = BaseStat + "/templates/"; + public const string GetForPeriod = BaseStat + "/templates/period"; } public static class StatHost diff --git a/PARR.API/Contracts/V1/Responses/Statistics/StatTemplatePeriodResponse.cs b/PARR.API/Contracts/V1/Responses/Statistics/StatTemplatePeriodResponse.cs new file mode 100644 index 00000000..ba016233 --- /dev/null +++ b/PARR.API/Contracts/V1/Responses/Statistics/StatTemplatePeriodResponse.cs @@ -0,0 +1,9 @@ +namespace PARR.API.Contracts.V1.Responses.Statistics +{ + public class StatTemplatePeriodResponse + { + public DateOnly Date { get; set; } + + public int CreatedTemplatesCount { get; set; } + } +} diff --git a/PARR.API/Controllers/V1/Statistics/StatTemplateController.cs b/PARR.API/Controllers/V1/Statistics/StatTemplateController.cs index f01f9fbc..75ad2b18 100644 --- a/PARR.API/Controllers/V1/Statistics/StatTemplateController.cs +++ b/PARR.API/Controllers/V1/Statistics/StatTemplateController.cs @@ -1,10 +1,13 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Npgsql.EntityFrameworkCore.PostgreSQL.Query.Expressions.Internal; 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.Statistics; using PARR.API.Controllers.V1.Base; +using PARR.BLL.Services.Interfaces; using PARR.Constants; using PARR.DAL.Contracts; using PARR.DAL.Services.Interfaces; @@ -18,12 +21,15 @@ namespace PARR.API.Controllers.V1.Statistics public class StatTemplateController : BaseApiController { private readonly ITemplateService templateService; + private readonly ICalendarService calendarService; public StatTemplateController( - ITemplateService templateService + ITemplateService templateService, + ICalendarService calendarService ) { this.templateService = templateService; + this.calendarService = calendarService; } /// @@ -33,21 +39,6 @@ namespace PARR.API.Controllers.V1.Statistics [HttpGet(ApiRoutes.StatTemplate.Get)] public async Task 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 { ActivateScheduleCount = await templateService.Get().CountAsync(t => t.IsActiveSchedule), @@ -60,5 +51,48 @@ namespace PARR.API.Controllers.V1.Statistics return Ok(new Response(response, true)); } + + + /// + /// Получить статистику созданных шаблонов (расписаний) в БД ПАРР + /// + /// + /// + /// + [HttpGet(ApiRoutes.StatTemplate.GetForPeriod)] + public async Task 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(); + + 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>(response, true)); + } + + } } diff --git a/PARR.BLL/Services/Implementations/IntervalService.cs b/PARR.BLL/Services/Implementations/IntervalService.cs index a39f8ef5..3534c131 100644 --- a/PARR.BLL/Services/Implementations/IntervalService.cs +++ b/PARR.BLL/Services/Implementations/IntervalService.cs @@ -26,7 +26,7 @@ namespace PARR.BLL.Services.Implementations nextStart = nextStart.Add(interval); - logger.LogInformation($"Следующая дата запуска: {nextStart}"); + logger.LogInformation($"Следующая дата запуска: {nextStart}. Интервал: {interval}."); var intervalOffset = nextStart - DateTimeOffset.UtcNow; if (intervalOffset < TimeSpan.Zero)