forked from ddrilling/AsbCloudServer
remove TelemetryTracker
This commit is contained in:
parent
9ad290550c
commit
ed156b1ce8
@ -16,11 +16,6 @@ namespace AsbCloudApp.Services
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
ITimezoneService TimeZoneService { get; }
|
ITimezoneService TimeZoneService { get; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// трекер запросов
|
|
||||||
/// </summary>
|
|
||||||
ITelemetryTracker TelemetryTracker { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// получить idWell по uid телеметрии
|
/// получить idWell по uid телеметрии
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -42,13 +37,6 @@ namespace AsbCloudApp.Services
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
SimpleTimezoneDto GetTimezone(int idTelemetry);
|
SimpleTimezoneDto GetTimezone(int idTelemetry);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Получить дату получения последних данных
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="idTelemetry"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
DateTime GetLastTelemetryDate(int idTelemetry);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// получить idTelemetry по IdWell
|
/// получить idTelemetry по IdWell
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
using AsbCloudApp.Data;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace AsbCloudApp.Services
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Сервис статистики телеметрии
|
|
||||||
/// </summary>
|
|
||||||
public interface ITelemetryTracker
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// получить дату последней отправки данных панелью
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="uid"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
DateTimeOffset GetLastTelemetryDateByUid(string uid);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// получить диапазон дат за которые есть данные по телеметрии
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="uid"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
DatesRangeDto GetTelemetryDateRangeByUid(string uid);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// обновить статистику по телеметрии
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="uid"></param>
|
|
||||||
/// <param name="remoteDate"></param>
|
|
||||||
void SaveRequestDate(string uid, DateTimeOffset remoteDate);
|
|
||||||
}
|
|
||||||
}
|
|
@ -63,7 +63,7 @@ namespace AsbCloudApp.Services
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idWell"></param>
|
/// <param name="idWell"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
DateTimeOffset GetLastTelemetryDate(int idWell);
|
DateTime GetLastTelemetryDate(int idWell);
|
||||||
|
|
||||||
//TODO: выяснить и удалить отсюда
|
//TODO: выяснить и удалить отсюда
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -102,7 +102,6 @@ namespace AsbCloudInfrastructure
|
|||||||
services.AddSingleton(new WitsInfoService());
|
services.AddSingleton(new WitsInfoService());
|
||||||
services.AddSingleton(provider => TelemetryDataCache<TelemetryDataSaubDto>.GetInstance<TelemetryDataSaub>(provider));
|
services.AddSingleton(provider => TelemetryDataCache<TelemetryDataSaubDto>.GetInstance<TelemetryDataSaub>(provider));
|
||||||
services.AddSingleton(provider => TelemetryDataCache<TelemetryDataSpinDto>.GetInstance<TelemetryDataSpin>(provider));
|
services.AddSingleton(provider => TelemetryDataCache<TelemetryDataSpinDto>.GetInstance<TelemetryDataSpin>(provider));
|
||||||
services.AddSingleton<ITelemetryTracker, TelemetryTracker>();
|
|
||||||
services.AddSingleton<IRequerstTrackerService, RequestTrackerService>();
|
services.AddSingleton<IRequerstTrackerService, RequestTrackerService>();
|
||||||
services.AddSingleton<BackgroundWorker>();
|
services.AddSingleton<BackgroundWorker>();
|
||||||
services.AddSingleton<IReduceSamplingService>(provider => ReduceSamplingService.GetInstance(configuration));
|
services.AddSingleton<IReduceSamplingService>(provider => ReduceSamplingService.GetInstance(configuration));
|
||||||
|
@ -111,7 +111,7 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
{
|
{
|
||||||
var dto = well.Adapt<WellDto>();
|
var dto = well.Adapt<WellDto>();
|
||||||
dto.WellType = well.WellType.Caption;
|
dto.WellType = well.WellType.Caption;
|
||||||
dto.LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id).DateTime;
|
dto.LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id);
|
||||||
dto.Cluster = gCluster.Key.Caption;
|
dto.Cluster = gCluster.Key.Caption;
|
||||||
dto.Deposit = gDeposit.Key.Caption;
|
dto.Deposit = gDeposit.Key.Caption;
|
||||||
return dto;
|
return dto;
|
||||||
|
@ -64,7 +64,6 @@ namespace AsbCloudInfrastructure.Services.SAUB
|
|||||||
});
|
});
|
||||||
|
|
||||||
var entityMaxDate = entities.Max(e => e.DateTime);
|
var entityMaxDate = entities.Max(e => e.DateTime);
|
||||||
telemetryService.TelemetryTracker.SaveRequestDate(uid, entityMaxDate);
|
|
||||||
|
|
||||||
var dbset = db.Set<TEntity>();
|
var dbset = db.Set<TEntity>();
|
||||||
var stopwatch = Stopwatch.StartNew();
|
var stopwatch = Stopwatch.StartNew();
|
||||||
|
@ -19,21 +19,20 @@ namespace AsbCloudInfrastructure.Services.SAUB
|
|||||||
{
|
{
|
||||||
private readonly IAsbCloudDbContext db;
|
private readonly IAsbCloudDbContext db;
|
||||||
private readonly IMemoryCache memoryCache;
|
private readonly IMemoryCache memoryCache;
|
||||||
private readonly ITelemetryTracker telemetryTracker;
|
private readonly TelemetryDataCache<TelemetryDataSaubDto> dataSaubCache;
|
||||||
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,
|
||||||
IMemoryCache memoryCache,
|
IMemoryCache memoryCache,
|
||||||
ITelemetryTracker telemetryTracker,
|
TelemetryDataCache<TelemetryDataSaubDto> dataSaubCache,
|
||||||
ITimezoneService timezoneService)
|
ITimezoneService timezoneService)
|
||||||
{
|
{
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.memoryCache = memoryCache;
|
this.memoryCache = memoryCache;
|
||||||
this.telemetryTracker = telemetryTracker;
|
this.dataSaubCache = dataSaubCache;
|
||||||
this.timezoneService = timezoneService;
|
this.timezoneService = timezoneService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,34 +46,11 @@ namespace AsbCloudInfrastructure.Services.SAUB
|
|||||||
memoryCache.DropBasic<Telemetry>();
|
memoryCache.DropBasic<Telemetry>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public DateTime GetLastTelemetryDate(int idTelemetry)
|
|
||||||
{
|
|
||||||
var telemetry = GetTelemetryCache().FirstOrDefault(t => t.Id == idTelemetry);
|
|
||||||
|
|
||||||
if (telemetry is null)
|
|
||||||
throw new Exception($"Telemetry id:{idTelemetry} does not exist");
|
|
||||||
|
|
||||||
var uid = telemetry.RemoteUid;
|
|
||||||
var timzone = GetTimezone(idTelemetry);
|
|
||||||
var lastTelemetryDate = telemetryTracker.GetLastTelemetryDateByUid(uid);
|
|
||||||
return lastTelemetryDate.ToRemoteDateTime(timzone.Hours);
|
|
||||||
}
|
|
||||||
|
|
||||||
public DatesRangeDto GetDatesRange(int idTelemetry)
|
public DatesRangeDto GetDatesRange(int idTelemetry)
|
||||||
{
|
{
|
||||||
var telemetry = GetTelemetryCache().FirstOrDefault(t => t.Id == idTelemetry);
|
var cacheDataRange = dataSaubCache.GetOrDefaultDataDateRange(idTelemetry)
|
||||||
if (telemetry is null)
|
?? new ();
|
||||||
throw new Exception($"Telemetry id:{idTelemetry} does not exist");
|
return cacheDataRange;
|
||||||
|
|
||||||
var dto = TelemetryTracker.GetTelemetryDateRangeByUid(telemetry.RemoteUid);
|
|
||||||
if (dto is null)
|
|
||||||
throw new Exception($"Telemetry id:{idTelemetry} has no data");
|
|
||||||
|
|
||||||
var timezone = GetTimezone(idTelemetry);
|
|
||||||
dto.From = dto.From.ToTimeZoneOffsetHours(timezone.Hours);
|
|
||||||
dto.To = dto.To.ToTimeZoneOffsetHours(timezone.Hours);
|
|
||||||
|
|
||||||
return dto;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetOrCreateTelemetryIdByUid(string uid)
|
public int GetOrCreateTelemetryIdByUid(string uid)
|
||||||
|
@ -1,154 +0,0 @@
|
|||||||
using AsbCloudApp.Data;
|
|
||||||
using AsbCloudApp.Services;
|
|
||||||
using AsbCloudDb.Model;
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace AsbCloudInfrastructure.Services.SAUB
|
|
||||||
{
|
|
||||||
|
|
||||||
public class TelemetryTracker : ITelemetryTracker
|
|
||||||
{
|
|
||||||
class TrackerStat
|
|
||||||
{
|
|
||||||
//public int Id { get; set; }
|
|
||||||
|
|
||||||
public string RemoteUid { get; set; } = null!;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Время последнего запроса (по времени сервера)
|
|
||||||
/// </summary>
|
|
||||||
public DateTimeOffset LastTimeServer { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Дата первых данных в БД
|
|
||||||
/// </summary>
|
|
||||||
public DateTimeOffset TelemetryDateUtcMin { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Дата последних данных в БД
|
|
||||||
/// </summary>
|
|
||||||
public DateTimeOffset TelemetryDateUtcMax { get; set; }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private readonly ConcurrentDictionary<string, TrackerStat> telemetriesStats;
|
|
||||||
|
|
||||||
public TelemetryTracker(IConfiguration configuration, IMemoryCache memoryCache)
|
|
||||||
{
|
|
||||||
// TODO: make this background work
|
|
||||||
var contextOptions = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
|
||||||
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
|
||||||
.Options;
|
|
||||||
var db = new AsbCloudDbContext(contextOptions);
|
|
||||||
|
|
||||||
var cacheTelemetry = memoryCache.GetOrCreateBasic(db.Set<Telemetry>().Include(t=>t.Well));
|
|
||||||
var keyValuePairs = new Dictionary<string, TrackerStat>(cacheTelemetry.Count());
|
|
||||||
foreach (var telemetry in cacheTelemetry)
|
|
||||||
{
|
|
||||||
var date = telemetry.Info?.DrillingStartDate
|
|
||||||
?? ParseDateFromUidOrDefault(telemetry.RemoteUid, DateTimeOffset.MinValue);
|
|
||||||
|
|
||||||
keyValuePairs[telemetry.RemoteUid] = new TrackerStat
|
|
||||||
{
|
|
||||||
RemoteUid = telemetry.RemoteUid,
|
|
||||||
TelemetryDateUtcMin = date,
|
|
||||||
TelemetryDateUtcMax = date,
|
|
||||||
LastTimeServer = date,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
telemetriesStats = new ConcurrentDictionary<string, TrackerStat>(keyValuePairs);
|
|
||||||
|
|
||||||
Task.Run(async () =>
|
|
||||||
{
|
|
||||||
db.Database.SetCommandTimeout(2 * 60);
|
|
||||||
var dates = await db.TelemetryDataSaub
|
|
||||||
.GroupBy(d => d.IdTelemetry)
|
|
||||||
.Select(g => new
|
|
||||||
{
|
|
||||||
IdTelemetry = g.Key,
|
|
||||||
DateMax = g.Max(d => d.DateTime),
|
|
||||||
DateMin = g.Min(d => d.DateTime),
|
|
||||||
})
|
|
||||||
.AsNoTracking()
|
|
||||||
.ToListAsync()
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
|
|
||||||
var oldRequests = dates.Select(t => new
|
|
||||||
{
|
|
||||||
Uid = cacheTelemetry.FirstOrDefault(c => c.Id == t.IdTelemetry)?.RemoteUid,
|
|
||||||
t.DateMax,
|
|
||||||
t.DateMin,
|
|
||||||
}).Where(s => !string.IsNullOrEmpty(s.Uid));
|
|
||||||
|
|
||||||
foreach (var oldReq in oldRequests)
|
|
||||||
{
|
|
||||||
if (oldReq.Uid is not null)
|
|
||||||
{
|
|
||||||
var telemetryStat = telemetriesStats.GetOrAdd(oldReq.Uid, (uid) => new TrackerStat { RemoteUid = uid });
|
|
||||||
telemetryStat.TelemetryDateUtcMin = oldReq.DateMin;
|
|
||||||
telemetryStat.TelemetryDateUtcMax = oldReq.DateMax;
|
|
||||||
telemetryStat.LastTimeServer = oldReq.DateMax;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}).ContinueWith((t) =>
|
|
||||||
{
|
|
||||||
db.Dispose();
|
|
||||||
return t;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static DateTimeOffset ParseDateFromUidOrDefault(string remoteUid, DateTimeOffset 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,
|
|
||||||
TelemetryDateUtcMin = remoteDate
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
stat.LastTimeServer = DateTime.Now;
|
|
||||||
|
|
||||||
if (stat.TelemetryDateUtcMax.ToUniversalTime() < remoteDate.ToUniversalTime())
|
|
||||||
stat.TelemetryDateUtcMax = remoteDate;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DateTimeOffset GetLastTelemetryDateByUid(string uid) =>
|
|
||||||
telemetriesStats.GetValueOrDefault(uid)?.TelemetryDateUtcMax ?? default;
|
|
||||||
|
|
||||||
public DatesRangeDto GetTelemetryDateRangeByUid(string uid)
|
|
||||||
{
|
|
||||||
var stat = telemetriesStats.GetValueOrDefault(uid);
|
|
||||||
var range = new DatesRangeDto
|
|
||||||
{
|
|
||||||
From = stat?.TelemetryDateUtcMin.UtcDateTime ?? default,
|
|
||||||
To = stat?.TelemetryDateUtcMax.UtcDateTime ?? default,
|
|
||||||
};
|
|
||||||
return range;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -151,7 +151,7 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
|
|||||||
WellType = wellType?.Caption ?? "",
|
WellType = wellType?.Caption ?? "",
|
||||||
IdState = well.IdState,
|
IdState = well.IdState,
|
||||||
State = wellService.GetStateText(well.IdState),
|
State = wellService.GetStateText(well.IdState),
|
||||||
LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id).DateTime,
|
LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id),
|
||||||
Companies = await wellService.GetCompaniesAsync(well.Id, token)
|
Companies = await wellService.GetCompaniesAsync(well.Id, token)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -57,15 +57,15 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
private void DropCacheRelationCompanyWell()
|
private void DropCacheRelationCompanyWell()
|
||||||
=> memoryCache.DropBasic<RelationCompanyWell>();
|
=> memoryCache.DropBasic<RelationCompanyWell>();
|
||||||
|
|
||||||
public DateTimeOffset GetLastTelemetryDate(int idWell)
|
public DateTime GetLastTelemetryDate(int idWell)
|
||||||
{
|
{
|
||||||
var well = GetOrDefault(idWell);
|
var well = GetOrDefault(idWell);
|
||||||
|
|
||||||
if (well?.IdTelemetry is null)
|
if (well?.IdTelemetry is null)
|
||||||
return DateTimeOffset.MinValue;
|
return DateTime.MinValue;
|
||||||
|
|
||||||
var lastTelemetryDate = telemetryService.GetLastTelemetryDate((int)well.IdTelemetry);
|
var datesRange = telemetryService.GetDatesRange(well.IdTelemetry.Value);
|
||||||
return lastTelemetryDate;
|
return datesRange.To;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
@ -97,7 +97,7 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
dto.Latitude ??= gCluster.Key.Latitude ?? gDeposit.Key.Latitude;
|
dto.Latitude ??= gCluster.Key.Latitude ?? gDeposit.Key.Latitude;
|
||||||
dto.Longitude ??= gCluster.Key.Longitude ?? gDeposit.Key.Longitude;
|
dto.Longitude ??= gCluster.Key.Longitude ?? gDeposit.Key.Longitude;
|
||||||
if (well.IdTelemetry is not null)
|
if (well.IdTelemetry is not null)
|
||||||
dto.LastTelemetryDate = telemetryService.GetLastTelemetryDate(well.IdTelemetry.Value);
|
dto.LastTelemetryDate = telemetryService.GetDatesRange(well.IdTelemetry.Value).To;
|
||||||
|
|
||||||
return dto;
|
return dto;
|
||||||
}),
|
}),
|
||||||
@ -256,7 +256,7 @@ namespace AsbCloudInfrastructure.Services
|
|||||||
dto.Cluster = entity.Cluster.Caption;
|
dto.Cluster = entity.Cluster.Caption;
|
||||||
dto.Deposit = entity.Cluster.Deposit.Caption;
|
dto.Deposit = entity.Cluster.Deposit.Caption;
|
||||||
if (entity.IdTelemetry is not null)
|
if (entity.IdTelemetry is not null)
|
||||||
dto.LastTelemetryDate = telemetryService.GetLastTelemetryDate((int)entity.IdTelemetry);
|
dto.LastTelemetryDate = telemetryService.GetDatesRange(entity.IdTelemetry.Value).To;
|
||||||
dto.Companies = entity.RelationCompaniesWells
|
dto.Companies = entity.RelationCompaniesWells
|
||||||
.Select(r => Convert(r.Company))
|
.Select(r => Convert(r.Company))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
Loading…
Reference in New Issue
Block a user