using AsbCloudApp.Data.SAUB; using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudInfrastructure.Services.SAUB { #nullable enable public class TelemetryDataSaubService : TelemetryDataBaseService, ITelemetryDataSaubService { private readonly ITelemetryUserService telemetryUserService; public TelemetryDataSaubService( IAsbCloudDbContext db, ITelemetryService telemetryService, ITelemetryUserService telemetryUserService, TelemetryDataCache telemetryDataCache) : base(db, telemetryService, telemetryDataCache) { this.telemetryUserService = telemetryUserService; } public async Task> GetTelemetryDataStatAsync(int idTelemetry, CancellationToken token) { var timezone = telemetryService.GetTimezone(idTelemetry); var timezoneOffset = TimeSpan.FromHours(timezone.Hours); var query = db.Set() .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(), DateMin = DateTime.SpecifyKind(g.Min(t => t.DateTime.UtcDateTime) + timezoneOffset, DateTimeKind.Unspecified), DateMax = DateTime.SpecifyKind(g.Max(t => t.DateTime.UtcDateTime) + timezoneOffset, DateTimeKind.Unspecified), 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), PressureDelta = g.Average(t => t.Pressure!.Value - t.PressureIdle!.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); } public override TelemetryDataSaub Convert(TelemetryDataSaubDto src, double timezoneOffset) { var entity = src.Adapt(); var telemetryUser = telemetryUserService .GetUsers(src.IdTelemetry, u => (u.Name == src.User || u.Surname == src.User)) .FirstOrDefault(); entity.IdUser = telemetryUser?.Id; entity.DateTime = src.DateTime.ToUtcDateTimeOffset(timezoneOffset); return entity; } public override TelemetryDataSaubDto Convert(TelemetryDataSaub src, double timezoneOffset) { var dto = src.Adapt(); var telemetryUser = telemetryUserService.GetOrDefault(src.IdTelemetry, src.IdUser??int.MinValue); dto.User = telemetryUser?.MakeDisplayName(); dto.DateTime = src.DateTime.ToRemoteDateTime(timezoneOffset); dto.BitDepth = src.BitDepth <= src.WellDepth ? src.BitDepth : src.WellDepth; return dto; } } #nullable disable }