forked from ddrilling/AsbCloudServer
rework get method in rep
This commit is contained in:
parent
2e58dea88a
commit
7c827f4fae
@ -11,7 +11,7 @@ namespace AsbCloudApp.Repositories
|
||||
/// <summary>
|
||||
/// данные ГТИ
|
||||
/// </summary>
|
||||
public interface IGtrRepository<T>
|
||||
public interface IGtrRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// добавить данные (для панели бурильщика)
|
||||
|
@ -138,9 +138,7 @@ namespace AsbCloudInfrastructure
|
||||
services.AddTransient<IProcessMapReportService, ProcessMapReportService>();
|
||||
services.AddTransient<IProcessMapService, ProcessMapService>();
|
||||
services.AddTransient<ITrajectoryVisualizationService, TrajectoryVisualizationService>();
|
||||
services.AddTransient<IGtrRepository<int>, GtrWitsRepository<WitsItemInt,int>>();
|
||||
services.AddTransient<IGtrRepository<float>, GtrWitsRepository<WitsItemFloat, float>>();
|
||||
services.AddTransient<IGtrRepository<string>, GtrWitsRepository<WitsItemString, string>>();
|
||||
services.AddTransient<IGtrRepository, GtrWitsRepository>();
|
||||
|
||||
// admin crud services:
|
||||
services.AddTransient<ICrudRepository<TelemetryDto>, CrudCacheRepositoryBase<TelemetryDto, Telemetry>>(s =>
|
||||
|
@ -13,19 +13,19 @@ using System.Threading.Tasks;
|
||||
namespace AsbCloudInfrastructure.Repository
|
||||
{
|
||||
#nullable enable
|
||||
public class GtrWitsRepository<TEntity, T> : IGtrRepository <T>
|
||||
where TEntity : WitsItemBase<T>
|
||||
public class GtrWitsRepository : IGtrRepository
|
||||
|
||||
{
|
||||
private readonly IAsbCloudDbContext db;
|
||||
private readonly ITelemetryService telemetryService;
|
||||
private readonly DbSet<TEntity> dbset;
|
||||
|
||||
private static Random random = new Random((int)(DateTime.Now.Ticks % 0xFFFFFFFF));
|
||||
|
||||
public GtrWitsRepository(
|
||||
IAsbCloudDbContext db,
|
||||
ITelemetryService telemetryService)
|
||||
{
|
||||
dbset = db.Set<TEntity>();
|
||||
|
||||
this.db = db;
|
||||
this.telemetryService = telemetryService;
|
||||
}
|
||||
@ -60,42 +60,98 @@ namespace AsbCloudInfrastructure.Repository
|
||||
// return cacheData;
|
||||
|
||||
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
|
||||
&& d.DateTime >= dateBeginUtc);
|
||||
|
||||
if (filterByDateEnd)
|
||||
query = query.Where(d => d.DateTime <= dateEnd);
|
||||
var recordAllInt = GetEntityIntAsync(queryWitsInt, dateEnd, filterByDateEnd, approxPointsCount, token).Result;
|
||||
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>();
|
||||
foreach(var group in groupRecord)
|
||||
if (recordAllInt.Any())
|
||||
{
|
||||
var dto = GetDto(group, telemetry.Id, timezone.Hours);
|
||||
dtos.Add(dto);
|
||||
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);
|
||||
}
|
||||
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;
|
||||
}
|
||||
@ -104,79 +160,145 @@ namespace AsbCloudInfrastructure.Repository
|
||||
|
||||
public async Task SaveDataAsync(int idTelemetry, WitsRecordDto dto, CancellationToken token)
|
||||
{
|
||||
if (dto is null)
|
||||
return;
|
||||
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
||||
var entities = GetEntities(dto,idTelemetry,timezoneHours);
|
||||
var dateMin = entities.Min(e => e.DateTime);
|
||||
var dateMax = entities.Max(e => e.DateTime);
|
||||
var existingEntities = await dbset
|
||||
.Where(e => e.IdTelemetry == idTelemetry)
|
||||
.Where(e => e.DateTime >= dateMin && e.DateTime <= dateMax)
|
||||
.Select(e => e.DateTime)
|
||||
.OrderBy(d => d)
|
||||
.ToArrayAsync(token);
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
if (!existingEntities.Any(e => e == entity.DateTime))
|
||||
{
|
||||
dbset.Add((TEntity)entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
var dt = entity.DateTime;
|
||||
entity.DateTime = new DateTimeOffset(
|
||||
dt.Year,
|
||||
dt.Month,
|
||||
dt.Day,
|
||||
dt.Hour,
|
||||
dt.Minute,
|
||||
dt.Second,
|
||||
(dt.Millisecond + random.Next(1, 283)) % 1000,
|
||||
dt.Offset);
|
||||
dbset.Add((TEntity)entity);
|
||||
}
|
||||
}
|
||||
await db.SaveChangesAsync(token);
|
||||
//if (dto is null)
|
||||
// return;
|
||||
//var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
||||
//var entities = GetEntities(dto, idTelemetry, timezoneHours);
|
||||
//var dateMin = entities.Min(e => e.DateTime);
|
||||
//var dateMax = entities.Max(e => e.DateTime);
|
||||
//var existingEntities = await dbset
|
||||
// .Where(e => e.IdTelemetry == idTelemetry)
|
||||
// .Where(e => e.DateTime >= dateMin && e.DateTime <= dateMax)
|
||||
// .Select(e => e.DateTime)
|
||||
// .OrderBy(d => d)
|
||||
// .ToArrayAsync(token);
|
||||
//foreach (var entity in entities)
|
||||
//{
|
||||
// if (!existingEntities.Any(e => e == entity.DateTime))
|
||||
// {
|
||||
// dbset.Add((TEntity)entity);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// var dt = entity.DateTime;
|
||||
// entity.DateTime = new DateTimeOffset(
|
||||
// dt.Year,
|
||||
// dt.Month,
|
||||
// dt.Day,
|
||||
// dt.Hour,
|
||||
// dt.Minute,
|
||||
// dt.Second,
|
||||
// (dt.Millisecond + random.Next(1, 283)) % 1000,
|
||||
// dt.Offset);
|
||||
// dbset.Add((TEntity)entity);
|
||||
// }
|
||||
//}
|
||||
//await db.SaveChangesAsync(token);
|
||||
}
|
||||
|
||||
private static IEnumerable<WitsItemBase<T>> 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)
|
||||
private static async Task<IEnumerable<WitsItemInt>> GetEntityIntAsync(IQueryable<WitsItemInt> 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<WitsItemInt>();
|
||||
|
||||
if (fullDataCount > 1.75 * approxPointsCount)
|
||||
{
|
||||
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);
|
||||
}
|
||||
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 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();
|
||||
var dto = new WitsRecordDto
|
||||
{
|
||||
Id = entity.IdRecord,
|
||||
Date = entity.DateTime.ToRemoteDateTime(timezoneHours),
|
||||
IdTelemetry = idTelemetry
|
||||
};
|
||||
if (filterByDateEnd)
|
||||
query = query.Where(d => d.DateTime <= dateEnd);
|
||||
|
||||
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!);
|
||||
dto.Items.Add(entityItems.IdItem, valueItem);
|
||||
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);
|
||||
}
|
||||
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
|
||||
}
|
||||
|
@ -20,19 +20,19 @@ namespace AsbCloudWebApi.Controllers.GTR
|
||||
{
|
||||
protected readonly IWellService wellService;
|
||||
private readonly ITelemetryService telemetryService;
|
||||
//private readonly IGtrRepository<float> gtrRepository;
|
||||
private readonly IGtrRepository gtrRepository;
|
||||
private readonly IHubContext<TelemetryHub> telemetryHubContext;
|
||||
|
||||
//public string SirnalRMethodGetDataName { get; protected set; } = "ReceiveData";
|
||||
|
||||
public GtrWitsController(
|
||||
ITelemetryService telemetryService,
|
||||
// IGtrRepository<float> gtrRepository,
|
||||
IGtrRepository gtrRepository,
|
||||
IWellService wellService,
|
||||
IHubContext<TelemetryHub> telemetryHubContext)
|
||||
{
|
||||
this.telemetryService = telemetryService;
|
||||
//this.gtrRepository = gtrRepository;
|
||||
this.gtrRepository = gtrRepository;
|
||||
this.wellService = wellService;
|
||||
this.telemetryHubContext = telemetryHubContext;
|
||||
}
|
||||
@ -41,32 +41,32 @@ namespace AsbCloudWebApi.Controllers.GTR
|
||||
/// Возвращает данные САУБ по скважине.
|
||||
/// По умолчанию за последние 10 минут.
|
||||
/// </summary>
|
||||
/// <param name="idWell">id скважины</param>
|
||||
/// <param name="begin">дата начала выборки. По умолчанию: текущее время - intervalSec</param>
|
||||
/// <param name="intervalSec">интервал времени даты начала выборки, секунды</param>
|
||||
/// <param name="approxPointsCount">желаемое количество точек. Если в выборке точек будет больше, то выборка будет прорежена.</param>
|
||||
/// <param name="token">Токен завершения задачи</param>
|
||||
/// <param name = "idWell" > id скважины</param>
|
||||
/// <param name = "begin" > дата начала выборки.По умолчанию: текущее время - intervalSec</param>
|
||||
/// <param name = "intervalSec" > интервал времени даты начала выборки, секунды</param>
|
||||
/// <param name = "approxPointsCount" > желаемое количество точек. Если в выборке точек будет больше, то выборка будет прорежена.</param>
|
||||
/// <param name = "token" > Токен завершения задачи</param>
|
||||
/// <returns></returns>
|
||||
//[HttpGet("{idWell}")]
|
||||
////[Permission]
|
||||
//public async Task<ActionResult<IEnumerable<WitsRecordDto>>> GetDataAsync(int idWell, DateTime begin = default,
|
||||
// int intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
|
||||
//{
|
||||
// //int? idCompany = User.GetCompanyId();
|
||||
[HttpGet("{idWell}")]
|
||||
//[Permission]
|
||||
public async Task<ActionResult<IEnumerable<WitsRecordDto>>> GetDataAsync(int idWell, DateTime begin = default,
|
||||
int intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
|
||||
{
|
||||
//int? idCompany = User.GetCompanyId();
|
||||
|
||||
// //if (idCompany is null)
|
||||
// // return Forbid();
|
||||
//if (idCompany is null)
|
||||
// return Forbid();
|
||||
|
||||
// //bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||||
// // idWell, token).ConfigureAwait(false);
|
||||
//bool isCompanyOwnsWell = await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||||
// idWell, token).ConfigureAwait(false);
|
||||
|
||||
// //if (!isCompanyOwnsWell)
|
||||
// // return Forbid();
|
||||
//if (!isCompanyOwnsWell)
|
||||
// return Forbid();
|
||||
|
||||
// //var content = await gtrRepository.GetAsync(idWell, begin,
|
||||
// // intervalSec, approxPointsCount, token).ConfigureAwait(false);
|
||||
var content = await gtrRepository.GetAsync(idWell, begin,
|
||||
intervalSec, approxPointsCount, token).ConfigureAwait(false);
|
||||
|
||||
// //return Ok(content);
|
||||
//}
|
||||
return Ok(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user