From 7514e3132ab80d974b95a27e3782227f77739b89 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 18 May 2023 15:31:27 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D0=93?= =?UTF-8?q?=D0=A2=D0=98=20=D0=BF=D0=BE=20id=20record?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Repositories/IGtrRepository.cs | 4 +- .../Repository/GtrWitsRepository.cs | 154 +++++++++--------- .../Controllers/SAUB/GtrWitsController.cs | 11 +- 3 files changed, 88 insertions(+), 81 deletions(-) diff --git a/AsbCloudApp/Repositories/IGtrRepository.cs b/AsbCloudApp/Repositories/IGtrRepository.cs index 9b8c35f6..06efac1e 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> GetByRecordAsync(int idWell, int idRecord, CancellationToken token = default); + Task> GetDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default); } #nullable disable } diff --git a/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs b/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs index 1d211734..9fc62845 100644 --- a/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs +++ b/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs @@ -3,7 +3,6 @@ using AsbCloudApp.Repositories; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudDb.Model.GTR; -using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -23,7 +22,7 @@ namespace AsbCloudInfrastructure.Repository IAsbCloudDbContext db, ITelemetryService telemetryService) { - + this.db = db; this.telemetryService = telemetryService; } @@ -36,12 +35,21 @@ namespace AsbCloudInfrastructure.Repository var timezone = telemetryService.GetTimezone(telemetry.Id); - DateTimeOffset? dateBeginUtc = dateBegin?.ToUtcDateTimeOffset(timezone.Hours); + DateTimeOffset? dateBeginUtc = dateBegin?.ToUtcDateTimeOffset(timezone.Hours); var dateEnd = dateBeginUtc?.AddSeconds(intervalSec); - var recordAllInt = await GetItemsOrDefaultAsync(telemetry.Id, dateBeginUtc, dateEnd, approxPointsCount, timezone.Hours, token); - var recordAllFloat = await GetItemsOrDefaultAsync(telemetry.Id, dateBeginUtc, dateEnd, approxPointsCount,timezone.Hours, token); - var recordAllString = await GetItemsOrDefaultAsync(telemetry.Id, dateBeginUtc, dateEnd, approxPointsCount, timezone.Hours, token); + var witsRequest = new WitsRequest() + { + IdTelemetry = telemetry.Id, + DateBeginUtc = dateBeginUtc, + DateEnd = dateEnd, + ApproxPointsCount = approxPointsCount, + TimezoneHours = timezone.Hours + }; + + var recordAllInt = await GetItemsOrDefaultAsync(witsRequest, token); + var recordAllFloat = await GetItemsOrDefaultAsync(witsRequest, token); + var recordAllString = await GetItemsOrDefaultAsync(witsRequest, token); var dtos = (recordAllFloat.Union(recordAllInt)).Union(recordAllString) .GroupBy(g => new @@ -53,15 +61,16 @@ namespace AsbCloudInfrastructure.Repository { Id = g.Key.IdRecord, Date = g.Key.Date, - Items = g.Select(r => new { - Key = r.IdItem, - Value = r.Item + Items = g.Select(r => new + { + Key = r.IdItem, + Value = r.Item }).ToDictionary(x => x.Key, x => x.Value) - }); + }); return dtos; } - public async Task> GetByRecordAsync(int idWell, int idRecord, CancellationToken token = default) + public async Task> GetDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default) { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); if (telemetry is null) @@ -69,9 +78,17 @@ namespace AsbCloudInfrastructure.Repository var timezone = telemetryService.GetTimezone(telemetry.Id); - var recordAllInt = await GetItemsOrDefaultByRecordAsync(telemetry.Id, idRecord, timezone.Hours, token); - var recordAllFloat = await GetItemsOrDefaultByRecordAsync(telemetry.Id, idRecord, timezone.Hours, token); - var recordAllString = await GetItemsOrDefaultByRecordAsync(telemetry.Id, idRecord, timezone.Hours, token); + var witsRequest = new WitsRequest() + { + IdTelemetry = telemetry.Id, + TimezoneHours = timezone.Hours, + IdRecord = idRecord, + }; + + var recordAllInt = await GetItemsOrDefaultAsync(witsRequest, token); + var recordAllFloat = await GetItemsOrDefaultAsync(witsRequest, token); + var recordAllString = await GetItemsOrDefaultAsync(witsRequest, token); + var dtos = (recordAllFloat.Union(recordAllInt)).Union(recordAllString) .GroupBy(g => new @@ -85,95 +102,68 @@ namespace AsbCloudInfrastructure.Repository IdRecord = g.Key.IdRecord, IdItem = g.Key.IdItem, Date = g.MaxBy(i => i.Date)!.Date, - Value = g.MaxBy(i => i.Date)!.Item.Value, + Value = g.MaxBy(i => i.Date)!.Item.Value, }).ToArray(); return dtos; } private async Task> GetItemsOrDefaultAsync( - int idTelemetry, - DateTimeOffset? dateBegin, - DateTimeOffset? dateEnd, - int approxPointsCount, - double timezoneHours, + WitsRequest request, CancellationToken token) - where TEntity: WitsItemBase - where TValue: notnull + where TEntity : WitsItemBase + where TValue : notnull { - var query = db.Set() - .Where(i => i.IdTelemetry == idTelemetry); - - if (dateBegin is not null) - query = query - .Where(d => d.DateTime >= dateBegin); - - if (dateEnd is not null) - query = query - .Where(d => d.DateTime <= dateEnd); + var query = BuildQuery(request, token); var fullDataCount = await query.CountAsync(token); - if (fullDataCount == 0) return Enumerable.Empty(); - if (fullDataCount > 1.75 * approxPointsCount) + if (request.ApproxPointsCount is not null && fullDataCount > 1.75 * request.ApproxPointsCount) { - var m = (int)Math.Round(1d * fullDataCount / 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(timezoneHours), + Date = e.DateTime.ToRemoteDateTime(request.TimezoneHours), IdItem = e.IdItem, Item = new JsonValue(e.Value) }); return items; } - private async Task> GetItemsOrDefaultByRecordAsync( - int idTelemetry, - int idRecord, - double timezoneHours, - CancellationToken token) + private IQueryable BuildQuery(WitsRequest request, CancellationToken token) where TEntity : WitsItemBase where TValue : notnull { - var query = db.Set() - .Where(i => i.IdTelemetry == idTelemetry) - .Where(i => i.IdRecord == idRecord); + var query = db.Set().Where(i => i.IdTelemetry == request.IdTelemetry); - var fullDataCount = await query.CountAsync(token); + if (request.IdRecord is not null) + query = query + .Where(d => d.IdRecord == request.IdRecord); - if (fullDataCount == 0) - return Enumerable.Empty(); + if (request.DateBeginUtc is not null) + query = query + .Where(d => d.DateTime >= request.DateBeginUtc); - var entities = await query - .OrderBy(d => d.DateTime) - .AsNoTracking() - .ToListAsync(token) - .ConfigureAwait(false); + if (request.DateEnd is not null) + query = query + .Where(d => d.DateTime <= request.DateEnd); - var items = entities.Select(e => new ItemRecord - { - IdRecord = e.IdRecord, - IdTelemetry = e.IdTelemetry, - Date = e.DateTime.ToRemoteDateTime(timezoneHours), - IdItem = e.IdItem, - Item = new JsonValue(e.Value) - }); - return items; + return query.OrderBy(d => d.DateTime); } + public async Task SaveDataAsync(int idTelemetry, WitsRecordDto dto, CancellationToken token) { var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours; @@ -182,7 +172,7 @@ namespace AsbCloudInfrastructure.Repository var dateTime = dto.Date.ToUtcDateTimeOffset(timezoneHours); if (item.Value.Value is string valueString) { - var entity = MakeEntity( dto.Id, item.Key, idTelemetry, dateTime, valueString); + var entity = MakeEntity(dto.Id, item.Key, idTelemetry, dateTime, valueString); db.WitsItemString.Add(entity); } if (item.Value.Value is float valueFloat) @@ -194,30 +184,40 @@ namespace AsbCloudInfrastructure.Repository { var entity = MakeEntity(dto.Id, item.Key, idTelemetry, dateTime, valueInt); db.WitsItemInt.Add(entity); - } - } + } + } await db.SaveChangesAsync(token); } private static TEntity MakeEntity(int idRecord, int idItem, int idTelemetry, DateTimeOffset dateTime, TValue value) where TEntity : WitsItemBase, new() - where TValue: notnull - => new TEntity() { - IdRecord = idRecord, - IdItem = idItem, - IdTelemetry = idTelemetry, - DateTime = dateTime, - Value = value, - }; + where TValue : notnull + => new TEntity() + { + IdRecord = idRecord, + IdItem = idItem, + IdTelemetry = idTelemetry, + DateTime = dateTime, + Value = value, + }; internal class ItemRecord { - public int IdRecord { get; set; } + 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; } + public DateTimeOffset? DateBeginUtc { get; set; } + public DateTimeOffset? DateEnd { get; set; } + public int? ApproxPointsCount { get; set; } + public double TimezoneHours { get; set; } + public int? IdRecord { get; set; } + } + } } diff --git a/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs b/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs index e97d551a..498399d9 100644 --- a/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs +++ b/AsbCloudWebApi/Controllers/SAUB/GtrWitsController.cs @@ -68,9 +68,16 @@ namespace AsbCloudWebApi.Controllers.SAUB return Ok(content); } + /// + /// получение данных ГТИ по ключу record + /// + /// id скважины + /// id record + /// + /// [HttpGet("{idWell}/{idRecord}")] [Permission] - public async Task>> GetDataByRecordAsync(int idWell, int idRecord, CancellationToken token = default) + public async Task>> GetDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default) { int? idCompany = User.GetCompanyId(); @@ -83,7 +90,7 @@ namespace AsbCloudWebApi.Controllers.SAUB if (!isCompanyOwnsWell) return Forbid(); - var content = await gtrRepository.GetByRecordAsync(idWell, idRecord, token).ConfigureAwait(false); + var content = await gtrRepository.GetDataByRecordIdAsync(idWell, idRecord, token).ConfigureAwait(false); return Ok(content); }