forked from ddrilling/AsbCloudServer
e185d8b82a
Добавить nullable. Заменить зависимость от CacheTable/CacheDb. Удалить не используемые методы.
153 lines
5.5 KiB
C#
153 lines
5.5 KiB
C#
using AsbCloudApp.Services;
|
|
using AsbCloudDb;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Services.SAUB
|
|
{
|
|
public abstract class TelemetryDataBaseService<TDto, TModel> : ITelemetryDataService<TDto>
|
|
where TDto : AsbCloudApp.Data.ITelemetryData
|
|
where TModel : class, ITelemetryData
|
|
{
|
|
protected readonly IAsbCloudDbContext db;
|
|
private readonly ITelemetryService telemetryService;
|
|
protected readonly CacheTable<TelemetryUser> cacheTelemetryUsers;
|
|
|
|
public TelemetryDataBaseService(
|
|
IAsbCloudDbContext db,
|
|
ITelemetryService telemetryService,
|
|
CacheDb cacheDb)
|
|
{
|
|
this.db = db;
|
|
this.telemetryService = telemetryService;
|
|
cacheTelemetryUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
|
|
}
|
|
|
|
public virtual async Task<int> UpdateDataAsync(string uid, IEnumerable<TDto> dtos, CancellationToken token = default)
|
|
{
|
|
if (dtos == default || !dtos.Any())
|
|
return 0;
|
|
|
|
var dtosList = dtos.OrderBy(d => d.DateTime).ToList();
|
|
|
|
var dtoMinDate = dtosList.First().DateTime;
|
|
var dtoMaxDate = dtosList.Last().DateTime;
|
|
|
|
if (dtosList.Count > 1)
|
|
{
|
|
var duplicates = new List<TDto>(8);
|
|
for (int i = 1; i < dtosList.Count; i++)
|
|
if (dtosList[i].DateTime - dtosList[i - 1].DateTime < TimeSpan.FromMilliseconds(100))
|
|
duplicates.Add(dtosList[i - 1]);
|
|
foreach (var duplicate in duplicates)
|
|
dtosList.Remove(duplicate);
|
|
}
|
|
|
|
var idTelemetry = telemetryService.GetOrCreateTelemetryIdByUid(uid);
|
|
var timezone = telemetryService.GetTimezone(idTelemetry);
|
|
|
|
var entities = dtosList.Select(dto =>
|
|
{
|
|
var entity = Convert(dto, timezone.Hours);
|
|
entity.IdTelemetry = idTelemetry;
|
|
return entity;
|
|
});
|
|
|
|
var entityMaxDate = entities.Max(e => e.DateTime);
|
|
telemetryService.TelemetryTracker.SaveRequestDate(uid, entityMaxDate);
|
|
|
|
var dbset = db.Set<TModel>();
|
|
var stopwatch = Stopwatch.StartNew();
|
|
try
|
|
{
|
|
return await db.Database.ExecInsertOrUpdateAsync(dbset, entities, token).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
stopwatch.Stop();
|
|
Trace.WriteLine($"Fail to save data telemetry " +
|
|
$"uid: {uid}, " +
|
|
$"idTelemetry {idTelemetry}, " +
|
|
$"count: {entities.Count()}, " +
|
|
$"dataDate: {entities.FirstOrDefault()?.DateTime}, " +
|
|
$"dbSaveDurationTime:{stopwatch.ElapsedMilliseconds}ms. " +
|
|
$"Message: {ex.Message}");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public virtual async Task<IEnumerable<TDto>> GetAsync(int idWell,
|
|
DateTime dateBegin = default, double intervalSec = 600d,
|
|
int approxPointsCount = 1024, CancellationToken token = default)
|
|
{
|
|
var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell) ?? -1;
|
|
if (idTelemetry == -1)
|
|
return null;
|
|
|
|
var timezone = telemetryService.GetTimezone(idTelemetry);
|
|
|
|
var filterByDateEnd = dateBegin != default;
|
|
DateTimeOffset dateBeginUtc;
|
|
if (dateBegin == default)
|
|
{
|
|
dateBeginUtc = telemetryService.GetLastTelemetryDate(idTelemetry)
|
|
.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);
|
|
var dbSet = db.Set<TModel>();
|
|
|
|
var query = dbSet
|
|
.Where(d => d.IdTelemetry == idTelemetry
|
|
&& d.DateTime >= dateBeginUtc);
|
|
|
|
if (filterByDateEnd)
|
|
query = query.Where(d => d.DateTime <= dateEnd);
|
|
|
|
var fullDataCount = await query.CountAsync(token)
|
|
.ConfigureAwait(false);
|
|
|
|
if (fullDataCount == 0)
|
|
return default;
|
|
|
|
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 dtos = entities.Select(e => Convert(e, timezone.Hours));
|
|
|
|
return dtos;
|
|
}
|
|
|
|
public abstract TDto Convert(TModel src, double timezoneOffset);
|
|
|
|
public abstract TModel Convert(TDto src, double timezoneOffset);
|
|
|
|
}
|
|
}
|