2022-04-11 18:00:34 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
2021-10-25 16:32:55 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-11-18 15:07:27 +05:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using System;
|
2021-11-10 16:59:48 +05:00
|
|
|
|
using System.Collections.Concurrent;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-11-10 16:59:48 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-05-12 16:03:14 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.SAUB
|
2021-05-12 16:03:14 +05:00
|
|
|
|
{
|
|
|
|
|
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>
|
2021-12-30 17:05:44 +05:00
|
|
|
|
public DateTimeOffset LastTimeServer { get; set; }
|
2021-10-28 10:56:18 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-11-22 16:02:15 +05:00
|
|
|
|
/// Дата первых данных в БД
|
2021-10-28 10:56:18 +05:00
|
|
|
|
/// </summary>
|
2022-01-05 17:50:45 +05:00
|
|
|
|
public DateTimeOffset TelemetryDateUtcMin { get; set; }
|
2021-11-22 16:02:15 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Дата последних данных в БД
|
|
|
|
|
/// </summary>
|
2022-01-05 17:50:45 +05:00
|
|
|
|
public DateTimeOffset TelemetryDateUtcMax { get; set; }
|
2021-11-22 16:02:15 +05:00
|
|
|
|
|
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
|
|
|
|
|
2022-11-18 15:07:27 +05:00
|
|
|
|
public TelemetryTracker(IConfiguration configuration, IMemoryCache memoryCache)
|
2021-10-25 16:32:55 +05:00
|
|
|
|
{
|
2022-12-02 17:18:16 +05:00
|
|
|
|
// TODO: make this background work
|
2022-02-17 15:37:27 +05:00
|
|
|
|
var contextOptions = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
2021-11-17 10:50:57 +05:00
|
|
|
|
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
2021-10-25 16:32:55 +05:00
|
|
|
|
.Options;
|
2022-02-17 15:37:27 +05:00
|
|
|
|
var db = new AsbCloudDbContext(contextOptions);
|
2021-10-27 17:48:19 +05:00
|
|
|
|
|
2022-11-18 15:07:27 +05:00
|
|
|
|
var cacheTelemetry = memoryCache.GetOrCreateBasic<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
|
2022-04-15 13:36:09 +05:00
|
|
|
|
?? ParseDateFromUidOrDefault(telemetry.RemoteUid, DateTimeOffset.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,
|
2022-01-05 17:50:45 +05:00
|
|
|
|
TelemetryDateUtcMin = date,
|
|
|
|
|
TelemetryDateUtcMax = 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-23 16:12:41 +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,
|
2022-04-01 17:55:44 +05:00
|
|
|
|
DateMax = g.Max(d => d.DateTime),
|
|
|
|
|
DateMin = g.Min(d => d.DateTime),
|
2021-11-10 16:59:48 +05:00
|
|
|
|
})
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync()
|
|
|
|
|
.ConfigureAwait(false);
|
2021-11-23 16:12:41 +05:00
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
var oldRequests = dates.Select(t => new
|
2021-11-10 16:59:48 +05:00
|
|
|
|
{
|
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
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
foreach (var oldReq in oldRequests)
|
2021-11-10 16:59:48 +05:00
|
|
|
|
{
|
2021-11-22 16:02:15 +05:00
|
|
|
|
var telemetryStat = telemetriesStats.GetOrAdd(oldReq.Uid, (uid) => new TrackerStat { RemoteUid = uid });
|
2022-01-05 17:50:45 +05:00
|
|
|
|
telemetryStat.TelemetryDateUtcMin = oldReq.DateMin;
|
|
|
|
|
telemetryStat.TelemetryDateUtcMax = oldReq.DateMax;
|
2021-12-30 17:05:44 +05:00
|
|
|
|
telemetryStat.LastTimeServer = oldReq.DateMax;
|
2021-11-10 16:59:48 +05:00
|
|
|
|
}
|
2021-11-23 16:12:41 +05:00
|
|
|
|
}).ContinueWith((t) =>
|
|
|
|
|
{
|
|
|
|
|
db.Dispose();
|
|
|
|
|
return t;
|
2021-10-28 10:56:18 +05:00
|
|
|
|
});
|
2021-10-25 16:32:55 +05:00
|
|
|
|
}
|
2021-05-12 17:25:52 +05:00
|
|
|
|
|
2022-04-15 13:36:09 +05:00
|
|
|
|
private static DateTimeOffset ParseDateFromUidOrDefault(string remoteUid, DateTimeOffset defaultValue = default)
|
2021-11-10 16:59:48 +05:00
|
|
|
|
{
|
2021-12-30 17:05:44 +05:00
|
|
|
|
//example: uid = 20211102_173407926
|
2022-04-11 18:00:34 +05:00
|
|
|
|
if (string.IsNullOrEmpty(remoteUid) || remoteUid.Length != 18)
|
2021-11-10 16:59:48 +05:00
|
|
|
|
return defaultValue;
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParseExact(remoteUid, "yyyyMMdd_HHmmssfff",
|
|
|
|
|
System.Globalization.CultureInfo.InvariantCulture,
|
|
|
|
|
System.Globalization.DateTimeStyles.AssumeUniversal,
|
|
|
|
|
out DateTime parsedDate))
|
|
|
|
|
return parsedDate;
|
|
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-30 17:05:44 +05:00
|
|
|
|
public void SaveRequestDate(string uid, DateTimeOffset remoteDate)
|
2021-10-28 10:56:18 +05:00
|
|
|
|
{
|
2022-04-11 18:00:34 +05:00
|
|
|
|
var stat = telemetriesStats.GetOrAdd(uid, _ => new TrackerStat
|
|
|
|
|
{
|
|
|
|
|
RemoteUid = uid,
|
|
|
|
|
TelemetryDateUtcMin = remoteDate
|
|
|
|
|
}
|
2021-11-22 16:02:15 +05:00
|
|
|
|
);
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-10-28 10:56:18 +05:00
|
|
|
|
stat.LastTimeServer = DateTime.Now;
|
2021-12-22 12:34:59 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
if (stat.TelemetryDateUtcMax.ToUniversalTime() < remoteDate.ToUniversalTime())
|
2022-01-05 17:50:45 +05:00
|
|
|
|
stat.TelemetryDateUtcMax = remoteDate;
|
2021-10-28 10:56:18 +05:00
|
|
|
|
}
|
2021-05-12 16:03:14 +05:00
|
|
|
|
|
2021-12-30 17:05:44 +05:00
|
|
|
|
public DateTimeOffset GetLastTelemetryDateByUid(string uid) =>
|
2022-01-05 17:50:45 +05:00
|
|
|
|
telemetriesStats.GetValueOrDefault(uid)?.TelemetryDateUtcMax ?? default;
|
2021-11-22 16:02:15 +05:00
|
|
|
|
|
|
|
|
|
public DatesRangeDto GetTelemetryDateRangeByUid(string uid)
|
|
|
|
|
{
|
|
|
|
|
var stat = telemetriesStats.GetValueOrDefault(uid);
|
|
|
|
|
var range = new DatesRangeDto
|
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
From = stat?.TelemetryDateUtcMin.UtcDateTime ?? default,
|
|
|
|
|
To = stat?.TelemetryDateUtcMax.UtcDateTime ?? default,
|
2021-11-22 16:02:15 +05:00
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|