From 1613f9a9f4323a8de4a3bca05428f7858ce81a68 Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Wed, 21 Sep 2022 13:06:40 +0500 Subject: [PATCH] =?UTF-8?q?-=20=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=BC=D0=BE=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D0=B8=20SubsystemStatDto=20-=20=D0=BA=D0=BE=D1=80=D1=80?= =?UTF-8?q?=D0=B5=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20GetDepthInterval=20-=20=D0=BA?= =?UTF-8?q?=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B0=20=D0=B2=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=BB=D0=BB=D0=B5=D1=80=D0=B5=20SubsystemOperationTime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Data/Subsystems/SubsystemStatDto.cs | 2 +- ...SubsystemOperationTimeBackgroundService.cs | 6 +- .../SubsystemOperationTimeService.cs | 78 ++++++++----------- .../SubsystemOperationTimeController.cs | 10 ++- 4 files changed, 44 insertions(+), 52 deletions(-) diff --git a/AsbCloudApp/Data/Subsystems/SubsystemStatDto.cs b/AsbCloudApp/Data/Subsystems/SubsystemStatDto.cs index c00a06ed..c257d029 100644 --- a/AsbCloudApp/Data/Subsystems/SubsystemStatDto.cs +++ b/AsbCloudApp/Data/Subsystems/SubsystemStatDto.cs @@ -25,7 +25,7 @@ namespace AsbCloudApp.Data.Subsystems /// /// сумма изменения глубин /// - public double SumDepthChange { get; set; } + public double SumDepthInterval { get; set; } /// /// количество операций /// diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index 62e3465c..da9e63a3 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -126,7 +126,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems return false; } - static List getSubsystemOperationTimes (List dataRows, Predicate satisfyCondition, int idSubsystem, int idTelemetry) + static List GetSubsystemOperationTimes (List dataRows, Predicate satisfyCondition, int idSubsystem, int idTelemetry) { if (dataRows is null) return new List(); @@ -206,8 +206,8 @@ namespace AsbCloudInfrastructure.Services.Subsystems }; dataRowList.Add(dateRowItem); } - var akbOperationTimes = getSubsystemOperationTimes(dataRowList, d => isSubsytemAkb(d.Mode), idSubsytemAkb, idTelemetry); - var mseOperationTimes = getSubsystemOperationTimes(dataRowList, d => IsSubsystemMse(d.State), idSubsytemMse, idTelemetry); + var akbOperationTimes = GetSubsystemOperationTimes(dataRowList, d => isSubsytemAkb(d.Mode), idSubsytemAkb, idTelemetry); + var mseOperationTimes = GetSubsystemOperationTimes(dataRowList, d => IsSubsystemMse(d.State), idSubsytemMse, idTelemetry); subsystemOperationTime.AddRange(akbOperationTimes); subsystemOperationTime.AddRange(mseOperationTimes); return subsystemOperationTime; diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeService.cs index 8f24bcdc..5d2d4750 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeService.cs @@ -23,11 +23,13 @@ namespace AsbCloudInfrastructure.Services.Subsystems private readonly IAsbCloudDbContext db; private readonly IWellService wellService; private readonly ICrudService subsystemService; - public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudService subsystemService) + private readonly IDetectedOperationService detectedOperationService; + public SubsystemOperationTimeService(IAsbCloudDbContext db, IWellService wellService, ICrudService subsystemService, IDetectedOperationService detectedOperationService) { this.db = db; this.wellService = wellService; this.subsystemService = subsystemService; + this.detectedOperationService = detectedOperationService; } /// @@ -83,7 +85,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems var data = await GetOperationTimeAsync(request, token); if (data is null) return null; - var statList = CalcStat(data, request); + var statList = CalcStat(data, request, token); return statList; } @@ -107,77 +109,65 @@ namespace AsbCloudInfrastructure.Services.Subsystems return items; } - private IEnumerable CalcStat(IEnumerable dtos, SubsystemOperationTimeRequest request) + private async Task> CalcStat(IEnumerable dtos, SubsystemOperationTimeRequest request, CancellationToken token) { var groupedDataSubsystems = dtos .GroupBy(o => o.IdSubsystem); + + //var _detectOperations = await detectedOperationService.GetAsync(, token); var periodGroupTotal = dtos.Sum(o => (o.DateEnd - o.DateStart).TotalHours); var gtDate = request.GtDate ?? dtos.Min(o => o.DateStart); var ltDate = request.LtDate ?? dtos.Max(o => o.DateEnd); + var detectedOperations = db.DetectedOperations + .Where(a => a.DateStart >= gtDate & a.DateEnd <= ltDate) + .ToList(); + var periodRequest = (ltDate - gtDate).TotalHours; - var result = groupedDataSubsystems.Select(item => + var result = groupedDataSubsystems.Select(g => { - var periodGroup = item.Sum(o => (o.DateEnd - o.DateStart).TotalHours); - var periodGroupDepth = item.Sum(o => o.DepthEnd - o.DepthStart); - var depthChange = GetDepthChange(item.Key, gtDate, ltDate); + var periodGroup = g.Sum(o => (o.DateEnd - o.DateStart).TotalHours); + var periodGroupDepth = g.Sum(o => o.DepthEnd - o.DepthStart); + var depthInterval = GetDepthInterval(g.Key, detectedOperations); var subsystemStat = new SubsystemStatDto() { - IdSubsystem = item.Key, - SubsystemName = subsystemService.GetOrDefault(item.Key)?.Name ?? "unknown", - UsedTimeHours = periodGroup, - //условие добавлено что бы избежать деление на ноль - //чисто теоретически такое событие может произойти только в случае - //если не было определенных операций по какому то из режимов - // тогда в этом поле статистики будет null - KUsage = depthChange==0? double.NaN : periodGroupDepth / depthChange, - SumDepthChange = periodGroupDepth, - OperationCount = item.Count() + IdSubsystem = g.Key, + SubsystemName = subsystemService.GetOrDefault(g.Key)?.Name ?? "unknown", + UsedTimeHours = periodGroup, + KUsage = periodGroupDepth / depthInterval, + SumDepthInterval = periodGroupDepth, + OperationCount = g.Count() }; return subsystemStat; }); return result; } - private double GetDepthChange (int idSubsystem, DateTime gtDate, DateTime ltDate) - { - double depthChange = 0; - var detectOperations = db.DetectedOperations - .Where(a => a.DateStart >= gtDate & a.DateEnd <= ltDate) - .ToList(); - //AKB - Rotor+Slide - if (idSubsystem == 1) - { - depthChange = detectOperations.Where(a => a.IdCategory == 1 & a.IdCategory == 3) + private double GetDepthInterval (int idSubsystem, List detectedOperations) + { + var depthIntervalRotor = detectedOperations.Where(o => o.IdCategory == 3) .Sum(o => o.DepthEnd - o.DepthStart); - return depthChange; - } - //Mse - all detect operations - if (idSubsystem == 2) - { - depthChange = detectOperations + var depthIntervalSlide = detectedOperations.Where(o => o.IdCategory == 3) .Sum(o => o.DepthEnd - o.DepthStart); - return depthChange; - } + //AKB - MSE + if (idSubsystem == 1 | idSubsystem ==2) + { + return depthIntervalRotor + depthIntervalSlide; + } //Slide if (idSubsystem == 65536) { - depthChange = detectOperations.Where(o => o.IdCategory == 3) - .Sum(o => o.DepthEnd - o.DepthStart); - return depthChange; + return depthIntervalSlide; } //Rotor if (idSubsystem == 65537) - { - depthChange = detectOperations.Where(o => o.IdCategory == 1) - .Sum(o => o.DepthEnd - o.DepthStart); - return depthChange; + { + return depthIntervalRotor; } - - return depthChange; + return 0; } private IQueryable? BuildQuery(SubsystemOperationTimeRequest request) diff --git a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs index f6f8a8a8..f585bbaf 100644 --- a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs +++ b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs @@ -40,7 +40,7 @@ namespace AsbCloudWebApi.Controllers.Subsystems if (!await UserHasAccesToWellAsync(request.IdWell, token)) return Forbid(); if (!await TimeRequestValidate(request, token)) - return BadRequest(); + return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC)."); var subsystemResult = await subsystemOperationTimeService.GetStatAsync(request, token); return Ok(subsystemResult); } @@ -72,7 +72,7 @@ namespace AsbCloudWebApi.Controllers.Subsystems if (!await UserHasAccesToWellAsync(request.IdWell, token)) return Forbid(); if (!await TimeRequestValidate(request, token)) - return BadRequest(); + return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC)."); var result = await subsystemOperationTimeService.GetOperationTimeAsync(request, token); return Ok(result); @@ -93,6 +93,8 @@ namespace AsbCloudWebApi.Controllers.Subsystems { if (!await UserHasAccesToWellAsync(request.IdWell, token)) return Forbid(); + if (!await TimeRequestValidate(request, token)) + return BadRequest("Запрашиваемый диапазон должен заканчиваться за 2 часа до текущего времени(после приведения к UTC)."); var result = await subsystemOperationTimeService.DeleteAsync(request, token); return Ok(result); } @@ -113,8 +115,8 @@ namespace AsbCloudWebApi.Controllers.Subsystems { var ltDate = (DateTimeOffset)request.LtDate; var utcDateRequest = ltDate.ToRemoteDateTime(well.Timezone.Hours); - var DateNow = ((DateTimeOffset)DateTime.Now).ToRemoteDateTime(well.Timezone.Hours); - if (utcDateRequest.AddHours(2)