From 41663eabcb3f4a0450912b1f6c124ee5db0688c8 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Fri, 19 May 2023 12:45:07 +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=20=D1=80=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/GTR/WitsItemRecordDto.cs | 2 +- AsbCloudApp/Repositories/IGtrRepository.cs | 4 +- .../Repository/GtrWitsRepository.cs | 132 ++++++++++-------- .../Controllers/SAUB/GtrWitsController.cs | 6 +- 4 files changed, 82 insertions(+), 62 deletions(-) diff --git a/AsbCloudApp/Data/GTR/WitsItemRecordDto.cs b/AsbCloudApp/Data/GTR/WitsItemRecordDto.cs index d2bb2722..46d82e6b 100644 --- a/AsbCloudApp/Data/GTR/WitsItemRecordDto.cs +++ b/AsbCloudApp/Data/GTR/WitsItemRecordDto.cs @@ -25,6 +25,6 @@ namespace AsbCloudApp.Data.GTR /// /// Значение /// - public object Value { get; set; } + public JsonValue Value { get; set; } = default!; } } \ No newline at end of file diff --git a/AsbCloudApp/Repositories/IGtrRepository.cs b/AsbCloudApp/Repositories/IGtrRepository.cs index 06efac1e..7781060f 100644 --- a/AsbCloudApp/Repositories/IGtrRepository.cs +++ b/AsbCloudApp/Repositories/IGtrRepository.cs @@ -36,13 +36,13 @@ namespace AsbCloudApp.Repositories int approxPointsCount = 1024, CancellationToken token = default); /// - /// получение данных ГТИ по record id + /// получение последних данных ГТИ по record id /// /// /// /// /// - Task> GetDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default); + Task> GetLastDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default); } #nullable disable } diff --git a/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs b/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs index b192826e..59d2f402 100644 --- a/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs +++ b/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs @@ -64,13 +64,13 @@ namespace AsbCloudInfrastructure.Repository Items = g.Select(r => new { Key = r.IdItem, - Value = r.Item + Value = r.Value }).ToDictionary(x => x.Key, x => x.Value) }); return dtos; } - public async Task> GetDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default) + public async Task> GetLastDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default) { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); if (telemetry is null) @@ -85,33 +85,87 @@ namespace AsbCloudInfrastructure.Repository IdRecord = idRecord, }; - var recordAllInt = await GetItemsOrDefaultAsync(witsRequest, token); - var recordAllFloat = await GetItemsOrDefaultAsync(witsRequest, token); - var recordAllString = await GetItemsOrDefaultAsync(witsRequest, token); + var recordAllInt = await GetGroupedItemsOrDefaultAsync(witsRequest, token); + var recordAllFloat = await GetGroupedItemsOrDefaultAsync(witsRequest, token); + var recordAllString = await GetGroupedItemsOrDefaultAsync(witsRequest, token); - - var dtos = (recordAllFloat.Union(recordAllInt)).Union(recordAllString) - .GroupBy(g => new - { - g.IdRecord, - g.IdTelemetry, - g.IdItem - }) - .Select(g => new WitsItemRecordDto() - { - IdRecord = g.Key.IdRecord, - IdItem = g.Key.IdItem, - Date = g.MaxBy(i => i.Date)!.Date, - Value = g.MaxBy(i => i.Date)!.Item.Value, - }).ToArray(); + var dtos = (recordAllFloat.Union(recordAllInt)).Union(recordAllString).ToArray(); return dtos; } - private async Task> GetItemsOrDefaultAsync( + private async Task> GetGroupedItemsOrDefaultAsync(WitsRequest request, CancellationToken token) + where TEntity : WitsItemBase + where TValue : notnull + { + var query = BuildQuery(request); + var groupedQuery = query.GroupBy(g => new + { + g.IdRecord, + g.IdTelemetry, + g.IdItem + }) + .Select(g => new + { + g.Key.IdRecord, + g.Key.IdItem, + Data = g.OrderByDescending(i => i.DateTime) + .FirstOrDefault() + }); + + var groupedEntities = await groupedQuery + .ToArrayAsync(token) + .ConfigureAwait(false); + + var entities = groupedEntities + .Select(e => new WitsItemRecordDto() + { + IdRecord = e.IdRecord, + IdItem = e.IdItem, + Date = e.Data!.DateTime.ToRemoteDateTime(request.TimezoneHours), + Value = new JsonValue(e.Data!.Value) + }); + + return entities; + } + + private async Task> GetItemsOrDefaultAsync( WitsRequest request, CancellationToken token) where TEntity : WitsItemBase where TValue : notnull + { + var query = BuildQuery(request); + + var fullDataCount = await query.CountAsync(token); + if (fullDataCount == 0) + return Enumerable.Empty(); + + if (request.ApproxPointsCount is not null && fullDataCount > 1.75 * request.ApproxPointsCount) + { + var m = (int)Math.Round(1d * fullDataCount / request.ApproxPointsCount!.Value); + if (m > 1) + query = query.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0); + } + + var entities = await query + .OrderBy(d => d.DateTime) + .AsNoTracking() + .ToListAsync(token) + .ConfigureAwait(false); + + var items = entities.Select(e => new WitsItemRecordDto + { + IdRecord = e.IdRecord, + Date = e.DateTime.ToRemoteDateTime(request.TimezoneHours), + IdItem = e.IdItem, + Value = new JsonValue(e.Value) + }); + return items; + } + + private IQueryable BuildQuery(WitsRequest request) + where TEntity : WitsItemBase + where TValue : notnull { var query = db.Set().Where(i => i.IdTelemetry == request.IdTelemetry); @@ -127,32 +181,7 @@ namespace AsbCloudInfrastructure.Repository query = query .Where(d => d.DateTime <= request.DateEnd); - var fullDataCount = await query.CountAsync(token); - if (fullDataCount == 0) - return Enumerable.Empty(); - - if (request.ApproxPointsCount is not null && fullDataCount > 1.75 * request.ApproxPointsCount) - { - var m = (int)Math.Round(1d * fullDataCount / request.ApproxPointsCount!.Value); - if (m > 1) - query = query.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0); - } - - var entities = await query - .OrderBy(d => d.DateTime) - .AsNoTracking() - .ToListAsync(token) - .ConfigureAwait(false); - - var items = entities.Select(e => new ItemRecord - { - IdRecord = e.IdRecord, - IdTelemetry = e.IdTelemetry, - Date = e.DateTime.ToRemoteDateTime(request.TimezoneHours), - IdItem = e.IdItem, - Item = new JsonValue(e.Value) - }); - return items; + return query; } public async Task SaveDataAsync(int idTelemetry, WitsRecordDto dto, CancellationToken token) @@ -192,15 +221,6 @@ namespace AsbCloudInfrastructure.Repository Value = value, }; - internal class ItemRecord - { - public int IdRecord { get; set; } - public int IdTelemetry { get; set; } - public DateTime Date { get; set; } - public int IdItem { get; set; } - public JsonValue Item { get; set; } = default!; - } - private class WitsRequest { public int IdTelemetry { get; set; } diff --git a/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs b/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs index 498399d9..791c73bf 100644 --- a/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs +++ b/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs @@ -69,7 +69,7 @@ namespace AsbCloudWebApi.Controllers.SAUB } /// - /// получение данных ГТИ по ключу record + /// получение последних данных ГТИ по ключу record /// /// id скважины /// id record @@ -77,7 +77,7 @@ namespace AsbCloudWebApi.Controllers.SAUB /// [HttpGet("{idWell}/{idRecord}")] [Permission] - public async Task>> GetDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default) + public async Task>> GetLastDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default) { int? idCompany = User.GetCompanyId(); @@ -90,7 +90,7 @@ namespace AsbCloudWebApi.Controllers.SAUB if (!isCompanyOwnsWell) return Forbid(); - var content = await gtrRepository.GetDataByRecordIdAsync(idWell, idRecord, token).ConfigureAwait(false); + var content = await gtrRepository.GetLastDataByRecordIdAsync(idWell, idRecord, token).ConfigureAwait(false); return Ok(content); }