CS2-24: Добавлены методы получения данных "глубина-день" и "скорость проходки-интервал".

This commit is contained in:
KharchenkoVV 2021-06-17 15:12:39 +05:00
parent 7069e8af24
commit 70dca51987
6 changed files with 203 additions and 3 deletions

View File

@ -0,0 +1,11 @@
using System;
namespace AsbCloudApp.Data
{
public class WellDepthToDayDto
{
public double? WellDepth { get; set; }
public double? BitDepth { get; set; }
public DateTime Date { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System;
namespace AsbCloudApp.Data
{
public class WellDepthToIntervalDto
{
public DateTime IntervalStartDate { get; set; }
public double IntervalDepthProgress { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System.Collections.Generic;
using AsbCloudApp.Data;
namespace AsbCloudApp.Services
{
public interface IAnalyticsService
{
IEnumerable<WellDepthToDayDto> GetWellDepthToDayData(int wellId);
IEnumerable<WellDepthToIntervalDto> GetWellDepthToIntervalData(int wellId,
double interval = 24, int beginHour = 8, int beginMinutes = 0);
}
}

View File

@ -34,6 +34,7 @@ namespace AsbCloudInfrastructure
services.AddTransient<IEventService, EventService>();
services.AddTransient<ITelemetryUserService, TelemetryUserService>();
services.AddTransient<IReportService, ReportService>();
services.AddTransient<IAnalyticsService, AnalyticsService>();
return services;
}

View File

@ -0,0 +1,102 @@
using System;
using System.Linq;
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using System.Collections.Generic;
namespace AsbCloudInfrastructure.Services
{
public class AnalyticsService : IAnalyticsService
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
public AnalyticsService(IAsbCloudDbContext db, ITelemetryService telemetryService)
{
this.db = db;
this.telemetryService = telemetryService;
}
public IEnumerable<WellDepthToDayDto> GetWellDepthToDayData(int wellId)
{
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
var depthToTimeData = (from d in db.DataSaubBases
where d.IdTelemetry == telemetry.Id
select new WellDepthToDayDto
{
WellDepth = d.WellDepth,
BitDepth =d.BitDepth,
Date = d.Date
}).ToList();
var m = (int)Math.Round(1d * depthToTimeData.Count / 2048);
if (m > 1)
depthToTimeData = depthToTimeData.Where((d, i) => i % m == 0).ToList();
return depthToTimeData;
}
public IEnumerable<WellDepthToIntervalDto> GetWellDepthToIntervalData(int wellId,
double interval = 24.0, int beginHour = 8, int beginMinutes = 0)
{
var intervalHours = TimeSpan.FromHours(interval);
var parseResult = TimeSpan.TryParse($"{beginHour}:{beginMinutes}", out var workDayBeginTime);
if (!parseResult)
workDayBeginTime = TimeSpan.FromHours(8);
var telemetry = telemetryService.GetTelemetryByWellId(wellId);
if (telemetry is null)
return null;
var drillingData = (from d in db.DataSaubBases
where d.IdTelemetry == telemetry.Id
select new
{
d.WellDepth,
d.Date
}).ToList();
if (!drillingData.Any())
return null;
var drillingStartDate = drillingData.First().Date;
var periodStart = drillingStartDate.Hour < 8
? new DateTime(drillingStartDate.Year, drillingStartDate.Month, drillingStartDate.Day).AddDays(-1) + workDayBeginTime
: new DateTime(drillingStartDate.Year, drillingStartDate.Month, drillingStartDate.Day) + workDayBeginTime;
var periodEnd = periodStart + intervalHours;
var onePeriodData = new List<(double?, DateTime)>();
var drillingPeriods = new List<IEnumerable<(double?, DateTime)>>();
foreach (var d in drillingData)
{
if (d.Date < periodEnd)
{
onePeriodData.Add((d.WellDepth, d.Date));
continue;
}
drillingPeriods.Add(onePeriodData);
onePeriodData.Clear();
periodStart = d.Date;
periodEnd = periodStart + intervalHours;
onePeriodData.Add((d.WellDepth, d.Date));
}
var wellDepthToIntervalData = drillingPeriods.Select(d => new WellDepthToIntervalDto
{
IntervalStartDate = d.FirstOrDefault().Item2,
IntervalDepthProgress = (d.Last().Item1 - d.FirstOrDefault().Item1) ?? 0.0 / interval
});
return wellDepthToIntervalData;
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using AsbCloudApp.Data;
using System.Collections.Generic;
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
@ -11,6 +12,61 @@ namespace AsbCloudWebApi.Controllers
[Authorize]
public class AnalyticsController : ControllerBase
{
private readonly IAnalyticsService analyticsService;
private readonly IWellService wellService;
public AnalyticsController(IAnalyticsService analyticsService, IWellService wellService)
{
this.analyticsService = analyticsService;
this.wellService = wellService;
}
/// <summary>
/// Возвращает данные по скважине "глубина-день"
/// </summary>
/// <param name="wellId">id скважины</param>
/// <returns>Коллекцию данных по скважине "глубина-день"</returns>
[HttpGet]
[Route("{wellId}/wellDepthToDay")]
[ProducesResponseType(typeof(List<WellDepthToDayDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetWellDepthToDay(int wellId)
{
int? idCustomer = User.GetCustomerId();
if (idCustomer is null)
return BadRequest();
if (!wellService.CheckWellOwnership((int)idCustomer, wellId))
return Forbid();
var wellDepthToDayData = analyticsService.GetWellDepthToDayData(wellId);
return Ok(wellDepthToDayData);
}
/// <summary>
/// Возвращает данные по глубине скважины за период
/// </summary>
/// <param name="wellId">id скважины</param>
/// <returns>Коллекцию данных по глубине скважины за период</returns>
[HttpGet]
[Route("{wellId}/wellDepthToInterval")]
[ProducesResponseType(typeof(List<WellDepthToIntervalDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetWellDepthToInterval(int wellId)
{
int? idCustomer = User.GetCustomerId();
if (idCustomer is null)
return BadRequest();
if (!wellService.CheckWellOwnership((int)idCustomer, wellId))
return Forbid();
var wellDepthToIntervalData = analyticsService.GetWellDepthToIntervalData(wellId);
return Ok(wellDepthToIntervalData);
}
/// <summary>
/// Возвращает данные по операциям на скважине за период
/// </summary>
@ -19,10 +75,18 @@ namespace AsbCloudWebApi.Controllers
/// <param name="end">дата окончания интервала</param>
/// <returns>Коллекцию операций на скважине</returns>
[HttpGet]
[Route("{wellId}/get")]
[Route("{wellId}/operationsToTime")]
[ProducesResponseType(typeof(List<OperationPercentageDto>), (int)System.Net.HttpStatusCode.OK)]
public IActionResult GetAnalytics(int wellId, DateTime begin = default, DateTime end = default)
public IActionResult GetOperationsToTime(int wellId, DateTime begin = default, DateTime end = default)
{
int? idCustomer = User.GetCustomerId();
if (idCustomer is null)
return BadRequest();
if (!wellService.CheckWellOwnership((int)idCustomer, wellId))
return Forbid();
var analytics = new List<OperationPercentageDto>
{
new OperationPercentageDto { ProcessName = "Роторное бурение", Percentage = 19.7 },