DD.WellWorkover.Cloud/AsbCloudInfrastructure/Repository/GtrWitsRepository.cs
2023-04-07 02:53:57 +05:00

183 lines
6.7 KiB
C#

using AsbCloudApp.Data.GTR;
using AsbCloudApp.Services;
using AsbCloudApp.Services.WITS;
using AsbCloudDb.Model;
using AsbCloudDb.Model.GTR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
#nullable enable
public class GtrWitsRepository<T> : IGtrRepository
where T : WitsItemBase<T>
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
private readonly DbSet<T> dbset;
private static Random random = new Random((int)(DateTime.Now.Ticks % 0xFFFFFFFF));
public GtrWitsRepository(
IAsbCloudDbContext db,
ITelemetryService telemetryService)
{
dbset = db.Set<T>();
this.db = db;
this.telemetryService = telemetryService;
}
public async Task<IEnumerable<WitsRecordDto>> GetAsync(int idWell, DateTime dateBegin = default, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
{
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
if (telemetry is null)
return Enumerable.Empty<WitsRecordDto>();
var timezone = telemetryService.GetTimezone(telemetry.Id);
var filterByDateEnd = dateBegin != default;
DateTimeOffset dateBeginUtc;
if (dateBegin == default)
{
dateBeginUtc = telemetryService.GetLastTelemetryDate(telemetry.Id)
.ToUtcDateTimeOffset(timezone.Hours);
if (dateBeginUtc != default)
dateBeginUtc = dateBeginUtc.AddSeconds(-intervalSec);
}
else
{
dateBeginUtc = dateBegin.ToUtcDateTimeOffset(timezone.Hours);
}
if (dateBeginUtc == default)
dateBeginUtc = DateTime.UtcNow.AddSeconds(-intervalSec);
//var cacheData = telemetryDataCache.GetOrDefault(telemetry.Id, dateBeginUtc.ToRemoteDateTime(timezone.Hours), intervalSec, approxPointsCount);
//if (cacheData is not null)
// return cacheData;
var dateEnd = dateBeginUtc.AddSeconds(intervalSec);
var dbSet = db.Set<T>();
var query = dbSet
.Where(d => d.IdTelemetry == telemetry.Id
&& d.DateTime >= dateBeginUtc);
if (filterByDateEnd)
query = query.Where(d => d.DateTime <= dateEnd);
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)
{
var dto = GetDto(group, telemetry.Id, timezone.Hours);
dtos.Add(dto);
}
return dtos;
}
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((T)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((T)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)
{
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;
}
private static WitsRecordDto GetDto(IEnumerable<WitsItemBase<T>> entities, int idTelemetry, double timezoneHours)
{
var entity = entities.First();
var dto = new WitsRecordDto
{
Id = entity.IdRecord,
Date = entity.DateTime.ToRemoteDateTime(timezoneHours),
IdTelemetry = idTelemetry
};
foreach (var entityItems in entities)
{
var valueItem = new JsonValue(entityItems.Value!);
dto.Items.Add(entityItems.IdItem, valueItem);
}
return dto;
}
}
#nullable disable
}