This commit is contained in:
Olga Nemt 2023-11-01 15:56:45 +05:00
commit 1f293dfcf9
6 changed files with 89 additions and 27 deletions

View File

@ -43,6 +43,13 @@ namespace AsbCloudApp.Repositories
/// <returns></returns>
IEnumerable<TDto>? GetOrDefault(int idTelemetry, TelemetryDataRequest request);
/// <summary>
/// Диапазон дат находящийся в кеше
/// </summary>
/// <param name="idTelemetry"></param>
/// <returns></returns>
DatesRangeDto? GetOrDefaultCachedaDateRange(int idTelemetry);
/// <summary>
/// Получить диапазон дат телеметрии.
/// Дата первой записи телеметрии храниться отдельно и запоняется при инициализации

View File

@ -37,13 +37,21 @@ namespace AsbCloudApp.Services
Task<IEnumerable<TDto>> GetAsync(int idWell, TelemetryDataRequest request, CancellationToken token);
/// <summary>
/// Получение периода за период
/// Период за который есть данные по скважине в рамках временного интервала
/// </summary>
/// <param name="idWell"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="geDate"></param>
/// <param name="leDate"></param>
/// <param name="token"></param>
/// <returns></returns>
DatesRangeDto? GetRange(int idWell, DateTimeOffset start, DateTimeOffset end);
Task<DatesRangeDto> GetRangeAsync(int idWell, DateTimeOffset geDate, DateTimeOffset? leDate, CancellationToken token);
/// <summary>
/// Период за который есть данные по скважине
/// </summary>
/// <param name="idWell"></param>
/// <returns></returns>
DatesRangeDto? GetRange(int idWell);
/// <summary>
/// добавить/изменить данные тех. процесса (используется панелью)

View File

@ -1,4 +1,5 @@
using AsbCloudApp.Data;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using AsbCloudDb;
@ -204,28 +205,60 @@ namespace AsbCloudInfrastructure.Services.SAUB
}
/// <inheritdoc/>
public DatesRangeDto? GetRange(int idWell, DateTimeOffset start, DateTimeOffset end)
public async Task<DatesRangeDto> GetRangeAsync(int idWell, DateTimeOffset geDate, DateTimeOffset? leDate, CancellationToken token)
{
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell)
?? throw new ArgumentInvalidException(nameof(idWell), $"По скважине id:{idWell} нет телеметрии");
if ((DateTimeOffset.UtcNow - geDate) < TimeSpan.FromHours(12))
{
// пробуем обойтись кешем
var cechedRange = telemetryDataCache.GetOrDefaultCachedaDateRange(telemetry.Id);
if (cechedRange?.From <= geDate)
{
var datesRange = new DatesRangeDto
{
From = geDate.DateTime,
To = cechedRange.To
};
if (leDate.HasValue && leDate > geDate)
datesRange.To = leDate.Value.Date;
return datesRange;
}
}
var query = db.Set<TEntity>()
.Where(entity => entity.IdTelemetry == telemetry.Id)
.Where(entity => entity.DateTime >= geDate.ToUniversalTime());
if(leDate.HasValue)
query = query.Where(entity => entity.DateTime <= leDate.Value.ToUniversalTime());
var gquery = query
.GroupBy(entity => entity.IdTelemetry)
.Select(group => new
{
MinDate = group.Min(entity => entity.DateTime),
MaxDate = group.Max(entity => entity.DateTime),
});
var result = await gquery.FirstAsync(token);
var range = new DatesRangeDto
{
From = result.MinDate.ToOffset(TimeSpan.FromHours(telemetry.TimeZone!.Hours)).DateTime,
To = result.MaxDate.ToOffset(TimeSpan.FromHours(telemetry.TimeZone!.Hours)).DateTime,
};
return range;
}
public DatesRangeDto? GetRange(int idWell)
{
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
if (telemetry is null)
return default;
var datesRange = telemetryDataCache.GetOrDefaultDataDateRange(telemetry.Id);
if (datesRange is null)
return null;
var from = datesRange.From > start.DateTime
? datesRange.From : start.DateTime;
var to = datesRange.To < end.DateTime
? datesRange.To : end.DateTime;
return new DatesRangeDto
{
From = from,
To = to,
};
return telemetryDataCache.GetOrDefaultDataDateRange(telemetry.Id);
}
public abstract TDto Convert(TEntity src, double timezoneOffset);

View File

@ -147,6 +147,20 @@ namespace AsbCloudInfrastructure.Services.SAUB
return new DatesRangeDto { From = from.Value, To = to };
}
public DatesRangeDto? GetOrDefaultCachedaDateRange(int idTelemetry)
{
if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))
return null;
if (cacheItem.LastData.Count < 2)
return null;
var to = cacheItem.LastData[^1].DateTime;
var from = cacheItem.LastData[0].DateTime;
return new DatesRangeDto { From = from, To = to };
}
public (TDto First, TDto Last)? GetOrDefaultFirstLast(int idTelemetry)
{
if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))

View File

@ -131,15 +131,15 @@ namespace AsbCloudWebApi.Controllers.SAUB
/// Возвращает диапазон дат за которые есть телеметрия за период времени
/// </summary>
/// <param name="idWell"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <param name="geDate"></param>
/// <param name="leDate"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("{idWell}/dateRange")]
public virtual async Task<ActionResult<DatesRangeDto?>> GetRangeAsync(
[FromRoute] int idWell,
[Required] DateTimeOffset start,
[Required] DateTimeOffset end,
[Required] DateTimeOffset geDate,
DateTimeOffset? leDate,
CancellationToken token)
{
int? idCompany = User.GetCompanyId();
@ -153,7 +153,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
if (!isCompanyOwnsWell)
return Forbid();
var content = telemetryDataService.GetRange(idWell, start, end);
var content = await telemetryDataService.GetRangeAsync(idWell, geDate, leDate, token);
return Ok(content);
}

View File

@ -68,7 +68,7 @@ namespace AsbCloudWebApi.Controllers.Subsystems
if (!await UserHasAccesToWellAsync(idWell, token))
return Forbid();
var dateRange = telemetryDataSaubService.GetRange(idWell, DateTimeOffset.MinValue, DateTimeOffset.MaxValue);
var dateRange = telemetryDataSaubService.GetRange(idWell);
return Ok(dateRange);
}