2023-12-20 13:43:03 +10:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using PARR.API.Contracts.V1;
|
2024-08-30 16:40:06 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Requests.BaseRequests;
|
2023-12-20 13:43:03 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Responses.Base;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Responses.Statistics;
|
|
|
|
|
|
using PARR.API.Controllers.V1.Base;
|
2024-08-30 16:40:06 +10:00
|
|
|
|
using PARR.BLL.Services.Interfaces;
|
2023-12-20 13:43:03 +10:00
|
|
|
|
using PARR.Constants;
|
2025-06-25 14:17:34 +10:00
|
|
|
|
using PARR.DAL.Services.Interfaces.Unit;
|
2023-12-20 13:43:03 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.API.Controllers.V1.Statistics
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-08-26 14:04:49 +10:00
|
|
|
|
/// Статистика по юнитам (ЭК)
|
2023-12-20 13:43:03 +10:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
2025-08-26 14:04:49 +10:00
|
|
|
|
public class StatUnitController : BaseApiController
|
2023-12-20 13:43:03 +10:00
|
|
|
|
{
|
2025-06-25 14:17:34 +10:00
|
|
|
|
private readonly IUnitService unitService;
|
2024-08-30 16:40:06 +10:00
|
|
|
|
private readonly ICalendarService calendarService;
|
2023-12-20 13:43:03 +10:00
|
|
|
|
|
2025-08-26 14:04:49 +10:00
|
|
|
|
public StatUnitController(
|
2025-06-25 14:17:34 +10:00
|
|
|
|
IUnitService unitService,
|
|
|
|
|
|
ICalendarService calendarService
|
|
|
|
|
|
)
|
2023-12-20 13:43:03 +10:00
|
|
|
|
{
|
2025-06-25 14:17:34 +10:00
|
|
|
|
this.unitService = unitService;
|
2024-08-30 16:40:06 +10:00
|
|
|
|
this.calendarService = calendarService;
|
2023-12-20 13:43:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-08-26 14:04:49 +10:00
|
|
|
|
/// Статистика синхронизации юнитов (ЭК) с АИХИТ за последние сутки
|
2023-12-20 13:43:03 +10:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2025-08-26 14:04:49 +10:00
|
|
|
|
[HttpGet(ApiRoutes.StatUnit.Get)]
|
2023-12-20 13:43:03 +10:00
|
|
|
|
public async Task<IActionResult> Get()
|
|
|
|
|
|
{
|
|
|
|
|
|
var queryDate = DateTimeOffset.UtcNow.AddDays(-1);
|
|
|
|
|
|
|
2025-08-26 14:04:49 +10:00
|
|
|
|
var response = new StatUnitResponse
|
2023-12-20 13:43:03 +10:00
|
|
|
|
{
|
2025-08-26 14:04:49 +10:00
|
|
|
|
AllHosts = await unitService.Get().CountAsync(),
|
|
|
|
|
|
CreatedLastDay = await unitService.Get().CountAsync(t => t.DateCreated >= queryDate),
|
|
|
|
|
|
//UpdatedLastDay = await hostService.Get().CountAsync(t => t.DateModified.HasValue && t.DateModified.Value >= queryDate)
|
|
|
|
|
|
UpdatedLastDay = await unitService.Get()
|
|
|
|
|
|
.Include(t => t.UnitValues)
|
|
|
|
|
|
.Where(t => t.UnitValues.Any(v => v.DateModified.HasValue && v.DateModified.Value >= queryDate))
|
|
|
|
|
|
.CountAsync()
|
2023-12-20 13:43:03 +10:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-26 14:04:49 +10:00
|
|
|
|
return Ok(new Response<StatUnitResponse>(response, true));
|
2023-12-20 13:43:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-30 16:40:06 +10:00
|
|
|
|
/// <summary>
|
2025-08-26 14:04:49 +10:00
|
|
|
|
/// Получить статистику созданных юнитов (ЭК) за период
|
2024-08-30 16:40:06 +10:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="timeZoneQuery"></param>
|
|
|
|
|
|
/// <param name="startPeriodDays"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-08-26 14:04:49 +10:00
|
|
|
|
[HttpGet(ApiRoutes.StatUnit.GetForPeriod)]
|
2024-08-30 16:40:06 +10:00
|
|
|
|
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);
|
|
|
|
|
|
|
2025-08-26 14:04:49 +10:00
|
|
|
|
var query = unitService.Get()
|
2024-09-04 09:30:57 +10:00
|
|
|
|
.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)
|
2024-08-30 16:40:06 +10:00
|
|
|
|
.Select(t => new
|
|
|
|
|
|
{
|
|
|
|
|
|
Date = t.Key,
|
2025-08-26 14:04:49 +10:00
|
|
|
|
UnitCount = t.Count()
|
2024-08-30 16:40:06 +10:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
var result = await query.ToListAsync();
|
|
|
|
|
|
|
|
|
|
|
|
var daysList = calendarService.GetDatesForPeriod(startPeriod, endPeriod);
|
|
|
|
|
|
|
2025-08-26 14:04:49 +10:00
|
|
|
|
var response = new List<StatUnitPeriodResponse>();
|
2024-08-30 16:40:06 +10:00
|
|
|
|
|
|
|
|
|
|
daysList.ForEach(date =>
|
|
|
|
|
|
{
|
2025-08-26 14:04:49 +10:00
|
|
|
|
response.Add(new StatUnitPeriodResponse
|
2024-08-30 16:40:06 +10:00
|
|
|
|
{
|
|
|
|
|
|
Date = date,
|
2025-08-26 14:04:49 +10:00
|
|
|
|
CreatedUnitsCount = result.FirstOrDefault(t => DateOnly.FromDateTime(t.Date) == date)?.UnitCount ?? 0
|
2024-08-30 16:40:06 +10:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
response = response.OrderBy(t => t.Date).ToList();
|
|
|
|
|
|
|
2025-08-26 14:04:49 +10:00
|
|
|
|
return Ok(new Response<List<StatUnitPeriodResponse>>(response, true));
|
2024-08-30 16:40:06 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-20 13:43:03 +10:00
|
|
|
|
/// <summary>
|
2025-08-26 14:04:49 +10:00
|
|
|
|
/// Количество юнитов (ЭК) в шаблонах
|
2023-12-20 13:43:03 +10:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2025-08-26 14:04:49 +10:00
|
|
|
|
[HttpGet(ApiRoutes.StatUnit.GetWithTemplates)]
|
2023-12-20 13:43:03 +10:00
|
|
|
|
public async Task<IActionResult> GetWithTemplates()
|
|
|
|
|
|
{
|
|
|
|
|
|
//считаем именно хосты в шаблонах, т е один хост может быть в нескольких шаблонах
|
|
|
|
|
|
|
2025-08-26 14:04:49 +10:00
|
|
|
|
var response = new StatUnitTemplateResponse
|
2023-12-20 13:43:03 +10:00
|
|
|
|
{
|
2025-06-25 14:17:34 +10:00
|
|
|
|
AllUnits = await unitService.Get().CountAsync(),
|
2025-08-26 14:04:49 +10:00
|
|
|
|
UnitsInTemplates = await unitService.Get().Include(t => t.Templates).CountAsync(t => t.Templates.Any())
|
2023-12-20 13:43:03 +10:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-26 14:04:49 +10:00
|
|
|
|
return Ok(new Response<StatUnitTemplateResponse>(response, true));
|
2023-12-20 13:43:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|