feat(api,dal,nextRun): добаботана логика расчета nextRun в API и NetRunWorker. Рассчет ведется относительно refDate

This commit is contained in:
Mikhail Trubnikov
2025-11-25 16:49:28 +10:00
parent ce12f1b5fe
commit 2823823784
4 changed files with 166 additions and 78 deletions

View File

@@ -201,21 +201,28 @@ namespace PARR.API.Controllers.V1
{ {
var template = task.Template!; var template = task.Template!;
#region old
// передаем LastRun, если его нет, то NextRun // передаем LastRun, если его нет, то NextRun
//var nextRun = await esppScheduleTransformService.GetNextDateAsync(task.Template!.ApplicationInWorkId, task.Template.ApplicationsInWork!.LastRun ?? task.Template.ApplicationsInWork.NextRun); //var nextRun = await esppScheduleTransformService.GetNextDateAsync(task.Template!.ApplicationInWorkId, task.Template.ApplicationsInWork!.LastRun ?? task.Template.ApplicationsInWork.NextRun);
// всегда считаем по nextRun // всегда считаем по nextRun
var nextRun = await esppScheduleTransformService.GetNextDateAsync(template.Job!.GroupId, template.NextRun); //var nextRun = await esppScheduleTransformService.GetNextDateAsync(template.Job!.GroupId, template.NextRun);
#endregion
//пока у нас выключено автораспределение, считает по refDate
//TODO: когда заработает автораспределение, будем думать!!!!!
var nextRun = await esppScheduleTransformService.GetNextDateAsync(template.Job!.GroupId, template!.Job!.Group!.ReferenceDate);
if (nextRun != template.NextRun) if (nextRun != template.NextRun)
{ {
logger.LogDebug($"Для шаблона id {template.Id} обновляю nextRun, новое значение {nextRun}, старое значение {template.NextRun}"); logger.LogDebug($"Для шаблона id {template.Id} обновляю nextRun, новое значение {nextRun}, старое значение {template.NextRun}");
if (template.NextRun < DateTimeOffset.UtcNow) //if (template.NextRun < DateTimeOffset.UtcNow)
{ //{
logger.LogDebug($"Для шаблона id {template.Id} обновляю lastRun, новое значение {template.NextRun}, старое значение {template.LastRun}"); // logger.LogDebug($"Для шаблона id {template.Id} обновляю lastRun, новое значение {template.NextRun}, старое значение {template.LastRun}");
template.LastRun = template.NextRun; // template.LastRun = template.NextRun;
} //}
template.LastRun = template.NextRun;
template.NextRun = nextRun; template.NextRun = nextRun;
await robotConfigurationService.CommitAsync(new HistoryInitiator { InitiatorComment = "При получении задания роботом, обновил NextRun", InitiatorIp = clientService.GetClientIp()?.ToString(), InitiatorParrComponentId = ParrComponentsEnum.Api }); await robotConfigurationService.CommitAsync(new HistoryInitiator { InitiatorComment = "При получении задания роботом, обновил NextRun", InitiatorIp = clientService.GetClientIp()?.ToString(), InitiatorParrComponentId = ParrComponentsEnum.Api });

View File

@@ -33,7 +33,7 @@ namespace PARR.DAL.Models
public bool IsActiveSchedule { get; set; } public bool IsActiveSchedule { get; set; }
/// <summary> /// <summary>
/// Шаблон не используется : перестал подходить условиям Job'а /// Шаблон не используется : перестал подходить условиям Job'а. true - не используется
/// </summary> /// </summary>
public bool IsUnused { get; set; } = false; public bool IsUnused { get; set; } = false;
@@ -60,9 +60,9 @@ namespace PARR.DAL.Models
//public Guid ApplicationInWorkId { get; set; } //public Guid ApplicationInWorkId { get; set; }
public Guid JobId { get; set; }//TODO Вернуть не NULL!!! public Guid JobId { get; set; }
public Guid UnitId { get; set; }//TODO Вернуть не NULL!!! public Guid UnitId { get; set; }
//public Guid HostId { get; set; } //public Guid HostId { get; set; }

View File

@@ -31,76 +31,97 @@ namespace PARR.DAL.Services.Implementations
.ThenInclude(t => t!.EsppSchValues); .ThenInclude(t => t!.EsppSchValues);
} }
public async Task<EsppScheduleDto?> GetEsppScheduleDtoAsync(Guid jobGroupId) public async Task<EsppScheduleDto?> GetEsppScheduleDtoAsync(Guid jobGroupId)
{ {
//Формирует расписание в нормальном понятном виде из БД //Формирует расписание в нормальном понятном виде из БД
var schValues = await dataContext.EsppSchValues var items = await dataContext.EsppSchValues.Where(t => t.JobGroupId == jobGroupId)
.Include(t => t.EsppSchTypeConfig) .Select(t => new
.ThenInclude(t => t!.EsppSchTypeSchedule) {
.Include(t => t.EsppSchTypeConfig) t.EsppSchTypeConfig!.Order,
.ThenInclude(t => t.EsppSchType) Type = t.EsppSchTypeConfig.EsppSchType!,
.Include(t => t.EsppSchTypeValue) Value = t.EsppSchTypeValue!,
.Include(t => t.JobGroup) TypeSchedule = t.EsppSchTypeConfig.EsppSchTypeSchedule!
.Where(t => t.JobGroup!.Id == jobGroupId)//TODO Migration to job })
.OrderBy(t => t.EsppSchTypeConfig!.Order) .OrderBy(t => t.Order)
.ToListAsync(); .ToListAsync();
if (items.Count == 0)
//var configs = await GetWithSchIncludes()
// .Where(t => t.EsppSchValues.Any(x => x.ApplicationsInWorkId == applicationInWorksId))
// .ToListAsync();
//if (!configs.Any())
// return null;
if (!schValues.Any())
return null; return null;
var typeSchedule = items.First().TypeSchedule;
var values = new List<EsppScheduleValDto>();
//foreach (var config in configs)
//{
// var value = new EsppScheduleValDto
// {
// Order = config.Order,
// Type = config.EsppSchType!,
// Value = config.EsppSchValues.First().EsppSchTypeValue!
// };
// values.Add(value);
//}
foreach (var schValue in schValues)
{
var value = new EsppScheduleValDto
{
Order = schValue.EsppSchTypeConfig!.Order,
Type = schValue.EsppSchTypeConfig!.EsppSchType!,
Value = schValue.EsppSchTypeValue!
};
values.Add(value);
}
var dto = new EsppScheduleDto var dto = new EsppScheduleDto
{ {
TypeSchedule = schValues.First().EsppSchTypeConfig!.EsppSchTypeSchedule!, TypeSchedule = typeSchedule,
Values = values.OrderBy(t => t.Order).ToList() Values = items.Select(t => new EsppScheduleValDto
{
Order = t.Order,
Type = t.Type,
Value = t.Value
}).ToList()
}; };
return dto; return dto;
} }
#region original GetEsppScheduleDtoAsync
//public async Task<EsppScheduleDto?> GetEsppScheduleDtoAsync(Guid jobGroupId)
//{
// //Формирует расписание в нормальном понятном виде из БД
// var schValues = await dataContext.EsppSchValues
// .AsNoTracking()
// .Include(t => t.EsppSchTypeConfig)
// .ThenInclude(t => t!.EsppSchTypeSchedule)
// .Include(t => t.EsppSchTypeConfig)
// .ThenInclude(t => t.EsppSchType)
// .Include(t => t.EsppSchTypeValue)
// .Include(t => t.JobGroup)
// .Where(t => t.JobGroup!.Id == jobGroupId)
// .OrderBy(t => t.EsppSchTypeConfig!.Order)
// .ToListAsync();
// if (!schValues.Any())
// return null;
// var values = new List<EsppScheduleValDto>();
// foreach (var schValue in schValues)
// {
// var value = new EsppScheduleValDto
// {
// Order = schValue.EsppSchTypeConfig!.Order,
// Type = schValue.EsppSchTypeConfig!.EsppSchType!,
// Value = schValue.EsppSchTypeValue!
// };
// values.Add(value);
// }
// var dto = new EsppScheduleDto
// {
// TypeSchedule = schValues.First().EsppSchTypeConfig!.EsppSchTypeSchedule!,
// Values = values.OrderBy(t => t.Order).ToList()
// };
// return dto;
//}
#endregion
public EsppScheduleDto? GetEsppScheduleDto(Guid jobGroupId) public EsppScheduleDto? GetEsppScheduleDto(Guid jobGroupId)
{ {
var result = Task.Run(async () => var result = Task.Run(async () =>
{ {
return await GetEsppScheduleDtoAsync(jobGroupId); return await GetEsppScheduleDtoAsync(jobGroupId);
}).Result; }).GetAwaiter().GetResult();
return result; return result;
} }

View File

@@ -3,7 +3,9 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using PARR.BLL.Services.Interfaces; using PARR.BLL.Services.Interfaces;
using PARR.Common.Domain; using PARR.Common.Domain;
using PARR.DAL.Models;
using PARR.DAL.Services.Interfaces; using PARR.DAL.Services.Interfaces;
using PARR.DAL.Services.Interfaces.Job;
using PARR.DAL.TransformServices; using PARR.DAL.TransformServices;
using PARR.NextRun.Settings; using PARR.NextRun.Settings;
@@ -40,11 +42,12 @@ namespace PARR.NextRun
//var appInWorkService = scope.ServiceProvider.GetService<IApplicationsInWorkService>(); //var appInWorkService = scope.ServiceProvider.GetService<IApplicationsInWorkService>();
var esppSchService = scope.ServiceProvider.GetService<IEsppScheduleTransformService>(); var esppSchService = scope.ServiceProvider.GetService<IEsppScheduleTransformService>();
var templateService = scope.ServiceProvider.GetService<ITemplateService>(); var templateService = scope.ServiceProvider.GetService<ITemplateService>();
var jobGroupService = scope.ServiceProvider.GetService<IJobGroupService>();
if (templateService == null || esppSchService == null) if (templateService == null || esppSchService == null || jobGroupService == null)
throw new Exception($"Не смог получить серивс {nameof(ITemplateService)} или {nameof(IEsppScheduleTransformService)}"); throw new Exception($"Не смог получить серивс {nameof(ITemplateService)} или {nameof(IEsppScheduleTransformService)} или {nameof(IJobGroupService)}");
await HandlerAsync(templateService, esppSchService); await HandlerAsync(templateService, esppSchService, jobGroupService);
} }
}, workerSettings.RepeatEvery); }, workerSettings.RepeatEvery);
} }
@@ -54,33 +57,90 @@ namespace PARR.NextRun
/// Рассчет следующей даты срабатываения /// Рассчет следующей даты срабатываения
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private async Task HandlerAsync(ITemplateService templateService, IEsppScheduleTransformService esppScheduleTransformService) private async Task HandlerAsync(ITemplateService templateService, IEsppScheduleTransformService esppScheduleTransformService, IJobGroupService jobGroupService)
{ {
var templates = await templateService.Get() #region логика для групп у которых не включено автораспределение
.Include(t => t.Job)
.ThenInclude(t => t.Group)
.Where(t => t.NextRun < DateTimeOffset.UtcNow).ToListAsync();
logger.LogDebug($"Шаблонов для обновления: {templates.Count()} шт."); // выбираем шаблоны которые не учавствуют в автораспределении
// и у которых IsUnuser==false
// затем группируем по GroupId, так как nextRun и расписание настраивается для группы, то для всех дочерних шаблонов, nextRun будет одинаковым
int errorsCount = 0;
foreach (var template in templates) var groups = await jobGroupService.Get()
.Where(t => t.IsAutoDistributionEnabled == false)
.Select(t => new { t.Id, t.ReferenceDate })
.ToListAsync();
logger.LogInformation("Найдено {GroupCount} групп для обработки.", groups.Count);
foreach (var group in groups)
{ {
var nextRun = await esppScheduleTransformService.GetNextDateAsync(template.Job!.GroupId, template.NextRun); //получаем по каждой группе следующий nextRun и сравниваем с существующим, если не равны, то обновляем
template.LastRun = template.NextRun; var nextRun = await esppScheduleTransformService.GetNextDateAsync(group.Id, group.ReferenceDate);
template.NextRun = nextRun;
// Загружаем только шаблоны текущей группы где nextRun в БД не равен расчетному
var templatesToUpdate = await templateService.Get()
.Where(t => t.IsUnused == false && t.Job!.GroupId == group.Id && t.NextRun != nextRun)
.ToListAsync();
if (!templatesToUpdate.Any())
continue;
// Обновляем
foreach (var template in templatesToUpdate)
{
template.LastRun = template.NextRun;
template.NextRun = nextRun;
}
// Коммитим изменения для этой группы
if (await templateService.CommitAsync(new HistoryInitiator { InitiatorComment = "Обновлён NextRun", InitiatorParrComponentId = Constants.ParrComponentsEnum.NextRun })) if (await templateService.CommitAsync(new HistoryInitiator { InitiatorComment = "Обновлён NextRun", InitiatorParrComponentId = Constants.ParrComponentsEnum.NextRun }))
logger.LogDebug($"Обновлены значения полей NextRun: {template.NextRun}, LastRun: {template.LastRun} для шаблона {template.Name}, {template.Id}"); logger.LogInformation($"Обновлены значения полей NextRun для шаблонов группы: {group.Id}, " +
$"refDate: {group.ReferenceDate}. Обновлен nextRun: {nextRun} для шаблонов {templatesToUpdate.Count} шт.");
else else
{ {
logger.LogError($"Ошибка при обновлении значений полей NextRun: {template.NextRun}, LastRun: {template.LastRun} для шаблона {template.Name}, {template.Id}"); logger.LogError($"Ошибка при обновлении значений полей NextRun: {nextRun}, для шаблонов {templatesToUpdate.Count} шт., группы {group.Id}, refDate: {group.ReferenceDate} ");
errorsCount++;
} }
} }
logger.LogInformation($"Завершено обновление полей NextRun. Полей в задании: {templates.Count()}, обновлено: {templates.Count() - errorsCount}, ошибок: {errorsCount}"); #endregion
//todo: когда будет автораспределение, для него придумать логику как считать
#region old logic
//var templates = await templateService.Get()
// .Include(t => t.Job)
// .ThenInclude(t => t.Group)
// .Where(t => t.NextRun < DateTimeOffset.UtcNow).ToListAsync();
//logger.LogDebug($"Шаблонов для обновления: {templates.Count()} шт.");
//int errorsCount = 0;
//foreach (var template in templates)
//{
// var nextRun = await esppScheduleTransformService.GetNextDateAsync(template.Job!.GroupId, template.NextRun);
// template.LastRun = template.NextRun;
// template.NextRun = nextRun;
// if (await templateService.CommitAsync(new HistoryInitiator { InitiatorComment = "Обновлён NextRun", InitiatorParrComponentId = Constants.ParrComponentsEnum.NextRun }))
// logger.LogDebug($"Обновлены значения полей NextRun: {template.NextRun}, LastRun: {template.LastRun} для шаблона {template.Name}, {template.Id}");
// else
// {
// logger.LogError($"Ошибка при обновлении значений полей NextRun: {template.NextRun}, LastRun: {template.LastRun} для шаблона {template.Name}, {template.Id}");
// errorsCount++;
// }
//}
//logger.LogInformation($"Завершено обновление полей NextRun. Полей в задании: {templates.Count()}, обновлено: {templates.Count() - errorsCount}, ошибок: {errorsCount}");
#endregion
logger.LogInformation($"Завершено обновление полей NextRun.");
} }
} }
} }