2024-07-29 10:25:57 +10:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using PARR.API.Contracts.V1;
|
2026-01-27 16:36:40 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Requests.BaseRequests;
|
2024-07-29 10:25:57 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Responses;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Responses.Base;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Responses.Statistics;
|
|
|
|
|
|
using PARR.API.Controllers.V1.Base;
|
|
|
|
|
|
using PARR.Constants;
|
2026-01-26 14:29:47 +10:00
|
|
|
|
using PARR.DAL.DomainServices.Shortcodes;
|
|
|
|
|
|
using PARR.DAL.Models;
|
|
|
|
|
|
using PARR.DAL.Models.Job;
|
|
|
|
|
|
using PARR.DAL.NextRunServices;
|
2024-07-29 10:25:57 +10:00
|
|
|
|
using PARR.DAL.Services.Interfaces;
|
2026-01-26 14:29:47 +10:00
|
|
|
|
using PARR.DAL.Services.Interfaces.Job;
|
2024-07-29 10:25:57 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.API.Controllers.V1.Statistics
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Статистика распределения шаблонов
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Authorize(Roles = ParrRoles.Administrator.Role)]
|
|
|
|
|
|
public class StatTemplateDistributionController : BaseApiController
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ITemplateService templateService;
|
2026-01-26 14:29:47 +10:00
|
|
|
|
private readonly IJobGroupService jobGroupService;
|
2024-07-29 10:25:57 +10:00
|
|
|
|
private readonly IMapper mapper;
|
2026-01-26 14:29:47 +10:00
|
|
|
|
private readonly IShortcodesService shortcodesService;
|
|
|
|
|
|
private readonly INextRunService nextRunService;
|
|
|
|
|
|
private readonly ILogger<StatTemplateDistributionController> logger;
|
2024-07-29 10:25:57 +10:00
|
|
|
|
|
|
|
|
|
|
public StatTemplateDistributionController(
|
|
|
|
|
|
ITemplateService templateService,
|
2026-01-26 14:29:47 +10:00
|
|
|
|
IJobGroupService jobGroupService,
|
|
|
|
|
|
IMapper mapper,
|
|
|
|
|
|
IShortcodesService shortcodesService,
|
|
|
|
|
|
INextRunService nextRunService,
|
|
|
|
|
|
ILogger<StatTemplateDistributionController> logger
|
2024-07-29 10:25:57 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.templateService = templateService;
|
2026-01-26 14:29:47 +10:00
|
|
|
|
this.jobGroupService = jobGroupService;
|
2024-07-29 10:25:57 +10:00
|
|
|
|
this.mapper = mapper;
|
2026-01-26 14:29:47 +10:00
|
|
|
|
this.shortcodesService = shortcodesService;
|
|
|
|
|
|
this.nextRunService = nextRunService;
|
|
|
|
|
|
this.logger = logger;
|
2024-07-29 10:25:57 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2026-01-26 14:29:47 +10:00
|
|
|
|
/// Статистика распределения шаблонов по jobGroupId, только для Used
|
2024-07-29 10:25:57 +10:00
|
|
|
|
/// </summary>
|
2026-01-26 14:29:47 +10:00
|
|
|
|
/// <param name="jobGroupId"></param>
|
2024-07-29 10:25:57 +10:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet(ApiRoutes.StatTemplateDistribution.Get)]
|
2026-01-27 16:36:40 +10:00
|
|
|
|
public async Task<IActionResult> Get([FromRoute] Guid jobGroupId, [FromQuery] TimeZoneOffsetClient timeZoneQuery)
|
2024-07-29 10:25:57 +10:00
|
|
|
|
{
|
2026-01-26 14:29:47 +10:00
|
|
|
|
// eadc5498-dba6-4f10-9b4b-a1653e3c3e61
|
|
|
|
|
|
|
|
|
|
|
|
var jobGroup = await jobGroupService.Get()
|
|
|
|
|
|
.Include(t => t.DistributionConfig)
|
|
|
|
|
|
.ThenInclude(t => t.DistributionPeriod)
|
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
|
.FirstOrDefaultAsync(t => t.Id == jobGroupId);
|
|
|
|
|
|
|
|
|
|
|
|
if (jobGroup == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Не найдена группа работ" } }));
|
|
|
|
|
|
|
|
|
|
|
|
if (!jobGroup.IsAutoDistributionEnabled || jobGroup.DistributionConfig == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Для группы работ не включено автораспределение" } }));
|
|
|
|
|
|
|
|
|
|
|
|
var items = await templateService.Get()
|
|
|
|
|
|
.Where(t => t.Job!.GroupId == jobGroupId && t.StatusTypeId == TemplateStatusTypeEnum.Used)
|
|
|
|
|
|
.Select(t => new
|
|
|
|
|
|
{
|
|
|
|
|
|
WorkGroup = t.Job!.WorkGroupMask,
|
|
|
|
|
|
Template = t
|
|
|
|
|
|
})
|
|
|
|
|
|
.AsNoTracking()
|
2024-07-29 10:25:57 +10:00
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
2026-01-26 14:29:47 +10:00
|
|
|
|
if (!items.Any())
|
|
|
|
|
|
return Ok(new Response<StatTemplateDistributorResponse>(new StatTemplateDistributorResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
JobGroup = mapper.Map<JobGroupWithDistributionConfigResponse>(jobGroup),
|
|
|
|
|
|
ItemsList = null
|
|
|
|
|
|
}, true));
|
|
|
|
|
|
|
|
|
|
|
|
// получаем список рабочих дней
|
|
|
|
|
|
var workDays = await GetWorkDaysAsync(jobGroup, items.Max(t => t.Template.NextRun));
|
|
|
|
|
|
|
|
|
|
|
|
var responseItemsList = new List<StatDistributorItemResponse>();
|
2024-07-29 10:25:57 +10:00
|
|
|
|
|
2026-01-26 14:29:47 +10:00
|
|
|
|
if (jobGroup.DistributionConfig.IsGroupingByWorkGroup)
|
2024-07-29 10:25:57 +10:00
|
|
|
|
{
|
2026-01-26 14:29:47 +10:00
|
|
|
|
// тут группируем по рабочим группам
|
|
|
|
|
|
|
|
|
|
|
|
// Получаем список пар (WorkGroupName, Template)
|
|
|
|
|
|
var templatesWithWorkGroupNames = new List<(string WorkGroupName, Template Template)>();
|
|
|
|
|
|
|
|
|
|
|
|
// получаем названия рабочих групп
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
|
|
{
|
|
|
|
|
|
var workGroupName = await shortcodesService.ApplyShortcodesAsync(item.WorkGroup, item.Template);
|
|
|
|
|
|
templatesWithWorkGroupNames.Add((workGroupName, item.Template));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Группируем по WorkGroupName
|
|
|
|
|
|
var grouped = templatesWithWorkGroupNames
|
|
|
|
|
|
.GroupBy(x => x.WorkGroupName)
|
|
|
|
|
|
.ToDictionary(g => g.Key, g => g.Select(x => x.Template).ToList());
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var groupedItem in grouped)
|
2024-08-22 14:32:48 +10:00
|
|
|
|
{
|
2026-01-27 16:36:40 +10:00
|
|
|
|
var statResult = GetResponseByWorkGroup(groupedItem.Key, groupedItem.Value, workDays, timeZoneQuery.TimeZoneOffsetHours);
|
2026-01-26 14:29:47 +10:00
|
|
|
|
responseItemsList.Add(statResult);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// не нужно группировать по рабочим группам
|
|
|
|
|
|
|
|
|
|
|
|
// сразу формируем response
|
2026-01-27 16:36:40 +10:00
|
|
|
|
responseItemsList.Add(GetResponseByWorkGroup(null, items.Select(t => t.Template).ToList(), workDays, timeZoneQuery.TimeZoneOffsetHours));
|
2026-01-26 14:29:47 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var response = new StatTemplateDistributorResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
JobGroup = mapper.Map<JobGroupWithDistributionConfigResponse>(jobGroup),
|
|
|
|
|
|
ItemsList = responseItemsList.OrderBy(t => t.WorkGroupName).ToList()
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(new Response<StatTemplateDistributorResponse>(response, true));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<List<DateOnly>> GetWorkDaysAsync(JobGroup jobGroup, DateTimeOffset maxNextRunTemplate)
|
|
|
|
|
|
{
|
|
|
|
|
|
var durationDays = nextRunService.GetDurationDays(jobGroup.DistributionConfig!);
|
|
|
|
|
|
|
|
|
|
|
|
var dateStart = DateOnly.FromDateTime(DateTime.UtcNow);
|
|
|
|
|
|
|
|
|
|
|
|
// последний день, не может быть меньше чем durationDays
|
|
|
|
|
|
var dateEnd = dateStart.AddDays(durationDays);
|
|
|
|
|
|
|
|
|
|
|
|
// последний день, не может быть меньше чем refDate+durationDays
|
|
|
|
|
|
var sumRefDuration = DateOnly.FromDateTime(jobGroup.ReferenceDate.AddDays(durationDays).Date);
|
|
|
|
|
|
if (dateEnd < sumRefDuration)
|
|
|
|
|
|
dateEnd = sumRefDuration;
|
|
|
|
|
|
|
|
|
|
|
|
// с DateEnd вообще какая-то шурпатня :(
|
|
|
|
|
|
// как наглядно понять ок не ок распределяется, если на каком-то графике dateEnd может ухеать за период распределения
|
|
|
|
|
|
// последний день, не может быть меньше чем дата последнего шаблона maxNextRunTemplate
|
|
|
|
|
|
var lastTempalteDate = DateOnly.FromDateTime(maxNextRunTemplate.Date);
|
|
|
|
|
|
if (dateEnd < lastTempalteDate)
|
|
|
|
|
|
dateEnd = lastTempalteDate;
|
|
|
|
|
|
|
|
|
|
|
|
return await nextRunService.GetWorkDaysAsync(dateStart, dateEnd, jobGroup.DistributionConfig!.IsExcludeWeekends);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-27 16:36:40 +10:00
|
|
|
|
private StatDistributorItemResponse GetResponseByWorkGroup(string? workGroupName, List<Template> templates, List<DateOnly> workDays, int timeZoneOffsetHours)
|
2026-01-26 14:29:47 +10:00
|
|
|
|
{
|
|
|
|
|
|
var result = new StatDistributorItemResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
WorkGroupName = workGroupName,
|
|
|
|
|
|
Statistics = workDays.OrderBy(t => t).Select(t => new StatTemplateDistributorDateStat
|
|
|
|
|
|
{
|
|
|
|
|
|
AllCount = 0,
|
|
|
|
|
|
Date = t,
|
|
|
|
|
|
IsActivatedCount = 0,
|
|
|
|
|
|
IsDeactivatedCount = 0,
|
|
|
|
|
|
Templates = new List<StatTempleteDistribItem>()
|
2024-08-22 14:32:48 +10:00
|
|
|
|
}).ToList()
|
2026-01-26 14:29:47 +10:00
|
|
|
|
};
|
2024-07-29 10:25:57 +10:00
|
|
|
|
|
2026-01-27 16:36:40 +10:00
|
|
|
|
//foreach (var templatesFromDate in templates.GroupBy(t => DateOnly.FromDateTime(t.NextRun.Date)))
|
|
|
|
|
|
// смещаем по часовой зоне и группируем по дате
|
|
|
|
|
|
foreach (var templatesFromDate in templates.GroupBy(t => DateOnly.FromDateTime(t.NextRun.AddHours(timeZoneOffsetHours).Date)))
|
2026-01-26 14:29:47 +10:00
|
|
|
|
{
|
|
|
|
|
|
var statisticsForDate = result.Statistics.FirstOrDefault(t => t.Date == templatesFromDate.Key);
|
|
|
|
|
|
if (statisticsForDate == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.LogWarning("В списке рабочих дней, нет дня для шаблонов {count} шт. выполняющихся в {date}", templatesFromDate.Count(), templatesFromDate.Key);
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
statisticsForDate.AllCount = templatesFromDate.Count();
|
|
|
|
|
|
statisticsForDate.IsDeactivatedCount = templatesFromDate.Count(t => !t.IsActiveTemplate || !t.IsActiveSchedule);
|
|
|
|
|
|
statisticsForDate.IsActivatedCount = templatesFromDate.Count(t => t.IsActiveTemplate && t.IsActiveSchedule);
|
|
|
|
|
|
statisticsForDate.Templates = templatesFromDate.Select(t => new StatTempleteDistribItem
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = t.Name,
|
|
|
|
|
|
IsActiveSchedular = t.IsActiveSchedule,
|
2026-01-27 15:45:38 +10:00
|
|
|
|
IsActiveTemplate = t.IsActiveTemplate,
|
|
|
|
|
|
NextRun = t.NextRun
|
2026-01-26 14:29:47 +10:00
|
|
|
|
})
|
|
|
|
|
|
.OrderBy(t => t.Name)
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
}
|
2024-07-29 10:25:57 +10:00
|
|
|
|
|
2026-01-26 14:29:47 +10:00
|
|
|
|
return result;
|
2024-07-29 10:25:57 +10:00
|
|
|
|
}
|
2026-01-26 14:29:47 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-07-29 10:25:57 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|