2023-04-04 21:21:06 +05:00
|
|
|
|
using AsbCloudApp.Data.GTR;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudApp.Services.WITS;
|
|
|
|
|
using AsbCloudDb.Model;
|
2023-04-06 04:02:47 +05:00
|
|
|
|
using AsbCloudDb.Model.GTR;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Newtonsoft.Json;
|
2023-04-04 21:21:06 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Repository
|
|
|
|
|
{
|
|
|
|
|
#nullable enable
|
2023-04-06 04:02:47 +05:00
|
|
|
|
public class GtrWitsRepository<T> : IGtrRepository
|
|
|
|
|
where T : WitsItemBase<T>, ITelemetryData
|
2023-04-04 21:21:06 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2023-04-06 04:02:47 +05:00
|
|
|
|
private readonly DbSet<T> dbset;
|
|
|
|
|
private static Random random = new Random((int)(DateTime.Now.Ticks % 0xFFFFFFFF));
|
2023-04-04 21:21:06 +05:00
|
|
|
|
|
|
|
|
|
public GtrWitsRepository(
|
|
|
|
|
IAsbCloudDbContext db,
|
|
|
|
|
ITelemetryService telemetryService)
|
|
|
|
|
{
|
2023-04-06 04:02:47 +05:00
|
|
|
|
dbset = db.Set<T>();
|
2023-04-04 21:21:06 +05:00
|
|
|
|
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 dateEnd = dateBeginUtc.AddSeconds(intervalSec);
|
|
|
|
|
|
|
|
|
|
//временная заглушка(для билда без ошибок)
|
|
|
|
|
return Enumerable.Empty<WitsRecordDto>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-04-06 04:02:47 +05:00
|
|
|
|
public async Task SaveDataAsync(int idTelemetry, WitsRecordDto dto, CancellationToken token)
|
2023-04-04 21:21:06 +05:00
|
|
|
|
{
|
2023-04-06 04:02:47 +05:00
|
|
|
|
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;
|
2023-04-04 21:21:06 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#nullable disable
|
|
|
|
|
}
|