forked from ddrilling/AsbCloudServer
103 lines
3.8 KiB
C#
103 lines
3.8 KiB
C#
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|