2023-03-09 12:32:09 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Data.SAUB;
|
|
|
|
|
using AsbCloudApp.Exceptions;
|
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;
|
2023-03-07 09:12:49 +05:00
|
|
|
|
using System.IO;
|
2023-03-09 12:32:09 +05:00
|
|
|
|
using System.IO.Compression;
|
2022-10-06 13:49:20 +05:00
|
|
|
|
using System.Linq;
|
2023-03-09 12:32:09 +05:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Csv;
|
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
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
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);
|
2023-04-05 13:35:05 +05:00
|
|
|
|
int[] modes = new int[] { 0, 1, 3 };
|
2023-01-13 14:34:26 +05:00
|
|
|
|
|
|
|
|
|
var query = db.Set<TelemetryDataSaub>()
|
|
|
|
|
.Where(t => t.IdTelemetry == idTelemetry)
|
|
|
|
|
.Where(t => t.BlockPosition > 0.0001)
|
|
|
|
|
.Where(t => t.WellDepth > 0.0001)
|
2023-04-03 14:59:59 +05:00
|
|
|
|
.Where(t => t.Mode != null)
|
2023-04-07 16:49:28 +05:00
|
|
|
|
.Where(t => modes.Contains(t.Mode!.Value))
|
2023-04-05 13:36:56 +05:00
|
|
|
|
.Where(t => t.WellDepth - t.BitDepth < 0.01)
|
2023-04-03 14:59:59 +05:00
|
|
|
|
.GroupBy(t => new {
|
|
|
|
|
t.DateTime.Hour,
|
|
|
|
|
WellDepthX10 = Math.Truncate(t.WellDepth!.Value * 10),
|
|
|
|
|
t.Mode,
|
|
|
|
|
t.IdFeedRegulator})
|
2023-01-13 14:34:26 +05:00
|
|
|
|
.Select(g => new TelemetryDataSaubStatDto
|
|
|
|
|
{
|
|
|
|
|
Count = g.Count(),
|
2023-04-05 13:35:05 +05:00
|
|
|
|
IdMode = g.Key.Mode??0,
|
2023-04-03 14:59:59 +05:00
|
|
|
|
IdFeedRegulator = g.Key.IdFeedRegulator,
|
2023-01-13 14:34:26 +05:00
|
|
|
|
|
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),
|
|
|
|
|
PressureIdle = g.Average(t => t.PressureIdle!.Value),
|
|
|
|
|
PressureDeltaLimitMax = g.Average(t => t.PressureDeltaLimitMax!.Value),
|
2023-01-24 09:59:18 +05:00
|
|
|
|
PressureDelta = g.Average(t => t.Pressure!.Value - t.PressureIdle!.Value),
|
2023-05-23 13:41:27 +05:00
|
|
|
|
PressureSpDelta = g.Average(t => t.PressureSp!.Value - t.PressureIdle!.Value),
|
2023-01-13 14:34:26 +05:00
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
})
|
|
|
|
|
.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;
|
|
|
|
|
}
|
2023-03-07 09:12:49 +05:00
|
|
|
|
|
2023-03-09 12:32:09 +05:00
|
|
|
|
public async Task<Stream> GetZippedCsv(int idWell, DateTime beginDate, DateTime endDate, CancellationToken token)
|
2023-03-07 09:12:49 +05:00
|
|
|
|
{
|
2023-03-09 12:32:09 +05:00
|
|
|
|
double intervalSec = (endDate - beginDate).TotalSeconds;
|
|
|
|
|
if (intervalSec > 60*60*24*3)
|
2023-09-29 12:06:46 +05:00
|
|
|
|
throw new ArgumentInvalidException(nameof(endDate), "Слишком большой диапазон");
|
2023-03-09 12:32:09 +05:00
|
|
|
|
|
|
|
|
|
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell)
|
2023-09-29 12:06:46 +05:00
|
|
|
|
?? throw new ArgumentInvalidException(nameof(idWell), $"Скважина id:{idWell} не содержит телеметрии");
|
2023-03-09 12:32:09 +05:00
|
|
|
|
|
|
|
|
|
var approxPointsCount = intervalSec switch
|
|
|
|
|
{
|
|
|
|
|
< 2048 => 2048,
|
|
|
|
|
< 8_192 => 4_096,
|
|
|
|
|
< 131_072 => 16_384,
|
|
|
|
|
_ => 32_768
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-27 09:40:51 +05:00
|
|
|
|
var data = await GetAsync(idWell, beginDate, intervalSec, approxPointsCount, token );
|
2023-03-09 12:32:09 +05:00
|
|
|
|
|
|
|
|
|
var fileName = $"DataSaub idWell{idWell}";
|
|
|
|
|
if (telemetry.Info is not null)
|
|
|
|
|
fileName += $" {telemetry.Info?.Cluster}, {telemetry.Info?.Well}";
|
|
|
|
|
fileName += $" {beginDate:yyyy-MM-DDTHH-mm} - {endDate:yyyy-MM-DDTHH-mm}.csv";
|
|
|
|
|
|
|
|
|
|
var outStream = new MemoryStream();
|
|
|
|
|
using (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true))
|
|
|
|
|
{
|
|
|
|
|
var entryFile = archive.CreateEntry(fileName, CompressionLevel.Optimal);
|
|
|
|
|
using var entryStream = entryFile.Open();
|
|
|
|
|
var serializer = new CsvSerializer<TelemetryDataSaubDto>();
|
|
|
|
|
serializer.Serialize(data, entryStream);
|
|
|
|
|
}
|
|
|
|
|
outStream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
return outStream;
|
2023-03-07 09:12:49 +05:00
|
|
|
|
}
|
2021-09-17 16:24:01 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2021-09-17 16:24:01 +05:00
|
|
|
|
}
|