diff --git a/AsbCloudInfrastructure/Services/WorkLimitingParameterCalc.cs b/AsbCloudInfrastructure/Background/WorkLimitingParameterCalc.cs similarity index 92% rename from AsbCloudInfrastructure/Services/WorkLimitingParameterCalc.cs rename to AsbCloudInfrastructure/Background/WorkLimitingParameterCalc.cs index 3ab159e0..76749e07 100644 --- a/AsbCloudInfrastructure/Services/WorkLimitingParameterCalc.cs +++ b/AsbCloudInfrastructure/Background/WorkLimitingParameterCalc.cs @@ -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> 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 { diff --git a/AsbCloudInfrastructure/Services/WellInfoService.cs b/AsbCloudInfrastructure/Services/WellInfoService.cs index af1265c3..f301fd02 100644 --- a/AsbCloudInfrastructure/Services/WellInfoService.cs +++ b/AsbCloudInfrastructure/Services/WellInfoService.cs @@ -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 onProgressCallback, CancellationToken token) @@ -40,8 +40,9 @@ public class WellInfoService var messageHub = services.GetRequiredService>(); 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(); wellMapInfo.IdState = well.IdState; onProgressCallback($"Start updating info by well({well.Id}): {well.Caption}", i++ / count); diff --git a/AsbCloudWebApi/Controllers/BackgroundWork.cs b/AsbCloudWebApi/Controllers/BackgroundWorkController.cs similarity index 63% rename from AsbCloudWebApi/Controllers/BackgroundWork.cs rename to AsbCloudWebApi/Controllers/BackgroundWorkController.cs index 316d796e..65e7171b 100644 --- a/AsbCloudWebApi/Controllers/BackgroundWork.cs +++ b/AsbCloudWebApi/Controllers/BackgroundWorkController.cs @@ -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), (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); + } } } diff --git a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs index 39d60ea9..efa60638 100644 --- a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs +++ b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs @@ -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; } - - /// - /// Валидирует запрос и бросает исключение ArgumentInvalidException - /// - /// - /// - /// - /// - 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 часа до текущего времени"); - } - } } }