From 619244c66dd16d9c0aebe617315ce73b5b5266de Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 8 Oct 2024 11:23:25 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB=D1=8C=D1=82=D0=B0=D1=82?= =?UTF-8?q?=D0=B0=D0=BC=20PR=20+=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20backgroundWorker=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D1=81=D1=82=D0=B0=D1=80=D1=82=D0=B0=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=8B=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81?= =?UTF-8?q?=D0=B0=20DataSaubStatDrillingQuality?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/SaubStat/IDataSaubStatDto.cs | 15 +++ .../IDataSaubStatDrillingQualityService.cs | 23 ++++ .../IDataSaubStatService.cs | 2 +- .../PeriodicWorks/WorkDataSaubStat.cs | 15 +-- .../WorkDataSaubStatDrillingQuality.cs | 30 +++++ AsbCloudInfrastructure/DependencyInjection.cs | 20 ++-- .../DataSaubStatAbstractRepository.cs | 111 ----------------- .../DataSaubStatDrillingQualityRepository.cs | 21 ---- .../Repository/DataSaubStatRepository.cs | 113 ++++++++++++++++-- .../DataSaubStatDrillingQualityService.cs | 2 +- .../Services/DataSaubStatService.cs | 2 +- AsbCloudInfrastructure/Startup.cs | 1 + 12 files changed, 188 insertions(+), 167 deletions(-) create mode 100644 AsbCloudApp/Services/DataSaubStat/IDataSaubStatDrillingQualityService.cs rename AsbCloudApp/Services/{ => DataSaubStat}/IDataSaubStatService.cs (81%) create mode 100644 AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStatDrillingQuality.cs delete mode 100644 AsbCloudInfrastructure/Repository/DataSaubStatAbstractRepository.cs delete mode 100644 AsbCloudInfrastructure/Repository/DataSaubStatDrillingQualityRepository.cs diff --git a/AsbCloudApp/Data/SaubStat/IDataSaubStatDto.cs b/AsbCloudApp/Data/SaubStat/IDataSaubStatDto.cs index d82e40bb..74db0065 100644 --- a/AsbCloudApp/Data/SaubStat/IDataSaubStatDto.cs +++ b/AsbCloudApp/Data/SaubStat/IDataSaubStatDto.cs @@ -16,4 +16,19 @@ public interface IDataSaubStatDto /// Дата и время окончания /// public DateTimeOffset DateEnd { get; set; } + + /// + /// Глубина забоя по стволу начальная + /// + public double DepthStart { get; set; } + + /// + /// Глубина забоя по стволу конечная + /// + public double DepthEnd { get; set; } + + /// + /// Ключ телеметрии + /// + public int IdTelemetry { get; set; } } diff --git a/AsbCloudApp/Services/DataSaubStat/IDataSaubStatDrillingQualityService.cs b/AsbCloudApp/Services/DataSaubStat/IDataSaubStatDrillingQualityService.cs new file mode 100644 index 00000000..5db1ff71 --- /dev/null +++ b/AsbCloudApp/Services/DataSaubStat/IDataSaubStatDrillingQualityService.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudApp.Services; + +/// +/// Сервис записи данных в таблицу t_data_saub_stat_drilling_quality, которая используется для построения РТК-отчета +/// +public interface IDataSaubStatDrillingQualityService +{ + /// + /// Расчет статистики DataSaubStat + /// + /// + /// Количество дней за которые должны были приходить данные, чтобы телеметрия попала в обработку. + /// + /// + /// + /// + Task CreateStatAsync(int lastDaysFilter, Action onProgressCallback, CancellationToken token); + +} diff --git a/AsbCloudApp/Services/IDataSaubStatService.cs b/AsbCloudApp/Services/DataSaubStat/IDataSaubStatService.cs similarity index 81% rename from AsbCloudApp/Services/IDataSaubStatService.cs rename to AsbCloudApp/Services/DataSaubStat/IDataSaubStatService.cs index 66088d31..4addcf88 100644 --- a/AsbCloudApp/Services/IDataSaubStatService.cs +++ b/AsbCloudApp/Services/DataSaubStat/IDataSaubStatService.cs @@ -5,7 +5,7 @@ using System.Threading.Tasks; namespace AsbCloudApp.Services; /// -/// Сервис записи данных в таблицу DataSaubStat, которая используется для построения РТК-отчета +/// Сервис записи данных в таблицу t_data_saub_stat, которая используется для построения РТК-отчета /// public interface IDataSaubStatService { diff --git a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs index 444672ad..afafb7d6 100644 --- a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs +++ b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs @@ -1,22 +1,13 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Data.DetectedOperation; -using AsbCloudApp.Data.SAUB; -using AsbCloudApp.Repositories; -using AsbCloudApp.Requests; using AsbCloudApp.Services; -using AsbCloudDb.Model; -using AsbCloudInfrastructure.Services.DetectOperations; using Microsoft.Extensions.DependencyInjection; using System; -using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Background.PeriodicWorks; /// -/// задача по добавлению данных в таблицу DataSaubStat, которая используется для построения РТК-отчета +/// задача по добавлению данных в таблицу t_data_saub_stat, которая используется для построения РТК-отчета /// internal class WorkDataSaubStat : Work { @@ -31,8 +22,8 @@ internal class WorkDataSaubStat : Work { var dataSaubStatService = services.GetRequiredService(); - if (dataSaubStatService != null ) + if (dataSaubStatService != null) await dataSaubStatService.CreateStatAsync(Gap, onProgressCallback, token); - } + } } diff --git a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStatDrillingQuality.cs b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStatDrillingQuality.cs new file mode 100644 index 00000000..69b0ddd9 --- /dev/null +++ b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStatDrillingQuality.cs @@ -0,0 +1,30 @@ +using AsbCloudApp.Services; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudInfrastructure.Background.PeriodicWorks; + +/// +/// задача по добавлению данных в таблицу t_data_saub_stat_drilling_quality, +/// которая используется для построения страницы "Качество" +/// +internal class WorkDataSaubStatDrillingQuality : Work +{ + private int Gap = 60; + + public WorkDataSaubStatDrillingQuality() : base("Generate DataSaubStatDrillingQuality entries and save them into Db") + { + Timeout = TimeSpan.FromMinutes(10); + } + + protected override async Task Action(string id, IServiceProvider services, Action onProgressCallback, CancellationToken token) + { + var dataSaubStatService = services.GetRequiredService(); + + if (dataSaubStatService != null) + await dataSaubStatService.CreateStatAsync(Gap, onProgressCallback, token); + + } +} diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 339f4736..03df1876 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -15,6 +15,7 @@ using AsbCloudApp.Services; using AsbCloudApp.Services.DailyReport; using AsbCloudApp.Services.Notifications; using AsbCloudApp.Services.ProcessMaps.WellDrilling; +using AsbCloudApp.Services.WellReport; using AsbCloudDb.Model; using AsbCloudDb.Model.DailyReports.Blocks.TimeBalance; using AsbCloudDb.Model.Manuals; @@ -41,14 +42,13 @@ using AsbCloudInfrastructure.Services.Trajectory.Parser; using AsbCloudInfrastructure.Services.WellOperations.Factories; using AsbCloudInfrastructure.Services.WellOperationService; using AsbCloudInfrastructure.Services.WellOperationService.WellOperationService; +using AsbCloudInfrastructure.Services.WellReport; using Mapster; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using System; -using AsbCloudApp.Services.WellReport; -using AsbCloudInfrastructure.Services.WellReport; namespace AsbCloudInfrastructure; @@ -318,10 +318,10 @@ public static class DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient, DataSaubStatRepository>(); - services.AddTransient, DataSaubStatDrillingQualityRepository>(); + services.AddTransient, DataSaubStatRepository>(); + services.AddTransient, DataSaubStatRepository>(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient< @@ -397,10 +397,10 @@ public static class DependencyInjection ProcessMapPlanBaseRepository>(); services.AddTransient< - IChangeLogRepository, - ProcessMapPlanBaseRepository>(); + IChangeLogRepository, + ProcessMapPlanBaseRepository>(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); @@ -475,7 +475,7 @@ public static class DependencyInjection services.AddTransient(); services.AddTransient, TrajectoryEditableRepository>(); services.AddTransient, TrajectoryEditableRepository>(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient, CrudCacheRepositoryBase(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Repository/DataSaubStatAbstractRepository.cs b/AsbCloudInfrastructure/Repository/DataSaubStatAbstractRepository.cs deleted file mode 100644 index 74000e43..00000000 --- a/AsbCloudInfrastructure/Repository/DataSaubStatAbstractRepository.cs +++ /dev/null @@ -1,111 +0,0 @@ -using AsbCloudApp.Repositories; -using AsbCloudApp.Requests; -using AsbCloudApp.Services; -using AsbCloudDb.Model; -using Mapster; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudInfrastructure.Repository; -public class DataSaubStatAbstractRepository : IDataSaubStatRepository - where TDto : AsbCloudApp.Data.IDataSaubStatDto - where TEntity : class, AsbCloudDb.Model.IDataSaubStat -{ - private readonly IAsbCloudDbContext db; - private readonly ITelemetryService telemetryService; - - public DataSaubStatAbstractRepository(IAsbCloudDbContext dbContext, ITelemetryService telemetryService) - { - db = dbContext; - this.telemetryService = telemetryService; - } - - public async Task> GetLastsAsync(int[] idTelemetries, CancellationToken token) - { - var timeZoneOffsets = idTelemetries - .Distinct() - .ToDictionary(idTelemetry => idTelemetry, idTelemetry => TimeSpan.FromHours(telemetryService.GetTimezone(idTelemetry).Hours)); - - var stats = await db.Set() - .Where(s => idTelemetries.Contains(s.IdTelemetry)) - .GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First()) - .ToArrayAsync(token); - - var result = stats.Select(s => ConvertToDto(s, timeZoneOffsets[s.IdTelemetry])); - - return result; - } - - public async Task> GetAsync(int idTelemetry, DateTimeOffset geDate, DateTimeOffset leDate, CancellationToken token) - { - var timeSpan = TimeSpan.FromHours(telemetryService.GetTimezone(idTelemetry).Hours); - var geDateUtc = geDate.ToUniversalTime(); - var leDateUtc = leDate.ToUniversalTime(); - - var stats = await db.Set() - .Where(s => s.IdTelemetry == idTelemetry) - .Where(s => s.DateStart >= geDateUtc) - .Where(s => s.DateEnd <= leDateUtc) - .ToArrayAsync(token); - - var result = stats.Select(s => ConvertToDto(s, timeSpan)); - - return result; - } - - public async Task InsertRangeAsync(IEnumerable dataSaubStats, CancellationToken token) - { - var entities = dataSaubStats.Select(data => ConvertToEntity(data)); - db.Set().AddRange(entities); - return await db.SaveChangesAsync(token); - } - - private static TDto ConvertToDto(TEntity entity, TimeSpan timeSpan) - { - var dto = entity.Adapt(); - dto.DateStart = dto.DateStart.ToOffset(timeSpan); - dto.DateEnd = dto.DateEnd.ToOffset(timeSpan); - - return dto; - } - - private static TEntity ConvertToEntity(TDto dto) - { - var entity = dto.Adapt(); - entity.DateStart = dto.DateStart.ToUniversalTime(); - entity.DateEnd = dto.DateEnd.ToUniversalTime(); - - return entity; - } - - private IQueryable BuildQuery(TelemetryPartDeleteRequest request) - { - var query = db.Set() - .Where(o => o.IdTelemetry == request.IdTelemetry); - - if (request.LeDate is not null) - { - var leDate = request.LeDate.Value.ToUniversalTime(); - query = query.Where(o => o.DateStart <= leDate); - } - - if (request.GeDate is not null) - { - var geDate = request.GeDate.Value.ToUniversalTime(); - query = query.Where(o => o.DateEnd >= geDate); - } - - return query; - } - - public async Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token) - { - var query = BuildQuery(request); - db.Set().RemoveRange(query); - return await db.SaveChangesAsync(token); - } -} diff --git a/AsbCloudInfrastructure/Repository/DataSaubStatDrillingQualityRepository.cs b/AsbCloudInfrastructure/Repository/DataSaubStatDrillingQualityRepository.cs deleted file mode 100644 index cf5ae36f..00000000 --- a/AsbCloudInfrastructure/Repository/DataSaubStatDrillingQualityRepository.cs +++ /dev/null @@ -1,21 +0,0 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Services; -using AsbCloudDb.Model; - -namespace AsbCloudInfrastructure.Repository; - -/// -/// Абстрактный репозиторий для работы с данными data_saub_stat -/// -public class DataSaubStatDrillingQualityRepository : DataSaubStatAbstractRepository -{ - /// - /// - /// - /// - /// - public DataSaubStatDrillingQualityRepository(IAsbCloudDbContext dbContext, ITelemetryService telemetryService) : base(dbContext, telemetryService) - { - - } -} diff --git a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs index dbfaa6bd..3a6c19e0 100644 --- a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs +++ b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs @@ -1,18 +1,111 @@ -using AsbCloudApp.Data; +using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; +using Mapster; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; namespace AsbCloudInfrastructure.Repository; - -public class DataSaubStatRepository : DataSaubStatAbstractRepository +public class DataSaubStatRepository : IDataSaubStatRepository + where TDto : AsbCloudApp.Data.IDataSaubStatDto + where TEntity : class, AsbCloudDb.Model.IDataSaubStat { - /// - /// - /// - /// - /// - public DataSaubStatRepository(IAsbCloudDbContext dbContext, ITelemetryService telemetryService) : base(dbContext, telemetryService) - { + private readonly IAsbCloudDbContext db; + private readonly ITelemetryService telemetryService; + public DataSaubStatRepository(IAsbCloudDbContext dbContext, ITelemetryService telemetryService) + { + db = dbContext; + this.telemetryService = telemetryService; + } + + public async Task> GetLastsAsync(int[] idTelemetries, CancellationToken token) + { + var timeZoneOffsets = idTelemetries + .Distinct() + .ToDictionary(idTelemetry => idTelemetry, idTelemetry => TimeSpan.FromHours(telemetryService.GetTimezone(idTelemetry).Hours)); + + var stats = await db.Set() + .Where(s => idTelemetries.Contains(s.IdTelemetry)) + .GroupBy(s => s.IdTelemetry, (key, group) => group.OrderByDescending(el => el.DateEnd).First()) + .ToArrayAsync(token); + + var result = stats.Select(s => ConvertToDto(s, timeZoneOffsets[s.IdTelemetry])); + + return result; + } + + public async Task> GetAsync(int idTelemetry, DateTimeOffset geDate, DateTimeOffset leDate, CancellationToken token) + { + var timeSpan = TimeSpan.FromHours(telemetryService.GetTimezone(idTelemetry).Hours); + var geDateUtc = geDate.ToUniversalTime(); + var leDateUtc = leDate.ToUniversalTime(); + + var stats = await db.Set() + .Where(s => s.IdTelemetry == idTelemetry) + .Where(s => s.DateStart >= geDateUtc) + .Where(s => s.DateEnd <= leDateUtc) + .ToArrayAsync(token); + + var result = stats.Select(s => ConvertToDto(s, timeSpan)); + + return result; + } + + public async Task InsertRangeAsync(IEnumerable dataSaubStats, CancellationToken token) + { + var entities = dataSaubStats.Select(data => ConvertToEntity(data)); + db.Set().AddRange(entities); + return await db.SaveChangesAsync(token); + } + + private static TDto ConvertToDto(TEntity entity, TimeSpan timeSpan) + { + var dto = entity.Adapt(); + dto.DateStart = dto.DateStart.ToOffset(timeSpan); + dto.DateEnd = dto.DateEnd.ToOffset(timeSpan); + + return dto; + } + + private static TEntity ConvertToEntity(TDto dto) + { + var entity = dto.Adapt(); + entity.DateStart = dto.DateStart.ToUniversalTime(); + entity.DateEnd = dto.DateEnd.ToUniversalTime(); + + return entity; + } + + private IQueryable BuildQuery(TelemetryPartDeleteRequest request) + { + var query = db.Set() + .Where(o => o.IdTelemetry == request.IdTelemetry); + + if (request.LeDate is not null) + { + var leDate = request.LeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateStart <= leDate); + } + + if (request.GeDate is not null) + { + var geDate = request.GeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateEnd >= geDate); + } + + return query; + } + + public async Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token) + { + var query = BuildQuery(request); + db.Set().RemoveRange(query); + return await db.SaveChangesAsync(token); } } diff --git a/AsbCloudInfrastructure/Services/DataSaubStatDrillingQualityService.cs b/AsbCloudInfrastructure/Services/DataSaubStatDrillingQualityService.cs index d5279a69..b51c69d6 100644 --- a/AsbCloudInfrastructure/Services/DataSaubStatDrillingQualityService.cs +++ b/AsbCloudInfrastructure/Services/DataSaubStatDrillingQualityService.cs @@ -12,7 +12,7 @@ using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services; -public class DataSaubStatDrillingQualityService : IDataSaubStatService +public class DataSaubStatDrillingQualityService : IDataSaubStatDrillingQualityService { private IDataSaubStatRepository dataSaubStatDrillingQualityRepository; private ITelemetryDataSaubService dataSaubService; diff --git a/AsbCloudInfrastructure/Services/DataSaubStatService.cs b/AsbCloudInfrastructure/Services/DataSaubStatService.cs index 84c51b56..db37758d 100644 --- a/AsbCloudInfrastructure/Services/DataSaubStatService.cs +++ b/AsbCloudInfrastructure/Services/DataSaubStatService.cs @@ -51,7 +51,7 @@ public class DataSaubStatService : IDataSaubStatService var idTelemetry = idTelemetries[i]; var lastDate = stats.FirstOrDefault(s => s.IdTelemetry == idTelemetry)?.DateEnd.ToUniversalTime() ?? DateTimeOffset.UnixEpoch; var statsCount = await CreateStatForTelemetryFromDate(idTelemetry, lastDate, token); - if(onProgressCallback != null) + if (onProgressCallback != null) onProgressCallback($"Calculate stat for telemetry: {idTelemetry}; from {lastDate}; results count: {statsCount};", 1d * i / idTelemetries.Length); } } diff --git a/AsbCloudInfrastructure/Startup.cs b/AsbCloudInfrastructure/Startup.cs index 83eba346..48ae8e26 100644 --- a/AsbCloudInfrastructure/Startup.cs +++ b/AsbCloudInfrastructure/Startup.cs @@ -33,6 +33,7 @@ public class Startup backgroundWorker.Add(TimeSpan.FromMinutes(30)); backgroundWorker.Add(TimeSpan.FromMinutes(0)); backgroundWorker.Add(TimeSpan.FromMinutes(0)); + backgroundWorker.Add(TimeSpan.FromMinutes(0)); backgroundWorker.Add(TimeSpan.FromMinutes(0)); backgroundWorker.Add(MakeMemoryMonitoringWork(), TimeSpan.FromMinutes(0));