rework get method in rep

This commit is contained in:
eugeniy_ivanov 2023-04-11 00:32:06 +05:00
parent 2e58dea88a
commit 7c827f4fae
4 changed files with 243 additions and 123 deletions

View File

@ -11,7 +11,7 @@ namespace AsbCloudApp.Repositories
/// <summary> /// <summary>
/// данные ГТИ /// данные ГТИ
/// </summary> /// </summary>
public interface IGtrRepository<T> public interface IGtrRepository
{ {
/// <summary> /// <summary>
/// добавить данные (для панели бурильщика) /// добавить данные (для панели бурильщика)

View File

@ -138,9 +138,7 @@ namespace AsbCloudInfrastructure
services.AddTransient<IProcessMapReportService, ProcessMapReportService>(); services.AddTransient<IProcessMapReportService, ProcessMapReportService>();
services.AddTransient<IProcessMapService, ProcessMapService>(); services.AddTransient<IProcessMapService, ProcessMapService>();
services.AddTransient<ITrajectoryVisualizationService, TrajectoryVisualizationService>(); services.AddTransient<ITrajectoryVisualizationService, TrajectoryVisualizationService>();
services.AddTransient<IGtrRepository<int>, GtrWitsRepository<WitsItemInt,int>>(); services.AddTransient<IGtrRepository, GtrWitsRepository>();
services.AddTransient<IGtrRepository<float>, GtrWitsRepository<WitsItemFloat, float>>();
services.AddTransient<IGtrRepository<string>, GtrWitsRepository<WitsItemString, string>>();
// admin crud services: // admin crud services:
services.AddTransient<ICrudRepository<TelemetryDto>, CrudCacheRepositoryBase<TelemetryDto, Telemetry>>(s => services.AddTransient<ICrudRepository<TelemetryDto>, CrudCacheRepositoryBase<TelemetryDto, Telemetry>>(s =>

View File

@ -13,19 +13,19 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository namespace AsbCloudInfrastructure.Repository
{ {
#nullable enable #nullable enable
public class GtrWitsRepository<TEntity, T> : IGtrRepository <T> public class GtrWitsRepository : IGtrRepository
where TEntity : WitsItemBase<T>
{ {
private readonly IAsbCloudDbContext db; private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService; private readonly ITelemetryService telemetryService;
private readonly DbSet<TEntity> dbset;
private static Random random = new Random((int)(DateTime.Now.Ticks % 0xFFFFFFFF)); private static Random random = new Random((int)(DateTime.Now.Ticks % 0xFFFFFFFF));
public GtrWitsRepository( public GtrWitsRepository(
IAsbCloudDbContext db, IAsbCloudDbContext db,
ITelemetryService telemetryService) ITelemetryService telemetryService)
{ {
dbset = db.Set<TEntity>();
this.db = db; this.db = db;
this.telemetryService = telemetryService; this.telemetryService = telemetryService;
} }
@ -60,43 +60,99 @@ namespace AsbCloudInfrastructure.Repository
// return cacheData; // return cacheData;
var dateEnd = dateBeginUtc.AddSeconds(intervalSec); var dateEnd = dateBeginUtc.AddSeconds(intervalSec);
var dbSet = db.Set<TEntity>();
var query = dbSet var queryWitsInt = db.WitsItemInt
.Where(d => d.IdTelemetry == telemetry.Id
&& d.DateTime >= dateBeginUtc);
var queryWitsStr = db.WitsItemString
.Where(d => d.IdTelemetry == telemetry.Id
&& d.DateTime >= dateBeginUtc);
var queryWitsFloat = db.WitsItemFloat
.Where(d => d.IdTelemetry == telemetry.Id .Where(d => d.IdTelemetry == telemetry.Id
&& d.DateTime >= dateBeginUtc); && d.DateTime >= dateBeginUtc);
if (filterByDateEnd) var recordAllInt = GetEntityIntAsync(queryWitsInt, dateEnd, filterByDateEnd, approxPointsCount, token).Result;
query = query.Where(d => d.DateTime <= dateEnd); var recordAllFloat = GetEntityFloatAsync(queryWitsFloat, dateEnd, filterByDateEnd, approxPointsCount, token).Result;
var recordAllStr = GetEntityStrAsync(queryWitsStr, dateEnd, filterByDateEnd, approxPointsCount, token).Result;
var fullDataCount = await query.CountAsync(token)
.ConfigureAwait(false);
//if (fullDataCount == 0)
// return Enumerable.Empty<WitsRecordDto>();
//if (fullDataCount > 1.75 * approxPointsCount)
//{
// var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
// 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 groupRecord = entities
.GroupBy(g => g.IdRecord)
.ToList();
var dtos = new List<WitsRecordDto>(); var dtos = new List<WitsRecordDto>();
foreach(var group in groupRecord) if (recordAllInt.Any())
{ {
var dto = GetDto(group, telemetry.Id, timezone.Hours); foreach (var record in recordAllInt)
{
var existingDto = dtos.Where(r => r.Id == record.IdRecord)
.Where(r => r.Date.ToUtcDateTimeOffset(timezone.Hours) == record.DateTime)
.FirstOrDefault();
if (existingDto is null)
{
var dto = new WitsRecordDto
{
IdTelemetry = record.IdTelemetry,
Id = record.IdRecord,
Date = record.DateTime.ToRemoteDateTime(timezone.Hours),
};
dto.Items.Add(record.IdItem, new JsonValue(record.Value!));
dtos.Add(dto); dtos.Add(dto);
} }
else
{
existingDto.Items.Add(record.IdItem, new JsonValue(record.Value!));
}
}
}
if (recordAllStr.Any())
{
foreach (var record in recordAllStr)
{
var existingDto = dtos.Where(r => r.Id == record.IdRecord)
.Where(r => r.Date.ToUtcDateTimeOffset(timezone.Hours) == record.DateTime)
.FirstOrDefault();
if (existingDto is null)
{
var dto = new WitsRecordDto
{
IdTelemetry = record.IdTelemetry,
Id = record.IdRecord,
Date = record.DateTime.ToRemoteDateTime(timezone.Hours),
};
dto.Items.Add(record.IdItem, new JsonValue(record.Value!));
dtos.Add(dto);
}
else
{
existingDto.Items.Add(record.IdItem, new JsonValue(record.Value!));
}
}
}
if (recordAllFloat.Any())
{
foreach (var record in recordAllStr)
{
var existingDto = dtos
.Where(r => r.Id == record.IdRecord)
.Where(r => r.Date.ToUtcDateTimeOffset(timezone.Hours) == record.DateTime)
.FirstOrDefault();
if (existingDto is null)
{
var dto = new WitsRecordDto
{
IdTelemetry = record.IdTelemetry,
Id = record.IdRecord,
Date = record.DateTime.ToRemoteDateTime(timezone.Hours),
};
dto.Items.Add(record.IdItem, new JsonValue(record.Value!));
dtos.Add(dto);
}
else
{
existingDto.Items.Add(record.IdItem, new JsonValue(record.Value!));
}
}
}
return dtos; return dtos;
} }
@ -104,79 +160,145 @@ namespace AsbCloudInfrastructure.Repository
public async Task SaveDataAsync(int idTelemetry, WitsRecordDto dto, CancellationToken token) public async Task SaveDataAsync(int idTelemetry, WitsRecordDto dto, CancellationToken token)
{ {
if (dto is null) //if (dto is null)
return; // return;
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours; //var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
var entities = GetEntities(dto,idTelemetry,timezoneHours); //var entities = GetEntities(dto, idTelemetry, timezoneHours);
var dateMin = entities.Min(e => e.DateTime); //var dateMin = entities.Min(e => e.DateTime);
var dateMax = entities.Max(e => e.DateTime); //var dateMax = entities.Max(e => e.DateTime);
var existingEntities = await dbset //var existingEntities = await dbset
.Where(e => e.IdTelemetry == idTelemetry) // .Where(e => e.IdTelemetry == idTelemetry)
.Where(e => e.DateTime >= dateMin && e.DateTime <= dateMax) // .Where(e => e.DateTime >= dateMin && e.DateTime <= dateMax)
.Select(e => e.DateTime) // .Select(e => e.DateTime)
.OrderBy(d => d) // .OrderBy(d => d)
.ToArrayAsync(token); // .ToArrayAsync(token);
foreach (var entity in entities) //foreach (var entity in entities)
{ //{
if (!existingEntities.Any(e => e == entity.DateTime)) // if (!existingEntities.Any(e => e == entity.DateTime))
{ // {
dbset.Add((TEntity)entity); // dbset.Add((TEntity)entity);
} // }
else // else
{ // {
var dt = entity.DateTime; // var dt = entity.DateTime;
entity.DateTime = new DateTimeOffset( // entity.DateTime = new DateTimeOffset(
dt.Year, // dt.Year,
dt.Month, // dt.Month,
dt.Day, // dt.Day,
dt.Hour, // dt.Hour,
dt.Minute, // dt.Minute,
dt.Second, // dt.Second,
(dt.Millisecond + random.Next(1, 283)) % 1000, // (dt.Millisecond + random.Next(1, 283)) % 1000,
dt.Offset); // dt.Offset);
dbset.Add((TEntity)entity); // dbset.Add((TEntity)entity);
} // }
} //}
await db.SaveChangesAsync(token); //await db.SaveChangesAsync(token);
} }
private static IEnumerable<WitsItemBase<T>> GetEntities(WitsRecordDto recordItems, int idTelemetry, double timezoneHours) private static async Task<IEnumerable<WitsItemInt>> GetEntityIntAsync(IQueryable<WitsItemInt> query,
DateTimeOffset dateEnd, bool filterByDateEnd, int approxPointsCount
, CancellationToken token)
{ {
var itemsCount = recordItems.Items.Count; if (filterByDateEnd)
var entities = new List<WitsItemBase<T>>(itemsCount); query = query.Where(d => d.DateTime <= dateEnd);
foreach (var dtoItems in recordItems.Items) var fullDataCount = await query.CountAsync(token)
.ConfigureAwait(false);
if (fullDataCount == 0)
return Enumerable.Empty<WitsItemInt>();
if (fullDataCount > 1.75 * approxPointsCount)
{ {
var entityItems = new WitsItemBase<T> var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
{ if (m > 1)
IdTelemetry = idTelemetry, query = query.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0);
DateTime = recordItems.Date.ToUtcDateTimeOffset(timezoneHours),
IdRecord = recordItems.Id,
IdItem = dtoItems.Key,
Value = System.Text.Json.JsonSerializer.Deserialize<T>(dtoItems.Value.ToString())
};
entities.Add(entityItems);
} }
var entities = await query
.OrderBy(d => d.DateTime)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
return entities; return entities;
} }
private static WitsRecordDto GetDto(IEnumerable<WitsItemBase<T>> entities, int idTelemetry, double timezoneHours) private static async Task<IEnumerable<WitsItemFloat>> GetEntityFloatAsync(IQueryable<WitsItemFloat> query,
DateTimeOffset dateEnd, bool filterByDateEnd, int approxPointsCount
, CancellationToken token)
{ {
var entity = entities.First(); if (filterByDateEnd)
var dto = new WitsRecordDto query = query.Where(d => d.DateTime <= dateEnd);
{
Id = entity.IdRecord,
Date = entity.DateTime.ToRemoteDateTime(timezoneHours),
IdTelemetry = idTelemetry
};
foreach (var entityItems in entities) var fullDataCount = await query.CountAsync(token)
.ConfigureAwait(false);
if (fullDataCount == 0)
return Enumerable.Empty<WitsItemFloat>();
if (fullDataCount > 1.75 * approxPointsCount)
{ {
var valueItem = new JsonValue(entityItems.Value!); var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
dto.Items.Add(entityItems.IdItem, valueItem); if (m > 1)
query = query.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0);
} }
return dto;
var entities = await query
.OrderBy(d => d.DateTime)
.AsNoTracking()
.ToListAsync(token)
.ConfigureAwait(false);
return entities;
} }
private static async Task<IEnumerable<WitsItemString>> GetEntityStrAsync(IQueryable<WitsItemString> query,
DateTimeOffset dateEnd, bool filterByDateEnd, int approxPointsCount
, CancellationToken token)
{
if (filterByDateEnd)
query = query.Where(d => d.DateTime <= dateEnd);
var fullDataCount = await query.CountAsync(token)
.ConfigureAwait(false);
if (fullDataCount == 0)
return Enumerable.Empty<WitsItemString>();
if (fullDataCount > 1.75 * approxPointsCount)
{
var m = (int)Math.Round(1d * fullDataCount / approxPointsCount);
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);
return entities;
}
//private static void GetEntities(WitsRecordDto recordItems, int idTelemetry, double timezoneHours)
//{
// var itemsCount = recordItems.Items.Count;
// var entities = new List<WitsItemBase<T>>(itemsCount);
// foreach (var dtoItems in recordItems.Items)
// {
// var entityItems = new WitsItemBase<T>
// {
// IdTelemetry = idTelemetry,
// DateTime = recordItems.Date.ToUtcDateTimeOffset(timezoneHours),
// IdRecord = recordItems.Id,
// IdItem = dtoItems.Key,
// Value = System.Text.Json.JsonSerializer.Deserialize<T>(dtoItems.Value.ToString())
// };
// entities.Add(entityItems);
// }
// return entities;
//}
} }
#nullable disable #nullable disable
} }

