2023-12-19 15:33:21 +10:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using PARR.API.Contracts.V1;
|
2024-09-16 10:04:45 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Requests.BaseRequests;
|
2023-12-19 15:33:21 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Responses.Base;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Responses.Statistics;
|
|
|
|
|
|
using PARR.API.Controllers.V1.Base;
|
2026-03-05 10:19:54 +10:00
|
|
|
|
using PARR.API.Helpers;
|
2026-04-30 10:35:31 +10:00
|
|
|
|
using PARR.Core.Repositories.Interfaces;
|
2026-05-04 11:55:07 +10:00
|
|
|
|
using PARR.Core.Services.NextRunServices;
|
2026-04-14 12:01:49 +10:00
|
|
|
|
using PARR.Domain.Common.Roles;
|
2026-07-17 15:35:46 +10:00
|
|
|
|
using PARR.Domain.Entities;
|
2026-04-13 16:58:30 +10:00
|
|
|
|
using PARR.Domain.Enums;
|
2023-12-19 15:33:21 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.API.Controllers.V1.Statistics
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Статистика по шаблонам
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
|
|
|
|
|
public class StatTemplateController : BaseApiController
|
|
|
|
|
|
{
|
2026-07-17 15:35:46 +10:00
|
|
|
|
private readonly ITemplateRepository _templateRepository;
|
|
|
|
|
|
private readonly INextRunService _nextRunService;
|
2023-12-19 15:33:21 +10:00
|
|
|
|
|
|
|
|
|
|
public StatTemplateController(
|
2026-07-17 15:35:46 +10:00
|
|
|
|
ITemplateRepository templateRepository,
|
2026-03-05 10:19:54 +10:00
|
|
|
|
INextRunService nextRunService
|
2023-12-19 15:33:21 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
2026-07-17 15:35:46 +10:00
|
|
|
|
_templateRepository = templateRepository;
|
|
|
|
|
|
_nextRunService = nextRunService;
|
2023-12-19 15:33:21 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-21 14:05:35 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Статистика по шаблонам
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet(ApiRoutes.StatTemplate.Get)]
|
|
|
|
|
|
public async Task<IActionResult> Get()
|
|
|
|
|
|
{
|
|
|
|
|
|
var response = new StatTemplateResponse
|
|
|
|
|
|
{
|
2026-07-17 15:35:46 +10:00
|
|
|
|
ActivateScheduleCount = await _templateRepository.Get().AsNoTracking().CountAsync(t => t.IsActiveSchedule),
|
|
|
|
|
|
ActivateTemplateCount = await _templateRepository.Get().AsNoTracking().CountAsync(t => t.IsActiveTemplate),
|
|
|
|
|
|
TemplateAgentCount = await _templateRepository.Get().AsNoTracking().CountAsync(t => t.Job!.Group!.IsAgent),
|
|
|
|
|
|
TemplateCount = await _templateRepository.Get().AsNoTracking().CountAsync(),
|
|
|
|
|
|
SyncEsppScheduleCount = await _templateRepository.Get().AsNoTracking().CountAsync(t => t.RobotConfigurations.Any(c => c.RobotCode == (int)RobotsEnum.ScheduleOrder && c.TaskStatusCode == (int)TaskStatusEnum.Ok)),
|
|
|
|
|
|
SyncEsppTemplatesCount = await _templateRepository.Get().AsNoTracking().CountAsync(t => t.RobotConfigurations.Any(c => c.RobotCode == (int)RobotsEnum.TemplateOrder && c.TaskStatusCode == (int)TaskStatusEnum.Ok))
|
2025-08-21 14:05:35 +10:00
|
|
|
|
};
|
2023-12-19 15:33:21 +10:00
|
|
|
|
|
2025-08-21 14:05:35 +10:00
|
|
|
|
return Ok(new Response<StatTemplateResponse>(response, true));
|
|
|
|
|
|
}
|
2024-09-16 10:04:45 +10:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-21 14:05:35 +10:00
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2026-03-05 10:19:54 +10:00
|
|
|
|
// Формируем период
|
|
|
|
|
|
var userEnd = DateOnly.FromDateTime(DateTimeOffset.UtcNow.ToOffset(timeZoneQuery.TimeZoneOffset).DateTime);
|
|
|
|
|
|
var userStart = userEnd.AddDays(-startPeriodDays);
|
2024-09-16 10:04:45 +10:00
|
|
|
|
|
2026-03-05 10:19:54 +10:00
|
|
|
|
var (utcStart, utcEnd) = TimeZoneDateHelper.GetUtcDateRange(
|
|
|
|
|
|
userStart,
|
|
|
|
|
|
userEnd.AddDays(1),
|
|
|
|
|
|
timeZoneQuery.TimeZoneOffset);
|
2024-09-16 10:04:45 +10:00
|
|
|
|
|
2026-07-17 15:35:46 +10:00
|
|
|
|
var allRecords = await _templateRepository.Get()
|
2026-03-05 10:19:54 +10:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.FilterByDateRangeUtc(t => t.DateCreated, utcStart, utcEnd)
|
|
|
|
|
|
.Select(t => new { t.Id, t.DateCreated })
|
|
|
|
|
|
.ToListAsync();
|
2024-09-16 10:04:45 +10:00
|
|
|
|
|
2026-03-05 10:19:54 +10:00
|
|
|
|
var resultDict = allRecords.GroupByUserDate(t => t.DateCreated, timeZoneQuery.TimeZoneOffset);
|
2024-09-16 10:04:45 +10:00
|
|
|
|
|
|
|
|
|
|
|
2026-07-17 15:35:46 +10:00
|
|
|
|
var daysList = await _nextRunService.GetWorkDaysAsync(userStart, userEnd, false);
|
2026-03-05 10:19:54 +10:00
|
|
|
|
|
|
|
|
|
|
var response = daysList.Select(date => new StatTemplatePeriodResponse
|
2025-08-21 14:05:35 +10:00
|
|
|
|
{
|
|
|
|
|
|
Date = date,
|
2026-03-05 10:19:54 +10:00
|
|
|
|
CreatedTemplatesCount = resultDict.TryGetValue(date, out var cnt) ? cnt : 0
|
|
|
|
|
|
}).OrderBy(t => t.Date).ToList();
|
2024-09-16 10:04:45 +10:00
|
|
|
|
|
2025-08-21 14:05:35 +10:00
|
|
|
|
return Ok(new Response<List<StatTemplatePeriodResponse>>(response, true));
|
|
|
|
|
|
}
|
2024-09-16 10:04:45 +10:00
|
|
|
|
|
|
|
|
|
|
|
2026-07-17 15:35:46 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Получить кол-во шаблонов у которых ИД расписания null и нет задания на создание расписания
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet(ApiRoutes.StatTemplate.GetTemplatesWithoutScheduleAndTaskCount)]
|
|
|
|
|
|
public async Task<IActionResult> GetTemplatesWithoutScheduleAndTaskCount()
|
|
|
|
|
|
{
|
|
|
|
|
|
var count = await _templateRepository.Get()
|
|
|
|
|
|
.CountAsync(t =>
|
|
|
|
|
|
t.ScheduleEsppId == null
|
|
|
|
|
|
&& !t.RobotConfigurations.Any(x =>
|
|
|
|
|
|
x.RobotCode == (int)RobotsEnum.ScheduleOrder
|
|
|
|
|
|
&& x.TaskStatusCode == (int)TaskStatusEnum.Creating
|
|
|
|
|
|
)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
var resposne = new StatTemplatesWithoutScheduleResponse(count);
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(new Response<StatTemplatesWithoutScheduleResponse>(resposne, true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-19 15:33:21 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|