Правки

1. Изменено описание API
2. Добавлено кеширвоание
This commit is contained in:
Степанов Дмитрий 2024-08-22 15:00:13 +05:00
parent 5a8d1e448d
commit f27bdb65b1
3 changed files with 42 additions and 67 deletions

View File

@ -1,28 +1,14 @@
using System.Collections.Generic; namespace AsbCloudApp.Requests;
namespace AsbCloudApp.Requests;
/// <summary> /// <summary>
/// Запрос получения информации по телеметрии /// Запрос получения информации по телеметрии
/// </summary> /// </summary>
public class TelemetryInfoRequestBase public class TelemetryInfoRequest
{ {
/// <summary> /// <summary>
/// Конструктор по умолчанию /// Идентификатор компании
/// </summary> /// </summary>
public TelemetryInfoRequestBase() public int IdCompany { get; set; }
{
}
/// <summary>
/// Копирующий конструктор
/// </summary>
/// <param name="request"></param>
public TelemetryInfoRequestBase(TelemetryInfoRequestBase request)
{
IdWellState = request.IdWellState;
SortFields = request.SortFields;
}
/// <summary> /// <summary>
/// Определяет состояние скважины /// Определяет состояние скважины
@ -31,32 +17,4 @@ public class TelemetryInfoRequestBase
/// 2 - завершена /// 2 - завершена
/// </summary> /// </summary>
public int IdWellState { get; set; } public int IdWellState { get; set; }
/// <summary>
/// Сортировки:
/// Содержат список названий полей сортировки
/// Указать направление сортировки можно через пробел "asc" или "desc"
/// </summary>
public IEnumerable<string>? SortFields { get; set; }
}
/// <summary>
/// Запрос получения информации по телеметрии
/// </summary>
public class TelemetryInfoRequest : TelemetryInfoRequestBase
{
/// <summary>
/// Копирующий конструктор
/// </summary>
public TelemetryInfoRequest(int idCompany,
TelemetryInfoRequestBase request)
: base(request)
{
IdCompany = idCompany;
}
/// <summary>
/// Идентификатор компании
/// </summary>
public int IdCompany { get; set; }
} }

View File

@ -25,7 +25,6 @@ public class TelemetryService : ITelemetryService
//TODO: методы использующие ITelemetryDataCache, скорее всего, тут не нужны //TODO: методы использующие ITelemetryDataCache, скорее всего, тут не нужны
private readonly ITelemetryDataCache<TelemetryDataSaubDto> dataSaubCache; private readonly ITelemetryDataCache<TelemetryDataSaubDto> dataSaubCache;
private readonly ITimezoneService timezoneService; private readonly ITimezoneService timezoneService;
public ITimezoneService TimeZoneService => timezoneService; public ITimezoneService TimeZoneService => timezoneService;
public TelemetryService( public TelemetryService(
@ -81,28 +80,39 @@ public class TelemetryService : ITelemetryService
public async Task<IEnumerable<TelemetryInfoDto>> GetInfoAsync(TelemetryInfoRequest request, CancellationToken token) public async Task<IEnumerable<TelemetryInfoDto>> GetInfoAsync(TelemetryInfoRequest request, CancellationToken token)
{ {
var idTelemetries = await db.Set<Well>().Include(x => x.RelationCompaniesWells) var idTelemetries = await db.Set<Well>()
.Include(x => x.RelationCompaniesWells)
.Where(x => x.RelationCompaniesWells.Any(y => y.IdCompany == request.IdCompany) && .Where(x => x.RelationCompaniesWells.Any(y => y.IdCompany == request.IdCompany) &&
x.IdState == request.IdWellState) x.IdState == request.IdWellState)
.Select(x => x.IdTelemetry) .Where(x => x.IdTelemetry.HasValue)
.Select(x => x.IdTelemetry!.Value)
.ToArrayAsync(token); .ToArrayAsync(token);
var query = db.Set<Telemetry>() var key = $"TelemetryInfo_{string.Join("", idTelemetries)}";
.Where(x => idTelemetries.Contains(x.Id));
if (request.SortFields?.Any() == true) var result = await memoryCache.GetOrCreateAsync(key, async entry =>
query = query.SortBy(request.SortFields); {
entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10);
var query = db.Set<Telemetry>()
.Where(x => idTelemetries.Contains(x.Id))
.OrderBy(x => x.Info.DrillingStartDate);
var entities = await query var entities = await query
.AsNoTracking() .AsNoTracking()
.ToArrayAsync(token); .ToArrayAsync(token);
return entities.Select(x => var dtos = entities.Select(x =>
{ {
var dto = x.Info.Adapt<TelemetryInfoDto>(); var dto = x.Info.Adapt<TelemetryInfoDto>();
dto.DrillingStartDate = x.Info.DrillingStartDate.ToRemoteDateTime(dto.TimeZoneOffsetTotalHours); dto.DrillingStartDate = x.Info.DrillingStartDate.ToRemoteDateTime(dto.TimeZoneOffsetTotalHours);
return dto; return dto;
}); });
return dtos;
});
return result!;
} }
[Obsolete("This method will be private. Use TelemetryDto.TimeZone prop.")] [Obsolete("This method will be private. Use TelemetryDto.TimeZone prop.")]

View File

@ -11,7 +11,7 @@ using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers; namespace AsbCloudWebApi.Controllers;
/// <summary> /// <summary>
/// Информация о телеметрии /// Версии прошивок
/// </summary> /// </summary>
[Route("api/[controller]")] [Route("api/[controller]")]
[ApiController] [ApiController]
@ -26,22 +26,29 @@ public class TelemetryInfoController : ControllerBase
} }
/// <summary> /// <summary>
/// Получить список информации о телеметрии /// Получить список версий прошивок
/// </summary> /// </summary>
/// <param name="request"></param> /// <param name="idWellState">Определяет состояние скважины
/// 0 - неизвестно,
/// 1 - в работе,
/// 2 - завершена</param>
/// <param name="token"></param> /// <param name="token"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet] [HttpGet]
[Permission] [Permission]
[ProducesResponseType(typeof(IEnumerable<TelemetryInfoDto>), StatusCodes.Status200OK)] [ProducesResponseType(typeof(IEnumerable<TelemetryInfoDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAsync([FromQuery] TelemetryInfoRequestBase request, CancellationToken token) public async Task<IActionResult> GetAsync([FromQuery] int idWellState, CancellationToken token)
{ {
var idCompany = User.GetCompanyId(); var idCompany = User.GetCompanyId();
if (!idCompany.HasValue) if (!idCompany.HasValue)
return Forbid(); return Forbid();
var requestToService = new TelemetryInfoRequest(idCompany.Value, request); var requestToService = new TelemetryInfoRequest
{
IdCompany = idCompany.Value,
IdWellState = idWellState
};
var telemetriesInfo = await telemetryService.GetInfoAsync(requestToService, token); var telemetriesInfo = await telemetryService.GetInfoAsync(requestToService, token);