2022-04-08 13:10:06 +05:00
|
|
|
|
using AsbCloudApp.Data.SAUB;
|
2021-09-17 16:24:01 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using Mapster;
|
2023-01-13 14:34:26 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-10-06 13:49:20 +05:00
|
|
|
|
using System.Linq;
|
2023-01-13 14:34:26 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-09-17 16:24:01 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.SAUB
|
2021-09-17 16:24:01 +05:00
|
|
|
|
{
|
2022-11-18 12:58:53 +05:00
|
|
|
|
#nullable enable
|
2023-01-13 14:34:26 +05:00
|
|
|
|
public class TelemetryDataSaubService : TelemetryDataBaseService<TelemetryDataSaubDto, TelemetryDataSaub>, ITelemetryDataSaubService
|
2021-09-17 16:24:01 +05:00
|
|
|
|
{
|
2022-11-18 12:58:53 +05:00
|
|
|
|
private readonly ITelemetryUserService telemetryUserService;
|
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
public TelemetryDataSaubService(
|
|
|
|
|
IAsbCloudDbContext db,
|
|
|
|
|
ITelemetryService telemetryService,
|
2022-11-18 12:58:53 +05:00
|
|
|
|
ITelemetryUserService telemetryUserService,
|
|
|
|
|
TelemetryDataCache<TelemetryDataSaubDto> telemetryDataCache)
|
|
|
|
|
: base(db, telemetryService, telemetryDataCache)
|
|
|
|
|
{
|
|
|
|
|
this.telemetryUserService = telemetryUserService;
|
|
|
|
|
}
|
2021-09-17 16:24:01 +05:00
|
|
|
|
|
2023-01-13 14:34:26 +05:00
|
|
|
|
public async Task<IEnumerable<TelemetryDataSaubStatDto>> GetTelemetryDataStatAsync(int idTelemetry, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var timezone = telemetryService.GetTimezone(idTelemetry);
|
|
|
|
|
var timezoneOffset = TimeSpan.FromHours(timezone.Hours);
|
|
|
|
|
|
|
|
|
|
var query = db.Set<TelemetryDataSaub>()
|
|
|
|
|
.Where(t => t.IdTelemetry == idTelemetry)
|
|
|
|
|
.Where(t => t.BlockPosition > 0.0001)
|
|
|
|
|
.Where(t => t.WellDepth > 0.0001)
|
|
|
|
|
.Where(t => t.WellDepth - t.BitDepth < 0.01)
|
|
|
|
|
.GroupBy(t => new { H = t.DateTime.Hour, W = Math.Truncate(t.WellDepth!.Value) })
|
|
|
|
|
.Select(g => new TelemetryDataSaubStatDto
|
|
|
|
|
{
|
|
|
|
|
Count = g.Count(),
|
|
|
|
|
|
2023-01-17 08:56:07 +05:00
|
|
|
|
DateMin = DateTime.SpecifyKind(g.Min(t => t.DateTime.UtcDateTime) + timezoneOffset, DateTimeKind.Unspecified),
|
|
|
|
|
DateMax = DateTime.SpecifyKind(g.Max(t => t.DateTime.UtcDateTime) + timezoneOffset, DateTimeKind.Unspecified),
|
2023-01-13 14:34:26 +05:00
|
|
|
|
|
|
|
|
|
WellDepthMin = g.Min(t => t.WellDepth!.Value),
|
|
|
|
|
WellDepthMax = g.Max(t => t.WellDepth!.Value),
|
|
|
|
|
|
|
|
|
|
Pressure = g.Average(t => t.Pressure!.Value),
|
|
|
|
|
PressureSp = g.Average(t => t.PressureSp!.Value),
|
|
|
|
|
PressureSpRotor = g.Average(t => t.PressureSpRotor!.Value),
|
|
|
|
|
PressureSpSlide = g.Average(t => t.PressureSpSlide!.Value),
|
|
|
|
|
PressureIdle = g.Average(t => t.PressureIdle!.Value),
|
|
|
|
|
PressureDeltaLimitMax = g.Average(t => t.PressureDeltaLimitMax!.Value),
|
|
|
|
|
|
|
|
|
|
AxialLoad = g.Average(t => t.AxialLoad!.Value),
|
|
|
|
|
AxialLoadSp = g.Average(t => t.AxialLoadSp!.Value),
|
|
|
|
|
AxialLoadLimitMax = g.Average(t => t.AxialLoadLimitMax!.Value),
|
|
|
|
|
|
|
|
|
|
RotorTorque = g.Average(t => t.RotorTorque!.Value),
|
|
|
|
|
RotorTorqueSp = g.Average(t => t.RotorTorqueSp!.Value),
|
|
|
|
|
RotorTorqueLimitMax = g.Average(t => t.RotorTorqueLimitMax!.Value),
|
|
|
|
|
|
|
|
|
|
BlockSpeed = g.Average(t => t.BlockSpeed!.Value),
|
|
|
|
|
BlockSpeedSp = g.Average(t => t.BlockSpeedSp!.Value),
|
|
|
|
|
BlockSpeedSpRotor = g.Average(t => t.BlockSpeedSpRotor!.Value),
|
|
|
|
|
BlockSpeedSpSlide = g.Average(t => t.BlockSpeedSpSlide!.Value),
|
|
|
|
|
})
|
|
|
|
|
.Where(s => s.WellDepthMin != s.WellDepthMax)
|
|
|
|
|
.Where(s => s.Count > 3)
|
|
|
|
|
.OrderBy(t => t.DateMin);
|
|
|
|
|
|
|
|
|
|
return await query.ToArrayAsync(token);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
public override TelemetryDataSaub Convert(TelemetryDataSaubDto src, double timezoneOffset)
|
2021-09-17 16:24:01 +05:00
|
|
|
|
{
|
|
|
|
|
var entity = src.Adapt<TelemetryDataSaub>();
|
2022-11-18 12:58:53 +05:00
|
|
|
|
var telemetryUser = telemetryUserService
|
|
|
|
|
.GetUsers(src.IdTelemetry, u => (u.Name == src.User || u.Surname == src.User))
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
entity.IdUser = telemetryUser?.Id;
|
2022-04-08 13:10:06 +05:00
|
|
|
|
entity.DateTime = src.DateTime.ToUtcDateTimeOffset(timezoneOffset);
|
2021-09-17 16:24:01 +05:00
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
public override TelemetryDataSaubDto Convert(TelemetryDataSaub src, double timezoneOffset)
|
2021-09-17 16:24:01 +05:00
|
|
|
|
{
|
|
|
|
|
var dto = src.Adapt<TelemetryDataSaubDto>();
|
2022-11-18 12:58:53 +05:00
|
|
|
|
var telemetryUser = telemetryUserService.GetOrDefault(src.IdTelemetry, src.IdUser??int.MinValue);
|
2021-09-23 10:53:48 +05:00
|
|
|
|
dto.User = telemetryUser?.MakeDisplayName();
|
2022-04-08 13:10:06 +05:00
|
|
|
|
dto.DateTime = src.DateTime.ToRemoteDateTime(timezoneOffset);
|
2022-11-25 15:26:45 +05:00
|
|
|
|
dto.BitDepth = src.BitDepth <= src.WellDepth
|
|
|
|
|
? src.BitDepth
|
|
|
|
|
: src.WellDepth;
|
2021-09-17 16:24:01 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-18 12:58:53 +05:00
|
|
|
|
#nullable disable
|
2021-09-17 16:24:01 +05:00
|
|
|
|
}
|