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

148 lines
5.4 KiB
C#
Raw Normal View History

2021-07-21 15:29:19 +05:00
using AsbCloudApp.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Microsoft.EntityFrameworkCore;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using AsbCloudApp.Data;
namespace AsbCloudInfrastructure.Services
{
public class TelemetryTracker : ITelemetryTracker
{
class TrackerStat
{
//public int Id { get; set; }
public string RemoteUid { get; set; }
/// <summary>
/// Время последнего запроса (по времени сервера)
/// </summary>
public DateTimeOffset LastTimeServer { get; set; }
/// <summary>
/// Дата первых данных в БД
/// </summary>
public DateTimeOffset TelemetryDateMin { get; set; }
/// <summary>
/// Дата последних данных в БД
/// </summary>
public DateTimeOffset TelemetryDateMax { get; set; }
}
private readonly ConcurrentDictionary<string, TrackerStat> telemetriesStats;
public TelemetryTracker(CacheDb cacheDb, IConfiguration configuration)
{
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
.Options;
var db = new AsbCloudDbContext(options);
2021-10-27 17:48:19 +05:00
var cacheTelemetry = cacheDb.GetCachedTable<Telemetry>(db);
var keyValuePairs = new Dictionary<string, TrackerStat>(cacheTelemetry.Count());
foreach (var telemetry in cacheTelemetry)
{
2021-11-22 11:30:08 +05:00
var date = telemetry.Info?.DrillingStartDate
?? ParseDateFromUidOrDefault(telemetry.RemoteUid, DateTime.MinValue);
2021-11-22 11:30:08 +05:00
keyValuePairs[telemetry.RemoteUid] = new TrackerStat
{
RemoteUid = telemetry.RemoteUid,
TelemetryDateMin = date,
TelemetryDateMax = date,
LastTimeServer = date,
};
}
telemetriesStats = new ConcurrentDictionary<string, TrackerStat>(keyValuePairs);
2021-11-22 11:30:08 +05:00
Task.Run(async () =>
{
db.Database.SetCommandTimeout(2 * 60);
var dates = await db.TelemetryDataSaub
.GroupBy(d => d.IdTelemetry)
.Select(g => new
{
IdTelemetry = g.Key,
2021-11-22 11:30:08 +05:00
DateMax = g.Max(d => d.Date),
DateMin = g.Min(d => d.Date),
})
.AsNoTracking()
.ToListAsync()
.ConfigureAwait(false);
var oldRequests = dates.Select(t => new
{
Uid = cacheTelemetry.FirstOrDefault(c => c.Id == t.IdTelemetry)?.RemoteUid,
2021-11-22 11:30:08 +05:00
t.DateMax,
t.DateMin,
}).Where(s => !string.IsNullOrEmpty(s.Uid));
foreach (var oldReq in oldRequests)
{
var telemetryStat = telemetriesStats.GetOrAdd(oldReq.Uid, (uid) => new TrackerStat { RemoteUid = uid });
telemetryStat.TelemetryDateMin = oldReq.DateMin;
telemetryStat.TelemetryDateMax = oldReq.DateMax;
telemetryStat.LastTimeServer = oldReq.DateMax;
}
}).ContinueWith((t) =>
{
db.Dispose();
return t;
});
}
private static DateTimeOffset ParseDateFromUidOrDefault(string remoteUid, DateTime defaultValue = default)
{
//example: uid = 20211102_173407926
if (string.IsNullOrEmpty(remoteUid) || (remoteUid.Length != 18))
return defaultValue;
if (DateTime.TryParseExact(remoteUid, "yyyyMMdd_HHmmssfff",
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.AssumeUniversal,
out DateTime parsedDate))
return parsedDate;
return defaultValue;
}
public void SaveRequestDate(string uid, DateTimeOffset remoteDate)
{
var stat = telemetriesStats.GetOrAdd(uid, _ => new TrackerStat {
RemoteUid = uid,
TelemetryDateMin = remoteDate}
);
2021-12-22 12:34:59 +05:00
stat.LastTimeServer = DateTime.Now;
2021-12-22 12:34:59 +05:00
2021-12-24 11:42:23 +05:00
if(stat.TelemetryDateMax.ToUniversalTime() < remoteDate.ToUniversalTime())
2021-12-22 12:34:59 +05:00
stat.TelemetryDateMax = remoteDate;
}
public DateTimeOffset GetLastTelemetryDateByUid(string uid) =>
telemetriesStats.GetValueOrDefault(uid)?.TelemetryDateMax ?? default;
public DatesRangeDto GetTelemetryDateRangeByUid(string uid)
{
var stat = telemetriesStats.GetValueOrDefault(uid);
var range = new DatesRangeDto
{
From = stat?.TelemetryDateMin.UtcDateTime ?? default,
To = stat?.TelemetryDateMax.UtcDateTime ?? default,
};
return range;
}
public IEnumerable<string> GetTransmittingTelemetriesUids() =>
telemetriesStats.Keys;
}
}