View File

@ -20,19 +20,19 @@ namespace AsbCloudWebApi.Controllers.GTR
{ {
protected readonly IWellService wellService; protected readonly IWellService wellService;
private readonly ITelemetryService telemetryService; private readonly ITelemetryService telemetryService;
//private readonly IGtrRepository<float> gtrRepository; private readonly IGtrRepository gtrRepository;
private readonly IHubContext<TelemetryHub> telemetryHubContext; private readonly IHubContext<TelemetryHub> telemetryHubContext;
//public string SirnalRMethodGetDataName { get; protected set; } = "ReceiveData"; //public string SirnalRMethodGetDataName { get; protected set; } = "ReceiveData";
public GtrWitsController( public GtrWitsController(
ITelemetryService telemetryService, ITelemetryService telemetryService,
// IGtrRepository<float> gtrRepository, IGtrRepository gtrRepository,
IWellService wellService, IWellService wellService,
IHubContext<TelemetryHub> telemetryHubContext) IHubContext<TelemetryHub> telemetryHubContext)
{ {
this.telemetryService = telemetryService; this.telemetryService = telemetryService;
//this.gtrRepository = gtrRepository; this.gtrRepository = gtrRepository;
this.wellService = wellService; this.wellService = wellService;
this.telemetryHubContext = telemetryHubContext; this.telemetryHubContext = telemetryHubContext;
} }
@ -47,26 +47,26 @@ namespace AsbCloudWebApi.Controllers.GTR
/// <param name = "approxPointsCount" > желаемое количество точек. Если в выборке точек будет больше, то выборка будет прорежена.</param> /// <param name = "approxPointsCount" > желаемое количество точек. Если в выборке точек будет больше, то выборка будет прорежена.</param>
/// <param name = "token" > Токен завершения задачи</param> /// <param name = "token" > Токен завершения задачи</param>
/// <returns></returns> /// <returns></returns>
//[HttpGet("{idWell}")] [HttpGet("{idWell}")]
////[Permission] //[Permission]
//public async Task<ActionResult<IEnumerable<WitsRecordDto>>> GetDataAsync(int idWell, DateTime begin = default, public async Task<ActionResult<IEnumerable<WitsRecordDto>>> GetDataAsync(int idWell, DateTime begin = default,
// int intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default) int intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
//{ {
// //int? idCompany = User.GetCompanyId(); //int? idCompany = User.GetCompanyId();
// //if (idCompany is null) //if (idCompany is null)
// // return Forbid(); // return Forbid();
// //bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, //bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
// // idWell, token).ConfigureAwait(false); // idWell, token).ConfigureAwait(false);
// //if (!isCompanyOwnsWell) //if (!isCompanyOwnsWell)
// // return Forbid(); // return Forbid();
// //var content = await gtrRepository.GetAsync(idWell, begin, var content = await gtrRepository.GetAsync(idWell, begin,
// // intervalSec, approxPointsCount, token).ConfigureAwait(false); intervalSec, approxPointsCount, token).ConfigureAwait(false);
// //return Ok(content); return Ok(content);
//} }
} }
} }