Merge branch 'dev' into feature/refactoring-daily-report-form

This commit is contained in:
on.nemtina 2023-03-27 17:15:31 +05:00
commit ee8f000ab1
12 changed files with 204 additions and 28 deletions

View File

@ -22,10 +22,6 @@ namespace AsbCloudApp.Data
[Range(1, int.MaxValue, ErrorMessage = "Id категории не может быть ниже 1")]
public int CategoryId { get; set; }
//TODO: в модели дто сообщения отсутствует поле Id скважины
// скорее всего опечатка т.к. используется глубина в правиле валидатора
//в других валидаторах парамтр глубины идет рэнжированный от...до
/// <summary>
/// глубина забоя, при котором событие возникло
/// </summary>

View File

@ -10,7 +10,6 @@ namespace AsbCloudApp.Services
/// </summary>
public interface IScheduleRepository : IRepositoryWellRelated<ScheduleDto>
{
// TODO: this should be nullable.
/// <summary>
/// получить бурильщика по idWell и времени
/// </summary>
@ -18,6 +17,6 @@ namespace AsbCloudApp.Services
/// <param name="workTime"></param>
/// <param name="token"></param>
/// <returns></returns>
Task<DrillerDto?> GetDrillerAsync(int idWell, DateTime workTime, CancellationToken token);
Task<DrillerDto?> GetOrDefaultDrillerAsync(int idWell, DateTime workTime, CancellationToken token);
}
}

View File

@ -22,7 +22,7 @@ namespace AsbCloudApp.Services
/// <param name="approxPointsCount">кол-во элементов до которых эти данные прореживаются</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<TDto>?> GetOrDefaultAsync(int idWell,
Task<IEnumerable<TDto>> GetAsync(int idWell,
DateTime dateBegin = default, double intervalSec = 600d,
int approxPointsCount = 1024, CancellationToken token = default);

View File

@ -0,0 +1,80 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace AsbCloudApp.ValidationAttributes
{
/// <summary>
/// Атрибут валидации даты-времени
/// </summary>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property)]
public class DateValidationAttribute : ValidationAttribute
{
private DateTime? gtDate;
/// <summary>
/// null разрешен
/// </summary>
public bool AllowNull { get; set; } = true;
/// <summary>
/// Разрешена только дат.
/// При наличии времени в DateTime инвалидирует.
/// При наличии UTC тоже инвалидирует.
/// </summary>
public bool IsDateOnly { get; set; } = false;
/// <summary>
/// Допустима дата-время в UTC
/// </summary>
public bool AllowUtc { get; set; } = true;
/// <summary>
/// Дата больше которой должно быть проверяемое значение.
/// Формат строки - любой поддерживаемый DateTime.Parse.
/// Желательно использовать ISO 8601 формат
/// </summary>
public string? GtDate {
get => gtDate.ToString();
set
{
if(value is null)
gtDate = null;
else
gtDate = DateTime.Parse(value);
}
}
/// <summary>
/// Проверка значения
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool IsValid(object? value)
{
if (value is null)
return AllowNull;
if (value is not DateTime dateTime)
return false;
if (IsDateOnly)
{
if (dateTime.Hour > 0 ||
dateTime.Minute > 0 ||
dateTime.Second > 0 ||
dateTime.Millisecond > 0 ||
dateTime.Kind == DateTimeKind.Utc)
return false;
}
if (!AllowUtc && dateTime.Kind == DateTimeKind.Utc)
return false;
if (gtDate.HasValue && dateTime <= gtDate)
return false;
return true;
}
}
}

View File

