feat(api): статистика созданных шаблонов в БД ПАРР
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace PARR.API.Contracts.V1.Responses.Statistics
|
||||
{
|
||||
public class StatTemplatePeriodResponse
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int CreatedTemplatesCount { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -33,21 +39,6 @@ namespace PARR.API.Controllers.V1.Statistics
|
||||
[HttpGet(ApiRoutes.StatTemplate.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
|
||||
{
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
/// <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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user