From 58edaf4204bbcd7f35145d73dcd230c5673b02b9 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 25 Dec 2023 10:53:45 +0500 Subject: [PATCH 1/5] =?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); } } } From ae072e74387082a223a85fdaeba6fdf7d07aad6f Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 25 Dec 2023 15:47:03 +0500 Subject: [PATCH 2/5] =?UTF-8?q?=D0=9E=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD?= =?UTF-8?q?=D1=8B=D0=B9=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20CreateReportAsy?= =?UTF-8?q?nc,=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B9=20=D0=B2=D1=8B?= =?UTF-8?q?=D0=B7=D1=8B=D0=B2=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20=D0=B8=D0=B7?= =?UTF-8?q?=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20=D0=B2=D0=BE=D1=80?= =?UTF-8?q?=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Services/IReportService.cs | 11 +++ .../Background/WorkToCreateReport.cs | 76 ++----------------- .../Services/ReportService.cs | 61 ++++++++++++++- 3 files changed, 76 insertions(+), 72 deletions(-) diff --git a/AsbCloudApp/Services/IReportService.cs b/AsbCloudApp/Services/IReportService.cs index 23d34b31..994db83a 100644 --- a/AsbCloudApp/Services/IReportService.cs +++ b/AsbCloudApp/Services/IReportService.cs @@ -24,6 +24,17 @@ namespace AsbCloudApp.Services string EnqueueCreateReportWork(int idWell, int idUser, ReportParametersRequest request, Action handleReportProgress); + /// + /// Создание отчета + /// + /// + /// + /// + /// + /// + /// + Task CreateReportAsync(int idWell, int idUser, ReportParametersRequest request, Action progressHandler, CancellationToken token); + /// /// Получить предполагаемый список страниц рапорта /// diff --git a/AsbCloudInfrastructure/Background/WorkToCreateReport.cs b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs index 11c4b1e5..4100634d 100644 --- a/AsbCloudInfrastructure/Background/WorkToCreateReport.cs +++ b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs @@ -1,14 +1,7 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Requests; +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; @@ -20,82 +13,23 @@ 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 ReportParametersRequest request; 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.request = request; 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(); + var reportService = services.GetRequiredService(); + await reportService.CreateReportAsync(idWell, idUser, request, progressHandler, token); } } } diff --git a/AsbCloudInfrastructure/Services/ReportService.cs b/AsbCloudInfrastructure/Services/ReportService.cs index cd026451..795be62a 100644 --- a/AsbCloudInfrastructure/Services/ReportService.cs +++ b/AsbCloudInfrastructure/Services/ReportService.cs @@ -4,9 +4,11 @@ using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Background; using AsbSaubReport; +using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -113,7 +115,64 @@ public class ReportService : IReportService return dtos; } - public static IReportGenerator GetReportGenerator(int idWell, DateTime begin, + + public async Task CreateReportAsync( + int idWell, + int idUser, + ReportParametersRequest request, + Action progressHandler, + CancellationToken token) + { + var workId = $"create report by wellid:{idWell} for userid:{idUser} requested at {DateTime.Now}"; + var timezoneOffset = wellService.GetTimezone(idWell).Hours; + var beginRemote = request.Begin.ToTimeZoneOffsetHours(timezoneOffset); + var endRemote = request.End.ToTimeZoneOffsetHours(timezoneOffset); + var beginUtc = request.Begin.ToUtcDateTimeOffset(timezoneOffset); + var endUtc = request.End.ToUtcDateTimeOffset(timezoneOffset); + + var tempDir = Path.Combine(Path.GetTempPath(), "report"); + + var generator = GetReportGenerator(idWell, beginRemote, endRemote, request.StepSeconds, request.Format, db); + var reportFileName = Path.Combine(tempDir, generator.GetReportDefaultFileName()); + var totalPages = generator.GetPagesCount(); + + generator.OnProgress += (s, e) => + { + var arg = e.Adapt(); + progressHandler(arg, workId); + }; + generator.Make(reportFileName); + + var ReportCategoryId = db.FileCategories + .AsNoTracking() + .First(c => c.Name.Equals("Рапорт")) + .Id; + + var fileInfo = (await fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName, token))!; + + progressHandler(new + { + Operation = "done", + Progress = 100f, + TotalPages = totalPages, + CurrentPage = totalPages, + file = fileInfo, + }, workId); + + var newReportProperties = new ReportProperty + { + IdWell = idWell, + IdFile = fileInfo.Id, + Begin = beginUtc, + End = endUtc, + Step = request.StepSeconds, + Format = request.Format + }; + db.ReportProperties.Add(newReportProperties); + db.SaveChanges(); + } + + private static IReportGenerator GetReportGenerator(int idWell, DateTime begin, DateTime end, int stepSeconds, int format, IAsbCloudDbContext context) { var dataSource = new ReportDataSourcePgCloud(context, idWell); From 1d3294e799210c619e686d07c3af4a082beb620f Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 26 Dec 2023 14:31:20 +0500 Subject: [PATCH 3/5] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80:=20=D0=BE=D0=B4=D0=B8=D0=BD=20=D1=8D=D0=BA=D1=88=D0=BD,?= =?UTF-8?q?=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B9=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B4=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20=D0=B2=20?= =?UTF-8?q?=D0=BA=D0=B0=D1=87=D0=B5=D1=81=D1=82=D0=B2=D0=B5=20=D0=BF=D0=B0?= =?UTF-8?q?=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D0=B0=20=D0=B2=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=20CreateReportAsync.=20=D0=92=D0=BD?= =?UTF-8?q?=D1=83=D1=82=D1=80=D0=B8=20=D1=8D=D0=BA=D1=88=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=B2=D1=8B=D0=B7=D1=8B=D0=B2=D0=B0=D1=8E=D1=82=D1=81=D1=8F=20?= =?UTF-8?q?2=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D0=B0:=20onProgress=20=D0=B8?= =?UTF-8?q?=20progressHandler.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/ReportProgressDto.cs | 5 +++++ AsbCloudApp/Services/IReportService.cs | 3 ++- .../Background/WorkToCreateReport.cs | 12 ++++++++++-- AsbCloudInfrastructure/Services/ReportService.cs | 6 +++--- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/AsbCloudApp/Data/ReportProgressDto.cs b/AsbCloudApp/Data/ReportProgressDto.cs index c7bf4cbb..5a271180 100644 --- a/AsbCloudApp/Data/ReportProgressDto.cs +++ b/AsbCloudApp/Data/ReportProgressDto.cs @@ -5,6 +5,11 @@ /// public class ReportProgressDto { + /// + /// файл + /// + public FileInfoDto file { get; set; } + /// /// прогресс 0 - 100% /// diff --git a/AsbCloudApp/Services/IReportService.cs b/AsbCloudApp/Services/IReportService.cs index 994db83a..283e27ee 100644 --- a/AsbCloudApp/Services/IReportService.cs +++ b/AsbCloudApp/Services/IReportService.cs @@ -27,13 +27,14 @@ namespace AsbCloudApp.Services /// /// Создание отчета /// + /// /// /// /// /// /// /// - Task CreateReportAsync(int idWell, int idUser, ReportParametersRequest request, Action progressHandler, CancellationToken token); + Task CreateReportAsync(string workId, int idWell, int idUser, ReportParametersRequest request, Action progressHandler, CancellationToken token); /// /// Получить предполагаемый список страниц рапорта diff --git a/AsbCloudInfrastructure/Background/WorkToCreateReport.cs b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs index 4100634d..671dff65 100644 --- a/AsbCloudInfrastructure/Background/WorkToCreateReport.cs +++ b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs @@ -1,4 +1,5 @@ -using AsbCloudApp.Requests; +using AsbCloudApp.Data; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using Microsoft.Extensions.DependencyInjection; using System; @@ -23,13 +24,20 @@ namespace AsbCloudInfrastructure.Background this.idUser = idUser; this.request = request; 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 reportService = services.GetRequiredService(); - await reportService.CreateReportAsync(idWell, idUser, request, progressHandler, token); + Action handler = (state, workId) => + { + onProgress?.Invoke(state.Operation ?? string.Empty, state.Progress); + progressHandler?.Invoke(state, workId); + }; + await reportService.CreateReportAsync(Id, idWell, idUser, request, handler, token); } } } diff --git a/AsbCloudInfrastructure/Services/ReportService.cs b/AsbCloudInfrastructure/Services/ReportService.cs index 795be62a..99ee490b 100644 --- a/AsbCloudInfrastructure/Services/ReportService.cs +++ b/AsbCloudInfrastructure/Services/ReportService.cs @@ -117,13 +117,13 @@ public class ReportService : IReportService public async Task CreateReportAsync( + string workId, int idWell, int idUser, ReportParametersRequest request, - Action progressHandler, + Action progressHandler, CancellationToken token) { - var workId = $"create report by wellid:{idWell} for userid:{idUser} requested at {DateTime.Now}"; var timezoneOffset = wellService.GetTimezone(idWell).Hours; var beginRemote = request.Begin.ToTimeZoneOffsetHours(timezoneOffset); var endRemote = request.End.ToTimeZoneOffsetHours(timezoneOffset); @@ -150,7 +150,7 @@ public class ReportService : IReportService var fileInfo = (await fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName, token))!; - progressHandler(new + progressHandler(new ReportProgressDto() { Operation = "done", Progress = 100f, From 617a529780e2ec9d1f8e0493a4e3e0fe6a3f40f3 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Wed, 27 Dec 2023 09:38:00 +0500 Subject: [PATCH 4/5] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BF=D0=BE=20=D1=80=D0=B5=D0=B7?= =?UTF-8?q?=D1=83=D0=BB=D1=8C=D1=82=D0=B0=D1=82=D0=B0=D0=BC=20=D1=80=D0=B5?= =?UTF-8?q?=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/Progress/ProgressDto.cs | 19 +++++++++++ .../Data/Progress/ReportProgressDto.cs | 13 ++++++++ .../Data/Progress/ReportProgressFinalDto.cs | 18 ++++++++++ AsbCloudApp/Data/ReportProgressDto.cs | 33 ------------------- AsbCloudApp/Services/IReportService.cs | 3 +- .../Background/WorkToCreateReport.cs | 4 +-- .../Services/ReportService.cs | 5 +-- 7 files changed, 57 insertions(+), 38 deletions(-) create mode 100644 AsbCloudApp/Data/Progress/ProgressDto.cs create mode 100644 AsbCloudApp/Data/Progress/ReportProgressDto.cs create mode 100644 AsbCloudApp/Data/Progress/ReportProgressFinalDto.cs delete mode 100644 AsbCloudApp/Data/ReportProgressDto.cs diff --git a/AsbCloudApp/Data/Progress/ProgressDto.cs b/AsbCloudApp/Data/Progress/ProgressDto.cs new file mode 100644 index 00000000..601268ca --- /dev/null +++ b/AsbCloudApp/Data/Progress/ProgressDto.cs @@ -0,0 +1,19 @@ +namespace AsbCloudApp.Data.Progress +{ + /// + /// DTO прогресса + /// + public class ProgressDto + { + /// + /// прогресс 0 - 100% + /// + public float Progress { get; set; } + + /// + /// название текущей операции генерации + /// + public string? Operation { get; set; } + + } +} diff --git a/AsbCloudApp/Data/Progress/ReportProgressDto.cs b/AsbCloudApp/Data/Progress/ReportProgressDto.cs new file mode 100644 index 00000000..00db12a8 --- /dev/null +++ b/AsbCloudApp/Data/Progress/ReportProgressDto.cs @@ -0,0 +1,13 @@ +namespace AsbCloudApp.Data.Progress +{ + /// + /// DTO завершенного прогресса генерации рапорта-диаграммы + /// + public class ReportProgressFinalDto : ReportProgressDto + { + /// + /// файл + /// + public FileInfoDto file { get; set; } + } +} diff --git a/AsbCloudApp/Data/Progress/ReportProgressFinalDto.cs b/AsbCloudApp/Data/Progress/ReportProgressFinalDto.cs new file mode 100644 index 00000000..29956546 --- /dev/null +++ b/AsbCloudApp/Data/Progress/ReportProgressFinalDto.cs @@ -0,0 +1,18 @@ +namespace AsbCloudApp.Data.Progress +{ + /// + /// DTO прогресса генерации рапорта-диаграммы + /// + public class ReportProgressDto : ProgressDto + { + /// + /// номер текущей страницы + /// + public int CurrentPage { get; set; } + + /// + /// предполагаемое суммарное количество страниц + /// + public int TotalPages { get; set; } + } +} diff --git a/AsbCloudApp/Data/ReportProgressDto.cs b/AsbCloudApp/Data/ReportProgressDto.cs deleted file mode 100644 index 5a271180..00000000 --- a/AsbCloudApp/Data/ReportProgressDto.cs +++ /dev/null @@ -1,33 +0,0 @@ -namespace AsbCloudApp.Data -{ - /// - /// DTO прогресса генерации рапорта-диаграммы - /// - public class ReportProgressDto - { - /// - /// файл - /// - public FileInfoDto file { get; set; } - - /// - /// прогресс 0 - 100% - /// - public float Progress { get; set; } - - /// - /// название текущей операции генерации - /// - public string? Operation { get; set; } - - /// - /// номер текущей страницы - /// - public int CurrentPage { get; set; } - - /// - /// предполагаемое суммарное количество страниц - /// - public int TotalPages { get; set; } - } -} diff --git a/AsbCloudApp/Services/IReportService.cs b/AsbCloudApp/Services/IReportService.cs index 283e27ee..5f992a20 100644 --- a/AsbCloudApp/Services/IReportService.cs +++ b/AsbCloudApp/Services/IReportService.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Data.Progress; using AsbCloudApp.Requests; using System; using System.Collections.Generic; @@ -34,7 +35,7 @@ namespace AsbCloudApp.Services /// /// /// - Task CreateReportAsync(string workId, int idWell, int idUser, ReportParametersRequest request, Action progressHandler, CancellationToken token); + Task CreateReportAsync(string workId, int idWell, int idUser, ReportParametersRequest request, Action progressHandler, CancellationToken token); /// /// Получить предполагаемый список страниц рапорта diff --git a/AsbCloudInfrastructure/Background/WorkToCreateReport.cs b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs index 671dff65..3381f033 100644 --- a/AsbCloudInfrastructure/Background/WorkToCreateReport.cs +++ b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs @@ -1,4 +1,4 @@ -using AsbCloudApp.Data; +using AsbCloudApp.Data.Progress; using AsbCloudApp.Requests; using AsbCloudApp.Services; using Microsoft.Extensions.DependencyInjection; @@ -32,7 +32,7 @@ namespace AsbCloudInfrastructure.Background protected override async Task Action(string id, IServiceProvider services, Action onProgress, CancellationToken token) { var reportService = services.GetRequiredService(); - Action handler = (state, workId) => + Action handler = (state, workId) => { onProgress?.Invoke(state.Operation ?? string.Empty, state.Progress); progressHandler?.Invoke(state, workId); diff --git a/AsbCloudInfrastructure/Services/ReportService.cs b/AsbCloudInfrastructure/Services/ReportService.cs index 99ee490b..59fd97ea 100644 --- a/AsbCloudInfrastructure/Services/ReportService.cs +++ b/AsbCloudInfrastructure/Services/ReportService.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Data.Progress; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; @@ -121,7 +122,7 @@ public class ReportService : IReportService int idWell, int idUser, ReportParametersRequest request, - Action progressHandler, + Action progressHandler, CancellationToken token) { var timezoneOffset = wellService.GetTimezone(idWell).Hours; @@ -150,7 +151,7 @@ public class ReportService : IReportService var fileInfo = (await fileService.MoveAsync(idWell, idUser, ReportCategoryId, reportFileName, reportFileName, token))!; - progressHandler(new ReportProgressDto() + progressHandler(new ReportProgressFinalDto() { Operation = "done", Progress = 100f, From 4c97c6459414cb653b54f9a7123270165d62c994 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Wed, 27 Dec 2023 13:07:41 +0500 Subject: [PATCH 5/5] =?UTF-8?q?Report*=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/Progress/ProgressDto.cs | 25 +++++---- .../Data/Progress/ProgressExceptionDto.cs | 29 ++++++++++ .../Data/Progress/ReportProgressDto.cs | 17 +++--- .../Data/Progress/ReportProgressFinalDto.cs | 25 +++++---- .../Background/WorkToCreateReport.cs | 54 +++++++++---------- .../Services/ReportService.cs | 19 +++---- 6 files changed, 97 insertions(+), 72 deletions(-) create mode 100644 AsbCloudApp/Data/Progress/ProgressExceptionDto.cs diff --git a/AsbCloudApp/Data/Progress/ProgressDto.cs b/AsbCloudApp/Data/Progress/ProgressDto.cs index 601268ca..3ad2c50a 100644 --- a/AsbCloudApp/Data/Progress/ProgressDto.cs +++ b/AsbCloudApp/Data/Progress/ProgressDto.cs @@ -1,19 +1,18 @@ -namespace AsbCloudApp.Data.Progress +namespace AsbCloudApp.Data.Progress; + +/// +/// DTO прогресса +/// +public class ProgressDto { /// - /// DTO прогресса + /// прогресс 0 - 100% /// - public class ProgressDto - { - /// - /// прогресс 0 - 100% - /// - public float Progress { get; set; } + public float Progress { get; set; } - /// - /// название текущей операции генерации - /// - public string? Operation { get; set; } + /// + /// название текущей операции генерации + /// + public string? Operation { get; set; } - } } diff --git a/AsbCloudApp/Data/Progress/ProgressExceptionDto.cs b/AsbCloudApp/Data/Progress/ProgressExceptionDto.cs new file mode 100644 index 00000000..cb264178 --- /dev/null +++ b/AsbCloudApp/Data/Progress/ProgressExceptionDto.cs @@ -0,0 +1,29 @@ +using System; + +namespace AsbCloudApp.Data.Progress; + +/// +/// DTO прогресса с ошибкой +/// +public class ProgressExceptionDto +{ + /// + /// прогресс 0 - 100% + /// + public float Progress { get; set; } + + /// + /// название текущей операции генерации + /// + public string? Operation { get; set; } + + /// + /// Отображаемый текст ошибки + /// + public string Message { get; set; } = null!; + + /// + /// Инфо об исключении + /// + public Exception Exception { get; set; } = null!; +} \ No newline at end of file diff --git a/AsbCloudApp/Data/Progress/ReportProgressDto.cs b/AsbCloudApp/Data/Progress/ReportProgressDto.cs index 00db12a8..b14166e9 100644 --- a/AsbCloudApp/Data/Progress/ReportProgressDto.cs +++ b/AsbCloudApp/Data/Progress/ReportProgressDto.cs @@ -1,13 +1,12 @@ -namespace AsbCloudApp.Data.Progress +namespace AsbCloudApp.Data.Progress; + +/// +/// DTO завершенного прогресса генерации рапорта-диаграммы +/// +public class ReportProgressFinalDto : ReportProgressDto { /// - /// DTO завершенного прогресса генерации рапорта-диаграммы + /// файл /// - public class ReportProgressFinalDto : ReportProgressDto - { - /// - /// файл - /// - public FileInfoDto file { get; set; } - } + public FileInfoDto file { get; set; } } diff --git a/AsbCloudApp/Data/Progress/ReportProgressFinalDto.cs b/AsbCloudApp/Data/Progress/ReportProgressFinalDto.cs index 29956546..b5ce965a 100644 --- a/AsbCloudApp/Data/Progress/ReportProgressFinalDto.cs +++ b/AsbCloudApp/Data/Progress/ReportProgressFinalDto.cs @@ -1,18 +1,17 @@ -namespace AsbCloudApp.Data.Progress +namespace AsbCloudApp.Data.Progress; + +/// +/// DTO прогресса генерации рапорта-диаграммы +/// +public class ReportProgressDto : ProgressDto { /// - /// DTO прогресса генерации рапорта-диаграммы + /// номер текущей страницы /// - public class ReportProgressDto : ProgressDto - { - /// - /// номер текущей страницы - /// - public int CurrentPage { get; set; } + public int CurrentPage { get; set; } - /// - /// предполагаемое суммарное количество страниц - /// - public int TotalPages { get; set; } - } + /// + /// предполагаемое суммарное количество страниц + /// + public int TotalPages { get; set; } } diff --git a/AsbCloudInfrastructure/Background/WorkToCreateReport.cs b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs index 3381f033..f07e7f85 100644 --- a/AsbCloudInfrastructure/Background/WorkToCreateReport.cs +++ b/AsbCloudInfrastructure/Background/WorkToCreateReport.cs @@ -6,38 +6,36 @@ using System; using System.Threading; using System.Threading.Tasks; -namespace AsbCloudInfrastructure.Background +namespace AsbCloudInfrastructure.Background; + +/// +/// Класс для создания отчета +/// +internal class WorkToCreateReport : Work { - /// - /// Класс для создания отчета - /// - internal class WorkToCreateReport : Work + private readonly int idWell; + private readonly int idUser; + private readonly ReportParametersRequest request; + private readonly Action progressHandler; + + public WorkToCreateReport(int idWell, int idUser, ReportParametersRequest request, Action progressHandler) : base("") { - private int idWell; - private int idUser; - private ReportParametersRequest request; - private Action progressHandler; + this.idWell = idWell; + this.idUser = idUser; + this.request = request; + this.progressHandler = progressHandler; - public WorkToCreateReport(int idWell, int idUser, ReportParametersRequest request, Action progressHandler) : base("") + 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 reportService = services.GetRequiredService(); + void handler(ProgressDto state, string workId) { - this.idWell = idWell; - this.idUser = idUser; - this.request = request; - 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 reportService = services.GetRequiredService(); - Action handler = (state, workId) => - { - onProgress?.Invoke(state.Operation ?? string.Empty, state.Progress); - progressHandler?.Invoke(state, workId); - }; - await reportService.CreateReportAsync(Id, idWell, idUser, request, handler, token); + onProgress(state.Operation ?? string.Empty, state.Progress); + progressHandler(state, workId); } + await reportService.CreateReportAsync(Id, idWell, idUser, request, handler, token); } } diff --git a/AsbCloudInfrastructure/Services/ReportService.cs b/AsbCloudInfrastructure/Services/ReportService.cs index 59fd97ea..3b7199a9 100644 --- a/AsbCloudInfrastructure/Services/ReportService.cs +++ b/AsbCloudInfrastructure/Services/ReportService.cs @@ -41,16 +41,18 @@ public class ReportService : IReportService { var work = new WorkToCreateReport(idWell, idUser, request, progressHandler); - work.OnErrorAsync = (message, exception, token) => Task.Run(() => progressHandler.Invoke(new - { - Operation = "error", - Progress = 100f, - Message = string.IsNullOrEmpty(message) + work.OnErrorAsync = (message, exception, token) => Task.Run(() => { + var state = new ProgressExceptionDto + { + Operation = "error", + Progress = 100f, + Message = string.IsNullOrEmpty(message) ? exception.Message : message, - Exception = exception, - }, work.Id) - , token); + Exception = exception, + }; + progressHandler.Invoke(state, work.Id); + }, token); backgroundWorkerService.Enqueue(work); @@ -116,7 +118,6 @@ public class ReportService : IReportService return dtos; } - public async Task CreateReportAsync( string workId, int idWell,