From 37c74a76a7785394e6daedc26b2ce801258eb2cd Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Mon, 22 Jul 2024 12:43:26 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=87=D0=B0=D1=81=D1=82=D0=B8=20=D1=82=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=BC=D0=B5=D1=82=D1=80=D0=B8=D0=B8=20=D0=BF=D0=BE=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D1=83=20(=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B4=D0=BE=D0=BB=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IDataSaubStatRepository.cs | 9 ++ .../IDetectedOperationRepository.cs | 8 ++ .../Repositories/IDrillTestRepository.cs | 8 ++ AsbCloudApp/Repositories/IGtrRepository.cs | 9 ++ .../ILimitingParameterRepository.cs | 8 +- .../Requests/TelemetryPartDeleteRequest.cs | 25 +++++ AsbCloudApp/Services/IMessageService.cs | 8 ++ .../Services/ITelemetryDataEditorService.cs | 19 ++++ AsbCloudApp/Services/ITelemetryDataService.cs | 9 ++ AsbCloudApp/Services/ITelemetryService.cs | 10 -- AsbCloudApp/Services/IWitsRepository.cs | 9 ++ AsbCloudInfrastructure/DependencyInjection.cs | 2 + .../Repository/DataSaubStatRepository.cs | 28 +++++ .../Repository/DetectedOperationRepository.cs | 29 ++++- .../Repository/DrillTestRepository.cs | 27 +++++ .../Repository/GtrWitsRepository.cs | 89 +++++++++++---- .../Repository/LimitingParameterRepository.cs | 32 ++++-- .../Repository/WitsRecordRepository.cs | 42 ++++++-- .../Services/SAUB/MessageService.cs | 31 +++++- .../Services/SAUB/TelemetryDataBaseService.cs | 43 ++++++-- .../SAUB/TelemetryDataEditorService.cs | 101 ++++++++++++++++++ .../Services/SAUB/TelemetryService.cs | 14 +-- .../Controllers/AdminTelemetryController.cs | 16 ++- 23 files changed, 494 insertions(+), 82 deletions(-) create mode 100644 AsbCloudApp/Requests/TelemetryPartDeleteRequest.cs create mode 100644 AsbCloudApp/Services/ITelemetryDataEditorService.cs create mode 100644 AsbCloudInfrastructure/Services/SAUB/TelemetryDataEditorService.cs diff --git a/AsbCloudApp/Repositories/IDataSaubStatRepository.cs b/AsbCloudApp/Repositories/IDataSaubStatRepository.cs index d0e3c5a7..bf3d59af 100644 --- a/AsbCloudApp/Repositories/IDataSaubStatRepository.cs +++ b/AsbCloudApp/Repositories/IDataSaubStatRepository.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Requests; using System; using System.Collections.Generic; using System.Threading; @@ -36,5 +37,13 @@ namespace AsbCloudApp.Repositories /// /// Task InsertRangeAsync(IEnumerable dataSaubStats, CancellationToken token); + + /// + /// Удаление записей по запросу на удаление куска телеметрии + /// + /// + /// + /// + Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token); } } diff --git a/AsbCloudApp/Repositories/IDetectedOperationRepository.cs b/AsbCloudApp/Repositories/IDetectedOperationRepository.cs index c588a168..ef98b854 100644 --- a/AsbCloudApp/Repositories/IDetectedOperationRepository.cs +++ b/AsbCloudApp/Repositories/IDetectedOperationRepository.cs @@ -68,4 +68,12 @@ public interface IDetectedOperationRepository /// /// Task DeleteAsync(DetectedOperationByTelemetryRequest request, CancellationToken token); + + /// + /// Удалить операции по запросу на удаление куска телеметрии + /// + /// запрос + /// + /// + Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token); } diff --git a/AsbCloudApp/Repositories/IDrillTestRepository.cs b/AsbCloudApp/Repositories/IDrillTestRepository.cs index b7dce27b..50d9e631 100644 --- a/AsbCloudApp/Repositories/IDrillTestRepository.cs +++ b/AsbCloudApp/Repositories/IDrillTestRepository.cs @@ -37,5 +37,13 @@ namespace AsbCloudApp.Repositories /// /// Task SaveDataAsync(int idTelemetry, IEnumerable dtos, CancellationToken token); + + /// + /// Удаление записей drill_test по запросу на удаление куска телеметрии + /// + /// + /// + /// + Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token); } } diff --git a/AsbCloudApp/Repositories/IGtrRepository.cs b/AsbCloudApp/Repositories/IGtrRepository.cs index 7ed3df25..8a50afd9 100644 --- a/AsbCloudApp/Repositories/IGtrRepository.cs +++ b/AsbCloudApp/Repositories/IGtrRepository.cs @@ -71,5 +71,14 @@ namespace AsbCloudApp.Repositories /// /// Task GetRangeAsync(int idWell, DateTimeOffset? geDate, DateTimeOffset? leDate, CancellationToken token); + + /// + /// удаление данных ГТИ по запросу на удаление куска телеметрии + /// + /// запрос + /// + /// + Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token) + where TType : notnull; } } diff --git a/AsbCloudApp/Repositories/ILimitingParameterRepository.cs b/AsbCloudApp/Repositories/ILimitingParameterRepository.cs index 02395d34..747f72c8 100644 --- a/AsbCloudApp/Repositories/ILimitingParameterRepository.cs +++ b/AsbCloudApp/Repositories/ILimitingParameterRepository.cs @@ -32,14 +32,12 @@ namespace AsbCloudApp.Repositories Task> GetLimitingParametersAsync(LimitingParameterRequest request, int idTelemetry, double timezoneHours, CancellationToken token); /// - /// Удаление ограничивающих параметров по ключу телеметрии и интервалу дат + /// Удаление ограничивающих параметров по запросу на удаление куска телеметрии /// - /// - /// - /// + /// /// /// - Task DeleteAsync(int telemetryId, DateTimeOffset? gDate, DateTimeOffset? lDate, CancellationToken token); + Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token); } diff --git a/AsbCloudApp/Requests/TelemetryPartDeleteRequest.cs b/AsbCloudApp/Requests/TelemetryPartDeleteRequest.cs new file mode 100644 index 00000000..30c25994 --- /dev/null +++ b/AsbCloudApp/Requests/TelemetryPartDeleteRequest.cs @@ -0,0 +1,25 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace AsbCloudApp.Requests; + +/// +/// Параметры запроса на удаление куска телеметрии +/// +public class TelemetryPartDeleteRequest +{ + /// + /// ключ телеметрии + /// + public int IdTelemetry { get; set; } + + /// + /// greater or equal then Date + /// + public DateTimeOffset? GeDate { get; set; } + + /// + /// less or equal then Date + /// + public DateTimeOffset? LeDate { get; set; } +} diff --git a/AsbCloudApp/Services/IMessageService.cs b/AsbCloudApp/Services/IMessageService.cs index a461b3e8..15b932bb 100644 --- a/AsbCloudApp/Services/IMessageService.cs +++ b/AsbCloudApp/Services/IMessageService.cs @@ -29,5 +29,13 @@ namespace AsbCloudApp.Services /// Task InsertAsync(string uid, IEnumerable dtos, CancellationToken token); + + /// + /// Удаление сообщений по запросу на удаление куска телеметрии + /// + /// запрос + /// + /// + Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token); } } \ No newline at end of file diff --git a/AsbCloudApp/Services/ITelemetryDataEditorService.cs b/AsbCloudApp/Services/ITelemetryDataEditorService.cs new file mode 100644 index 00000000..59493a4e --- /dev/null +++ b/AsbCloudApp/Services/ITelemetryDataEditorService.cs @@ -0,0 +1,19 @@ +using AsbCloudApp.Requests; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudApp.Services; + +/// +/// Сервис по работе с данными телеметрии +/// +public interface ITelemetryDataEditorService +{ + /// + /// Удаление части телеметрии по запросу + /// + /// + /// + /// + Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token); +} diff --git a/AsbCloudApp/Services/ITelemetryDataService.cs b/AsbCloudApp/Services/ITelemetryDataService.cs index 6b42c490..dcfdcce4 100644 --- a/AsbCloudApp/Services/ITelemetryDataService.cs +++ b/AsbCloudApp/Services/ITelemetryDataService.cs @@ -70,5 +70,14 @@ namespace AsbCloudApp.Services /// /// Task UpdateDataAsync(string uid, IEnumerable dtos, CancellationToken token); + + /// + /// удалить данные по запросу + /// + /// запрос + /// + /// + Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token); + } } \ No newline at end of file diff --git a/AsbCloudApp/Services/ITelemetryService.cs b/AsbCloudApp/Services/ITelemetryService.cs index 54b56226..49b28388 100644 --- a/AsbCloudApp/Services/ITelemetryService.cs +++ b/AsbCloudApp/Services/ITelemetryService.cs @@ -78,15 +78,5 @@ namespace AsbCloudApp.Services /// /// Task GetTelemetriesInfoByLastData(DateTimeOffset from, CancellationToken token); - - /// - /// Удаление части телеметрии - /// - /// ключ телеметрии - /// начало временного диапазона - /// конец временного диапазона - /// - /// - Task DeleteAsync(int telemetryId, DateTimeOffset? gDate, DateTimeOffset? lDate, CancellationToken token); } } \ No newline at end of file diff --git a/AsbCloudApp/Services/IWitsRepository.cs b/AsbCloudApp/Services/IWitsRepository.cs index e9a81278..f49832af 100644 --- a/AsbCloudApp/Services/IWitsRepository.cs +++ b/AsbCloudApp/Services/IWitsRepository.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Requests; using System; using System.Collections.Generic; using System.Threading; @@ -45,5 +46,13 @@ namespace AsbCloudApp.Services /// The token. /// A Task. Task<(DateTime begin, DateTime end, int count)?> GetStatAsync(int idTelemetry, CancellationToken token); + + /// + /// Удалить записи по запросу на удаление куска телеметрии + /// + /// + /// + /// + Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token); } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index b539e561..9fbe1721 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -493,6 +493,8 @@ namespace AsbCloudInfrastructure services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs index 9d252647..1d517563 100644 --- a/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs +++ b/AsbCloudInfrastructure/Repository/DataSaubStatRepository.cs @@ -1,5 +1,6 @@ using AsbCloudApp.Data; using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; @@ -81,5 +82,32 @@ namespace AsbCloudInfrastructure.Repository return entity; } + + private IQueryable BuildQuery(TelemetryPartDeleteRequest request) + { + var query = db.Set() + .Where(o => o.IdTelemetry == request.IdTelemetry); + + if (request.GeDate is not null) + { + var geDate = request.GeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateStart <= geDate); + } + + if (request.LeDate is not null) + { + var leDate = request.LeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateEnd >= leDate); + } + + 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/DetectedOperationRepository.cs b/AsbCloudInfrastructure/Repository/DetectedOperationRepository.cs index b788dbda..da148ab7 100644 --- a/AsbCloudInfrastructure/Repository/DetectedOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/DetectedOperationRepository.cs @@ -33,6 +33,13 @@ public class DetectedOperationRepository : CrudRepositoryBase DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token) + { + var query = BuildQuery(request); + dbContext.Set().RemoveRange(query); + return await dbContext.SaveChangesAsync(token); + } + public async Task> GetPageAsync(DetectedOperationByTelemetryRequest request, CancellationToken token) { var skip = request.Skip ?? 0; @@ -112,7 +119,27 @@ public class DetectedOperationRepository : CrudRepositoryBase BuildQuery(TelemetryPartDeleteRequest request) + { + var query = dbContext.Set() + .Where(o => o.IdTelemetry == request.IdTelemetry); + + if (request.GeDate is not null) + { + var geDate = request.GeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateStart <= geDate); + } + + if (request.LeDate is not null) + { + var leDate = request.LeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateEnd >= leDate); + } + + return query; + } + private static DetectedOperationDto Convert(DetectedOperation src, TimeSpan offset) { var dto = src.Adapt(); diff --git a/AsbCloudInfrastructure/Repository/DrillTestRepository.cs b/AsbCloudInfrastructure/Repository/DrillTestRepository.cs index ce27ff0f..e620ca6f 100644 --- a/AsbCloudInfrastructure/Repository/DrillTestRepository.cs +++ b/AsbCloudInfrastructure/Repository/DrillTestRepository.cs @@ -84,5 +84,32 @@ namespace AsbCloudInfrastructure.Repository dto.TimeStampStart = dto.TimeStampStart.ToRemoteDateTime(dto.Telemetry?.TimeZone?.Hours ?? 0); return dto; } + + private IQueryable BuildQuery(TelemetryPartDeleteRequest request) + { + var query = db.Set() + .Where(o => o.IdTelemetry == request.IdTelemetry); + + if (request.GeDate is not null) + { + var geDate = request.GeDate.Value.ToUniversalTime(); + query = query.Where(o => o.TimeStampStart <= geDate); + } + + if (request.LeDate is not null) + { + var leDate = request.LeDate.Value.ToUniversalTime(); + query = query.Where(o => o.TimeStampStart >= leDate); + } + + 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/GtrWitsRepository.cs b/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs index 94c89cb1..451387ae 100644 --- a/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs +++ b/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs @@ -1,9 +1,13 @@ +using AsbCloudApp.Data; using AsbCloudApp.Data.GTR; +using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb; using AsbCloudDb.Model; using AsbCloudDb.Model.GTR; +using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Concurrent; @@ -12,11 +16,6 @@ using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; -using AsbCloudApp.Exceptions; -using AsbCloudApp.Requests; -using Mapster; -using AsbCloudApp.Data; -using System.ComponentModel.DataAnnotations; namespace AsbCloudInfrastructure.Repository { @@ -87,7 +86,7 @@ namespace AsbCloudInfrastructure.Repository public async Task GetRangeAsync(int idWell, DateTimeOffset? geDate, DateTimeOffset? leDate, CancellationToken token) { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); - + if (telemetry is null) return null; @@ -109,7 +108,7 @@ namespace AsbCloudInfrastructure.Repository }); var range = await groupedQuery.FirstOrDefaultAsync(token); - if (range is null) + if (range is null) return null; var result = new DatesRangeDto @@ -121,16 +120,16 @@ namespace AsbCloudInfrastructure.Repository } private async Task> GetAsync(int idWell, GtrRequest request, CancellationToken token) - where TEntity : WitsItemBase + where TEntity : WitsItemBase where TType : notnull { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); - + if (telemetry is null) return Enumerable.Empty(); if (telemetry.TimeZone is null) - throw new ArgumentInvalidException(nameof(idWell),$"Telemetry id: {telemetry.Id} can't find timezone"); + throw new ArgumentInvalidException(nameof(idWell), $"Telemetry id: {telemetry.Id} can't find timezone"); var query = BuildQuery(telemetry.Id, request); @@ -147,7 +146,7 @@ namespace AsbCloudInfrastructure.Repository var interval = TimeSpan.FromSeconds(10); var timezoneOffset = TimeSpan.FromHours(telemetry.TimeZone.Hours); - + var dtos = entities .GroupBy(e => e.DateTime.Ticks / interval.Ticks) .Select(groupByInterval => @@ -166,9 +165,9 @@ namespace AsbCloudInfrastructure.Repository } private IQueryable BuildQuery(int idTelemetry, GtrRequest request) - where TEntity : WitsItemBase + where TEntity : WitsItemBase where TType : notnull - { + { var query = db.Set() .Where(e => e.IdTelemetry == idTelemetry); @@ -183,9 +182,9 @@ namespace AsbCloudInfrastructure.Repository else { var lastDate = query - .OrderBy(e=>e.DateTime) + .OrderBy(e => e.DateTime) .LastOrDefault() - ?.DateTime + ?.DateTime ?? DateTimeOffset.UtcNow; var dateBegin = lastDate.AddSeconds(-request.IntervalSec); var dateEnd = lastDate; @@ -329,10 +328,10 @@ namespace AsbCloudInfrastructure.Repository var dateTime = record.Date.ToUtcDateTimeOffset(timezoneHours); foreach (var item in record.Items) { - if (cacheTelemetryItems?.TryGetValue((record.Id, item.Key), out var cacheItem) == true) + if (cacheTelemetryItems?.TryGetValue((record.Id, item.Key), out var cacheItem) == true) if (Math.Abs((dateTime - cacheItem.Date).TotalSeconds) < 1) - continue; - + continue; + if (item.Value.Value is string valueString) { var entity = MakeEntity(record.Id, item.Key, idTelemetry, dateTime, valueString); @@ -362,14 +361,15 @@ namespace AsbCloudInfrastructure.Repository if (ints.Any()) await db.Database.ExecInsertOrIgnoreAsync(db.Set(), ints, token); } - catch(Exception ex) + catch (Exception ex) { Trace.TraceError("Exception while saving GTR Wits data", ex); } cache.AddOrUpdate(idTelemetry, (_) => MakeNewCache(dtos), - (_, oldItemsDictionary) => { + (_, oldItemsDictionary) => + { foreach (var record in dtos) foreach (var item in record.Items) { @@ -398,7 +398,8 @@ namespace AsbCloudInfrastructure.Repository { var items = dtos.SelectMany(record => record.Items.Select( - item => new WitsItemRecordDto { + item => new WitsItemRecordDto + { IdItem = item.Key, IdRecord = record.Id, Date = record.Date, @@ -409,7 +410,7 @@ namespace AsbCloudInfrastructure.Repository .GroupBy(item => (item.IdRecord, item.IdItem)); var pairs = groups.Select(group => new KeyValuePair<(int, int), WitsItemRecordDto>( - group.Key, + group.Key, group.OrderByDescending(item => item.Date).First())); return new ConcurrentDictionary<(int, int), WitsItemRecordDto>(pairs); @@ -439,6 +440,50 @@ namespace AsbCloudInfrastructure.Repository Value = (TValue)dto.Value.Value, }; + private IQueryable BuildQuery(TelemetryPartDeleteRequest request) + where TEntity : WitsItemBase + where TValue : notnull + { + var query = db.Set().Where(i => i.IdTelemetry == request.IdTelemetry); + + if (request.GeDate is not null) + { + var geDate = request.GeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateTime <= geDate); + } + + if (request.LeDate is not null) + { + var leDate = request.LeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateTime >= leDate); + } + + return query; + } + + public async Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token) + where TType : notnull + { + if (typeof(TType) == typeof(float)) + return await DeleteAsync(request, token); + if (typeof(TType) == typeof(int)) + return await DeleteAsync(request, token); + if (typeof(TType) == typeof(string)) + return await DeleteAsync(request, token); + + return await Task.FromResult(0); + } + + private async Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token) + where TEntity : WitsItemBase + where TType : notnull + { + var query = BuildQuery(request); + db.Set().RemoveRange(query); + return await db.SaveChangesAsync(token); + } + + private class WitsRequest { public int IdTelemetry { get; set; } diff --git a/AsbCloudInfrastructure/Repository/LimitingParameterRepository.cs b/AsbCloudInfrastructure/Repository/LimitingParameterRepository.cs index c6e76d07..bf5957a6 100644 --- a/AsbCloudInfrastructure/Repository/LimitingParameterRepository.cs +++ b/AsbCloudInfrastructure/Repository/LimitingParameterRepository.cs @@ -21,17 +21,9 @@ namespace AsbCloudInfrastructure.Repository this.context = context; } - public async Task DeleteAsync(int telemetryId, DateTimeOffset? gDate, DateTimeOffset? lDate, CancellationToken token) + public async Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token) { - var query = context.Set() - .Where(o => o.IdTelemetry == telemetryId); - - if (gDate.HasValue) - query = query.Where(o => o.DateStart > gDate.Value.ToUniversalTime()); - - if (lDate.HasValue) - query = query.Where(o => o.DateStart < lDate.Value.ToUniversalTime()); - + var query = BuildQuery(request); context.Set().RemoveRange(query); return await context.SaveChangesAsync(token); } @@ -89,6 +81,26 @@ namespace AsbCloudInfrastructure.Repository return query; } + + private IQueryable BuildQuery(TelemetryPartDeleteRequest request) + { + var query = context.Set() + .Where(o => o.IdTelemetry == request.IdTelemetry); + + if (request.GeDate is not null) + { + var geDate = request.GeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateStart <= geDate); + } + + if (request.LeDate is not null) + { + var leDate = request.LeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateEnd >= leDate); + } + + return query; + } } } diff --git a/AsbCloudInfrastructure/Repository/WitsRecordRepository.cs b/AsbCloudInfrastructure/Repository/WitsRecordRepository.cs index 9eca40bf..7239aa9c 100644 --- a/AsbCloudInfrastructure/Repository/WitsRecordRepository.cs +++ b/AsbCloudInfrastructure/Repository/WitsRecordRepository.cs @@ -1,3 +1,4 @@ +using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; @@ -20,7 +21,7 @@ namespace AsbCloudInfrastructure.Repository private readonly IAsbCloudDbContext db; private readonly ITelemetryService telemetryService; - private static readonly ConcurrentDictionary cache = new (); + private static readonly ConcurrentDictionary cache = new(); public WitsRecordRepository(IAsbCloudDbContext db, ITelemetryService telemetryService) { @@ -58,6 +59,33 @@ namespace AsbCloudInfrastructure.Repository return data.Select(d => Convert(d, timezoneHours)); } + private IQueryable BuildQuery(TelemetryPartDeleteRequest request) + { + var query = db.Set() + .Where(o => o.IdTelemetry == request.IdTelemetry); + + if (request.GeDate is not null) + { + var geDate = request.GeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateTime <= geDate); + } + + if (request.LeDate is not null) + { + var leDate = request.LeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateTime >= leDate); + } + + return query; + } + + public async Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token) + { + var query = BuildQuery(request); + dbset.RemoveRange(query); + return await db.SaveChangesAsync(token); + } + public TDto? GetLastOrDefault(int idTelemetry) => cache.GetValueOrDefault(idTelemetry); @@ -66,7 +94,7 @@ namespace AsbCloudInfrastructure.Repository if (!dtos.Any()) return; - cache.AddOrUpdate(idTelemetry, dtos.Last(), (_,_) => dtos.OrderBy(r => r.DateTime).Last()); + cache.AddOrUpdate(idTelemetry, dtos.Last(), (_, _) => dtos.OrderBy(r => r.DateTime).Last()); var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours; var entities = dtos @@ -81,7 +109,7 @@ namespace AsbCloudInfrastructure.Repository .Select(e => e.DateTime) .OrderBy(d => d) .ToArrayAsync(token); - + foreach (var entity in entities) { if (!existingEntities.Any(e => e == entity.DateTime)) @@ -101,9 +129,9 @@ namespace AsbCloudInfrastructure.Repository (dt.Millisecond + random.Next(1, 283)) % 1000, dt.Offset); dbset.Add(entity); - } + } } - + await db.SaveChangesAsync(token); } @@ -126,8 +154,8 @@ namespace AsbCloudInfrastructure.Repository { var entity = dto.Adapt(); entity.Recid = GetRecId(dto); - entity.IdTelemetry = idTelemetry; - entity.DateTime = dto.DateTime.ToUtcDateTimeOffset(timezoneHours); + entity.IdTelemetry = idTelemetry; + entity.DateTime = dto.DateTime.ToUtcDateTimeOffset(timezoneHours); return entity; } diff --git a/AsbCloudInfrastructure/Services/SAUB/MessageService.cs b/AsbCloudInfrastructure/Services/SAUB/MessageService.cs index f4edfc66..e19e6a54 100644 --- a/AsbCloudInfrastructure/Services/SAUB/MessageService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/MessageService.cs @@ -96,7 +96,7 @@ namespace AsbCloudInfrastructure.Services.SAUB var allUsers = await memoryCache.GetOrCreateBasicAsync(db.Set(), token); var users = allUsers.Where(u => u.IdTelemetry == telemetry.Id); - var eventsDict = events.ToDictionary(x=>x.IdEvent, x => x); + var eventsDict = events.ToDictionary(x => x.IdEvent, x => x); var usersDict = users.ToDictionary(x => x.IdUser, x => x); var messagesDtoList = new List(); @@ -142,7 +142,7 @@ namespace AsbCloudInfrastructure.Services.SAUB return Task.CompletedTask; var telemetry = telemetryService.GetOrCreateTelemetryByUid(uid); - + foreach (var dto in dtos) { var entity = dto.Adapt(); @@ -154,6 +154,33 @@ namespace AsbCloudInfrastructure.Services.SAUB return db.SaveChangesAsync(token); } + + private IQueryable BuildQuery(TelemetryPartDeleteRequest request) + { + var query = db.Set() + .Where(o => o.IdTelemetry == request.IdTelemetry); + + if (request.GeDate is not null) + { + var geDate = request.GeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateTime <= geDate); + } + + if (request.LeDate is not null) + { + var leDate = request.LeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateTime >= leDate); + } + + 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/SAUB/TelemetryDataBaseService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs index 88b11941..b3705109 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb; using AsbCloudDb.Model; @@ -11,8 +12,6 @@ using System.Diagnostics; using System.Linq; using System.Threading; using System.Threading.Tasks; -using AsbCloudApp.Requests; -using Mapster; namespace AsbCloudInfrastructure.Services.SAUB { @@ -160,12 +159,12 @@ namespace AsbCloudInfrastructure.Services.SAUB public async Task> GetByTelemetryAsync(int idTelemetry, TelemetryDataRequest request, CancellationToken token) { var timezone = telemetryService.GetTimezone(idTelemetry); - + var cache = telemetryDataCache.GetOrDefault(idTelemetry, request); - - if(cache is not null) + + if (cache is not null) return cache; - + var query = BuildQuery(idTelemetry, request); var entities = await query @@ -219,10 +218,30 @@ namespace AsbCloudInfrastructure.Services.SAUB return query; } + private IQueryable BuildQuery(TelemetryPartDeleteRequest request) + { + var query = db.Set() + .Where(o => o.IdTelemetry == request.IdTelemetry); + + if (request.GeDate is not null) + { + var geDate = request.GeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateTime <= geDate); + } + + if (request.LeDate is not null) + { + var leDate = request.LeDate.Value.ToUniversalTime(); + query = query.Where(o => o.DateTime >= leDate); + } + + return query; + } + /// public async Task GetRangeAsync(int idWell, DateTimeOffset geDate, DateTimeOffset? leDate, CancellationToken token) { - var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell) + var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell) ?? throw new ArgumentInvalidException(nameof(idWell), $"По скважине id:{idWell} нет телеметрии"); if ((DateTimeOffset.UtcNow - geDate) < TimeSpan.FromHours(12)) @@ -231,7 +250,7 @@ namespace AsbCloudInfrastructure.Services.SAUB var cachedRange = telemetryDataCache.GetOrDefaultCachedDataDateRange(telemetry.Id); if (cachedRange is not null) { - var datesRange = new DatesRangeDto {From = cachedRange.From, To = cachedRange.To }; + var datesRange = new DatesRangeDto { From = cachedRange.From, To = cachedRange.To }; if (geDate >= cachedRange.From) datesRange.From = geDate.ToOffset(cachedRange.From.Offset); @@ -246,7 +265,7 @@ namespace AsbCloudInfrastructure.Services.SAUB .Where(entity => entity.IdTelemetry == telemetry.Id) .Where(entity => entity.DateTime >= geDate.ToUniversalTime()); - if(leDate.HasValue) + if (leDate.HasValue) query = query.Where(entity => entity.DateTime <= leDate.Value.ToUniversalTime()); var groupQuery = query @@ -282,5 +301,11 @@ namespace AsbCloudInfrastructure.Services.SAUB protected abstract TEntity Convert(TDto src, double timezoneOffset); + 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/SAUB/TelemetryDataEditorService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataEditorService.cs new file mode 100644 index 00000000..eebe6706 --- /dev/null +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataEditorService.cs @@ -0,0 +1,101 @@ +using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Data.WITS; +using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; +using AsbCloudApp.Services; +using System.Threading; +using System.Threading.Tasks; + +namespace AsbCloudInfrastructure.Services.SAUB +{ + /// + /// Сервис по работе с данными телеметрии + /// + public class TelemetryDataEditorService : ITelemetryDataEditorService + { + private readonly ITelemetryDataSaubService dataSaubService; + private readonly ITelemetryDataService dataSpinService; + private readonly IDataSaubStatRepository dataSaubStatRepository; + private readonly IMessageService messageService; + private readonly IDrillTestRepository drillTestRepository; + private readonly ILimitingParameterRepository limitingParameterRepository; + private readonly IDetectedOperationRepository detectedOperationRepository; + private readonly IWitsRecordRepository witsRecord1Repository; + private readonly IWitsRecordRepository witsRecord7Repository; + private readonly IWitsRecordRepository witsRecord8Repository; + private readonly IWitsRecordRepository witsRecord50Repository; + private readonly IWitsRecordRepository witsRecord60Repository; + private readonly IWitsRecordRepository witsRecord61Repository; + private readonly IGtrRepository gtrRepository; + + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public TelemetryDataEditorService( + ITelemetryDataSaubService dataSaubService, + ITelemetryDataService dataSpinService, + IDataSaubStatRepository dataSaubStatRepository, + IMessageService messageService, + IDrillTestRepository drillTestRepository, + ILimitingParameterRepository limitingParameterRepository, + IDetectedOperationRepository detectedOperationRepository, + IWitsRecordRepository witsRecord1Repository, + IWitsRecordRepository witsRecord7Repository, + IWitsRecordRepository witsRecord8Repository, + IWitsRecordRepository witsRecord50Repository, + IWitsRecordRepository witsRecord60Repository, + IWitsRecordRepository witsRecord61Repository, + IGtrRepository gtrRepository + ) + { + this.dataSaubService = dataSaubService; + this.dataSpinService = dataSpinService; + this.dataSaubStatRepository = dataSaubStatRepository; + this.messageService = messageService; + this.drillTestRepository = drillTestRepository; + this.limitingParameterRepository = limitingParameterRepository; + this.detectedOperationRepository = detectedOperationRepository; + this.witsRecord1Repository = witsRecord1Repository; + this.witsRecord7Repository = witsRecord7Repository; + this.witsRecord8Repository = witsRecord8Repository; + this.witsRecord50Repository = witsRecord50Repository; + this.witsRecord60Repository = witsRecord60Repository; + this.witsRecord61Repository = witsRecord61Repository; + this.gtrRepository = gtrRepository; + } + + public async Task DeleteAsync(TelemetryPartDeleteRequest request, CancellationToken token) + { + await dataSaubService.DeleteAsync(request, token); + await dataSpinService.DeleteAsync(request, token); + await dataSaubStatRepository.DeleteAsync(request, token); + await messageService.DeleteAsync(request, token); + await drillTestRepository.DeleteAsync(request, token); + await limitingParameterRepository.DeleteAsync(request, token); + await detectedOperationRepository.DeleteAsync(request, token); + await witsRecord1Repository.DeleteAsync(request, token); + await witsRecord7Repository.DeleteAsync(request, token); + await witsRecord8Repository.DeleteAsync(request, token); + await witsRecord50Repository.DeleteAsync(request, token); + await witsRecord60Repository.DeleteAsync(request, token); + await witsRecord61Repository.DeleteAsync(request, token); + await gtrRepository.DeleteAsync(request, token); + await gtrRepository.DeleteAsync(request, token); + await gtrRepository.DeleteAsync(request, token); + } + } +} diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs index 4032dd87..4200595e 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryService.cs @@ -24,8 +24,6 @@ namespace AsbCloudInfrastructure.Services.SAUB //TODO: методы использующие ITelemetryDataCache, скорее всего, тут не нужны private readonly ITelemetryDataCache dataSaubCache; private readonly ITimezoneService timezoneService; - private readonly ITelemetryDataSaubService dataSaubService; - private readonly ILimitingParameterRepository limitingParameterRepository; public ITimezoneService TimeZoneService => timezoneService; @@ -33,17 +31,12 @@ namespace AsbCloudInfrastructure.Services.SAUB IAsbCloudDbContext db, IMemoryCache memoryCache, ITelemetryDataCache dataSaubCache, - ITimezoneService timezoneService, - ITelemetryDataSaubService dataSaubService, - ILimitingParameterRepository limitingParameterRepository) + ITimezoneService timezoneService) { this.db = db; this.memoryCache = memoryCache; this.dataSaubCache = dataSaubCache; this.timezoneService = timezoneService; - - this.dataSaubService = dataSaubService; - this.limitingParameterRepository = limitingParameterRepository; } private IEnumerable GetTelemetryCache() @@ -359,10 +352,5 @@ namespace AsbCloudInfrastructure.Services.SAUB return affected; } - - public async Task DeleteAsync(int telemetryId, DateTimeOffset? gDate, DateTimeOffset? lDate, CancellationToken token) - { - return await limitingParameterRepository.DeleteAsync(telemetryId, gDate, lDate, token); - } } } diff --git a/AsbCloudWebApi/Controllers/AdminTelemetryController.cs b/AsbCloudWebApi/Controllers/AdminTelemetryController.cs index a0f7348a..8f5eef80 100644 --- a/AsbCloudWebApi/Controllers/AdminTelemetryController.cs +++ b/AsbCloudWebApi/Controllers/AdminTelemetryController.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -17,12 +18,15 @@ namespace AsbCloudWebApi.Controllers public class AdminTelemetryController : CrudController> { private readonly ITelemetryService telemetryService; + private readonly ITelemetryDataEditorService telemetryEditorService; public AdminTelemetryController(ICrudRepository service, - ITelemetryService telemetryService) + ITelemetryService telemetryService, + ITelemetryDataEditorService telemetryEditorService) : base(service) { this.telemetryService = telemetryService; + this.telemetryEditorService = telemetryEditorService; } /// @@ -46,14 +50,20 @@ namespace AsbCloudWebApi.Controllers /// конец интервала удаления /// /// - [HttpDelete("{telemetryId}")] + [HttpDelete("/part/{telemetryId}")] [Permission] public async Task DeleteAsync(int telemetryId, DateTimeOffset? gDate, DateTimeOffset? lDate, CancellationToken token) { - await telemetryService.DeleteAsync(telemetryId, gDate, lDate, token); + var request = new TelemetryPartDeleteRequest() + { + IdTelemetry = telemetryId, + GeDate = gDate, + LeDate = lDate + }; + await telemetryEditorService.DeleteAsync(request, token); return Ok(); } }