CS2-34: Добавлена пагинация в метод, возвращающий список событий на скважине

This commit is contained in:
KharchenkoVV 2021-07-21 16:49:24 +05:00
parent beab5a39a1
commit e447473a71
6 changed files with 90 additions and 40 deletions

View File

@ -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; }
}
}

View File

@ -2,7 +2,7 @@
{
public class OperationDurationDto
{
public string ProcessName { get; set; }
public string OperationName { get; set; }
public double Duration { get; set; }
}
}

View File

@ -7,7 +7,9 @@ namespace AsbCloudApp.Services
{
public interface IAnalyticsService
{
IEnumerable<OperationDto> GetOperations(int wellId);
PaginationContainer<OperationDto> GetOperationsByWell(int wellId,
IEnumerable<int> categoryids = default, DateTime begin = default,
DateTime end = default, int skip = 0, int take = 32);
IEnumerable<WellDepthToDayDto> GetWellDepthToDay(int wellId);
IEnumerable<WellDepthToIntervalDto> GetWellDepthToInterval(int wellId,
int intervalHoursTimestamp, int workBeginTimestamp);

View File

@ -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; }

View File

@ -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<OperationDto> 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<WellDepthToDayDto> GetWellDepthToDay(int wellId)
{
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
@ -104,6 +83,70 @@ namespace AsbCloudInfrastructure.Services
return wellDepthToIntervalData;
}
public PaginationContainer<OperationDto> GetOperationsByWell(int wellId,
IEnumerable<int> 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<OperationDto>
{
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<OperationDurationDto> 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
{

View File

@ -26,20 +26,26 @@ namespace AsbCloudWebApi.Controllers
/// Возвращает список операций на скважине за все время
/// </summary>
/// <param name="wellId">id скважины</param>
/// <param name="categoryIds">список категорий</param>
/// <param name="begin">дата начала</param>
/// <param name="end">окончание</param>
/// <param name="skip">для пагинации кол-во записей пропустить</param>
/// <param name="take">для пагинации кол-во записей </param>
/// <returns>Список операций на скважине за все время</returns>
[HttpGet]
[Route("{wellId}/operations")]
[ProducesResponseType(typeof(IEnumerable<OperationDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetOperations(int wellId)
[Route("{wellId}/operationsByWell")]
[ProducesResponseType(typeof(PaginationContainer<OperationDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetOperationsByWell(int wellId, int skip = 0, int take = 32,
[FromQuery] IEnumerable<int> 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);