Merge branch 'dev' into feature/refactoring_process_map

This commit is contained in:
ngfrolov 2023-10-10 17:44:45 +05:00
commit b36b1bdd3c
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
4 changed files with 33 additions and 41 deletions

View File

@ -7,14 +7,13 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using AsbCloudInfrastructure.Background;
using Microsoft.Extensions.DependencyInjection;
namespace AsbCloudInfrastructure.Services;
namespace AsbCloudInfrastructure.Background;
public class WorkLimitingParameterCalc : Work
{
public WorkLimitingParameterCalc()
public WorkLimitingParameterCalc()
: base("Limiting parameter calc")
{
Timeout = TimeSpan.FromMinutes(30);
@ -51,7 +50,7 @@ public class WorkLimitingParameterCalc : Work
var i = 0d;
foreach (var item in telemetryLastDetectedDates)
{
onProgressCallback($"Start hanling telemetry: {item.IdTelemetry} from {item.LastDate}", i++/count);
onProgressCallback($"Start hanling telemetry: {item.IdTelemetry} from {item.LastDate}", i++ / count);
var newLimitingParameters = await GetLimitingParameterAsync(item.IdTelemetry, item.LastDate ?? DateTimeOffset.MinValue, db, token);
if (newLimitingParameters?.Any() == true)
{
@ -63,7 +62,7 @@ public class WorkLimitingParameterCalc : Work
private static async Task<IEnumerable<LimitingParameter>> GetLimitingParameterAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
{
var query =
var query =
$"select " +
$"limiting_parameters.date, limiting_parameters.id_feed_regulator, limiting_parameters.well_depth " +
$"from ( " +
@ -72,9 +71,9 @@ public class WorkLimitingParameterCalc : Work
$"lag(id_feed_regulator, 1) over (order by date) as id_feed_regulator_lag, " +
$"lead(id_feed_regulator, 1) over (order by date) as id_feed_regulator_lead " +
$"from t_telemetry_data_saub " +
$"where id_feed_regulator is not null " +
$"and id_telemetry = {idTelemetry}" +
$"and date >= '{begin:u}'" +
$"where id_feed_regulator is not null " +
$"and id_telemetry = {idTelemetry} " +
$"and date >= '{begin:u}' " +
$"order by date) as limiting_parameters " +
$"where id_feed_regulator_lag is null " +
$"or (id_feed_regulator != id_feed_regulator_lag and id_feed_regulator_lead != id_feed_regulator_lag) " +
@ -102,14 +101,15 @@ public class WorkLimitingParameterCalc : Work
if (limitingLast.IdFeedRegulator != idLimiting || limitingLast.DepthStart < wellDepth)
{
limitingParameters.Add(new LimitingParameter {
limitingParameters.Add(new LimitingParameter
{
IdTelemetry = idTelemetry,
IdFeedRegulator = limitingLast.IdFeedRegulator,
DateStart = limitingLast.DateStart,
DateEnd = date,
DepthStart = limitingLast.DepthStart,
DepthEnd = wellDepth
});
});
limitingLast = new LimitingParameter
{

View File

@ -27,7 +27,7 @@ public class WellInfoService
public WorkWellInfoUpdate()
: base("Well statistics update")
{
Timeout = TimeSpan.FromMinutes(20);
Timeout = TimeSpan.FromMinutes(30);
}
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
@ -40,8 +40,9 @@ public class WellInfoService
var messageHub = services.GetRequiredService<IIntegrationEventHandler<UpdateWellInfoEvent>>();
var wells = await wellService.GetAllAsync(token);
var activeWells = wells.Where(well => well.IdState == 1);
var wellsIds = wells.Select(w => w.Id);
var wellsIds = activeWells.Select(w => w.Id);
var wellDrillingProcessMapRequests = wellsIds.Select(id => new WellDrillingProcessMapRequest { IdWell = id });
var wellDrillingProcessMaps = await wellDrillingProcessMapRepository.GetAsync(wellDrillingProcessMapRequests, token);
@ -58,9 +59,11 @@ public class WellInfoService
var subsystemStat = await subsystemOperationTimeService
.GetStatByActiveWells(wellsIds, token);
var count = wells.Count();
subsystemStat = subsystemStat.ToArray();
var count = activeWells.Count();
var i = 0d;
WellMapInfo = wells.Select(well => {
WellMapInfo = activeWells.Select(well => {
var wellMapInfo = well.Adapt<WellMapInfoWithComanies>();
wellMapInfo.IdState = well.IdState;
onProgressCallback($"Start updating info by well({well.Id}): {well.Caption}", i++ / count);

View File

@ -9,17 +9,16 @@ namespace AsbCloudWebApi.Controllers
[Route("api/[controller]")]
[Authorize]
[ApiController]
public class BackgroundWork : ControllerBase
public class BackgroundWorkController : ControllerBase
{
private readonly BackgroundWorker backgroundWorker;
public BackgroundWork(BackgroundWorker backgroundWorker)
public BackgroundWorkController(BackgroundWorker backgroundWorker)
{
this.backgroundWorker = backgroundWorker;
}
[HttpGet]
//[ProducesResponseType(typeof(IEnumerable<DepositDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetAll()
{
var result = new {
@ -30,5 +29,19 @@ namespace AsbCloudWebApi.Controllers
};
return Ok(result);
}
[HttpGet("Current")]
public IActionResult GetCurrent()
{
var result = (BackgroundWorkDto?)backgroundWorker.CurrentWork;
return Ok(result);
}
[HttpGet("Failed")]
public IActionResult GetFelled()
{
var result = backgroundWorker.WorkStore.Felled.Select(work => (BackgroundWorkDto)work);
return Ok(result);
}
}
}

View File

@ -1,11 +1,9 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.Subsystems;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Requests;
using AsbCloudApp.Services;
using AsbCloudApp.Services.Subsystems;
using AsbCloudDb.Model;
using AsbCloudInfrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
@ -56,7 +54,6 @@ namespace AsbCloudWebApi.Controllers.Subsystems
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
await CustomValidate(request, token);
var subsystemResult = await subsystemOperationTimeService.GetStatAsync(request, token);
return Ok(subsystemResult);
}
@ -133,7 +130,6 @@ namespace AsbCloudWebApi.Controllers.Subsystems
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
await CustomValidate(request, token);
var result = await subsystemOperationTimeService.GetOperationTimeAsync(request, token);
return Ok(result);
@ -155,7 +151,6 @@ namespace AsbCloudWebApi.Controllers.Subsystems
{
if (!await UserHasAccesToWellAsync(request.IdWell, token))
return Forbid();
await CustomValidate(request, token);
var result = await subsystemOperationTimeService.DeleteAsync(request, token);
return Ok(result);
}
@ -180,24 +175,5 @@ namespace AsbCloudWebApi.Controllers.Subsystems
return true;
return false;
}
/// <summary>
/// Валидирует запрос и бросает исключение ArgumentInvalidException
/// </summary>
/// <param name="request"></param>
/// <param name="token"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
private async Task CustomValidate(SubsystemOperationTimeRequest request, CancellationToken token)
{
var well = await wellService.GetOrDefaultAsync(request.IdWell, token);
if (well is not null && request.LtDate.HasValue)
{
var ltDate = request.LtDate.Value;
var utcDateRequest = ltDate.ToUtcDateTimeOffset(well.Timezone.Hours);
if (utcDateRequest.AddHours(2) > DateTime.UtcNow)
throw new ArgumentInvalidException(nameof(request.LtDate), "Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени");
}
}
}
}