From e447473a71f3c1fe2c351a6af85b0e7245b6e744 Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Wed, 21 Jul 2021 16:49:24 +0500 Subject: [PATCH] =?UTF-8?q?CS2-34:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D0=B0=D0=B3=D0=B8=D0=BD=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4,=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89=D0=B0=D1=8E=D1=89?= =?UTF-8?q?=D0=B8=D0=B9=20=D1=81=D0=BF=D0=B8=D1=81=D0=BE=D0=BA=20=D1=81?= =?UTF-8?q?=D0=BE=D0=B1=D1=8B=D1=82=D0=B8=D0=B9=20=D0=BD=D0=B0=20=D1=81?= =?UTF-8?q?=D0=BA=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/OperationDto.cs | 9 +- AsbCloudApp/Data/OperationDurationDto.cs | 2 +- AsbCloudApp/Services/IAnalyticsService.cs | 4 +- AsbCloudDb/Model/TelemetryAnalysis.cs | 2 +- .../Services/AnalyticsService.cs | 97 +++++++++++++------ .../Controllers/AnalyticsController.cs | 16 ++- 6 files changed, 90 insertions(+), 40 deletions(-) diff --git a/AsbCloudApp/Data/OperationDto.cs b/AsbCloudApp/Data/OperationDto.cs index b74cd57e..8a4d8e14 100644 --- a/AsbCloudApp/Data/OperationDto.cs +++ b/AsbCloudApp/Data/OperationDto.cs @@ -4,10 +4,11 @@ namespace AsbCloudApp.Data { public class OperationDto { + public int Id { get; set; } public string Name { get; set; } - public DateTime Begin { get; set; } - public DateTime End { get; set; } - public double OperationStartDepth { get; set; } - public double OperationEndDepth { get; set; } + public DateTime BeginDate { get; set; } + public DateTime EndDate { get; set; } + public double StartWellDepth { get; set; } + public double EndWellDepth { get; set; } } } diff --git a/AsbCloudApp/Data/OperationDurationDto.cs b/AsbCloudApp/Data/OperationDurationDto.cs index 199f5634..15dc1f80 100644 --- a/AsbCloudApp/Data/OperationDurationDto.cs +++ b/AsbCloudApp/Data/OperationDurationDto.cs @@ -2,7 +2,7 @@ { public class OperationDurationDto { - public string ProcessName { get; set; } + public string OperationName { get; set; } public double Duration { get; set; } } } diff --git a/AsbCloudApp/Services/IAnalyticsService.cs b/AsbCloudApp/Services/IAnalyticsService.cs index d7952a74..af18cde3 100644 --- a/AsbCloudApp/Services/IAnalyticsService.cs +++ b/AsbCloudApp/Services/IAnalyticsService.cs @@ -7,7 +7,9 @@ namespace AsbCloudApp.Services { public interface IAnalyticsService { - IEnumerable GetOperations(int wellId); + PaginationContainer GetOperationsByWell(int wellId, + IEnumerable categoryids = default, DateTime begin = default, + DateTime end = default, int skip = 0, int take = 32); IEnumerable GetWellDepthToDay(int wellId); IEnumerable GetWellDepthToInterval(int wellId, int intervalHoursTimestamp, int workBeginTimestamp); diff --git a/AsbCloudDb/Model/TelemetryAnalysis.cs b/AsbCloudDb/Model/TelemetryAnalysis.cs index 0312ecf2..114c6b16 100644 --- a/AsbCloudDb/Model/TelemetryAnalysis.cs +++ b/AsbCloudDb/Model/TelemetryAnalysis.cs @@ -17,7 +17,7 @@ namespace AsbCloudDb.Model public int IdTelemetry { get; set; } [Column("id_operation")] - public int? IdOperation { get; set; } + public int IdOperation { get; set; } diff --git a/AsbCloudInfrastructure/Services/AnalyticsService.cs b/AsbCloudInfrastructure/Services/AnalyticsService.cs index 0fee272b..9e81e0fd 100644 --- a/AsbCloudInfrastructure/Services/AnalyticsService.cs +++ b/AsbCloudInfrastructure/Services/AnalyticsService.cs @@ -5,6 +5,7 @@ using AsbCloudInfrastructure.Services.Cache; using System; using System.Collections.Generic; using System.Linq; +using Microsoft.EntityFrameworkCore; namespace AsbCloudInfrastructure.Services { @@ -28,28 +29,6 @@ namespace AsbCloudInfrastructure.Services operationDetectorService = new OperationDetectorService(operations); } - public IEnumerable GetOperations(int wellId) - { - var telemetry = telemetryService.GetTelemetryByWellId(wellId); - - if (telemetry is null) - return null; - - var operations = (from a in db.TelemetryAnalysis - where a.IdTelemetry == telemetry.Id - join o in db.Operations on a.IdOperation equals o.Id - select new OperationDto - { - Name = o.Name, - Begin = DateTimeOffset.FromUnixTimeSeconds(a.UnixDate).DateTime, - End = DateTimeOffset.FromUnixTimeSeconds(a.UnixDate + a.Duration).DateTime, - OperationStartDepth = a.OperationStartDepth ?? 0.0, - OperationEndDepth = a.OperationEndDepth ?? 0.0 - }).ToList().OrderBy(op => op.Begin); - - return operations; - } - public IEnumerable GetWellDepthToDay(int wellId) { var telemetry = telemetryService.GetTelemetryByWellId(wellId); @@ -104,6 +83,70 @@ namespace AsbCloudInfrastructure.Services return wellDepthToIntervalData; } + public PaginationContainer GetOperationsByWell(int wellId, + IEnumerable categoryIds = default, DateTime begin = default, + DateTime end = default, int skip = 0, int take = 32) + { + var telemetry = telemetryService.GetTelemetryByWellId(wellId); + + if (telemetry is null) + return null; + + var operations = from a in db.TelemetryAnalysis.Include(t => t.Operation) + where a.IdTelemetry == telemetry.Id + select a; + + if ((categoryIds != default) && (categoryIds.Any())) + operations = operations.Where(o => categoryIds.Contains(o.IdOperation)); + + var result = new PaginationContainer + { + Skip = skip, + Take = take + }; + + operations = operations.OrderBy(o => o.UnixDate); + + if (begin != default) + { + var unixBegin = (begin - new DateTime(1970, 1, 1)).TotalSeconds; + operations = operations.Where(o => o.UnixDate >= unixBegin); + } + + if (end != default) + { + var unixEnd = (end - new DateTime(1970, 1, 1)).TotalSeconds; + operations = operations.Where(m => (m.UnixDate + m.Duration) <= unixEnd); + } + + result.Count = operations.Count(); + + if (skip > 0) + operations = operations.Skip(skip); + + var operationsList = operations.Take(take).ToList(); + + if (operationsList.Count == 0) + return result; + + foreach(var operation in operations) + { + var operationDto = new OperationDto + { + Id = operation.Id, + Name = operation.Operation.Name, + BeginDate = DateTimeOffset.FromUnixTimeSeconds(operation.UnixDate).DateTime, + EndDate = DateTimeOffset.FromUnixTimeSeconds(operation.UnixDate + operation.Duration).DateTime, + StartWellDepth = operation.OperationStartDepth ?? 0.0, + EndWellDepth = operation.OperationEndDepth ?? 0.0 + }; + + result.Items.Add(operationDto); + } + + return result; + } + public IEnumerable GetOperationsSummary(int wellId, DateTime begin = default, DateTime end = default) { @@ -116,14 +159,13 @@ namespace AsbCloudInfrastructure.Services var unixEnd = (end - new DateTime(1970, 1, 1)).TotalSeconds; var operations = (from a in db.TelemetryAnalysis - where a.IdTelemetry == telemetry.Id && - a.UnixDate > unixBegin && a.UnixDate < unixEnd && - a.IdOperation != null + where a.IdTelemetry == telemetry.Id && + a.UnixDate > unixBegin && a.UnixDate < unixEnd join o in db.Operations on a.IdOperation equals o.Id group a by new { a.IdOperation, o.Name } into g select new OperationDurationDto { - ProcessName = g.Key.Name, + OperationName = g.Key.Name, Duration = g.Where(g => g.Duration > 0) .Sum(a => a.Duration) }).ToList(); @@ -144,8 +186,7 @@ namespace AsbCloudInfrastructure.Services var timezoneOffset = telemetryService.GetTimezoneOffsetByTelemetryId(telemetry.Id); var operations = (from a in db.TelemetryAnalysis - where a.IdTelemetry == telemetry.Id && - a.IdOperation != null + where a.IdTelemetry == telemetry.Id join o in db.Operations on a.IdOperation equals o.Id group a by new { diff --git a/AsbCloudWebApi/Controllers/AnalyticsController.cs b/AsbCloudWebApi/Controllers/AnalyticsController.cs index a7a79fa6..a1943a95 100644 --- a/AsbCloudWebApi/Controllers/AnalyticsController.cs +++ b/AsbCloudWebApi/Controllers/AnalyticsController.cs @@ -26,20 +26,26 @@ namespace AsbCloudWebApi.Controllers /// Возвращает список операций на скважине за все время /// /// id скважины + /// список категорий + /// дата начала + /// окончание + /// для пагинации кол-во записей пропустить + /// для пагинации кол-во записей /// Список операций на скважине за все время [HttpGet] - [Route("{wellId}/operations")] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public IActionResult GetOperations(int wellId) + [Route("{wellId}/operationsByWell")] + [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] + public IActionResult GetOperationsByWell(int wellId, int skip = 0, int take = 32, + [FromQuery] IEnumerable categoryIds = default, DateTime begin = default, DateTime end = default) { int? idCompany = User.GetCompanyId(); if (idCompany is null || !wellService.CheckWellOwnership((int)idCompany, wellId)) return Forbid(); - var analytics = analyticsService.GetOperations(wellId); + var analytics = analyticsService.GetOperationsByWell(wellId, categoryIds, begin, end, skip, take); - if (analytics is null || !analytics.Any()) + if (analytics is null || analytics.Count == 0) return NoContent(); return Ok(analytics);