Files
parr_api/PARR.API/Controllers/V1/Statistics/StatWorkloadController.cs

79 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.API.Services.Interfaces;
using PARR.API.Settings;
using PARR.Core.Services.Task.Interfaces;
using PARR.Domain.Common.Roles;
using PARR.Domain.Entities.Base.History;
using PARR.Domain.Enums;
namespace PARR.API.Controllers.V1.Statistics
{
/// <summary>
/// Статистика загруженности по ЗО, РГ
/// </summary>
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
public class StatWorkloadController : BaseApiController
{
private readonly ITaskManagementService taskManagementService;
private readonly IClientService clientService;
private readonly MqSettings mqSettings;
private readonly ILogger<StatWorkloadController> logger;
public StatWorkloadController(
ITaskManagementService taskManagementService,
IClientService clientService,
MqSettings mqSettings,
ILogger<StatWorkloadController> logger
)
{
this.taskManagementService = taskManagementService;
this.clientService = clientService;
this.mqSettings = mqSettings;
this.logger = logger;
}
/// <summary>
/// Сформировать отчетность
/// </summary>
/// <returns></returns>
[HttpPost(ApiRoutes.Workload.Build)]
public async Task<IActionResult> Build()
{
var initiator = new HistoryInitiator
{
InitiatorIp = clientService.GetClientIp()?.ToString(),
InitiatorParrComponentId = ParrComponentsEnum.Api,
InitiatorComment = "Отправлен запрос из API на формирование отчета о загруженности"
};
//todo: избавиться от try/catch
try
{
mqSettings.Tasks.TryGetValue(TaskTypeEnum.Workload, out var queueSettings);
if (queueSettings == null)
{
logger.LogError("Не удалось получить настройки очереди для TaskType: {TaskType}", TaskTypeEnum.Workload);
throw new InvalidOperationException($"Не удалось получить настройки очереди для TaskTypeEnum.Workload");
}
var taskId = await taskManagementService.CreateTaskAsync<object?>(TaskTypeEnum.Workload, default, initiator, queueSettings);
return Ok(new Response<string?>(null, true, null!, $"Создана задача {taskId} на формирование отчета"));
}
catch (Exception ex)
{
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = ex.Message } }));
}
}
}
}