diff --git a/AsbCloudApp/Data/GTR/GtrWitsDto.cs b/AsbCloudApp/Data/GTR/GtrWitsDto.cs index a8ecb939..20d08dd6 100644 --- a/AsbCloudApp/Data/GTR/GtrWitsDto.cs +++ b/AsbCloudApp/Data/GTR/GtrWitsDto.cs @@ -1,7 +1,27 @@ +using System; + namespace AsbCloudApp.Data.GTR; +/// +/// ГТИ +/// public class GtrWitsDto { + /// + /// Дата получения записи + /// + public DateTimeOffset DateTime { get; set; } + + /// + /// Забой (скважины), м + /// + public float? DEPTMEAS { get; set; } + + /// + /// Долото, м + /// + public float DEPTBITM { get; set; } + /// /// Вес на крюке /// diff --git a/AsbCloudApp/Repositories/IGtrRepository.cs b/AsbCloudApp/Repositories/IGtrRepository.cs index 1aadfb96..706b6be2 100644 --- a/AsbCloudApp/Repositories/IGtrRepository.cs +++ b/AsbCloudApp/Repositories/IGtrRepository.cs @@ -24,10 +24,11 @@ namespace AsbCloudApp.Repositories /// /// получить данные ГТИ /// + /// /// /// /// - Task> GetAsync(GtrRequest request, CancellationToken token); + Task> GetAsync(int idWell, GtrRequest request, CancellationToken token); /// /// получить данные для клиента diff --git a/AsbCloudApp/Requests/GtrRequest.cs b/AsbCloudApp/Requests/GtrRequest.cs index 8ba678b2..43de25c4 100644 --- a/AsbCloudApp/Requests/GtrRequest.cs +++ b/AsbCloudApp/Requests/GtrRequest.cs @@ -5,7 +5,7 @@ namespace AsbCloudApp.Requests; /// /// Параметры запроса для получения загруженных данных ГТИ по скважине /// -public class GtrRequestBase +public class GtrRequest { /// /// Дата начала выборки.По умолчанию: текущее время - IntervalSec @@ -22,20 +22,4 @@ public class GtrRequestBase /// [Obsolete] public int ApproxPointsCount { get; set; } = 1024; -} - -/// -/// Параметры запроса для получения загруженных данных ГТИ по скважине -/// -public class GtrRequest : GtrRequestBase -{ - public GtrRequest(int idWell, GtrRequestBase request) - { - IdWell = idWell; - Begin = request.Begin; - IntervalSec = request.IntervalSec; - ApproxPointsCount = request.ApproxPointsCount; - } - - public int IdWell { get; set; } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs b/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs index 5d0cda7b..022532f2 100644 --- a/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs +++ b/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs @@ -21,6 +21,8 @@ namespace AsbCloudInfrastructure.Repository { private static IDictionary<(int, int), string> WitsParameters = new Dictionary<(int, int), string> { + { (1, 8), nameof(GtrWitsDto.DEPTBITM) }, + { (1, 10), nameof(GtrWitsDto.DEPTMEAS) }, { (1, 14), nameof(GtrWitsDto.HKLA) }, { (1, 12), nameof(GtrWitsDto.BLKPOS) }, { (1, 16), nameof(GtrWitsDto.WOBA) }, @@ -75,39 +77,60 @@ namespace AsbCloudInfrastructure.Repository this.db = db; this.telemetryService = telemetryService; } - - public async Task> GetAsync(GtrRequest request, CancellationToken token) + + public async Task> GetAsync(int idWell, GtrRequest request, CancellationToken token) => + await GetAsync(idWell, request, token); + + private async Task> GetAsync(int idWell, GtrRequest request, CancellationToken token) + where TEntity : WitsItemBase + where TType : notnull { - var interval = TimeSpan.FromSeconds(10); - var query = BuildQuery(request); - var entities = await query.AsNoTracking().ToArrayAsync(token); + var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); + if (telemetry is null) + return Enumerable.Empty(); + + var timezone = telemetryService.GetTimezone(telemetry.Id); + var timezoneOffset = TimeSpan.FromHours(timezone.Hours); + + var query = BuildQuery(telemetry.Id, request); + + if (!await query.AnyAsync(token)) + return Enumerable.Empty(); + + var interval = TimeSpan.FromSeconds(10); + + var entities = await query + .OrderBy(e => e.DateTime) + .AsNoTracking() + .ToArrayAsync(token); + var dtos = entities .GroupBy(e => e.DateTime.Ticks / interval.Ticks) - .Select(Convert); + .Select(groupByInterval => + { + var items = groupByInterval.Select(e => e); + var values = items.GroupBy(e => (e.IdRecord, e.IdItem)) + .Where(parameter => parameter.Any()) + .ToDictionary(parameter => WitsParameters[parameter.Key], g => g.Last().Value.ToString()); + + var dto = values.Adapt(); + dto.DateTime = items.Last().DateTime.ToOffset(timezoneOffset); + return dto; + }); return dtos; } - private static GtrWitsDto Convert(IEnumerable scr) - { - var values = scr.GroupBy(e => (e.IdRecord, e.IdItem)) - .ToDictionary(g => WitsParameters[g.Key], g => g.LastOrDefault()?.Value); - - var dto = values.Adapt(); - - return dto; - } - - private IQueryable BuildQuery(GtrRequest request) + private IQueryable BuildQuery(int idTelemetry, GtrRequest request) where TEntity : WitsItemBase where TType : notnull { - var query = db.Set().OrderBy(e => e.DateTime) - .Where(e => e.Telemetry != null && - e.Telemetry.Well != null && - e.Telemetry.Well.Id == request.IdWell) - .Where(e => e.DateTime >= DateTime.UtcNow.AddSeconds(-request.IntervalSec)); + var dateIntervalStart = DateTime.UtcNow.AddSeconds(-request.IntervalSec); + + var query = db.Set() + .Where(e => e.IdTelemetry == idTelemetry) + .Where(e => e.DateTime >= dateIntervalStart); if (request.Begin.HasValue) { @@ -118,6 +141,7 @@ namespace AsbCloudInfrastructure.Repository return query; } + [Obsolete] public async Task> GetAsync(int idWell, DateTime? dateBegin, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default) { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); diff --git a/AsbCloudWebApi.IntegrationTests/Clients/IGtrWitsClient.cs b/AsbCloudWebApi.IntegrationTests/Clients/IGtrWitsClient.cs index 17f76a33..517b98ef 100644 --- a/AsbCloudWebApi.IntegrationTests/Clients/IGtrWitsClient.cs +++ b/AsbCloudWebApi.IntegrationTests/Clients/IGtrWitsClient.cs @@ -6,8 +6,8 @@ namespace AsbCloudWebApi.IntegrationTests.Clients; public interface IGtrWitsClient { - private const string BaseRoute = "/api/well/{idWell}/gtrWits"; + private const string BaseRoute = "/api/gtrWits"; [Get(BaseRoute)] - Task>> GetAllAsync(int idWell, [Query] GtrRequestBase request); + Task>> GetAllAsync(int idWell, [Query] GtrRequest request); } \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/GtrControllerTests.cs b/AsbCloudWebApi.IntegrationTests/Controllers/GtrControllerTests.cs index eb1e96d8..e951c13e 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/GtrControllerTests.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/GtrControllerTests.cs @@ -26,7 +26,7 @@ public class GtrControllerTests : BaseIntegrationTest dbContext.WitsItemFloat.AddRange(witsItems); await dbContext.SaveChangesAsync(); - var request = new GtrRequestBase(); + var request = new GtrRequest(); //act var response = await client.GetAllAsync(well.Id, request); diff --git a/AsbCloudWebApi/Controllers/GtrWitsController.cs b/AsbCloudWebApi/Controllers/GtrWitsController.cs deleted file mode 100644 index 69d8a7d2..00000000 --- a/AsbCloudWebApi/Controllers/GtrWitsController.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using AsbCloudApp.Data.GTR; -using AsbCloudApp.Exceptions; -using AsbCloudApp.Repositories; -using AsbCloudApp.Requests; -using AsbCloudApp.Services; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.Mvc; - -namespace AsbCloudWebApi.Controllers; - -[ApiController] -[Route("api/well/{idWell}/[controller]")] -[Authorize] -public class GtrWitsController : ControllerBase -{ - private readonly IWellService wellService; - private readonly IGtrRepository gtrRepository; - - public GtrWitsController(IWellService wellService, - IGtrRepository gtrRepository) - { - this.wellService = wellService; - this.gtrRepository = gtrRepository; - } - - /// - /// Получить значение от ГТИ - /// - /// - /// - /// - /// - [HttpGet] - [Permission] - [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] - public async Task GetAllAsync(int idWell, [FromQuery] GtrRequestBase request, CancellationToken token) - { - await AssertUserHasAccessToWellAsync(idWell, token); - - var requestToRepository = new GtrRequest(idWell, request); - - var dtos = await gtrRepository.GetAsync(requestToRepository, token); - - return Ok(dtos); - } - - private async Task AssertUserHasAccessToWellAsync(int idWell, CancellationToken token) - { - var idUser = User.GetUserId(); - var idCompany = User.GetCompanyId(); - - if (!idUser.HasValue) - throw new ForbidException("Нет доступа к скважине"); - - if (!idCompany.HasValue) - throw new ForbidException("Нет доступа к скважине"); - - if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token)) - throw new ForbidException("Нет доступа к скважине"); - } -} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs b/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs index 95662fe2..8f38b2ad 100644 --- a/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs +++ b/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs @@ -8,7 +8,9 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Threading; using System.Threading.Tasks; +using AsbCloudApp.Exceptions; using AsbCloudApp.Requests; +using Microsoft.AspNetCore.Http; namespace AsbCloudWebApi.Controllers.SAUB { @@ -36,6 +38,25 @@ namespace AsbCloudWebApi.Controllers.SAUB this.wellService = wellService; this.telemetryHubContext = telemetryHubContext; } + + /// + /// Получить значение от ГТИ + /// + /// + /// + /// + /// + [HttpGet] + [Permission] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAllAsync(int idWell, [FromQuery] GtrRequest request, CancellationToken token) + { + await AssertUserHasAccessToWellAsync(idWell, token); + + var dtos = await gtrRepository.GetAsync(idWell, request, token); + + return Ok(dtos); + } /// /// Получить загруженные данные ГТИ по скважине @@ -114,6 +135,21 @@ namespace AsbCloudWebApi.Controllers.SAUB .SendAsync(SignalRMethodGetDataName, dtos), CancellationToken.None); return Ok(); } + + private async Task AssertUserHasAccessToWellAsync(int idWell, CancellationToken token) + { + var idUser = User.GetUserId(); + var idCompany = User.GetCompanyId(); + + if (!idUser.HasValue) + throw new ForbidException("Нет доступа к скважине"); + + if (!idCompany.HasValue) + throw new ForbidException("Нет доступа к скважине"); + + if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token)) + throw new ForbidException("Нет доступа к скважине"); + } } }