2021-07-21 15:29:19 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using System;
|
2021-05-12 16:03:14 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-10-25 16:32:55 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-11-10 16:59:48 +05:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-11-17 10:50:57 +05:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2021-11-22 16:02:15 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-05-12 16:03:14 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public class TelemetryTracker : ITelemetryTracker
|
|
|
|
|
{
|
2021-10-28 10:56:18 +05:00
|
|
|
|
class TrackerStat
|
|
|
|
|
{
|
|
|
|
|
//public int Id { get; set; }
|
|
|
|
|
|
|
|
|
|
public string RemoteUid { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Время последнего запроса (по времени сервера)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DateTime LastTimeServer { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-11-22 16:02:15 +05:00
|
|
|
|
/// Дата первых данных в БД
|
2021-10-28 10:56:18 +05:00
|
|
|
|
/// </summary>
|
2021-11-22 16:02:15 +05:00
|
|
|
|
public DateTime TelemetryDateMin { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Дата последних данных в БД
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DateTime TelemetryDateMax { get; set; }
|
|
|
|
|
|
2021-10-28 10:56:18 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-22 16:02:15 +05:00
|
|
|
|
private readonly ConcurrentDictionary<string, TrackerStat> telemetriesStats;
|
2021-10-25 16:32:55 +05:00
|
|
|
|
|
2021-11-17 10:50:57 +05:00
|
|
|
|
public TelemetryTracker(CacheDb cacheDb, IConfiguration configuration)
|
2021-10-25 16:32:55 +05:00
|
|
|
|
{
|
|
|
|
|
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
2021-11-17 10:50:57 +05:00
|
|
|
|
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
2021-10-25 16:32:55 +05:00
|
|
|
|
.Options;
|
2021-11-22 16:02:15 +05:00
|
|
|
|
var db = new AsbCloudDbContext(options);
|
2021-10-27 17:48:19 +05:00
|
|
|
|
|
2021-10-25 16:32:55 +05:00
|
|
|
|
var cacheTelemetry = cacheDb.GetCachedTable<Telemetry>(db);
|
2021-11-10 16:59:48 +05:00
|
|
|
|
var keyValuePairs = new Dictionary<string, TrackerStat>(cacheTelemetry.Count());
|
|
|
|
|
foreach (var telemetry in cacheTelemetry)
|
2021-10-25 16:32:55 +05:00
|
|
|
|
{
|
2021-11-22 11:30:08 +05:00
|
|
|
|
var date = telemetry.Info?.DrillingStartDate
|
2021-11-22 16:02:15 +05:00
|
|
|
|
?? ParseDateFromUidOrDefault(telemetry.RemoteUid, DateTime.MinValue);
|
2021-11-10 16:59:48 +05:00
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
keyValuePairs[telemetry.RemoteUid] = new TrackerStat
|
|
|
|
|
{
|
2021-11-10 16:59:48 +05:00
|
|
|
|
RemoteUid = telemetry.RemoteUid,
|
2021-11-22 16:02:15 +05:00
|
|
|
|
TelemetryDateMin = date,
|
|
|
|
|
TelemetryDateMax = date,
|
2021-11-10 16:59:48 +05:00
|
|
|
|
LastTimeServer = date,
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-11-22 16:02:15 +05:00
|
|
|
|
telemetriesStats = new ConcurrentDictionary<string, TrackerStat>(keyValuePairs);
|
2021-11-10 16:59:48 +05:00
|
|
|
|
|
2021-11-22 11:30:08 +05:00
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
2021-11-22 16:02:15 +05:00
|
|
|
|
db.Database.SetCommandTimeout(2 * 60);
|
2021-11-10 16:59:48 +05:00
|
|
|
|
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),
|
2021-11-10 16:59:48 +05:00
|
|
|
|
})
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync()
|
|
|
|
|
.ConfigureAwait(false);
|
2021-11-22 16:02:15 +05:00
|
|
|
|
|
2021-11-10 16:59:48 +05:00
|
|
|
|
db.Dispose();
|
2021-11-22 16:02:15 +05:00
|
|
|
|
|
2021-11-10 16:59:48 +05:00
|
|
|
|
var oldReqs = dates.Select(t => new
|
|
|
|
|
{
|
2021-11-22 16:02:15 +05:00
|
|
|
|
Uid = cacheTelemetry.FirstOrDefault(c => c.Id == t.IdTelemetry)?.RemoteUid,
|
2021-11-22 11:30:08 +05:00
|
|
|
|
t.DateMax,
|
2021-11-22 16:02:15 +05:00
|
|
|
|
t.DateMin,
|
|
|
|
|
}).Where(s => !string.IsNullOrEmpty(s.Uid));
|
2021-11-10 16:59:48 +05:00
|
|
|
|
|
|
|
|
|
foreach (var oldReq in oldReqs)
|
|
|
|
|
{
|
2021-11-22 16:02:15 +05:00
|
|
|
|
var telemetryStat = telemetriesStats.GetOrAdd(oldReq.Uid, (uid) => new TrackerStat { RemoteUid = uid });
|
|
|
|
|
telemetryStat.TelemetryDateMin = oldReq.DateMin;
|
|
|
|
|
telemetryStat.TelemetryDateMax = oldReq.DateMax;
|
|
|
|
|
telemetryStat.LastTimeServer = oldReq.DateMax;
|
2021-11-10 16:59:48 +05:00
|
|
|
|
}
|
2021-10-28 10:56:18 +05:00
|
|
|
|
});
|
2021-10-25 16:32:55 +05:00
|
|
|
|
}
|
2021-05-12 17:25:52 +05:00
|
|
|
|
|
2021-11-22 16:02:15 +05:00
|
|
|
|
private static DateTime ParseDateFromUidOrDefault(string remoteUid, DateTime defaultValue = default)
|
2021-11-10 16:59:48 +05:00
|
|
|
|
{
|
|
|
|
|
//eg: 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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 10:56:18 +05:00
|
|
|
|
public void SaveRequestDate(string uid, DateTime remoteDate)
|
|
|
|
|
{
|
2021-11-22 16:02:15 +05:00
|
|
|
|
var stat = telemetriesStats.GetOrAdd(uid, (uid) => new TrackerStat {
|
|
|
|
|
RemoteUid = uid,
|
|
|
|
|
TelemetryDateMin = remoteDate}
|
|
|
|
|
);
|
2021-10-28 10:56:18 +05:00
|
|
|
|
stat.LastTimeServer = DateTime.Now;
|
2021-11-22 16:02:15 +05:00
|
|
|
|
stat.TelemetryDateMax = remoteDate;
|
2021-10-28 10:56:18 +05:00
|
|
|
|
}
|
2021-05-12 16:03:14 +05:00
|
|
|
|
|
2021-10-15 12:24:04 +05:00
|
|
|
|
public DateTime GetLastTelemetryDateByUid(string uid) =>
|
2021-11-22 16:02:15 +05:00
|
|
|
|
telemetriesStats.GetValueOrDefault(uid)?.TelemetryDateMax ?? default;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DatesRangeDto GetTelemetryDateRangeByUid(string uid)
|
|
|
|
|
{
|
|
|
|
|
var stat = telemetriesStats.GetValueOrDefault(uid);
|
|
|
|
|
var range = new DatesRangeDto
|
|
|
|
|
{
|
|
|
|
|
From = stat?.TelemetryDateMin ?? default,
|
|
|
|
|
To = stat?.TelemetryDateMax ?? default,
|
|
|
|
|
};
|
|
|
|
|
return range;
|
|
|
|
|
}
|
2021-10-15 12:24:04 +05:00
|
|
|
|
|
2021-10-28 10:56:18 +05:00
|
|
|
|
public IEnumerable<string> GetTransmittingTelemetriesUids() =>
|
2021-11-22 16:02:15 +05:00
|
|
|
|
telemetriesStats.Keys;
|
2021-05-12 16:03:14 +05:00
|
|
|
|
}
|
|
|
|
|
}
|