Метод CreateReport внутри ReportsHub для скачивания отчета и одновременной подписки

This commit is contained in:
Olga Nemt 2023-12-04 09:38:42 +05:00
parent c28ea98569
commit 82e7ff8cb4

View File

@ -1,8 +1,9 @@
using AsbCloudApp.Data; using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Background; using AsbCloudInfrastructure.Background;
using AsbCloudWebApi.SignalR.Clients; using AsbCloudWebApi.SignalR.Clients;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using System.Linq; using Microsoft.AspNetCore.Mvc;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -17,39 +18,44 @@ namespace AsbCloudWebApi.SignalR
[Authorize] [Authorize]
public class ReportsHub : BaseHub<IReportHubClient> public class ReportsHub : BaseHub<IReportHubClient>
{ {
private readonly BackgroundWorker backgroundWorker; private readonly IWellService wellService;
private readonly IReportService reportService;
public ReportsHub(BackgroundWorker backgroundWorker) public ReportsHub(IWellService wellService, IReportService reportService)
{ {
this.backgroundWorker = backgroundWorker; this.wellService = wellService;
this.reportService = reportService;
} }
/// <summary> /// <summary>
/// Добавление в группу, отправка данных о формировании отчета /// Добавление в группу, отправка данных о формировании отчета
/// </summary> /// </summary>
/// <param name="groupName"></param> /// <param name="groupName">группа</param>
/// <param name="idWell">ключ скважины</param>
/// <param name="request">параметры запроса</param>
/// <returns></returns> /// <returns></returns>
public override async Task AddToGroup(string groupName) public async Task CreateReport(string groupName, int idWell, ReportParametersRequest request)
{ {
var idUser = Context.User?.GetUserId();
var idCompany = Context.User?.GetCompanyId();
if ((idCompany is null) || (idUser is null))
return;
if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, CancellationToken.None).ConfigureAwait(false))
return;
await base.AddToGroup(groupName); await base.AddToGroup(groupName);
var workId = groupName.Replace("Report_", ""); void HandleReportProgressAsync(object progress, string id) =>
var work = backgroundWorker.Works.FirstOrDefault(work => work.Id == workId); Task.Run(async () =>
{
await Clients.Group(groupName)
.GetReportProgress(progress, CancellationToken.None);
}, CancellationToken.None);
var progress = new ReportProgressDto() var id = reportService.EnqueueCreateReportWork(idWell, (int)idUser,
{ request.StepSeconds, request.Format, request.Begin, request.End, HandleReportProgressAsync);
Operation = "Ожидает начала в очереди.",
Progress = 0f,
};
var state = work?.CurrentState;
if (state is not null)
{
progress.Operation = state.State;
progress.Progress = (float)state.Progress;
}
await Clients.Group(groupName).GetReportProgress(progress, CancellationToken.None);
} }
} }
} }