2023-12-04 10:16:00 +10:00
using Microsoft.AspNetCore.Authorization ;
using Microsoft.AspNetCore.Mvc ;
2023-10-17 16:59:53 +10:00
using Microsoft.EntityFrameworkCore ;
using PARR.API.Contracts.V1 ;
using PARR.API.Contracts.V1.Requests.Queries ;
using PARR.API.Contracts.V1.Responses ;
using PARR.API.Contracts.V1.Responses.Base ;
using PARR.API.Controllers.V1.Base ;
using PARR.API.Services.Interfaces ;
2023-12-04 10:16:00 +10:00
using PARR.Constants ;
2023-10-17 16:59:53 +10:00
using PARR.DAL.Contracts ;
using PARR.DAL.Services.Interfaces ;
using PARR.DAL.TransformServices ;
namespace PARR.API.Controllers.V1
{
/// <summary>
/// Выдает задания Агенту сервера
/// </summary>
2023-12-04 10:16:00 +10:00
[Authorize(Roles = ParrRoles.Agent.RoleOrAdmin)]
2023-10-17 16:59:53 +10:00
public class AgentTaskController : BaseApiController
{
private readonly IClientService clientService ;
private readonly ITemplateService templateService ;
private readonly IEsppScheduleTransformService esppScheduleTransformService ;
private readonly ILogger < AgentTaskController > logger ;
public AgentTaskController (
IClientService clientService ,
ITemplateService templateService ,
IEsppScheduleTransformService esppScheduleTransformService ,
ILogger < AgentTaskController > logger
)
{
this . clientService = clientService ;
this . templateService = templateService ;
this . esppScheduleTransformService = esppScheduleTransformService ;
this . logger = logger ;
}
/// <summary>
/// Список заданий агенту по ip-адресу клиента исполнителя
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpGet(ApiRoutes.AgentTask.GetByIp)]
public async Task < IActionResult > GetByClientIp ( [ FromQuery ] AgentTaskGetByIpQuery request )
{
//ПАРР-ДВС-ПТ К __В Р Т -DVGD-SDMI-WEB-01-ДВ С __ПР О ЧЕ Е (РАБОТЫ)
//10.99.253.65
//2023-10-16
var ip = request . Ip ? ? clientService . GetClientIp ( ) ? . ToString ( ) ;
if ( string . IsNullOrEmpty ( ip ) )
return BadRequest ( new Response ( false , new List < ErrorModel > { new ErrorModel { Message = "Client IP address is null." } } ) ) ;
2023-11-28 12:07:59 +10:00
//var date = request.Date ?? DateTimeOffset.UtcNow;
var date = DateTimeOffset . UtcNow ;
2023-10-17 16:59:53 +10:00
2023-10-18 08:46:35 +10:00
//Агент получает задания, если `IsAgent = true`, шаблон и расписания активны, и статус синхронизации шаблона и расписания `= Ok`, и `NextRun = сегодня`, так же если у ApplicationInWork есть расписание.
2023-10-17 16:59:53 +10:00
// С одним IP может быть несколько информационных систем, так что может быть несколько хостов
var templates = await templateService . Get ( )
. Include ( t = > t . RobotConfigurations )
. Include ( t = > t . ApplicationsInWork )
2023-10-18 08:46:35 +10:00
. ThenInclude ( t = > t ! . EsppSchValues )
2023-10-17 16:59:53 +10:00
. Include ( t = > t . Host )
. Where ( t = > t . Host ! . IP = = ip
& & t . ApplicationsInWork ! . IsAgent = = true
& & t . IsActiveTemplate = = true
& & t . IsActiveSchedule = = true
& & t . ApplicationsInWork ! . NextRun . DateTime . Date = = date . Date . Date
& & t . RobotConfigurations . All ( c = > c . TaskStatusCode = = ( int ) TaskStatusEnum . Ok )
2023-10-18 08:46:35 +10:00
& & t . ApplicationsInWork . EsppSchValues . Any ( )
2023-10-17 16:59:53 +10:00
) . ToListAsync ( ) ;
if ( ! templates . Any ( ) )
return NoContent ( ) ;
var response = new AgentTaskMinResponse
{
Scheduled = new List < AgentTaskMinScheduleResponse > ( )
} ;
foreach ( var template in templates )
{
2024-02-28 08:32:57 +10:00
//var templateSchedule = await esppScheduleTransformService.GetNextScheduleAsync(template.ApplicationInWorkId, template.ApplicationsInWork!.LastRun ?? template.ApplicationsInWork!.NextRun);
var templateSchedule = await esppScheduleTransformService . GetNextScheduleAsync ( template . ApplicationInWorkId , template . ApplicationsInWork ! . NextRun ) ;
2023-10-17 16:59:53 +10:00
if ( ! templateSchedule . Any ( ) )
{
2024-02-28 08:32:57 +10:00
logger . LogWarning ( $"Запросили раписание для агента по NextRun и вернулся пустой список! Такого не должно быть! " +
2023-10-17 16:59:53 +10:00
$"ApplicationInWorkId: {template.ApplicationInWorkId}, latRun: {template.ApplicationsInWork!.LastRun}, NextRun: {template.ApplicationsInWork!.NextRun}, ip: {ip}, date: {date}" ) ;
continue ;
}
2023-11-28 12:16:12 +10:00
if ( string . IsNullOrEmpty ( template . ApplicationsInWork . AgentName ) & & string . IsNullOrEmpty ( template . ApplicationsInWork . AgentScript ) )
2023-10-18 08:46:35 +10:00
{
2023-11-28 12:16:12 +10:00
logger . LogWarning ( $"Запросили задание для агента с пустыми значениями AgentName && AgentScript. Такого не должно быть! " +
2023-10-18 08:46:35 +10:00
$"ApplicationInWorkId: {template.ApplicationInWorkId}, AgentName: {template.ApplicationsInWork.AgentName}, AgentScript: {template.ApplicationsInWork.AgentScript} , ip: {ip}, date: {date}" ) ;
continue ;
}
2023-10-19 11:22:35 +10:00
templateSchedule . ForEach ( item = > response . Scheduled . Add ( new AgentTaskMinScheduleResponse
{
2023-11-28 12:16:12 +10:00
Name = template . ApplicationsInWork ! . AgentName ? ? "" ,
Script = template . ApplicationsInWork ! . AgentScript ? ? "" ,
2023-10-19 11:22:35 +10:00
StartAt = item ,
TemplateId = template . Id
} ) ) ;
2023-10-17 16:59:53 +10:00
}
2023-10-18 08:46:35 +10:00
if ( ! response . Scheduled . Any ( ) )
return NoContent ( ) ;
2023-10-17 16:59:53 +10:00
2023-10-19 11:31:20 +10:00
response . Scheduled = response . Scheduled . OrderBy ( t = > t . StartAt ) . ToList ( ) ;
return Ok ( response ) ;
2023-10-17 16:59:53 +10:00
}
}
}