2022-11-03 16:57:41 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System;
|
2022-11-08 17:49:04 +05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Mapster;
|
2022-11-15 17:44:48 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2023-05-19 16:30:41 +05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using AsbCloudInfrastructure.Background;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using AsbCloudApp.Data;
|
2023-09-11 13:48:47 +05:00
|
|
|
|
using AsbCloudApp.Requests;
|
2023-10-24 09:23:07 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-11-03 16:57:41 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.SAUB
|
|
|
|
|
{
|
2023-10-24 09:23:07 +05:00
|
|
|
|
public class TelemetryDataCache<TDto> : ITelemetryDataCache<TDto> where TDto : AsbCloudApp.Data.ITelemetryData
|
2022-11-03 16:57:41 +05:00
|
|
|
|
{
|
2023-05-19 16:30:41 +05:00
|
|
|
|
class TelemetryDataCacheItem
|
|
|
|
|
{
|
2023-10-23 18:06:57 +05:00
|
|
|
|
public TDto FirstByDate { get; init; } = default!;
|
2023-05-19 16:30:41 +05:00
|
|
|
|
public CyclycArray<TDto> LastData { get; init; } = null!;
|
2023-09-12 16:22:01 +05:00
|
|
|
|
public double TimezoneHours { get; init; } = 5;
|
2023-05-19 16:30:41 +05:00
|
|
|
|
}
|
2023-10-24 10:55:50 +05:00
|
|
|
|
|
2022-12-05 10:53:24 +05:00
|
|
|
|
private const int activeWellCapacity = 12 * 60 * 60;
|
2022-11-08 17:49:04 +05:00
|
|
|
|
private const int doneWellCapacity = 65 * 60;
|
2022-11-03 16:57:41 +05:00
|
|
|
|
|
2023-05-19 16:30:41 +05:00
|
|
|
|
// key == idTelemetry
|
|
|
|
|
private readonly ConcurrentDictionary<int, TelemetryDataCacheItem> caches;
|
2022-11-15 17:44:48 +05:00
|
|
|
|
private bool isLoading = false;
|
|
|
|
|
|
|
|
|
|
private TelemetryDataCache()
|
2022-11-03 16:57:41 +05:00
|
|
|
|
{
|
2022-11-15 17:44:48 +05:00
|
|
|
|
caches = new();
|
2022-11-03 16:57:41 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-15 17:44:48 +05:00
|
|
|
|
private static TelemetryDataCache<TDto>? instance;
|
2022-11-08 17:49:04 +05:00
|
|
|
|
|
2023-05-19 16:30:41 +05:00
|
|
|
|
public static TelemetryDataCache<TDto> GetInstance<TEntity>(IServiceProvider provider)
|
|
|
|
|
where TEntity : class, AsbCloudDb.Model.ITelemetryData
|
2022-11-08 17:49:04 +05:00
|
|
|
|
{
|
2022-11-15 17:44:48 +05:00
|
|
|
|
if (instance is null)
|
|
|
|
|
{
|
|
|
|
|
instance = new TelemetryDataCache<TDto>();
|
2023-05-19 16:30:41 +05:00
|
|
|
|
var worker = provider.GetRequiredService<BackgroundWorker>();
|
|
|
|
|
var workId = $"Telemetry cache loading from DB {typeof(TEntity).Name}";
|
2023-10-24 09:23:07 +05:00
|
|
|
|
var work = Work.CreateByDelegate(workId, async (workId, provider, onProgress, token) =>
|
|
|
|
|
{
|
2023-05-19 16:30:41 +05:00
|
|
|
|
var db = provider.GetRequiredService<IAsbCloudDbContext>();
|
2023-10-08 19:45:21 +05:00
|
|
|
|
await instance.InitializeCacheFromDBAsync<TEntity>(db, onProgress, token);
|
2022-11-15 17:44:48 +05:00
|
|
|
|
});
|
2023-10-16 10:11:24 +05:00
|
|
|
|
work.Timeout = TimeSpan.FromMinutes(15);
|
2023-10-08 21:20:28 +05:00
|
|
|
|
worker.WorkStore.RunOnceQueue.Enqueue(work);
|
2022-11-15 17:44:48 +05:00
|
|
|
|
}
|
|
|
|
|
return instance;
|
2022-11-08 17:49:04 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-11-15 17:44:48 +05:00
|
|
|
|
/// Добавить новые элементы в кеш
|
2022-11-08 17:49:04 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idTelemetry"></param>
|
|
|
|
|
/// <param name="range"></param>
|
2022-11-15 17:44:48 +05:00
|
|
|
|
public void AddRange(int idTelemetry, IEnumerable<TDto> range)
|
2022-11-08 17:49:04 +05:00
|
|
|
|
{
|
2023-05-19 16:30:41 +05:00
|
|
|
|
if (!range.Any())
|
|
|
|
|
return;
|
|
|
|
|
|
2023-10-24 10:55:50 +05:00
|
|
|
|
range = range.OrderBy(x => x.DateTime);
|
2023-05-19 16:30:41 +05:00
|
|
|
|
|
2023-10-24 10:55:50 +05:00
|
|
|
|
foreach (var item in range)
|
2023-05-19 16:30:41 +05:00
|
|
|
|
item.IdTelemetry = idTelemetry;
|
|
|
|
|
|
|
|
|
|
TelemetryDataCacheItem cacheItem;
|
2022-11-15 17:44:48 +05:00
|
|
|
|
if (isLoading)
|
|
|
|
|
{
|
2023-05-19 16:30:41 +05:00
|
|
|
|
if (caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? localCacheItem))
|
2022-11-15 17:44:48 +05:00
|
|
|
|
cacheItem = localCacheItem;
|
|
|
|
|
else
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-05-19 16:30:41 +05:00
|
|
|
|
cacheItem = caches.GetOrAdd(idTelemetry, _ => new TelemetryDataCacheItem()
|
|
|
|
|
{
|
2023-10-24 10:55:50 +05:00
|
|
|
|
FirstByDate = range.ElementAt(0),
|
2023-05-19 16:30:41 +05:00
|
|
|
|
LastData = new CyclycArray<TDto>(activeWellCapacity)
|
|
|
|
|
});
|
2022-11-15 17:44:48 +05:00
|
|
|
|
}
|
2023-05-19 16:30:41 +05:00
|
|
|
|
|
2023-10-24 10:55:50 +05:00
|
|
|
|
cacheItem.LastData.AddRange(range);
|
2022-11-08 17:49:04 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить данные из кеша. <br/>
|
|
|
|
|
/// Если dateBegin меньше минимального элемента в кеше, то вернется null.
|
|
|
|
|
/// Даже если intervalSec частично перекрыт данными из кеша.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idTelemetry"></param>
|
|
|
|
|
/// <param name="dateBegin"></param>
|
|
|
|
|
/// <param name="intervalSec"></param>
|
|
|
|
|
/// <param name="approxPointsCount">кол-во элементов до которых эти данные прореживаются</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public IEnumerable<TDto>? GetOrDefault(int idTelemetry, DateTime dateBegin, double intervalSec = 600d, int approxPointsCount = 1024)
|
|
|
|
|
{
|
2023-10-24 09:23:07 +05:00
|
|
|
|
if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
|
2022-11-08 17:49:04 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
2023-05-19 16:30:41 +05:00
|
|
|
|
var cacheLastData = cacheItem.LastData;
|
|
|
|
|
|
|
|
|
|
if (!cacheLastData.Any() || cacheLastData[0].DateTime > dateBegin)
|
2022-11-08 17:49:04 +05:00
|
|
|
|
return null;
|
2023-10-24 09:23:07 +05:00
|
|
|
|
|
2022-11-08 17:49:04 +05:00
|
|
|
|
var dateEnd = dateBegin.AddSeconds(intervalSec);
|
2023-05-19 16:30:41 +05:00
|
|
|
|
var items = cacheLastData
|
2022-11-08 17:49:04 +05:00
|
|
|
|
.Where(i => i.DateTime >= dateBegin && i.DateTime <= dateEnd);
|
|
|
|
|
|
|
|
|
|
var ratio = items.Count() / approxPointsCount;
|
|
|
|
|
if (ratio > 1)
|
|
|
|
|
items = items
|
|
|
|
|
.Where((_, index) => index % ratio == 0);
|
|
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
2022-11-15 17:44:48 +05:00
|
|
|
|
|
2023-10-23 18:06:57 +05:00
|
|
|
|
public virtual TDto? GetLastOrDefault(int idTelemetry)
|
2023-05-22 10:20:45 +05:00
|
|
|
|
{
|
|
|
|
|
if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
return cacheItem.LastData.LastOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 16:30:41 +05:00
|
|
|
|
public DatesRangeDto? GetOrDefaultDataDateRange(int idTelemetry)
|
|
|
|
|
{
|
|
|
|
|
if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var from = cacheItem.FirstByDate?.DateTime;
|
2023-10-24 09:23:07 +05:00
|
|
|
|
if (!cacheItem.LastData.Any())
|
2023-05-19 16:30:41 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var to = cacheItem.LastData[^1].DateTime;
|
|
|
|
|
from = from ?? cacheItem.LastData[0].DateTime;
|
|
|
|
|
|
|
|
|
|
return new DatesRangeDto { From = from.Value, To = to };
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 18:06:57 +05:00
|
|
|
|
public (TDto First, TDto Last)? GetOrDefaultFirstLast(int idTelemetry)
|
|
|
|
|
{
|
|
|
|
|
if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!cacheItem.LastData.Any())
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var last = cacheItem.LastData[^1];
|
|
|
|
|
var first = cacheItem.FirstByDate;
|
|
|
|
|
return (first, last);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-08 19:45:21 +05:00
|
|
|
|
private async Task InitializeCacheFromDBAsync<TEntity>(IAsbCloudDbContext db, Action<string, double?> onProgress, CancellationToken token)
|
2023-05-19 16:30:41 +05:00
|
|
|
|
where TEntity : class, AsbCloudDb.Model.ITelemetryData
|
2022-11-15 17:44:48 +05:00
|
|
|
|
{
|
2023-10-24 10:55:50 +05:00
|
|
|
|
try
|
2022-11-15 17:44:48 +05:00
|
|
|
|
{
|
2023-10-24 10:55:50 +05:00
|
|
|
|
if (isLoading)
|
|
|
|
|
throw new Exception("Multiple cache loading detected.");
|
2022-11-15 17:44:48 +05:00
|
|
|
|
|
2023-10-24 10:55:50 +05:00
|
|
|
|
isLoading = true;
|
|
|
|
|
|
|
|
|
|
db.Database.SetCommandTimeout(TimeSpan.FromMinutes(5));
|
|
|
|
|
|
|
|
|
|
Well[] wells = await db.Set<Well>()
|
|
|
|
|
.Include(well => well.Telemetry)
|
|
|
|
|
.Include(well => well.Cluster)
|
|
|
|
|
.Where(well => well.IdTelemetry != null)
|
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var count = wells.Length;
|
|
|
|
|
var i = 0d;
|
|
|
|
|
foreach (Well well in wells)
|
|
|
|
|
{
|
|
|
|
|
var capacity = well.IdState == 1
|
|
|
|
|
? activeWellCapacity
|
|
|
|
|
: doneWellCapacity;
|
|
|
|
|
|
|
|
|
|
var idTelemetry = well.IdTelemetry!.Value;
|
|
|
|
|
var hoursOffset = well.Timezone.Hours;
|
2023-10-24 11:29:33 +05:00
|
|
|
|
|
|
|
|
|
onProgress($"Loading for well: {well.Cluster?.Caption}/{well.Caption} (capacity:{capacity}) idTelemetry:{idTelemetry}", i++ / count);
|
2023-10-24 10:55:50 +05:00
|
|
|
|
var cacheItem = await GetOrDefaultCacheDataFromDbAsync<TEntity>(db, idTelemetry, capacity, hoursOffset, token);
|
2023-10-24 11:29:33 +05:00
|
|
|
|
if (cacheItem is not null)
|
2023-10-24 10:55:50 +05:00
|
|
|
|
caches.TryAdd(idTelemetry, cacheItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
isLoading = false;
|
|
|
|
|
var defaultTimeout = db.Database.GetCommandTimeout();
|
|
|
|
|
db.Database.SetCommandTimeout(defaultTimeout);
|
2022-11-15 17:44:48 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 16:30:41 +05:00
|
|
|
|
private static async Task<TelemetryDataCacheItem?> GetOrDefaultCacheDataFromDbAsync<TEntity>(IAsbCloudDbContext db, int idTelemetry, int capacity, double hoursOffset, CancellationToken token)
|
|
|
|
|
where TEntity : class, AsbCloudDb.Model.ITelemetryData
|
2022-11-15 17:44:48 +05:00
|
|
|
|
{
|
2023-05-19 16:30:41 +05:00
|
|
|
|
var query = db.Set<TEntity>()
|
|
|
|
|
.Where(i => i.IdTelemetry == idTelemetry);
|
2022-11-15 17:44:48 +05:00
|
|
|
|
|
2023-05-19 16:30:41 +05:00
|
|
|
|
var firstDbEntity = await query
|
|
|
|
|
.OrderBy(i => i.DateTime)
|
|
|
|
|
.FirstOrDefaultAsync(token);
|
|
|
|
|
|
|
|
|
|
if (firstDbEntity is null)
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
var first = firstDbEntity.Adapt<TDto>();
|
|
|
|
|
first.DateTime = firstDbEntity.DateTime.ToRemoteDateTime(hoursOffset);
|
|
|
|
|
|
|
|
|
|
var entities = await query
|
2022-11-15 17:44:48 +05:00
|
|
|
|
.OrderByDescending(i => i.DateTime)
|
|
|
|
|
.Take(capacity)
|
2023-05-19 16:30:41 +05:00
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
var dtos = entities
|
2022-11-15 17:44:48 +05:00
|
|
|
|
.AsEnumerable()
|
2023-05-19 16:30:41 +05:00
|
|
|
|
.Reverse()
|
2023-10-24 09:23:07 +05:00
|
|
|
|
.Select(entity =>
|
|
|
|
|
{
|
2023-05-19 16:30:41 +05:00
|
|
|
|
var dto = entity.Adapt<TDto>();
|
|
|
|
|
dto.DateTime = entity.DateTime.ToRemoteDateTime(hoursOffset);
|
|
|
|
|
return dto;
|
|
|
|
|
});
|
2022-11-15 17:44:48 +05:00
|
|
|
|
|
2023-05-19 16:30:41 +05:00
|
|
|
|
var cacheItem = new CyclycArray<TDto>(capacity);
|
|
|
|
|
cacheItem.AddRange(dtos);
|
2022-11-15 17:44:48 +05:00
|
|
|
|
|
2023-05-19 16:30:41 +05:00
|
|
|
|
var item = new TelemetryDataCacheItem
|
|
|
|
|
{
|
|
|
|
|
FirstByDate = first,
|
|
|
|
|
LastData = cacheItem,
|
2023-09-12 16:22:01 +05:00
|
|
|
|
TimezoneHours = hoursOffset,
|
2023-05-19 16:30:41 +05:00
|
|
|
|
};
|
|
|
|
|
return item;
|
2022-11-15 17:44:48 +05:00
|
|
|
|
}
|
2023-09-11 13:48:47 +05:00
|
|
|
|
|
|
|
|
|
public IEnumerable<TDto>? GetOrDefault(int idTelemetry, TelemetryDataRequest request)
|
|
|
|
|
{
|
|
|
|
|
if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
|
|
|
|
|
return null;
|
2023-09-12 16:22:01 +05:00
|
|
|
|
|
2023-09-11 13:48:47 +05:00
|
|
|
|
IEnumerable<TDto> data = cacheItem.LastData;
|
|
|
|
|
|
|
|
|
|
if (!data.Any())
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (request.GeDate.HasValue)
|
|
|
|
|
{
|
2023-09-12 16:22:01 +05:00
|
|
|
|
var geDate = request.GeDate.Value.ToRemoteDateTime(cacheItem.TimezoneHours);
|
|
|
|
|
if (data.First().DateTime > geDate)
|
2023-09-11 13:48:47 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
2023-09-12 16:22:01 +05:00
|
|
|
|
data = data.Where(d => d.DateTime >= geDate);
|
2023-09-11 13:48:47 +05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (request.Order == 0)
|
|
|
|
|
return null;
|
2023-09-12 16:22:01 +05:00
|
|
|
|
}
|
2023-09-11 13:48:47 +05:00
|
|
|
|
|
2023-09-12 16:22:01 +05:00
|
|
|
|
if (request.LeDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var leDate = request.LeDate.Value.ToRemoteDateTime(cacheItem.TimezoneHours);
|
2023-09-11 13:48:47 +05:00
|
|
|
|
data = data.Where(d => d.DateTime >= request.LeDate);
|
2023-09-12 16:22:01 +05:00
|
|
|
|
}
|
2023-09-11 13:48:47 +05:00
|
|
|
|
|
|
|
|
|
if (request.Divider > 1)
|
|
|
|
|
data = data.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % request.Divider == 0);
|
|
|
|
|
|
|
|
|
|
switch (request.Order)
|
|
|
|
|
{
|
|
|
|
|
case 1: // Поздние вперед
|
|
|
|
|
data = data
|
|
|
|
|
.OrderByDescending(d => d.DateTime)
|
|
|
|
|
.Skip(request.Skip)
|
|
|
|
|
.Take(request.Take)
|
|
|
|
|
.OrderBy(d => d.DateTime);
|
|
|
|
|
break;
|
|
|
|
|
default: // Ранние вперед
|
|
|
|
|
data = data
|
|
|
|
|
.OrderBy(d => d.DateTime)
|
|
|
|
|
.Skip(request.Skip)
|
|
|
|
|
.Take(request.Take);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
2022-11-03 16:57:41 +05:00
|
|
|
|
}
|
|
|
|
|
}
|