From 00d489a849ae415281f32ea25cd1c2a81ecc249d Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 10 Oct 2024 15:29:27 +0500 Subject: [PATCH] CsvSerializer --- .../AsbCloudInfrastructure.Tests.csproj | 1 + .../DataSaubStatDtillingQualityServiceTest.cs | 210 +++++++++++------- .../DataSaubStat/TelemetryDataSaubMap.cs | 60 +++++ .../Services/DataSaubStat/test.csv | 12 + .../Services/SAUB/CsvSerializer.cs | 2 + 5 files changed, 205 insertions(+), 80 deletions(-) create mode 100644 AsbCloudInfrastructure.Tests/Services/DataSaubStat/TelemetryDataSaubMap.cs create mode 100644 AsbCloudInfrastructure.Tests/Services/DataSaubStat/test.csv diff --git a/AsbCloudInfrastructure.Tests/AsbCloudInfrastructure.Tests.csproj b/AsbCloudInfrastructure.Tests/AsbCloudInfrastructure.Tests.csproj index 9d052820..a9cf66be 100644 --- a/AsbCloudInfrastructure.Tests/AsbCloudInfrastructure.Tests.csproj +++ b/AsbCloudInfrastructure.Tests/AsbCloudInfrastructure.Tests.csproj @@ -17,6 +17,7 @@ + diff --git a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs index 17700f54..99818138 100644 --- a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs +++ b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/DataSaubStatDtillingQualityServiceTest.cs @@ -4,11 +4,17 @@ using AsbCloudApp.Data.SAUB; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; +using AsbCloudDb.Model; using AsbCloudInfrastructure.Services; +using AsbCloudInfrastructure.Services.SAUB; +using CsvHelper; using Mapster; using NSubstitute; using System; using System.Collections.Generic; +using System.Formats.Asn1; +using System.Globalization; +using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -18,6 +24,7 @@ namespace AsbCloudInfrastructure.Tests.Services; public class DataSaubStatDtillingQualityServiceTest { + private const string UsingTemplateFile = "AsbCloudInfrastructure/Tests/Services/DataSaubStat"; private readonly int Gap = 5; private readonly IDataSaubStatRepository dataSaubStatRepositoryMock = Substitute.For>(); private readonly ITelemetryDataCache telemetryDataCacheMock = Substitute.For>(); @@ -142,96 +149,132 @@ public class DataSaubStatDtillingQualityServiceTest [Fact] public async Task Create_1DataSaubStatItems_ShouldReturn__Success() { - var telemetrySaubDto = telemetryDataSaubDtos.FirstOrDefault(); - if (telemetrySaubDto != null) + var telemetryDataSaubDtos2 = new List(); + string fileName = @"C:\Users\admin\source\repos\AsbCloudServer\AsbCloudInfrastructure.Tests\Services\DataSaubStat\test.csv"; + using (var reader = new StreamReader(fileName)) + using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture)) { - //idFeedRegulator depthWell - //1 800 - //1 900 - //1 1000 - //1 900 - //1 1200 - //1 1300 - //1 1100 - //1 1500 - //2 1600 - //Ожидаемые результаты для idFeedRegulator: - // количество возвращаемых записей = 3 - // общая глубина = (1000 - 800) + (1300 - 900) + (1600 - 1100) = 1100 - var telemetrySaubDto1 = CreateTelemetryDataSaubItem( - telemetrySaubDto, - wellDepth: 900, - bitDepth: 900, - idFeedRegulator: 1, - dateTime: DateTime.UtcNow.AddMinutes(20)); - telemetryDataSaubDtos.Add(telemetrySaubDto1); + try + { + csv.Context.RegisterClassMap(); + var records = csv.GetRecords().ToList(); - var telemetrySaubDto2 = CreateTelemetryDataSaubItem( - telemetrySaubDto, - wellDepth: 1000, - bitDepth: 1000, - idFeedRegulator: 1, - dateTime: DateTime.UtcNow.AddMinutes(20)); - telemetryDataSaubDtos.Add(telemetrySaubDto2); - var telemetrySaubDto3 = CreateTelemetryDataSaubItem( - telemetrySaubDto, - wellDepth: 900, - bitDepth: 900, - idFeedRegulator: 1, - dateTime: DateTime.UtcNow.AddMinutes(20)); - telemetryDataSaubDtos.Add(telemetrySaubDto3); - var telemetrySaubDto4 = CreateTelemetryDataSaubItem( - telemetrySaubDto, - wellDepth: 1200, - bitDepth: 1200, - idFeedRegulator: 1, - dateTime: DateTime.UtcNow.AddMinutes(20)); - telemetryDataSaubDtos.Add(telemetrySaubDto4); + dataSaubServiceMock + .Get(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) + .Returns(records); - var telemetrySaubDto5 = CreateTelemetryDataSaubItem( - telemetrySaubDto, - wellDepth: 1300, - bitDepth: 1300, - idFeedRegulator: 1, - dateTime: DateTime.UtcNow.AddMinutes(20)); - telemetryDataSaubDtos.Add(telemetrySaubDto5); + //act + var result = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None); - var telemetrySaubDto6 = CreateTelemetryDataSaubItem( - telemetrySaubDto, - wellDepth: 1100, - bitDepth: 1100, - idFeedRegulator: 1, - dateTime: DateTime.UtcNow.AddMinutes(20)); - telemetryDataSaubDtos.Add(telemetrySaubDto6); - - var telemetrySaubDto7 = CreateTelemetryDataSaubItem( - telemetrySaubDto, - wellDepth: 1500, - bitDepth: 1500, - idFeedRegulator: 1, - dateTime: DateTime.UtcNow.AddMinutes(20)); - telemetryDataSaubDtos.Add(telemetrySaubDto7); - - var telemetrySaubDto8 = CreateTelemetryDataSaubItem( - telemetrySaubDto, - wellDepth: 1600, - bitDepth: 1600, - idFeedRegulator: 2, - dateTime: DateTime.UtcNow.AddMinutes(20)); - telemetryDataSaubDtos.Add(telemetrySaubDto8); + //assert + Assert.Equal(3, result.Count()); + } + catch (Exception ex) + { + ; + } } - dataSaubServiceMock - .Get(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()) - .Returns(telemetryDataSaubDtos); + //var telemetrySaubDto = telemetryDataSaubDtos.FirstOrDefault(); + //if (telemetrySaubDto != null) + //{ + // //idFeedRegulator depthWell + // //1 800 + // //1 900 + // //1 1000 + // //1 900 + // //1 1200 + // //1 1300 + // //1 1100 + // //1 1500 + // //2 1600 + // //Ожидаемые результаты для idFeedRegulator: + // // количество возвращаемых записей = 3 + // // общая глубина = (1000 - 800) + (1300 - 900) + (1600 - 1100) = 1100 + // var telemetrySaubDto1 = CreateTelemetryDataSaubItem( + // telemetrySaubDto, + // wellDepth: 900, + // bitDepth: 900, + // idFeedRegulator: 1, + // dateTime: DateTime.UtcNow.AddMinutes(20)); + // telemetryDataSaubDtos.Add(telemetrySaubDto1); - //act - var result = await dataSaubStatService.CreateStatDrillingQualityForTelemetry(1, DateTimeOffset.UnixEpoch, CancellationToken.None); + // var telemetrySaubDto2 = CreateTelemetryDataSaubItem( + // telemetrySaubDto, + // wellDepth: 1000, + // bitDepth: 1000, + // idFeedRegulator: 1, + // dateTime: DateTime.UtcNow.AddMinutes(20)); + // telemetryDataSaubDtos.Add(telemetrySaubDto2); - //assert - Assert.Equal(3, result.Count()); + // var telemetrySaubDto3 = CreateTelemetryDataSaubItem( + // telemetrySaubDto, + // wellDepth: 900, + // bitDepth: 900, + // idFeedRegulator: 1, + // dateTime: DateTime.UtcNow.AddMinutes(20)); + // telemetryDataSaubDtos.Add(telemetrySaubDto3); + + // var telemetrySaubDto4 = CreateTelemetryDataSaubItem( + // telemetrySaubDto, + // wellDepth: 1200, + // bitDepth: 1200, + // idFeedRegulator: 1, + // dateTime: DateTime.UtcNow.AddMinutes(20)); + // telemetryDataSaubDtos.Add(telemetrySaubDto4); + + // var telemetrySaubDto5 = CreateTelemetryDataSaubItem( + // telemetrySaubDto, + // wellDepth: 1300, + // bitDepth: 1300, + // idFeedRegulator: 1, + // dateTime: DateTime.UtcNow.AddMinutes(20)); + // telemetryDataSaubDtos.Add(telemetrySaubDto5); + + // var telemetrySaubDto6 = CreateTelemetryDataSaubItem( + // telemetrySaubDto, + // wellDepth: 1100, + // bitDepth: 1100, + // idFeedRegulator: 1, + // dateTime: DateTime.UtcNow.AddMinutes(20)); + // telemetryDataSaubDtos.Add(telemetrySaubDto6); + + // var telemetrySaubDto7 = CreateTelemetryDataSaubItem( + // telemetrySaubDto, + // wellDepth: 1500, + // bitDepth: 1500, + // idFeedRegulator: 1, + // dateTime: DateTime.UtcNow.AddMinutes(20)); + // telemetryDataSaubDtos.Add(telemetrySaubDto7); + + // var telemetrySaubDto8 = CreateTelemetryDataSaubItem( + // telemetrySaubDto, + // wellDepth: 1600, + // bitDepth: 1600, + // idFeedRegulator: 2, + // dateTime: DateTime.UtcNow.AddMinutes(20)); + // telemetryDataSaubDtos.Add(telemetrySaubDto8); + //} + + + } + + private TelemetryDataSaubDto Convert(TelemetryDataSaub src, double timezoneOffset) + { + var mapper = MapsterForUnitTests.GetMapper(); + + var res = new TelemetryDataSaubDto(); + var dto = mapper.Map(src, res); + //var telemetryUser = telemetryUserService.GetOrDefault(src.IdTelemetry, src.IdUser ?? int.MinValue); + dto.User = "dfsdf"; + //telemetryUser?.MakeDisplayName(); + dto.DateTime = src.DateTime.ToRemoteDateTime(timezoneOffset); + dto.BitDepth = src.BitDepth <= src.WellDepth + ? src.BitDepth + : src.WellDepth; + return dto; } @@ -294,4 +337,11 @@ public class DataSaubStatDtillingQualityServiceTest // Arg.Is>(l => l.Count() == insertedDataSaubStatCount), // Arg.Any()); //} +} + +internal class TestDto +{ + public int Id { get; set; } + public string Name { get; set; } + public int Age { get; set; } } \ No newline at end of file diff --git a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/TelemetryDataSaubMap.cs b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/TelemetryDataSaubMap.cs new file mode 100644 index 00000000..aa23cf94 --- /dev/null +++ b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/TelemetryDataSaubMap.cs @@ -0,0 +1,60 @@ +using AsbCloudApp.Data.SAUB; +using CsvHelper.Configuration; +using AsbCloudInfrastructure.Services; +using CsvHelper.Configuration.Attributes; +using Mapster; +using Microsoft.EntityFrameworkCore; +using System; +using System.Text.Json.Serialization; + +namespace AsbCloudDb.Model +{ + public class TelemetryDataSaubMap : ClassMap + { + public TelemetryDataSaubMap() + { + Map(m => m.AxialLoad).Name("axial_load"); + Map(m => m.AxialLoadLimitMax).Name("axial_load_limit_max"); + Map(m => m.AxialLoadSp).Name("axial_load_sp"); + Map(m => m.BitDepth).Name("bit_depth"); + Map(m => m.BlockPosition).Name("block_position"); + Map(m => m.BlockPositionMax).Name("block_position_max"); + Map(m => m.BlockPositionMin).Name("block_position_min"); + Map(m => m.BlockSpeed).Name("block_speed"); + Map(m => m.BlockSpeedSp).Name("block_speed_sp"); + Map(m => m.BlockSpeedSpDevelop).Name("block_speed_sp_develop"); + Map(m => m.BlockSpeedSpRotor).Name("block_speed_sp_rotor"); + Map(m => m.BlockSpeedSpSlide).Name("block_speed_sp_slide"); + Map(m => m.DateTime).Name("date"); + Map(m => m.Flow).Name("flow"); + Map(m => m.FlowDeltaLimitMax).Name("flow_delta_limit_max"); + Map(m => m.FlowIdle).Name("flow_idle"); + Map(m => m.HookWeight).Name("hook_weight"); + Map(m => m.HookWeightIdle).Name("hook_weight_idle"); + Map(m => m.HookWeightLimitMax).Name("hook_weight_limit_max"); + Map(m => m.HookWeightLimitMin).Name("hook_weight_limit_min"); + Map(m => m.IdFeedRegulator).Name("id_feed_regulator"); + Map(m => m.IdTelemetry).Name("id_telemetry"); + Map(m => m.Mode).Name("mode"); + Map(m => m.Mse).Name("mse"); + Map(m => m.MseState).Name("mse_state"); + Map(m => m.Pressure).Name("pressure"); + Map(m => m.PressureDeltaLimitMax).Name("pressure_delta_limit_max"); + Map(m => m.PressureIdle).Name("pressure_idle"); + Map(m => m.PressureSp).Name("pressure_sp"); + Map(m => m.PressureSpDevelop).Name("pressure_sp_develop"); + Map(m => m.PressureSpRotor).Name("pressure_sp_rotor"); + Map(m => m.PressureSpSlide).Name("pressure_sp_slide"); + Map(m => m.Pump1Flow).Name("pump1_flow"); + Map(m => m.Pump0Flow).Name("pump0_flow"); + Map(m => m.Pump2Flow).Name("pump2_flow"); + Map(m => m.RotorSpeed).Name("rotor_speed"); + Map(m => m.RotorTorque).Name("rotor_torque"); + Map(m => m.RotorTorqueIdle).Name("rotor_torque_idle"); + Map(m => m.RotorTorqueLimitMax).Name("rotor_torque_limit_max"); + Map(m => m.RotorTorqueSp).Name("rotor_torque_sp"); + Map(m => m.WellDepth).Name("well_depth"); + + } + } +} diff --git a/AsbCloudInfrastructure.Tests/Services/DataSaubStat/test.csv b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/test.csv new file mode 100644 index 00000000..2b765dd9 --- /dev/null +++ b/AsbCloudInfrastructure.Tests/Services/DataSaubStat/test.csv @@ -0,0 +1,12 @@ +"id_telemetry","date","mode","id_user","well_depth","bit_depth","block_position","block_position_min","block_position_max","block_speed","block_speed_sp","block_speed_sp_rotor","block_speed_sp_slide","block_speed_sp_develop","pressure","pressure_idle","pressure_sp","pressure_sp_rotor","pressure_sp_slide","pressure_sp_develop","pressure_delta_limit_max","axial_load","axial_load_sp","axial_load_limit_max","hook_weight","hook_weight_idle","hook_weight_limit_min","hook_weight_limit_max","rotor_torque","rotor_torque_idle","rotor_torque_sp","rotor_torque_limit_max","rotor_speed","flow","flow_idle","flow_delta_limit_max","id_feed_regulator","mse_state","mse","pump0_flow","pump1_flow","pump2_flow" +419,2023-02-24 06:52:03.000 +0500,1,1,46.0,46.0,18.561,0.0,0.0,0,100.0,100.0,50.0,100.0,3.2787943,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.296837,25.0,0.0,0.0,0.9,0.0,24.0,28.0,20.1,0.0,0.0,3.0,1,0,0.62367916,,, +419,2023-02-24 06:52:27.000 +0500,1,1,46.0,46.0,18.561,0.0,0.0,0.0,100.0,100.0,50.0,100.0,3.2657084,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.288881,25.0,0.0,0.0,0.7005794,0.0,24.0,28.0,20.1,0.0,0.0,3.0,1,0,0.58552027,,, +419,2023-02-24 06:52:01.000 +0500,1,1,46.0,46.0,18.561,0.0,0.0,0,100.0,100.0,50.0,100.0,3.281889,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.290037,25.0,0.0,0.0,1.1,0.0,24.0,28.0,20.0,0.0,0.0,3.0,1,0,0.6230831,,, +419,2023-02-24 06:52:02.000 +0500,1,1,46.0,46.0,18.561,0.0,0.0,0,100.0,100.0,50.0,100.0,3.2758698,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.298437,25.0,0.0,0.0,0.8,0.0,24.0,28.0,20.1,0.0,0.0,3.0,1,0,0.61103106,,, +419,2023-02-24 06:52:04.000 +0500,0,1,46.0,46.0,18.561,0.0,0.0,0,100.0,100.0,50.0,100.0,3.2403529,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.283327,25.0,0.0,0.0,0.99897236,0.0,24.0,28.0,20.1,0.0,0.0,3.0,1,0,0.5451977,,, +419,2023-02-24 06:52:28.000 +0500,1,1,46.0,46.0,18.561,0.0,0.0,0.0,100.0,100.0,50.0,100.0,3.2521386,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.271387,25.0,0.0,0.0,0.9986298,0.0,24.0,28.0,20.1,0.0,0.0,3.0,1,0,0.5644083,,, +419,2023-02-24 06:52:29.000 +0500,0,1,46.0,46.0,18.559,0.0,0.0,0.0,100.0,100.0,50.0,100.0,3.2965276,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.279978,25.0,0.0,0.0,0.7880506,0.0,24.0,28.0,20.1,0.0,0.0,3.0,1,0,0.6650865,,, +419,2023-02-24 06:52:49.000 +0500,1,1,46.0,46.0,18.347,0.0,0.0,0.02323297,100.0,100.0,50.0,100.0,3.3215039,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.196453,25.0,0.0,0.0,0.8,0.0,24.0,28.0,20.1,0.0,0.0,3.0,1,0,0.7197559,,, +419,2023-02-24 06:52:50.000 +0500,1,1,46.0,46.0,18.347,0.0,0.0,0.008542753,100.0,100.0,50.0,100.0,3.2545505,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.18471,25.0,0.0,0.0,0.8,0.0,24.0,28.0,20.1,0.0,0.0,3.0,1,0,0.5640507,,, +419,2023-02-24 06:52:53.000 +0500,1,1,46.0,46.0,18.207,0.0,0.0,11.310025,100.0,100.0,50.0,100.0,3.271237,3.0,35.0,35.0,21.0,18.0,45.0,0.0,4.0,4.5,25.770308,25.0,0.0,0.0,0.99922925,0.0,24.0,28.0,20.1,0.0,0.0,3.0,1,0,0.60237646,,, +419,2023-02-24 15:29:12.000 +0500,1,1,251.28,251.28,11.062,0.5,0.0,52.383633,53.0,53.0,50.0,50.0,46.114864,32.0,68.0,68.0,50.0,47.0,45.0,0.5221672,8.0,11.0,28.4907,29.0,0.0,0.0,5.401707,0.0,24.0,28.0,19.8,0.0,0.0,3.0,1,0,26.786428,,, diff --git a/AsbCloudInfrastructure/Services/SAUB/CsvSerializer.cs b/AsbCloudInfrastructure/Services/SAUB/CsvSerializer.cs index cc92f6bc..b117aaed 100644 --- a/AsbCloudInfrastructure/Services/SAUB/CsvSerializer.cs +++ b/AsbCloudInfrastructure/Services/SAUB/CsvSerializer.cs @@ -49,6 +49,8 @@ public class CsvSerializer HandleRow(props.Select(p => CsvSerializer.Escape(Fromat(p.GetValue(item))))); } + + private string Fromat(object? obj) { if (obj is double objDouble)