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