CS2-1: Добавлена возможность получения диапазона дат архива

This commit is contained in:
KharchenkoVV 2021-05-17 12:53:30 +05:00
parent 335792ae94
commit abbc461330
12 changed files with 93 additions and 30 deletions

View File

@ -8,4 +8,8 @@
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AsbCloudDb\AsbCloudDb.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -9,5 +9,6 @@ namespace AsbCloudApp.Services
IEnumerable<DataSaubBaseDto> Get(int wellId, DateTime dateBegin = default, double intervalSec = 600d, int approxPointsCount = 1024); IEnumerable<DataSaubBaseDto> Get(int wellId, DateTime dateBegin = default, double intervalSec = 600d, int approxPointsCount = 1024);
void UpdateData(string uid, IEnumerable<DataSaubBaseDto> dtos); void UpdateData(string uid, IEnumerable<DataSaubBaseDto> dtos);
DatesRangeDto GetDataDatesRange(int wellId);
} }
} }

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudDb.Model;
namespace AsbCloudApp.Services namespace AsbCloudApp.Services
{ {
@ -7,5 +8,6 @@ namespace AsbCloudApp.Services
int? GetWellIdByTelemetryUid(string uid); int? GetWellIdByTelemetryUid(string uid);
int GetOrCreateTemetryIdByUid(string uid); int GetOrCreateTemetryIdByUid(string uid);
void UpdateInfo(string uid, TelemetryInfoDto info); void UpdateInfo(string uid, TelemetryInfoDto info);
Telemetry GetTelemetryByWellId(int wellId);
} }
} }

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
@ -194,6 +195,23 @@ namespace AsbCloudDb.Model
.Include(e => e.Customer) .Include(e => e.Customer)
.Where(e => e.Login == login); .Where(e => e.Login == login);
public (DateTime From, DateTime To) GetDatesRange<T>(DbSet<T> 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<int> CreatePartitionAsync<TEntity>(string propertyName, int id, CancellationToken token = default) public async Task<int> CreatePartitionAsync<TEntity>(string propertyName, int id, CancellationToken token = default)
where TEntity : class where TEntity : class
{ {

View File

@ -9,7 +9,7 @@ using System.Text.Json.Serialization;
namespace AsbCloudDb.Model namespace AsbCloudDb.Model
{ {
[Table("t_data_saub_base"), Comment("набор основных данных по SAUB")] [Table("t_data_saub_base"), Comment("набор основных данных по SAUB")]
public partial class DataSaubBase : IId public partial class DataSaubBase : IId, IIdTelemetryDate
{ {
[Key] [Key]
[Column("id")] [Column("id")]

View File

@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -25,6 +26,7 @@ namespace AsbCloudDb.Model
IQueryable<Well> GetWellsByCustomer(int idCustomer); IQueryable<Well> GetWellsByCustomer(int idCustomer);
IQueryable<User> GetUsersByLogin(string login); IQueryable<User> GetUsersByLogin(string login);
(DateTime From, DateTime To) GetDatesRange<T>(DbSet<T> dbSet, int idTelemetry) where T : class, IIdTelemetryDate;
Task<int> CreatePartitionAsync<TEntity>(string propertyName, int id, CancellationToken token = default) where TEntity : class; Task<int> CreatePartitionAsync<TEntity>(string propertyName, int id, CancellationToken token = default) where TEntity : class;
} }
} }

View File

@ -0,0 +1,10 @@
using System;
namespace AsbCloudDb.Model
{
public interface IIdTelemetryDate
{
int IdTelemetry { get; set; }
DateTime Date { get; set; }
}
}

View File

@ -8,14 +8,14 @@ using System.ComponentModel.DataAnnotations.Schema;
namespace AsbCloudDb.Model namespace AsbCloudDb.Model
{ {
[Table("t_message"), Comment("Сообщения на буровых")] [Table("t_message"), Comment("Сообщения на буровых")]
public partial class Message : IId public partial class Message : IId, IIdTelemetryDate
{ {
[Key] [Key]
[Column("id")] [Column("id")]
public int Id { get; set; } public int Id { get; set; }
[Column("id_telemetry")] [Column("id_telemetry")]
public int? IdTelemetry { get; set; } public int IdTelemetry { get; set; }
[Column("id_event")] [Column("id_event")]
public int IdEvent { get; set; } public int IdEvent { get; set; }

View File

@ -96,5 +96,15 @@ namespace AsbCloudInfrastructure.Services
db.SaveChanges(); db.SaveChanges();
} }
public DatesRangeDto GetDataDatesRange(int wellId)
{
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
var result = db.GetDatesRange<DataSaubBase>(db.DataSaubBases, telemetry.Id);
return new DatesRangeDto { From = result.From, To = result.To};
}
} }
} }

View File

@ -31,22 +31,9 @@ namespace AsbCloudInfrastructure.Services
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db); cacheWells = cacheDb.GetCachedTable<Well>((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<MessageDto> GetMessages(int wellId, IEnumerable<int> categoryids = default, DateTime begin = default, DateTime end = default, int skip = 0, int take = 32) public PaginationContainer<MessageDto> GetMessages(int wellId, IEnumerable<int> 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) if (telemetry is null)
return null; return null;
@ -117,19 +104,13 @@ namespace AsbCloudInfrastructure.Services
public DatesRangeDto GetMessagesDatesRange(int wellId) public DatesRangeDto GetMessagesDatesRange(int wellId)
{ {
var telemetry = GetTelemetryByWellId(wellId); var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null) if (telemetry is null)
return null; return null;
var messagesDatesRange = (from m in db.Messages var result = db.GetDatesRange<Message>(db.Messages, telemetry.Id);
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();
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<TelemetryMessageDto> dtos) public void Insert(string uid, IEnumerable<TelemetryMessageDto> dtos)

View File

@ -10,13 +10,13 @@ namespace AsbCloudInfrastructure.Services
{ {
private readonly IMapper mapper; private readonly IMapper mapper;
private readonly ICacheTable<Telemetry> cacheTelemetry; private readonly ICacheTable<Telemetry> cacheTelemetry;
private readonly ICacheTable<Well> cacheWell; private readonly ICacheTable<Well> cacheWells;
public TelemetryService(IAsbCloudDbContext db, CacheDb cacheDb, MapperConfiguration mapperConfiguration) public TelemetryService(IAsbCloudDbContext db, CacheDb cacheDb, MapperConfiguration mapperConfiguration)
{ {
mapper = mapperConfiguration.CreateMapper(); mapper = mapperConfiguration.CreateMapper();
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db); cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
cacheWell = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db); cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
} }
public int GetOrCreateTemetryIdByUid(string uid) public int GetOrCreateTemetryIdByUid(string uid)
@ -32,13 +32,26 @@ namespace AsbCloudInfrastructure.Services
cacheTelemetry.Upsert(telemetry); 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) private Well GetWellByTelemetryUid(string uid)
{ {
var tele = cacheTelemetry.FirstOrDefault(t => t.RemoteUid == uid, RefreshMode.IfResultEmpty); var tele = cacheTelemetry.FirstOrDefault(t => t.RemoteUid == uid, RefreshMode.IfResultEmpty);
if (tele is null) if (tele is null)
return null; return null;
return cacheWell.FirstOrDefault(w => w?.IdTelemetry == tele.Id); return cacheWells.FirstOrDefault(w => w?.IdTelemetry == tele.Id);
} }
private Telemetry GetOrCreateTelemetryByUid(string uid) private Telemetry GetOrCreateTelemetryByUid(string uid)

View File

@ -16,10 +16,12 @@ namespace AsbCloudWebApi.Controllers
public class DataController : ControllerBase public class DataController : ControllerBase
{ {
private readonly IDataService telemetryDataService; private readonly IDataService telemetryDataService;
private readonly IWellService wellService;
public DataController(IDataService telemetryDataService) public DataController(IDataService telemetryDataService, IWellService wellService)
{ {
this.telemetryDataService = telemetryDataService; this.telemetryDataService = telemetryDataService;
this.wellService = wellService;
} }
/// <summary> /// <summary>
@ -41,5 +43,25 @@ namespace AsbCloudWebApi.Controllers
var content = telemetryDataService.Get(wellId, begin, intervalSec, approxPointsCount); var content = telemetryDataService.Get(wellId, begin, intervalSec, approxPointsCount);
return Ok(content); 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);
}
} }
} }