From abbc461330691a8a31b0784b8a8200da1b66a4a3 Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Mon, 17 May 2021 12:53:30 +0500 Subject: [PATCH] =?UTF-8?q?CS2-1:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=8C=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B4=D0=B8=D0=B0=D0=BF=D0=B0=D0=B7=D0=BE?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B4=D0=B0=D1=82=20=D0=B0=D1=80=D1=85=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/AsbCloudApp.csproj | 4 +++ AsbCloudApp/Services/IDataService.cs | 1 + AsbCloudApp/Services/ITelemetryService.cs | 2 ++ AsbCloudDb/Model/AsbCloudDbContext.cs | 18 +++++++++++++ AsbCloudDb/Model/DataSaubBase.cs | 2 +- AsbCloudDb/Model/IAsbCloudDbContext.cs | 2 ++ AsbCloudDb/Model/IIdTelemetryDate.cs | 10 +++++++ AsbCloudDb/Model/Message.cs | 4 +-- .../Services/DataService.cs | 10 +++++++ .../Services/MessageService.cs | 27 +++---------------- .../Services/TelemetryService.cs | 19 ++++++++++--- AsbCloudWebApi/Controllers/DataController.cs | 24 ++++++++++++++++- 12 files changed, 93 insertions(+), 30 deletions(-) create mode 100644 AsbCloudDb/Model/IIdTelemetryDate.cs diff --git a/AsbCloudApp/AsbCloudApp.csproj b/AsbCloudApp/AsbCloudApp.csproj index a20524d3..8e347065 100644 --- a/AsbCloudApp/AsbCloudApp.csproj +++ b/AsbCloudApp/AsbCloudApp.csproj @@ -8,4 +8,8 @@ + + + + diff --git a/AsbCloudApp/Services/IDataService.cs b/AsbCloudApp/Services/IDataService.cs index 168b1d6b..bbc927c4 100644 --- a/AsbCloudApp/Services/IDataService.cs +++ b/AsbCloudApp/Services/IDataService.cs @@ -9,5 +9,6 @@ namespace AsbCloudApp.Services IEnumerable Get(int wellId, DateTime dateBegin = default, double intervalSec = 600d, int approxPointsCount = 1024); void UpdateData(string uid, IEnumerable dtos); + DatesRangeDto GetDataDatesRange(int wellId); } } \ No newline at end of file diff --git a/AsbCloudApp/Services/ITelemetryService.cs b/AsbCloudApp/Services/ITelemetryService.cs index a59a57fa..91abbfc4 100644 --- a/AsbCloudApp/Services/ITelemetryService.cs +++ b/AsbCloudApp/Services/ITelemetryService.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using AsbCloudDb.Model; namespace AsbCloudApp.Services { @@ -7,5 +8,6 @@ namespace AsbCloudApp.Services int? GetWellIdByTelemetryUid(string uid); int GetOrCreateTemetryIdByUid(string uid); void UpdateInfo(string uid, TelemetryInfoDto info); + Telemetry GetTelemetryByWellId(int wellId); } } diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index 75527dde..ab74e970 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -194,6 +195,23 @@ namespace AsbCloudDb.Model .Include(e => e.Customer) .Where(e => e.Login == login); + public (DateTime From, DateTime To) GetDatesRange(DbSet dbSet, int idTelemetry) where T : class, IIdTelemetryDate + { + var datesRange = (from m in dbSet + where m.IdTelemetry == idTelemetry + group m by m.IdTelemetry into g + select new + { + From = g.Min(d => d.Date), + To = g.Max(d => d.Date) + }).FirstOrDefault(); + + if (datesRange is null) + return (From: DateTime.MinValue, To: DateTime.MaxValue); + + return (From: datesRange.From, To: datesRange.To); + } + public async Task CreatePartitionAsync(string propertyName, int id, CancellationToken token = default) where TEntity : class { diff --git a/AsbCloudDb/Model/DataSaubBase.cs b/AsbCloudDb/Model/DataSaubBase.cs index c051ce22..f1cf4573 100644 --- a/AsbCloudDb/Model/DataSaubBase.cs +++ b/AsbCloudDb/Model/DataSaubBase.cs @@ -9,7 +9,7 @@ using System.Text.Json.Serialization; namespace AsbCloudDb.Model { [Table("t_data_saub_base"), Comment("набор основных данных по SAUB")] - public partial class DataSaubBase : IId + public partial class DataSaubBase : IId, IIdTelemetryDate { [Key] [Column("id")] diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index 8c0e3ce2..da11c888 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -1,4 +1,5 @@ using Microsoft.EntityFrameworkCore; +using System; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -25,6 +26,7 @@ namespace AsbCloudDb.Model IQueryable GetWellsByCustomer(int idCustomer); IQueryable GetUsersByLogin(string login); + (DateTime From, DateTime To) GetDatesRange(DbSet dbSet, int idTelemetry) where T : class, IIdTelemetryDate; Task CreatePartitionAsync(string propertyName, int id, CancellationToken token = default) where TEntity : class; } } \ No newline at end of file diff --git a/AsbCloudDb/Model/IIdTelemetryDate.cs b/AsbCloudDb/Model/IIdTelemetryDate.cs new file mode 100644 index 00000000..61fb9dab --- /dev/null +++ b/AsbCloudDb/Model/IIdTelemetryDate.cs @@ -0,0 +1,10 @@ +using System; + +namespace AsbCloudDb.Model +{ + public interface IIdTelemetryDate + { + int IdTelemetry { get; set; } + DateTime Date { get; set; } + } +} diff --git a/AsbCloudDb/Model/Message.cs b/AsbCloudDb/Model/Message.cs index e8bcee04..07a635fd 100644 --- a/AsbCloudDb/Model/Message.cs +++ b/AsbCloudDb/Model/Message.cs @@ -8,14 +8,14 @@ using System.ComponentModel.DataAnnotations.Schema; namespace AsbCloudDb.Model { [Table("t_message"), Comment("Сообщения на буровых")] - public partial class Message : IId + public partial class Message : IId, IIdTelemetryDate { [Key] [Column("id")] public int Id { get; set; } [Column("id_telemetry")] - public int? IdTelemetry { get; set; } + public int IdTelemetry { get; set; } [Column("id_event")] public int IdEvent { get; set; } diff --git a/AsbCloudInfrastructure/Services/DataService.cs b/AsbCloudInfrastructure/Services/DataService.cs index 2cfb9d08..323878d8 100644 --- a/AsbCloudInfrastructure/Services/DataService.cs +++ b/AsbCloudInfrastructure/Services/DataService.cs @@ -96,5 +96,15 @@ namespace AsbCloudInfrastructure.Services db.SaveChanges(); } + public DatesRangeDto GetDataDatesRange(int wellId) + { + var telemetry = telemetryService.GetTelemetryByWellId(wellId); + if (telemetry is null) + return null; + + var result = db.GetDatesRange(db.DataSaubBases, telemetry.Id); + + return new DatesRangeDto { From = result.From, To = result.To}; + } } } diff --git a/AsbCloudInfrastructure/Services/MessageService.cs b/AsbCloudInfrastructure/Services/MessageService.cs index ce840653..0d848e3c 100644 --- a/AsbCloudInfrastructure/Services/MessageService.cs +++ b/AsbCloudInfrastructure/Services/MessageService.cs @@ -31,22 +31,9 @@ namespace AsbCloudInfrastructure.Services cacheWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); } - private Telemetry GetTelemetryByWellId(int wellId) - { - var well = cacheWells.FirstOrDefault(w => w.Id == wellId); - if (well is null) - return null; - - var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == well.Id); - if (telemetry is null) - return null; - - return telemetry; - } - public PaginationContainer GetMessages(int wellId, IEnumerable categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32) { - var telemetry = GetTelemetryByWellId(wellId); + var telemetry = telemetryService.GetTelemetryByWellId(wellId); if (telemetry is null) return null; @@ -117,19 +104,13 @@ namespace AsbCloudInfrastructure.Services public DatesRangeDto GetMessagesDatesRange(int wellId) { - var telemetry = GetTelemetryByWellId(wellId); + var telemetry = telemetryService.GetTelemetryByWellId(wellId); if (telemetry is null) return null; - var messagesDatesRange = (from m in db.Messages - where m.IdTelemetry == telemetry.Id - group m by m.IdTelemetry into g - select new DatesRangeDto { - From = g.Min(d => d.Date), - To = g.Max(d => d.Date) - }).FirstOrDefault(); + var result = db.GetDatesRange(db.Messages, telemetry.Id); - return messagesDatesRange ?? new DatesRangeDto { From = DateTime.MinValue, To = DateTime.MaxValue}; + return new DatesRangeDto { From = result.From, To = result.To }; } public void Insert(string uid, IEnumerable dtos) diff --git a/AsbCloudInfrastructure/Services/TelemetryService.cs b/AsbCloudInfrastructure/Services/TelemetryService.cs index e81f80e1..5691e0fe 100644 --- a/AsbCloudInfrastructure/Services/TelemetryService.cs +++ b/AsbCloudInfrastructure/Services/TelemetryService.cs @@ -10,13 +10,13 @@ namespace AsbCloudInfrastructure.Services { private readonly IMapper mapper; private readonly ICacheTable cacheTelemetry; - private readonly ICacheTable cacheWell; + private readonly ICacheTable cacheWells; public TelemetryService(IAsbCloudDbContext db, CacheDb cacheDb, MapperConfiguration mapperConfiguration) { mapper = mapperConfiguration.CreateMapper(); cacheTelemetry = cacheDb.GetCachedTable((AsbCloudDbContext)db); - cacheWell = cacheDb.GetCachedTable((AsbCloudDbContext)db); + cacheWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); } public int GetOrCreateTemetryIdByUid(string uid) @@ -32,13 +32,26 @@ namespace AsbCloudInfrastructure.Services cacheTelemetry.Upsert(telemetry); } + public Telemetry GetTelemetryByWellId(int wellId) + { + var well = cacheWells.FirstOrDefault(w => w.Id == wellId); + if (well is null) + return null; + + var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == well.Id); + if (telemetry is null) + return null; + + return telemetry; + } + private Well GetWellByTelemetryUid(string uid) { var tele = cacheTelemetry.FirstOrDefault(t => t.RemoteUid == uid, RefreshMode.IfResultEmpty); if (tele is null) return null; - return cacheWell.FirstOrDefault(w => w?.IdTelemetry == tele.Id); + return cacheWells.FirstOrDefault(w => w?.IdTelemetry == tele.Id); } private Telemetry GetOrCreateTelemetryByUid(string uid) diff --git a/AsbCloudWebApi/Controllers/DataController.cs b/AsbCloudWebApi/Controllers/DataController.cs index 171d3d28..1778ff00 100644 --- a/AsbCloudWebApi/Controllers/DataController.cs +++ b/AsbCloudWebApi/Controllers/DataController.cs @@ -16,10 +16,12 @@ namespace AsbCloudWebApi.Controllers public class DataController : ControllerBase { private readonly IDataService telemetryDataService; + private readonly IWellService wellService; - public DataController(IDataService telemetryDataService) + public DataController(IDataService telemetryDataService, IWellService wellService) { this.telemetryDataService = telemetryDataService; + this.wellService = wellService; } /// @@ -41,5 +43,25 @@ namespace AsbCloudWebApi.Controllers var content = telemetryDataService.Get(wellId, begin, intervalSec, approxPointsCount); return Ok(content); } + + [HttpGet] + [Route("{wellId}/dataDatesRange")] + [ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)] + public IActionResult GetDataDatesRange(int wellId) + { + int? idCustomer = User.GetCustomerId(); + + if (idCustomer is null) + return BadRequest(); + + bool isCustomerOwnsWell = wellService.CheckWellOwnership((int)idCustomer, wellId); + + if (!isCustomerOwnsWell) + return Forbid(); + + DatesRangeDto dataDatesRange = telemetryDataService.GetDataDatesRange(wellId); + + return Ok(dataDatesRange); + } } }