forked from ddrilling/AsbCloudServer
Improve + fix TelemetryTracker. Now it keeps date range of telemetry data.
This commit is contained in:
parent
0b66648267
commit
49eaa76bb0
@ -9,6 +9,7 @@ namespace AsbCloudApp.Services
|
||||
public interface ITelemetryService
|
||||
{
|
||||
ITimeZoneService TimeZoneService { get; }
|
||||
ITelemetryTracker TelemetryTracker { get; }
|
||||
|
||||
int? GetidWellByTelemetryUid(string uid);
|
||||
int GetOrCreateTemetryIdByUid(string uid);
|
||||
|
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using AsbCloudApp.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace AsbCloudApp.Services
|
||||
@ -6,6 +7,7 @@ namespace AsbCloudApp.Services
|
||||
public interface ITelemetryTracker
|
||||
{
|
||||
DateTime GetLastTelemetryDateByUid(string uid);
|
||||
DatesRangeDto GetTelemetryDateRangeByUid(string uid);
|
||||
IEnumerable<string> GetTransmittingTelemetriesUids();
|
||||
void SaveRequestDate(string uid, DateTime remoteDate);
|
||||
}
|
||||
|
@ -159,18 +159,18 @@ namespace AsbCloudInfrastructure.Services
|
||||
if (telemetryId is null)
|
||||
return null;
|
||||
|
||||
var (From, To) = await db.GetDatesRangeAsync<TModel>((int)telemetryId, token)
|
||||
var telemetry = await cacheTelemetry.FirstOrDefaultAsync(t => t.Id == telemetryId, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
var result = new DatesRangeDto {From = From, To = To};
|
||||
var dto = telemetryService.TelemetryTracker.GetTelemetryDateRangeByUid(telemetry.RemoteUid);
|
||||
|
||||
if (isUtc)
|
||||
return result;
|
||||
return dto;
|
||||
|
||||
result = await telemetryService.FixDatesRangeByTimeZoneAsync((int)telemetryId, result, token)
|
||||
dto = await telemetryService.FixDatesRangeByTimeZoneAsync((int)telemetryId, dto, token)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return result;
|
||||
return dto;
|
||||
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ namespace AsbCloudInfrastructure.Services
|
||||
private readonly ITimeZoneService timeZoneService;
|
||||
|
||||
public ITimeZoneService TimeZoneService => timeZoneService;
|
||||
public ITelemetryTracker TelemetryTracker => telemetryTracker;
|
||||
|
||||
public TelemetryService(
|
||||
IAsbCloudDbContext db,
|
||||
|
@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using AsbCloudApp.Data;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
@ -25,39 +26,46 @@ namespace AsbCloudInfrastructure.Services
|
||||
public DateTime LastTimeServer { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Время указанное в данных последнего запроса (из удаленного источника)
|
||||
/// Дата первых данных в БД
|
||||
/// </summary>
|
||||
public DateTime LastTimeRemote { get; set; }
|
||||
public DateTime TelemetryDateMin { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Дата последних данных в БД
|
||||
/// </summary>
|
||||
public DateTime TelemetryDateMax { get; set; }
|
||||
|
||||
}
|
||||
|
||||
private readonly ConcurrentDictionary<string, TrackerStat> requests;
|
||||
private readonly ConcurrentDictionary<string, TrackerStat> telemetriesStats;
|
||||
|
||||
public TelemetryTracker(CacheDb cacheDb, IConfiguration configuration)
|
||||
{
|
||||
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
||||
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
||||
.Options;
|
||||
using var db = new AsbCloudDbContext(options);
|
||||
var db = new AsbCloudDbContext(options);
|
||||
|
||||
var cacheTelemetry = cacheDb.GetCachedTable<Telemetry>(db);
|
||||
var keyValuePairs = new Dictionary<string, TrackerStat>(cacheTelemetry.Count());
|
||||
foreach (var telemetry in cacheTelemetry)
|
||||
{
|
||||
var date = telemetry.Info?.DrillingStartDate
|
||||
?? GetDateByUidOrDefault(telemetry.RemoteUid, DateTime.MinValue);
|
||||
?? ParseDateFromUidOrDefault(telemetry.RemoteUid, DateTime.MinValue);
|
||||
|
||||
keyValuePairs[telemetry.RemoteUid] = new TrackerStat
|
||||
{
|
||||
RemoteUid = telemetry.RemoteUid,
|
||||
LastTimeRemote = date,
|
||||
TelemetryDateMin = date,
|
||||
TelemetryDateMax = date,
|
||||
LastTimeServer = date,
|
||||
};
|
||||
}
|
||||
requests = new ConcurrentDictionary<string, TrackerStat>(keyValuePairs);
|
||||
telemetriesStats = new ConcurrentDictionary<string, TrackerStat>(keyValuePairs);
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
db.Database.SetCommandTimeout(2 * 60);
|
||||
db.Database.SetCommandTimeout(2 * 60);
|
||||
var dates = await db.TelemetryDataSaub
|
||||
.GroupBy(d => d.IdTelemetry)
|
||||
.Select(g => new
|
||||
@ -69,29 +77,27 @@ namespace AsbCloudInfrastructure.Services
|
||||
.AsNoTracking()
|
||||
.ToListAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
db.Dispose();
|
||||
|
||||
|
||||
var oldReqs = dates.Select(t => new
|
||||
{
|
||||
Uid = cacheTelemetry.FirstOrDefault(c => c.Id == t.IdTelemetry).RemoteUid,
|
||||
Uid = cacheTelemetry.FirstOrDefault(c => c.Id == t.IdTelemetry)?.RemoteUid,
|
||||
t.DateMax,
|
||||
});
|
||||
t.DateMin,
|
||||
}).Where(s => !string.IsNullOrEmpty(s.Uid));
|
||||
|
||||
foreach (var oldReq in oldReqs)
|
||||
{
|
||||
var request = requests.GetValueOrDefault(oldReq.Uid);
|
||||
if (request is not null)
|
||||
{
|
||||
if (request.LastTimeRemote < oldReq.DateMax)
|
||||
request.LastTimeRemote = oldReq.DateMax;
|
||||
if (request.LastTimeServer < oldReq.DateMax)
|
||||
request.LastTimeServer = oldReq.DateMax;
|
||||
}
|
||||
var telemetryStat = telemetriesStats.GetOrAdd(oldReq.Uid, (uid) => new TrackerStat { RemoteUid = uid });
|
||||
telemetryStat.TelemetryDateMin = oldReq.DateMin;
|
||||
telemetryStat.TelemetryDateMax = oldReq.DateMax;
|
||||
telemetryStat.LastTimeServer = oldReq.DateMax;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static DateTime GetDateByUidOrDefault(string remoteUid, DateTime defaultValue = default)
|
||||
private static DateTime ParseDateFromUidOrDefault(string remoteUid, DateTime defaultValue = default)
|
||||
{
|
||||
//eg: uid = 20211102_173407926
|
||||
if (string.IsNullOrEmpty(remoteUid) || (remoteUid.Length != 18))
|
||||
@ -108,20 +114,30 @@ namespace AsbCloudInfrastructure.Services
|
||||
|
||||
public void SaveRequestDate(string uid, DateTime remoteDate)
|
||||
{
|
||||
var stat = requests.GetValueOrDefault(uid);
|
||||
if(stat is null)
|
||||
{
|
||||
stat = new TrackerStat{ RemoteUid = uid };
|
||||
requests[uid] = stat;
|
||||
}
|
||||
var stat = telemetriesStats.GetOrAdd(uid, (uid) => new TrackerStat {
|
||||
RemoteUid = uid,
|
||||
TelemetryDateMin = remoteDate}
|
||||
);
|
||||
stat.LastTimeServer = DateTime.Now;
|
||||
stat.LastTimeRemote = remoteDate;
|
||||
stat.TelemetryDateMax = remoteDate;
|
||||
}
|
||||
|
||||
public DateTime GetLastTelemetryDateByUid(string uid) =>
|
||||
requests.GetValueOrDefault(uid)?.LastTimeRemote ?? default;
|
||||
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;
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetTransmittingTelemetriesUids() =>
|
||||
requests.Keys;
|
||||
telemetriesStats.Keys;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user