@ -21,7 +21,7 @@ namespace AsbCloudInfrastructure.Repository
this.wellService = wellService;
}
public async Task<DrillerDto?> GetDrillerAsync(int idWell, DateTime workTime, CancellationToken token)
public async Task<DrillerDto?> GetOrDefaultDrillerAsync(int idWell, DateTime workTime, CancellationToken token)
{
var hoursOffset = wellService.GetTimezone(idWell).Hours;
var date = workTime.ToUtcDateTimeOffset(hoursOffset);

View File

@ -86,16 +86,14 @@ namespace AsbCloudInfrastructure.Services.SAUB
}
}
// TODO: It shouldn`t be nullable. Throw exceptions instead and return empty.
/// <inheritdoc/>
public virtual async Task<IEnumerable<TDto>?> GetOrDefaultAsync(int idWell,
public virtual async Task<IEnumerable<TDto>> GetAsync(int idWell,
DateTime dateBegin = default, double intervalSec = 600d,
int approxPointsCount = 1024, CancellationToken token = default)
{
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
if (telemetry is null)
return null;
return Enumerable.Empty<TDto>();
var timezone = telemetryService.GetTimezone(telemetry.Id);
@ -134,7 +132,7 @@ namespace AsbCloudInfrastructure.Services.SAUB
.ConfigureAwait(false);
if (fullDataCount == 0)
return default;
return Enumerable.Empty<TDto>();
if (fullDataCount > 1.75 * approxPointsCount)
{

View File

@ -121,8 +121,7 @@ namespace AsbCloudInfrastructure.Services.SAUB
_ => 32_768
};
var data = await GetOrDefaultAsync(idWell, beginDate, intervalSec, approxPointsCount, token )
?? Enumerable.Empty<TelemetryDataSaubDto>();
var data = await GetAsync(idWell, beginDate, intervalSec, approxPointsCount, token );
var fileName = $"DataSaub idWell{idWell}";
if (telemetry.Info is not null)

View File

@ -0,0 +1,108 @@
using AsbCloudApp.ValidationAttributes;
using System;
using Xunit;
namespace AsbCloudWebApi.Tests.AttributeTest
{
public class DateValidationAttributeTest
{
[Fact]
public void AllowNull_true_on_null_valid()
{
var attribute = new DateValidationAttribute { AllowNull = true };
var result = attribute.IsValid(null);
Assert.True(result);
}
[Fact]
public void AllowNull_false_on_null_invalid()
{
var attribute = new DateValidationAttribute { AllowNull = false };
var result = attribute.IsValid(null);
Assert.False(result);
}
[Fact]
public void IsDateOnly_true_on_empty_timePart_valid()
{
var attribute = new DateValidationAttribute { IsDateOnly = true };
var date = new DateTime(2023, 01, 01, 00, 00, 00);
var result = attribute.IsValid(date);
Assert.True(result);
}
[Fact]
public void IsDateOnly_true_on_timePart_invalid()
{
var attribute = new DateValidationAttribute { IsDateOnly = true };
var date = new DateTime(2023, 01, 01, 01, 01, 01);
var result = attribute.IsValid(date);
Assert.False(result);
}
[Fact]
public void IsDateOnly_true_on_utc_invalid()
{
var attribute = new DateValidationAttribute { IsDateOnly = true };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Utc);
var result = attribute.IsValid(date);
Assert.False(result);
}
[Fact]
public void AllowUtc_true_on_unspecified_valid()
{
var attribute = new DateValidationAttribute { AllowUtc = true };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Unspecified);
var result = attribute.IsValid(date);
Assert.True(result);
}
[Fact]
public void AllowUtc_true_on_utc_valid()
{
var attribute = new DateValidationAttribute { AllowUtc = true };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Utc);
var result = attribute.IsValid(date);
Assert.True(result);
}
[Fact]
public void AllowUtc_false_on_utc_invalid()
{
var attribute = new DateValidationAttribute { AllowUtc = false };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Utc);
var result = attribute.IsValid(date);
Assert.False(result);
}
[Fact]
public void AllowUtc_false_on_unspecified_valid()
{
var attribute = new DateValidationAttribute { AllowUtc = false };
var date = new DateTime(2023, 01, 01, 00, 00, 00, DateTimeKind.Unspecified);
var result = attribute.IsValid(date);
Assert.True(result);
}
[Fact]
public void GtDate_on_same_date_invalid()
{
var gtDate = "2023-01-01T00:00:00";
var date = DateTime.Parse(gtDate);
var attribute = new DateValidationAttribute { GtDate = gtDate };
var result = attribute.IsValid(date);
Assert.False(result);
}
[Fact]
public void GtDate_on_greater_date_valid()
{
var gtDate = "2023-01-01T00:00:00";
var date = DateTime.Parse(gtDate).AddMilliseconds(1);
var attribute = new DateValidationAttribute { GtDate = gtDate };
var result = attribute.IsValid(date);
Assert.True(result);
}
}
}

View File

@ -35,7 +35,7 @@ namespace AsbCloudWebApi.Tests.Middlware
public class TelemetryDataSaubService : ITelemetryDataSaubService
{
public async Task<IEnumerable<TelemetryDataSaubDto>?> GetOrDefaultAsync(int idWell, DateTime dateBegin = default, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
public async Task<IEnumerable<TelemetryDataSaubDto>?> GetAsync(int idWell, DateTime dateBegin = default, double intervalSec = 600, int approxPointsCount = 1024, CancellationToken token = default)
{
await Task.Delay(1000, token);
return Enumerable.Empty<TelemetryDataSaubDto>();

View File

@ -77,22 +77,17 @@ namespace AsbCloudWebApi.Controllers
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[Route("cluster/{idCluster}/stat")] // TODO: Это статистика кластера, перенести в ClusterOperationStatController
[Route("cluster/{idCluster}/stat")]
[Permission]
[ProducesResponseType(typeof(StatClusterDto), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetStatClusterAsync(int idCluster,
CancellationToken token = default)
{
int? idCompanyOrNull = User.GetCompanyId();
if (idCompanyOrNull is null)
int? idCompany = User.GetCompanyId();
if (idCompany is null)
return Forbid();
int idCompany = idCompanyOrNull ?? 0;
// TODO: Fix next commented lines
//if (!await CanUserAccessToWellAsync(idCluster, token).ConfigureAwait(false))
// return Forbid();
var result = await operationsStatService.GetStatClusterAsync(idCluster, idCompany, token)
var result = await operationsStatService.GetStatClusterAsync(idCluster, idCompany.Value, token)
.ConfigureAwait(false);
return Ok(result);
}

View File

@ -86,7 +86,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
if (!isCompanyOwnsWell)
return Forbid();
var content = await telemetryDataService.GetOrDefaultAsync(idWell, begin,
var content = await telemetryDataService.GetAsync(idWell, begin,
intervalSec, approxPointsCount, token).ConfigureAwait(false);
return Ok(content);

View File

@ -8,6 +8,7 @@ using System.Threading.Tasks;
namespace AsbCloudWebApi.Controllers
{
#nullable enable
/// <summary>
/// Расписание бурильщиков
/// </summary>
@ -32,12 +33,12 @@ namespace AsbCloudWebApi.Controllers
/// <param name="token"></param>
/// <returns>бурильщик</returns>
[HttpGet("driller")]
public async Task<ActionResult<DrillerDto>> GetDrillerAsync(int idWell, DateTime workTime, CancellationToken token)
public async Task<ActionResult<DrillerDto?>> GetDrillerAsync(int idWell, DateTime workTime, CancellationToken token)
{
if (!await UserHasAccesToWellAsync(idWell, token))
return Forbid();
var result = await scheduleService.GetDrillerAsync(idWell, workTime, token);
var result = await scheduleService.GetOrDefaultDrillerAsync(idWell, workTime, token);
return Ok(result);
}