From 58edaf4204bbcd7f35145d73dcd230c5673b02b9 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 25 Dec 2023 10:53:45 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20work=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BE=D0=B7?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B8=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D0=BC=D0=BC=D1=8B=20=D0=BE=D1=82=D1=87=D0=B5=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Services/IReportService.cs | 13 +-- .../Background/WorkToCreateReport.cs | 101 ++++++++++++++++++ .../Services/ReportService.cs | 85 +++------------ .../Controllers/ReportController.cs | 3 +- AsbCloudWebApi/SignalR/ReportsHub.cs | 3 +- 5 files changed, 119 insertions(+), 86 deletions(-) create mode 100644 AsbCloudInfrastructure/Background/WorkToCreateReport.cs diff --git a/AsbCloudApp/Services/IReportService.cs b/AsbCloudApp/Services/IReportService.cs index 85d72020..23d34b31 100644 --- a/AsbCloudApp/Services/IReportService.cs +++ b/AsbCloudApp/Services/IReportService.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Requests; using System; using System.Collections.Generic; using System.Threading; @@ -11,24 +12,16 @@ namespace AsbCloudApp.Services /// public interface IReportService { - /// - /// категория рапорта - /// - int ReportCategoryId { get; } /// /// Поставить рапорт в очередь на формирование /// /// /// - /// - /// - /// - /// + /// /// /// - string EnqueueCreateReportWork(int idWell, int idUser, int stepSeconds, - int format, DateTime begin, DateTime end, + string EnqueueCreateReportWork(int idWell, int idUser, ReportParametersRequest request, Action handleReportProgress); /// diff --git a/AsbCloudInfrastructure/Background/WorkToCreateReport.cs b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs new file mode 100644 index 00000000..11c4b1e5 --- /dev/null +++ b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs @@ -0,0 +1,101 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Requests; +using AsbCloudApp.Services; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Services; +using Mapster; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudInfrastructure.Background +{ + /// + /// Класс для создания отчета + /// + internal class WorkToCreateReport : Work + { + private int idWell; + private DateTime begin; + private DateTime end; + private int stepSeconds; + private int format; + private int idUser; + private Action progressHandler; + + public WorkToCreateReport(int idWell, int idUser, ReportParametersRequest request, Action progressHandler) : base("") + { + this.idWell = idWell; + this.idUser = idUser; + + this.begin = request.Begin; + this.end = request.End; + this.stepSeconds = request.StepSeconds; + this.format = request.Format; + + this.progressHandler = progressHandler; + + Id = $"create report by wellid:{idWell} for userid:{idUser} requested at {DateTime.Now}"; + } + + + protected override async Task Action(string id, IServiceProvider services, Action onProgress, CancellationToken token) + { + var wellService = services.GetRequiredService(); + using var context = services.GetRequiredService(); + var fileService = services.GetRequiredService(); + + var timezoneOffset = wellService.GetTimezone(idWell).Hours; + var beginRemote = begin.ToTimeZoneOffsetHours(timezoneOffset); + var endRemote = end.ToTimeZoneOffsetHours(timezoneOffset); + var beginUtc = begin.ToUtcDateTimeOffset(timezoneOffset); + var endUtc = end.ToUtcDateTimeOffset(timezoneOffset); + + var tempDir = Path.Combine(Path.GetTempPath(), "report"); + + var generator = ReportService.GetReportGenerator(idWell, beginRemote, endRemote, stepSeconds, format, context); + var reportFileName = Path.Combine(tempDir, generator.GetReportDefaultFileName()); + var totalPages = generator.GetPagesCount(); + + generator.OnProgress += (s, e) => + { + var arg = e.Adapt(); + onProgress(arg.Operation ?? string.Empty, arg.Progress); + progressHandler.Invoke(arg, id); + }; + generator.Make(reportFileName); + + var ReportCategoryId = context.FileCategories + .AsNoTracking() + .First(c => c.Name.Equals("Рапорт")) + .Id; + + var fileInfo = (await fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName, token))!; + + progressHandler.Invoke(new + { + Operation = "done", + Progress = 100f, + TotalPages = totalPages, + CurrentPage = totalPages, + file = fileInfo, + }, id); + + var newReportProperties = new ReportProperty + { + IdWell = idWell, + IdFile = fileInfo.Id, + Begin = beginUtc, + End = endUtc, + Step = stepSeconds, + Format = format + }; + context.ReportProperties.Add(newReportProperties); + context.SaveChanges(); + } + } +} diff --git a/AsbCloudInfrastructure/Services/ReportService.cs b/AsbCloudInfrastructure/Services/ReportService.cs index e1cd558a..cd026451 100644 --- a/AsbCloudInfrastructure/Services/ReportService.cs +++ b/AsbCloudInfrastructure/Services/ReportService.cs @@ -1,14 +1,12 @@ using AsbCloudApp.Data; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Background; using AsbSaubReport; -using Mapster; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -23,8 +21,6 @@ public class ReportService : IReportService private readonly IWellService wellService; private readonly BackgroundWorker backgroundWorkerService; - public int ReportCategoryId { get; private set; } - public ReportService(IAsbCloudDbContext db, ITelemetryService telemetryService, IWellService wellService, @@ -36,76 +32,21 @@ public class ReportService : IReportService this.backgroundWorkerService = backgroundWorkerService; this.telemetryService = telemetryService; this.fileService = fileService; - ReportCategoryId = db.FileCategories - .AsNoTracking() - .First(c => c.Name.Equals("Рапорт")) - .Id; } - public string EnqueueCreateReportWork(int idWell, int idUser, int stepSeconds, int format, DateTime begin, - DateTime end, Action progressHandler) + public string EnqueueCreateReportWork(int idWell, int idUser, ReportParametersRequest request, Action progressHandler) { - var timezoneOffset = wellService.GetTimezone(idWell).Hours; - var beginUtc = begin.ToUtcDateTimeOffset(timezoneOffset); - var endUtc = end.ToUtcDateTimeOffset(timezoneOffset); - var beginRemote = begin.ToTimeZoneOffsetHours(timezoneOffset); - var endRemote = end.ToTimeZoneOffsetHours(timezoneOffset); + var work = new WorkToCreateReport(idWell, idUser, request, progressHandler); - var workId = $"create report by wellid:{idWell} for userid:{idUser} requested at {DateTime.Now}"; - - var workAction = async (string id, IServiceProvider serviceProvider, Action onProgress, CancellationToken token) => - { - using var context = serviceProvider.GetRequiredService(); - var fileService = serviceProvider.GetRequiredService(); - - var tempDir = Path.Combine(Path.GetTempPath(), "report"); - - var generator = GetReportGenerator(idWell, beginRemote, endRemote, stepSeconds, format, context); - var reportFileName = Path.Combine(tempDir, generator.GetReportDefaultFileName()); - var totalPages = generator.GetPagesCount(); - - generator.OnProgress += (s, e) => - { - var arg = e.Adapt(); - onProgress(arg.Operation?? string.Empty, arg.Progress); - progressHandler.Invoke(arg, id); - }; - generator.Make(reportFileName); - - var fileInfo = (await fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName, token))!; - - progressHandler.Invoke(new - { - Operation = "done", - Progress = 100f, - TotalPages = totalPages, - CurrentPage = totalPages, - file = fileInfo, - }, id); - - var newReportProperties = new ReportProperty - { - IdWell = idWell, - IdFile = fileInfo.Id, - Begin = beginUtc, - End = endUtc, - Step = stepSeconds, - Format = format - }; - context.ReportProperties.Add(newReportProperties); - context.SaveChanges(); - }; - - var work = Work.CreateByDelegate(workId, workAction); work.OnErrorAsync = (message, exception, token) => Task.Run(() => progressHandler.Invoke(new - { - Operation = "error", - Progress = 100f, - Message = string.IsNullOrEmpty(message) + { + Operation = "error", + Progress = 100f, + Message = string.IsNullOrEmpty(message) ? exception.Message : message, - Exception = exception, - }, workId) + Exception = exception, + }, work.Id) , token); backgroundWorkerService.Enqueue(work); @@ -114,8 +55,8 @@ public class ReportService : IReportService { Operation = "Ожидает начала в очереди.", Progress = 0f, - }, workId); - return workId; + }, work.Id); + return work.Id; } public int GetReportPagesCount(int idWell, DateTime begin, DateTime end, int stepSeconds, int format) @@ -172,7 +113,7 @@ public class ReportService : IReportService return dtos; } - private static IReportGenerator GetReportGenerator(int idWell, DateTime begin, + public static IReportGenerator GetReportGenerator(int idWell, DateTime begin, DateTime end, int stepSeconds, int format, IAsbCloudDbContext context) { var dataSource = new ReportDataSourcePgCloud(context, idWell); @@ -184,7 +125,7 @@ public class ReportService : IReportService _ => new AsbSaubReportPdf.ReprotGeneratorPdf(dataSource), }; - if(begin == default || end == default) + if (begin == default || end == default) { var analyzeResult = dataSource.Analyze(); begin = begin == default ? analyzeResult.MinDate : begin; diff --git a/AsbCloudWebApi/Controllers/ReportController.cs b/AsbCloudWebApi/Controllers/ReportController.cs index ffcc8b9b..ec1b7228 100644 --- a/AsbCloudWebApi/Controllers/ReportController.cs +++ b/AsbCloudWebApi/Controllers/ReportController.cs @@ -65,8 +65,7 @@ namespace AsbCloudWebApi.Controllers .GetReportProgress(progress, token); }, token); - var id = reportService.EnqueueCreateReportWork(idWell, (int)idUser, - request.StepSeconds, request.Format, request.Begin, request.End, HandleReportProgressAsync); + var id = reportService.EnqueueCreateReportWork(idWell, (int)idUser, request, HandleReportProgressAsync); return Ok(id); } diff --git a/AsbCloudWebApi/SignalR/ReportsHub.cs b/AsbCloudWebApi/SignalR/ReportsHub.cs index 5b9aec9b..2bf098f8 100644 --- a/AsbCloudWebApi/SignalR/ReportsHub.cs +++ b/AsbCloudWebApi/SignalR/ReportsHub.cs @@ -54,8 +54,7 @@ namespace AsbCloudWebApi.SignalR .GetReportProgress(progress, CancellationToken.None); }, CancellationToken.None); - var id = reportService.EnqueueCreateReportWork(idWell, (int)idUser, - request.StepSeconds, request.Format, request.Begin, request.End, HandleReportProgressAsync); + var id = reportService.EnqueueCreateReportWork(idWell, (int)idUser, request, HandleReportProgressAsync); } } }