DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/TelemetryDataBaseService.cs

194 lines
7.7 KiB
C#
Raw Normal View History

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudDb;
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
{
public abstract class TelemetryDataBaseService<TDto, TModel> : ITelemetryDataService<TDto>
where TDto : AsbCloudApp.Data.ITelemetryData
where TModel : class, AsbCloudDb.Model.ITelemetryData
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
protected readonly CacheTable<Telemetry> cacheTelemetry;
protected readonly CacheTable<TelemetryUser> cacheTelemetryUsers;
protected readonly CacheTable<Well> cacheWells;
public TelemetryDataBaseService(
IAsbCloudDbContext db,
ITelemetryService telemetryService,
CacheDb cacheDb)
{
this.db = db;
this.telemetryService = telemetryService;
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
cacheTelemetryUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
}
public virtual async Task<int> UpdateDataAsync(string uid, IEnumerable<TDto> dtos, CancellationToken token = default)
{
if (dtos == default || !dtos.Any())
return 0;
2022-04-08 13:10:06 +05:00
var dtosList = dtos.OrderBy(d => d.DateTime).ToList();
2022-04-08 13:10:06 +05:00
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++)
2022-04-08 13:10:06 +05:00
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;
});
2022-04-01 17:55:44 +05:00
var entityMaxDate = entities.Max(e => e.DateTime);
telemetryService.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()}, " +
2022-04-01 17:55:44 +05:00
$"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 well = cacheWells.FirstOrDefault(w => w.Id == idWell);
2021-10-27 17:48:19 +05:00
if (well?.IdTelemetry is null)
return default;
2021-11-22 11:30:08 +05:00
var idTelemetry = well?.IdTelemetry ?? default;
var timezone = telemetryService.GetTimezone(idTelemetry);
2021-12-03 17:34:24 +05:00
var filterByDateEnd = dateBegin != default;
DateTimeOffset dateBeginUtc;
2021-10-27 17:48:19 +05:00
if (dateBegin == default)
{
dateBeginUtc = telemetryService.GetLastTelemetryDate(idTelemetry, true);
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>();
2021-11-22 11:30:08 +05:00
var query = dbSet
.Where(d => d.IdTelemetry == idTelemetry
2022-04-01 17:55:44 +05:00
&& d.DateTime >= dateBeginUtc);
2021-12-03 17:34:24 +05:00
if (filterByDateEnd)
2022-04-01 17:55:44 +05:00
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);
switch (m)
{
//case var i when i <= 1: // тут для полноты, но никогда не сработает из-за условия выше
// break;
case var i when i < 10:
2022-04-01 17:55:44 +05:00
query = query.Where((d) => d.DateTime.Second % m == 0);
break;
case var i when i < 30:
2022-04-01 17:55:44 +05:00
query = query.Where((d) => (d.DateTime.Minute * 60 + d.DateTime.Second) % m == 0);
break;
case var i when i < 600:
2022-04-01 17:55:44 +05:00
query = query.Where((d) => ((d.DateTime.Hour * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0);
break;
default:
2022-04-01 17:55:44 +05:00
query = query.Where((d) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0);
break;
}
if (m > 1)
2022-04-01 17:55:44 +05:00
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
2022-04-01 17:55:44 +05:00
.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);
private static double AssumeTimezoneOffset(DateTime nearToCurrentDate)
{
var offset = 5d;
if (nearToCurrentDate.Kind == DateTimeKind.Unspecified)
{
var now = DateTime.UtcNow;
var minutes = 60 * (now.Hour - nearToCurrentDate.Hour) + now.Minute - nearToCurrentDate.Minute;
var minutesPositive = (1440_0000 + minutes) % 1440; //60*24
var halfsHours = Math.Round(1d * minutesPositive / 30d); // quarters are ignored
var hours = halfsHours / 2;
offset = hours < 12 ? hours : 24 - hours;
}
if (nearToCurrentDate.Kind == DateTimeKind.Local)
offset = TimeZoneInfo.Local.BaseUtcOffset.TotalHours;
return offset;
}
}
}