From 579b5f26da2e2243b75a42cbadfdf0ecd82881af Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Fri, 29 Apr 2022 12:27:12 +0500 Subject: [PATCH] Remove legacy telemetry analysis, except db model --- AsbCloudApp/Data/TelemetryAnalysisDto.cs | 26 - .../Data/TelemetryOperationDetailsDto.cs | 8 - AsbCloudApp/Data/TelemetryOperationDto.cs | 14 - .../Data/TelemetryOperationDurationDto.cs | 8 - AsbCloudApp/Data/TelemetryOperationInfoDto.cs | 11 - AsbCloudApp/Data/WellDepthToDayDto.cs | 11 - AsbCloudApp/Data/WellDepthToIntervalDto.cs | 10 - .../Services/ITelemetryAnalyticsService.cs | 30 - .../AsbCloudDbContextModelSnapshot.cs | 86 +-- AsbCloudInfrastructure/DependencyInjection.cs | 2 - .../Services/Analysis/DataSaubAnalyse.cs | 16 - .../Services/Analysis/InterpolationLine.cs | 49 -- .../TelemetryAnalyticsBackgroundService.cs | 72 --- .../Analysis/TelemetryAnalyticsService.cs | 533 ------------------ .../Analysis/TelemetryOperationDetector.cs | 12 - .../TelemetryOperationDetectorService.cs | 205 ------- .../SAUB/TelemetryAnalyticsController.cs | 205 ------- 17 files changed, 43 insertions(+), 1255 deletions(-) delete mode 100644 AsbCloudApp/Data/TelemetryAnalysisDto.cs delete mode 100644 AsbCloudApp/Data/TelemetryOperationDetailsDto.cs delete mode 100644 AsbCloudApp/Data/TelemetryOperationDto.cs delete mode 100644 AsbCloudApp/Data/TelemetryOperationDurationDto.cs delete mode 100644 AsbCloudApp/Data/TelemetryOperationInfoDto.cs delete mode 100644 AsbCloudApp/Data/WellDepthToDayDto.cs delete mode 100644 AsbCloudApp/Data/WellDepthToIntervalDto.cs delete mode 100644 AsbCloudApp/Services/ITelemetryAnalyticsService.cs delete mode 100644 AsbCloudInfrastructure/Services/Analysis/DataSaubAnalyse.cs delete mode 100644 AsbCloudInfrastructure/Services/Analysis/InterpolationLine.cs delete mode 100644 AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsBackgroundService.cs delete mode 100644 AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsService.cs delete mode 100644 AsbCloudInfrastructure/Services/Analysis/TelemetryOperationDetector.cs delete mode 100644 AsbCloudInfrastructure/Services/Analysis/TelemetryOperationDetectorService.cs delete mode 100644 AsbCloudWebApi/Controllers/SAUB/TelemetryAnalyticsController.cs diff --git a/AsbCloudApp/Data/TelemetryAnalysisDto.cs b/AsbCloudApp/Data/TelemetryAnalysisDto.cs deleted file mode 100644 index aa9d938d..00000000 --- a/AsbCloudApp/Data/TelemetryAnalysisDto.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace AsbCloudApp.Data -{ - public class TelemetryAnalysisDto : IId - { - public int Id { get; set; } - public int IdTelemetry { get; set; } - public int IdOperation { get; set; } - public long UnixDate { get; set; } - public int DurationSec { get; set; } - public double? OperationStartDepth { get; set; } - public double? OperationEndDepth { get; set; } - public bool IsWellDepthIncreasing { get; set; } - public bool IsWellDepthDecreasing { get; set; } - public bool IsBitPositionIncreasing { get; set; } - public bool IsBitPositionDecreasing { get; set; } - public bool IsBitPositionLt20 { get; set; } - public bool IsBlockPositionIncreasing { get; set; } - public bool IsBlockPositionDecreasing { get; set; } - public bool IsRotorSpeedLt3 { get; set; } - public bool IsRotorSpeedGt3 { get; set; } - public bool IsPressureLt20 { get; set; } - public bool IsPressureGt20 { get; set; } - public bool IsHookWeightNotChanges { get; set; } - public bool IsHookWeightLt3 { get; set; } - } -} diff --git a/AsbCloudApp/Data/TelemetryOperationDetailsDto.cs b/AsbCloudApp/Data/TelemetryOperationDetailsDto.cs deleted file mode 100644 index 2e0c6670..00000000 --- a/AsbCloudApp/Data/TelemetryOperationDetailsDto.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace AsbCloudApp.Data -{ - public class TelemetryOperationDetailsDto - { - public string OperationName { get; set; } - public int DurationSec { get; set; } - } -} diff --git a/AsbCloudApp/Data/TelemetryOperationDto.cs b/AsbCloudApp/Data/TelemetryOperationDto.cs deleted file mode 100644 index 2597d66a..00000000 --- a/AsbCloudApp/Data/TelemetryOperationDto.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -namespace AsbCloudApp.Data -{ - public class TelemetryOperationDto : IId - { - public int Id { get; set; } - public string Name { get; set; } - public DateTime BeginDate { get; set; } - public DateTime EndDate { get; set; } - public double StartWellDepth { get; set; } - public double EndWellDepth { get; set; } - } -} diff --git a/AsbCloudApp/Data/TelemetryOperationDurationDto.cs b/AsbCloudApp/Data/TelemetryOperationDurationDto.cs deleted file mode 100644 index 183dd7e8..00000000 --- a/AsbCloudApp/Data/TelemetryOperationDurationDto.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace AsbCloudApp.Data -{ - public class TelemetryOperationDurationDto - { - public string OperationName { get; set; } - public double Duration { get; set; } - } -} diff --git a/AsbCloudApp/Data/TelemetryOperationInfoDto.cs b/AsbCloudApp/Data/TelemetryOperationInfoDto.cs deleted file mode 100644 index 5ce268d7..00000000 --- a/AsbCloudApp/Data/TelemetryOperationInfoDto.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace AsbCloudApp.Data -{ - public class TelemetryOperationInfoDto - { - public DateTime IntervalBegin { get; set; } - public IList Operations { get; set; } - } -} diff --git a/AsbCloudApp/Data/WellDepthToDayDto.cs b/AsbCloudApp/Data/WellDepthToDayDto.cs deleted file mode 100644 index 2fddc8b8..00000000 --- a/AsbCloudApp/Data/WellDepthToDayDto.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace AsbCloudApp.Data -{ - public class WellDepthToDayDto - { - public double WellDepth { get; set; } - public double BitDepth { get; set; } - public DateTime Date { get; set; } - } -} diff --git a/AsbCloudApp/Data/WellDepthToIntervalDto.cs b/AsbCloudApp/Data/WellDepthToIntervalDto.cs deleted file mode 100644 index 8f6aef84..00000000 --- a/AsbCloudApp/Data/WellDepthToIntervalDto.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace AsbCloudApp.Data -{ - public class WellDepthToIntervalDto - { - public DateTime IntervalStartDate { get; set; } - public double IntervalDepthProgress { get; set; } - } -} diff --git a/AsbCloudApp/Services/ITelemetryAnalyticsService.cs b/AsbCloudApp/Services/ITelemetryAnalyticsService.cs deleted file mode 100644 index d621901f..00000000 --- a/AsbCloudApp/Services/ITelemetryAnalyticsService.cs +++ /dev/null @@ -1,30 +0,0 @@ -using AsbCloudApp.Data; -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudApp.Services -{ - public interface ITelemetryAnalyticsService - { - Task> GetOperationsByWellAsync(int idWell, - IEnumerable categoryids = default, DateTime begin = default, - DateTime end = default, int skip = 0, int take = 32, - CancellationToken token = default); - Task> GetWellDepthToDayAsync(int idWell, - CancellationToken token = default); - Task> GetWellDepthToIntervalAsync(int idWell, - int intervalHoursTimestamp, int shiftStartSec, - CancellationToken token = default); - Task> GetOperationsSummaryAsync(int idWell, - DateTime begin = default, DateTime end = default, - CancellationToken token = default); - Task> GetOperationsToIntervalAsync(int idWell, - int intervalHoursTimestamp, int workBeginTimestamp, - CancellationToken token = default); - Task AnalyzeAndSaveTelemetriesAsync(CancellationToken token = default); - Task GetOperationsDateRangeAsync(int idWell, - CancellationToken token = default); - } -} diff --git a/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs b/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs index c7ff7580..71e82b16 100644 --- a/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs +++ b/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs @@ -61,7 +61,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdDeposit"); - b.ToTable("t_cluster"); + b.ToTable("t_cluster", (string)null); b.HasComment("Кусты"); }); @@ -90,7 +90,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdCompanyType"); - b.ToTable("t_company"); + b.ToTable("t_company", (string)null); b.HasData( new @@ -117,7 +117,7 @@ namespace AsbCloudDb.Migrations b.HasKey("Id"); - b.ToTable("t_company_type"); + b.ToTable("t_company_type", (string)null); b.HasData( new @@ -166,7 +166,7 @@ namespace AsbCloudDb.Migrations b.HasKey("Id"); - b.ToTable("t_deposit"); + b.ToTable("t_deposit", (string)null); b.HasComment("Месторождение"); }); @@ -220,7 +220,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdTelemetry"); - b.ToTable("t_detected_operation"); + b.ToTable("t_detected_operation", (string)null); b.HasComment("автоматически определенные операции по телеметрии"); }); @@ -315,7 +315,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWellOperationCategory"); - b.ToTable("t_drill_flow_chart"); + b.ToTable("t_drill_flow_chart", (string)null); b.HasComment("Параметры коридоров бурения (диапазоны параметров бурения)"); }); @@ -344,7 +344,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWell", "IdFileCategory") .IsUnique(); - b.ToTable("t_drilling_program_part"); + b.ToTable("t_drilling_program_part", (string)null); b.HasComment("части программ бурения"); }); @@ -459,7 +459,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWellSectionType"); - b.ToTable("t_drill_params"); + b.ToTable("t_drill_params", (string)null); b.HasComment("Режим бурения в секции (диапазоны параметров бурения)"); }); @@ -485,7 +485,7 @@ namespace AsbCloudDb.Migrations b.HasKey("Id"); - b.ToTable("t_file_category"); + b.ToTable("t_file_category", (string)null); b.HasComment("Категории файлов"); @@ -659,7 +659,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWell"); - b.ToTable("t_file_info"); + b.ToTable("t_file_info", (string)null); b.HasComment("Файлы всех категорий"); }); @@ -710,7 +710,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdUser"); - b.ToTable("t_file_mark"); + b.ToTable("t_file_mark", (string)null); b.HasComment("Действия с файлами."); }); @@ -755,7 +755,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWell"); - b.ToTable("t_measure"); + b.ToTable("t_measure", (string)null); b.HasComment("Таблица c данными для вкладки 'Последние данные'"); }); @@ -781,7 +781,7 @@ namespace AsbCloudDb.Migrations b.HasKey("Id"); - b.ToTable("t_measure_category"); + b.ToTable("t_measure_category", (string)null); b.HasComment("Категория последних данных"); @@ -829,7 +829,7 @@ namespace AsbCloudDb.Migrations b.HasKey("Id"); - b.ToTable("t_permission"); + b.ToTable("t_permission", (string)null); b.HasComment("Разрешения на доступ к данным"); @@ -1432,7 +1432,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWell"); - b.ToTable("t_relation_company_well"); + b.ToTable("t_relation_company_well", (string)null); b.HasComment("отношение скважин и компаний"); }); @@ -1457,7 +1457,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdDrillingProgramPart"); - b.ToTable("t_relation_user_drilling_program_part"); + b.ToTable("t_relation_user_drilling_program_part", (string)null); b.HasComment("Отношение пользователей и частей ПБ"); }); @@ -1476,7 +1476,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdPermission"); - b.ToTable("t_relation_user_role_permission"); + b.ToTable("t_relation_user_role_permission", (string)null); b.HasComment("Отношение ролей пользователей и разрешений доступа"); @@ -2058,7 +2058,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdInclude"); - b.ToTable("t_relation_user_role_user_role"); + b.ToTable("t_relation_user_role_user_role", (string)null); b.HasComment("Отношение ролей к ролям"); @@ -2349,7 +2349,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdUserRole"); - b.ToTable("t_relation_user_user_role"); + b.ToTable("t_relation_user_user_role", (string)null); b.HasComment("Отношение пользователей и ролей"); @@ -2405,7 +2405,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWell"); - b.ToTable("t_report_property"); + b.ToTable("t_report_property", (string)null); b.HasComment("Отчеты с данными по буровым"); }); @@ -2459,7 +2459,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWell"); - b.ToTable("t_setpoints_rquest"); + b.ToTable("t_setpoints_rquest", (string)null); b.HasComment("Запросы на изменение уставок панели оператора"); }); @@ -2492,7 +2492,7 @@ namespace AsbCloudDb.Migrations b.HasIndex(new[] { "RemoteUid" }, "t_telemetry_remote_uid_index"); - b.ToTable("t_telemetry"); + b.ToTable("t_telemetry", (string)null); b.HasComment("таблица привязки телеметрии от комплектов к конкретной скважине."); }); @@ -2605,7 +2605,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdTelemetry"); - b.ToTable("t_telemetry_analysis"); + b.ToTable("t_telemetry_analysis", (string)null); b.HasComment("События на скважине"); }); @@ -2808,7 +2808,7 @@ namespace AsbCloudDb.Migrations b.HasKey("IdTelemetry", "DateTime"); - b.ToTable("t_telemetry_data_saub"); + b.ToTable("t_telemetry_data_saub", (string)null); b.HasComment("набор основных данных по SAUB"); }); @@ -3120,7 +3120,7 @@ namespace AsbCloudDb.Migrations b.HasKey("IdTelemetry", "DateTime"); - b.ToTable("t_telemetry_data_spin"); + b.ToTable("t_telemetry_data_spin", (string)null); b.HasComment("набор основных данных по SpinMaster"); }); @@ -3145,7 +3145,7 @@ namespace AsbCloudDb.Migrations b.HasKey("IdTelemetry", "IdEvent"); - b.ToTable("t_telemetry_event"); + b.ToTable("t_telemetry_event", (string)null); b.HasComment("Справочник событий. События формируют сообщения. Разделено по версиям посылок от телеметрии."); }); @@ -3205,7 +3205,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdTelemetry"); - b.ToTable("t_telemetry_message"); + b.ToTable("t_telemetry_message", (string)null); b.HasComment("Сообщения на буровых"); }); @@ -3241,7 +3241,7 @@ namespace AsbCloudDb.Migrations b.HasKey("IdTelemetry", "IdUser"); - b.ToTable("t_telemetry_user"); + b.ToTable("t_telemetry_user", (string)null); b.HasComment("Пользователи панели САУБ. Для сообщений."); }); @@ -3318,7 +3318,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("Login") .IsUnique(); - b.ToTable("t_user"); + b.ToTable("t_user", (string)null); b.HasComment("Пользователи облака"); @@ -3355,7 +3355,7 @@ namespace AsbCloudDb.Migrations b.HasKey("Id"); - b.ToTable("t_user_role"); + b.ToTable("t_user_role", (string)null); b.HasComment("Роли пользователей в системе"); @@ -3727,7 +3727,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWellType"); - b.ToTable("t_well"); + b.ToTable("t_well", (string)null); b.HasComment("скважины"); }); @@ -3755,7 +3755,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWellSrc"); - b.ToTable("t_well_composite"); + b.ToTable("t_well_composite", (string)null); b.HasComment("Композитная скважина"); }); @@ -3831,7 +3831,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdWellSectionType"); - b.ToTable("t_well_operation"); + b.ToTable("t_well_operation", (string)null); b.HasComment("Данные по операциям на скважине"); }); @@ -3857,7 +3857,7 @@ namespace AsbCloudDb.Migrations b.HasKey("Id"); - b.ToTable("t_well_operation_category"); + b.ToTable("t_well_operation_category", (string)null); b.HasComment("Справочник операций на скважине"); @@ -4289,7 +4289,7 @@ namespace AsbCloudDb.Migrations b.HasKey("Id"); - b.ToTable("t_well_section_type"); + b.ToTable("t_well_section_type", (string)null); b.HasComment("конструкция секции скважины"); @@ -4463,7 +4463,7 @@ namespace AsbCloudDb.Migrations b.HasKey("Id"); - b.ToTable("t_well_type"); + b.ToTable("t_well_type", (string)null); b.HasComment("конструкция скважины"); @@ -4520,7 +4520,7 @@ namespace AsbCloudDb.Migrations b.HasKey("IdTelemetry", "DateTime"); - b.ToTable("RecordBase"); + b.ToTable("RecordBase", (string)null); }); modelBuilder.Entity("AsbCloudDb.Model.WITS.Record1", b => @@ -4684,7 +4684,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("TelemetryId"); - b.ToTable("t_telemetry_wits_1"); + b.ToTable("t_telemetry_wits_1", (string)null); }); modelBuilder.Entity("AsbCloudDb.Model.WITS.Record50", b => @@ -4772,7 +4772,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("TelemetryId"); - b.ToTable("t_telemetry_wits_50"); + b.ToTable("t_telemetry_wits_50", (string)null); }); modelBuilder.Entity("AsbCloudDb.Model.WITS.Record60", b => @@ -4824,7 +4824,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("TelemetryId"); - b.ToTable("t_telemetry_wits_60"); + b.ToTable("t_telemetry_wits_60", (string)null); }); modelBuilder.Entity("AsbCloudDb.Model.WITS.Record61", b => @@ -4880,7 +4880,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("TelemetryId"); - b.ToTable("t_telemetry_wits_61"); + b.ToTable("t_telemetry_wits_61", (string)null); }); modelBuilder.Entity("AsbCloudDb.Model.WITS.Record7", b => @@ -4968,7 +4968,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("TelemetryId"); - b.ToTable("t_telemetry_wits_7"); + b.ToTable("t_telemetry_wits_7", (string)null); }); modelBuilder.Entity("AsbCloudDb.Model.WITS.Record8", b => @@ -5172,7 +5172,7 @@ namespace AsbCloudDb.Migrations b.HasIndex("TelemetryId"); - b.ToTable("t_telemetry_wits_8"); + b.ToTable("t_telemetry_wits_8", (string)null); }); modelBuilder.Entity("AsbCloudDb.Model.Cluster", b => diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index eeafa2bd..03c704bb 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -3,7 +3,6 @@ using AsbCloudApp.Data.SAUB; using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services; -using AsbCloudInfrastructure.Services.Analysis; using AsbCloudInfrastructure.Services.Cache; using AsbCloudInfrastructure.Services.DetectOperations; using AsbCloudInfrastructure.Services.DrillingProgram; @@ -75,7 +74,6 @@ namespace AsbCloudInfrastructure services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Services/Analysis/DataSaubAnalyse.cs b/AsbCloudInfrastructure/Services/Analysis/DataSaubAnalyse.cs deleted file mode 100644 index ff8d1119..00000000 --- a/AsbCloudInfrastructure/Services/Analysis/DataSaubAnalyse.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace AsbCloudInfrastructure.Services.Analysis -{ - class DataSaubAnalyse - { - public int IdTelemetry { get; internal set; } - public DateTimeOffset Date { get; internal set; } - public double WellDepth { get; internal set; } - public double BitDepth { get; internal set; } - public double BlockPosition { get; internal set; } - public double RotorSpeed { get; internal set; } - public double Pressure { get; internal set; } - public double HookWeight { get; internal set; } - } -} diff --git a/AsbCloudInfrastructure/Services/Analysis/InterpolationLine.cs b/AsbCloudInfrastructure/Services/Analysis/InterpolationLine.cs deleted file mode 100644 index 93f1e5ee..00000000 --- a/AsbCloudInfrastructure/Services/Analysis/InterpolationLine.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.Collections.Generic; - -namespace AsbCloudInfrastructure.Services.Analysis -{ - public class InterpolationLine - { - private readonly double xSum; - private readonly double ySum; - private readonly double xySum; - private readonly double x2Sum; - private readonly int count; - - public InterpolationLine(IEnumerable<(double Y, double X)> rawData) - { - var iterator = rawData.GetEnumerator(); - while (iterator.MoveNext()) - { - xSum += iterator.Current.X; - ySum += iterator.Current.Y; - xySum += iterator.Current.X * iterator.Current.Y; - x2Sum += iterator.Current.X * iterator.Current.X; - count++; - } - } - - public double A => - (xSum * ySum - count * xySum) / - (xSum * xSum - count * x2Sum); - - public double B => - (xSum * xySum - x2Sum * ySum) / - (xSum * xSum - count * x2Sum); - - public bool IsYNotChanges(double upperBound = 0d, double lowerBound = 0d) => - A < upperBound && A > lowerBound; - - public bool IsYIncreases(double bound = 0d) => - A > bound; - - public bool IsYDecreases(double bound = 0d) => - A < bound; - - public bool IsAverageYLessThanBound(double bound) => - (ySum / count) < bound; - - public bool IsAverageYMoreThanBound(double bound) => - (ySum / count) >= bound; - } -} diff --git a/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsBackgroundService.cs b/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsBackgroundService.cs deleted file mode 100644 index 5db2de25..00000000 --- a/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsBackgroundService.cs +++ /dev/null @@ -1,72 +0,0 @@ -using AsbCloudApp.Services; -using AsbCloudDb.Model; -using AsbCloudInfrastructure.Services.Cache; -using AsbCloudInfrastructure.Services.SAUB; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; -using System; -using System.Diagnostics; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudInfrastructure.Services.Analysis -{ - public class TelemetryAnalyticsBackgroundService : BackgroundService - { - private readonly CacheDb cacheDb; - private readonly ITelemetryTracker telemetryTracker; - private readonly string connectionString; - private readonly TimeSpan period = TimeSpan.FromHours(1); - - public TelemetryAnalyticsBackgroundService(CacheDb cacheDb, ITelemetryTracker telemetryTracker, IConfiguration configuration) - { - this.cacheDb = cacheDb; - this.telemetryTracker = telemetryTracker; - connectionString = configuration.GetConnectionString("DefaultConnection"); - } - - protected override async Task ExecuteAsync(CancellationToken token = default) - { - var timeToStartAnalysis = DateTime.Now; - var options = new DbContextOptionsBuilder() - .UseNpgsql(connectionString) - .Options; - - while (!token.IsCancellationRequested) - { - if (DateTime.Now > timeToStartAnalysis) - { - timeToStartAnalysis = DateTime.Now + period; - try - { - using var context = new AsbCloudDbContext(options); - var timezoneService = new TimezoneService(); - var telemetryService = new TelemetryService(context, telemetryTracker, timezoneService, cacheDb); - var analyticsService = new TelemetryAnalyticsService(context, - telemetryService, cacheDb); - - await analyticsService.AnalyzeAndSaveTelemetriesAsync(token).ConfigureAwait(false); - context.Dispose(); - } - catch (Exception ex) - { - Trace.TraceError(ex.Message); - Console.WriteLine(ex.Message); - } - GC.Collect(); - } - - var ms = (int)(timeToStartAnalysis - DateTime.Now).TotalMilliseconds; - ms = ms > 100 ? ms : 100; - await Task.Delay(ms, token).ConfigureAwait(false); - } - } - - public override async Task StopAsync(CancellationToken token) - { - await base.StopAsync(token).ConfigureAwait(false); - } - - } -} diff --git a/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsService.cs b/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsService.cs deleted file mode 100644 index c7e2edcc..00000000 --- a/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsService.cs +++ /dev/null @@ -1,533 +0,0 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Services; -using AsbCloudDb.Model; -using AsbCloudInfrastructure.Services.Cache; -using Microsoft.EntityFrameworkCore; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudInfrastructure.Services.Analysis -{ - public class TelemetryAnalyticsService : ITelemetryAnalyticsService - { - private readonly IAsbCloudDbContext db; - private readonly ITelemetryService telemetryService; - private readonly CacheTable cacheOperations; - private readonly TelemetryOperationDetectorService operationDetectorService; - private readonly IEnumerable operations; - - private const int countOfRecordsForInterpolation = 12 * 60 * 60; - - public TelemetryAnalyticsService(IAsbCloudDbContext db, ITelemetryService telemetryService, - CacheDb cacheDb) - { - this.db = db; - this.telemetryService = telemetryService; - cacheOperations = cacheDb.GetCachedTable((AsbCloudDbContext)db); - operations = cacheOperations.Where(); - operationDetectorService = new TelemetryOperationDetectorService(operations); - } - - public async Task> GetWellDepthToDayAsync(int idWell, CancellationToken token = default) - { - var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell); - - if (idTelemetry is null) - return null; - - var timezone = telemetryService.GetTimezone((int)idTelemetry); - var depthToTimeData = (from d in db.TelemetryDataSaub - where d.IdTelemetry == idTelemetry - select new - { - d.WellDepth, - d.BitDepth, - d.DateTime - }); - - var m = (int)Math.Round(1d * depthToTimeData.Count() / 2048); - - if (m > 1) - depthToTimeData = depthToTimeData.Where((d, i) => (((d.DateTime.DayOfYear * 24 + d.DateTime.Hour) * 60 + d.DateTime.Minute) * 60 + d.DateTime.Second) % m == 0); - - return await depthToTimeData.Select(d => new WellDepthToDayDto - { - WellDepth = d.WellDepth ?? 0.0, - BitDepth = d.BitDepth ?? 0.0, - Date = d.DateTime.ToRemoteDateTime(timezone.Hours), - }).AsNoTracking().ToListAsync(token).ConfigureAwait(false); - } - - public async Task> GetWellDepthToIntervalAsync(int idWell, - int intervalSeconds, int shiftStartSec, CancellationToken token = default) - { - intervalSeconds = intervalSeconds == 0 ? 86400 : intervalSeconds; - - var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell); - - if (telemetryId is null) - return null; - - var timezone = telemetryService.GetTimezone((int)telemetryId); - - var drillingPeriodsInfo = await db.TelemetryDataSaub - .Where(t => t.IdTelemetry == telemetryId) - .GroupBy(t => Math.Floor((((t.DateTime.DayOfYear * 24 + t.DateTime.Hour) * 60 + t.DateTime.Minute) * 60 + t.DateTime.Second + timezone.Hours - shiftStartSec) / intervalSeconds)) - .Select(g => new - { - WellDepthMin = g.Min(t => t.WellDepth), - WellDepthMax = g.Max(t => t.WellDepth), - DateMin = g.Min(t => t.DateTime), - DateMax = g.Max(t => t.DateTime), - }) - .OrderBy(g => g.DateMin) - .ToListAsync(token); - - var wellDepthToIntervalData = drillingPeriodsInfo.Select(d => new WellDepthToIntervalDto - { - IntervalStartDate = d.DateMin.ToRemoteDateTime(timezone.Hours), - IntervalDepthProgress = (d.WellDepthMax - d.WellDepthMin) ?? 0.0 - // / (d.DateMax - d.DateMin).TotalHours, - }).OrderBy(d => d.IntervalStartDate).ToList(); - - return wellDepthToIntervalData; - } - - public async Task> GetOperationsByWellAsync(int idWell, - IEnumerable categoryIds = default, DateTime begin = default, - DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default) - { - var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell); - - if (telemetryId is null) - return null; - - var operations = from a in db.TelemetryAnalysis.Include(t => t.Operation) - where a.IdTelemetry == telemetryId - select a; - - if ((categoryIds != default) && (categoryIds.Any())) - operations = operations.Where(o => categoryIds.Contains(o.IdOperation)); - - var result = new PaginationContainer - { - Skip = skip, - Take = take - }; - - operations = operations.OrderBy(o => o.UnixDate); - - if (begin != default) - { - var unixBegin = (begin - new DateTime(1970, 1, 1)).TotalSeconds; - operations = operations.Where(o => o.UnixDate >= unixBegin); - } - - if (end != default) - { - var unixEnd = (end - new DateTime(1970, 1, 1)).TotalSeconds; - operations = operations.Where(m => (m.UnixDate + m.DurationSec) <= unixEnd); - } - - result.Count = await operations.CountAsync(token).ConfigureAwait(false); - - if (skip > 0) - operations = operations.Skip(skip); - - var operationsList = await operations.Take(take) - .AsNoTracking() - .ToListAsync(token) - .ConfigureAwait(false); - - if (operationsList.Count == 0) - return result; - - foreach (var operation in operations) - { - var operationDto = new TelemetryOperationDto - { - Id = operation.Id, - Name = operation.Operation.Name, - BeginDate = DateTimeOffset.FromUnixTimeSeconds(operation.UnixDate).DateTime, - EndDate = DateTimeOffset.FromUnixTimeSeconds(operation.UnixDate + operation.DurationSec).DateTime, - StartWellDepth = operation.OperationStartDepth ?? 0.0, - EndWellDepth = operation.OperationEndDepth ?? 0.0 - }; - - result.Items.Add(operationDto); - } - - return result; - } - - public async Task> GetOperationsSummaryAsync(int idWell, - DateTime begin = default, DateTime end = default, CancellationToken token = default) - { - var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell); - - if (telemetryId is null) - return null; - - var unixBegin = begin == default - ? 0 - : (begin - new DateTime(1970, 1, 1)).TotalSeconds; - var unixEnd = end == default - ? (DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds - : (end - new DateTime(1970, 1, 1)).TotalSeconds; - - return await (from a in db.TelemetryAnalysis - where a.IdTelemetry == telemetryId && - a.UnixDate > unixBegin && a.UnixDate < unixEnd - join o in db.WellOperationCategories on a.IdOperation equals o.Id - group a by new { a.IdOperation, o.Name } into g - select new TelemetryOperationDurationDto - { - OperationName = g.Key.Name, - Duration = g.Where(g => g.DurationSec > 0) - .Sum(a => a.DurationSec) - }).AsNoTracking().ToListAsync(token) - .ConfigureAwait(false); - } - - // This method is not finished (only half done). It returns not correct Dtos. - public async Task> GetOperationsToIntervalAsync(int idWell, - int intervalSeconds, int workBeginSeconds, CancellationToken token = default) - { - intervalSeconds = intervalSeconds == 0 ? 86400 : intervalSeconds; - - var telemetryId = telemetryService.GetIdTelemetryByIdWell(idWell); - - if (telemetryId is null) - return null; - - var timezone = telemetryService.GetTimezone((int)telemetryId); - - // Get'n'Group all operations only by start date and by name (if there were several operations in interval). - // Without dividing these operations duration by given interval - var ops = await (from a in db.TelemetryAnalysis - where a.IdTelemetry == telemetryId - join o in db.WellOperationCategories on a.IdOperation equals o.Id - group a by new - { - Interval = Math.Floor((a.UnixDate - workBeginSeconds + timezone.Hours) / intervalSeconds), - o.Name - } into g - select new - { - IntervalStart = g.Min(d => d.UnixDate), - OperationName = g.Key.Name, - OperationDuration = g.Sum(an => an.DurationSec) - }).AsNoTracking() - .OrderBy(op => op.IntervalStart) - .ToListAsync(token) - .ConfigureAwait(false); - - var groupedOperationsList = new List(); - - if (operations is not null && operations.Any()) - { - var operations = ops.Select(o => (o.IntervalStart, o.OperationName, o.OperationDuration)); - - var splittedOperationsByInterval = DivideOperationsByIntervalLength(operations, intervalSeconds); // divides good - - groupedOperationsList = UniteOperationsInDto(splittedOperationsByInterval, intervalSeconds, timezone.Hours).ToList(); // unites not good - } - - return groupedOperationsList; - } - - public async Task AnalyzeAndSaveTelemetriesAsync(CancellationToken token = default) - { - var allTelemetryIds = await db.Telemetries.Select(t => t.Id).ToListAsync(token).ConfigureAwait(false); - - foreach (var idTelemetry in allTelemetryIds) - { - var analyzeStartDate = await GetLastAnalysisDateAsync(idTelemetry, token).ConfigureAwait(false); - await AnalyseAndSaveTelemetryAsync(idTelemetry, analyzeStartDate, token).ConfigureAwait(false); - GC.Collect(); - } - } - - private async Task AnalyseAndSaveTelemetryAsync(int idTelemetry, DateTimeOffset analyzeStartDate, CancellationToken token = default) - { - const int step = 10; - const int take = step * 2; - - TelemetryAnalysis currentAnalysis = null; - - while (true) - { - var dataSaubPart = await GetDataSaubPartOrDefaultAsync(idTelemetry, analyzeStartDate, token).ConfigureAwait(false); - if (dataSaubPart is null) - break; - - var count = dataSaubPart.Count; - var skip = 0; - - if (step > count) - break; - - analyzeStartDate = dataSaubPart.Last().Date; - for (; (skip + step) < count; skip += step) - { - var dataSaubPartOfPart = dataSaubPart.Skip(skip).Take(take); - var telemetryAnalysis = GetDrillingAnalysis(dataSaubPartOfPart); - - if (currentAnalysis is not null) - { - if (currentAnalysis.IdOperation == telemetryAnalysis.IdOperation) - currentAnalysis.DurationSec += telemetryAnalysis.DurationSec; - else - { - currentAnalysis.OperationEndDepth = dataSaubPartOfPart.LastOrDefault()?.WellDepth; - db.TelemetryAnalysis.Add(currentAnalysis); - currentAnalysis = null; - } - } - - if (currentAnalysis is null) - { - currentAnalysis = telemetryAnalysis; - currentAnalysis.OperationStartDepth = dataSaubPartOfPart.FirstOrDefault()?.WellDepth; - } - } - - await db.SaveChangesAsync(token).ConfigureAwait(false); - GC.Collect(); - } - } - - public async Task GetOperationsDateRangeAsync(int idWell, CancellationToken token = default) - { - var idTelemetry = telemetryService.GetIdTelemetryByIdWell(idWell); - - if (idTelemetry is null) - return null; - - var timezone = telemetryService.GetTimezone((int)idTelemetry); - - var datesRange = await (from d in db.TelemetryAnalysis - where d.IdTelemetry == idTelemetry - select d.UnixDate).DefaultIfEmpty() - .GroupBy(g => true) - .AsNoTracking() - .Select(g => new - { - From = g.Min(), - To = g.Max() - }).OrderBy(gr => gr.From) - .FirstOrDefaultAsync(token) - .ConfigureAwait(false); - - var result = new DatesRangeDto - { - From = DateTimeOffset.FromUnixTimeSeconds(datesRange.From).ToRemoteDateTime(timezone.Hours), - To = (datesRange.To == default - ? DateTime.MaxValue - : DateTimeOffset.FromUnixTimeSeconds(datesRange.To)).ToRemoteDateTime(timezone.Hours), - }; - - return result; - } - - private async Task GetLastAnalysisDateAsync(int idTelemetry, CancellationToken token = default) - { - var lastAnalysisInDb = await (from analysis in db.TelemetryAnalysis - where analysis.IdTelemetry == idTelemetry - orderby analysis.UnixDate - select analysis) - .LastOrDefaultAsync(token) - .ConfigureAwait(false); - - DateTime lastAnalysisDate = new DateTime(0, DateTimeKind.Utc); - - if (lastAnalysisInDb is not null) - lastAnalysisDate = DateTime.UnixEpoch.AddSeconds(lastAnalysisInDb.DurationSec + lastAnalysisInDb.UnixDate); - - return lastAnalysisDate; - } - - private Task> GetDataSaubPartOrDefaultAsync(int idTelemetry, DateTimeOffset analyzeStartDate, CancellationToken token) => - db.TelemetryDataSaub - .Where(d => - d.IdTelemetry == idTelemetry && - d.DateTime > analyzeStartDate && - d.BitDepth != null && - d.BlockPosition != null && - d.HookWeight != null && - d.Pressure != null && - d.RotorSpeed != null && - d.WellDepth != null - ) - .OrderBy(d => d.DateTime) - .Take(countOfRecordsForInterpolation) - .Select(d => new DataSaubAnalyse - { - IdTelemetry = d.IdTelemetry, - Date = d.DateTime, - BitDepth = d.BitDepth ?? 0, - BlockPosition = d.BlockPosition ?? 0, - HookWeight = d.HookWeight ?? 0, - Pressure = d.Pressure ?? 0, - RotorSpeed = d.RotorSpeed ?? 0, - WellDepth = d.WellDepth ?? 0, - }) - .ToListAsync(token); - - private static IEnumerable<(long IntervalStart, string OperationName, int OperationDuration)> DivideOperationsByIntervalLength( - IEnumerable<(long IntervalStart, string OperationName, int OperationDuration)> operations, int intervalSeconds) - { - var splittedOperationsByInterval = new List<(long IntervalStart, string OperationName, int OperationDuration)>(); - - var operationDurationTimeCounter = 0; - - foreach (var (IntervalStart, OperationName, OperationDuration) in operations) - { - if (OperationDuration < (intervalSeconds - operationDurationTimeCounter)) - { - splittedOperationsByInterval.Add((IntervalStart, OperationName, OperationDuration)); - operationDurationTimeCounter += OperationDuration; - } - else - { // if operation duration overflows current interval it should be divided into 2 or more parts for this and next intervals - var remainingIntervalTime = intervalSeconds - operationDurationTimeCounter; - splittedOperationsByInterval.Add((IntervalStart, OperationName, remainingIntervalTime)); // first part of long operation - - var operationDurationAfterDividing = OperationDuration - remainingIntervalTime; // second part of long operation. Can be less or more than interval - - // If operation duration even after dividing is still more than interval, - // it should be divided several times to several intervals. - if (operationDurationAfterDividing > intervalSeconds) - { - var counter = 0; - var updatedIntervalStartTime = IntervalStart + remainingIntervalTime; - - while (operationDurationAfterDividing > intervalSeconds) - { - splittedOperationsByInterval.Add((updatedIntervalStartTime + intervalSeconds * counter, OperationName, intervalSeconds)); - operationDurationAfterDividing -= intervalSeconds; - counter++; - } - - splittedOperationsByInterval.Add((updatedIntervalStartTime + operationDurationAfterDividing, OperationName, operationDurationAfterDividing)); - - operationDurationTimeCounter = operationDurationAfterDividing; - } - else - { - splittedOperationsByInterval.Add((IntervalStart, OperationName, operationDurationAfterDividing)); - operationDurationTimeCounter = operationDurationAfterDividing; - } - } - } - - return splittedOperationsByInterval; - } - - private static IEnumerable UniteOperationsInDto( - IEnumerable<(long IntervalStart, string OperationName, int OperationDuration)> operations, int intervalSeconds, double timezoneOffset) - { - var groupedOperationsList = new List(); - - var groupedOperationsObj = new TelemetryOperationInfoDto - { - IntervalBegin = DateTimeOffset.FromUnixTimeSeconds(operations.First().IntervalStart) - .ToRemoteDateTime(timezoneOffset), - Operations = new List() - }; - - var intervalEndDate = operations.First().IntervalStart + intervalSeconds; - - foreach (var (IntervalStart, OperationName, OperationDuration) in operations) - { - if (IntervalStart < intervalEndDate) - { - groupedOperationsObj.Operations.Add(new TelemetryOperationDetailsDto - { - OperationName = OperationName, - DurationSec = OperationDuration - }); - } - else - { - groupedOperationsList.Add(groupedOperationsObj); - - intervalEndDate = IntervalStart + intervalSeconds; - groupedOperationsObj = new TelemetryOperationInfoDto - { - IntervalBegin = DateTimeOffset.FromUnixTimeSeconds(IntervalStart) - .ToRemoteDateTime(timezoneOffset), - Operations = new List() - }; - - groupedOperationsObj.Operations.Add(new TelemetryOperationDetailsDto - { - OperationName = OperationName, - DurationSec = OperationDuration - }); - } - } - - groupedOperationsList.Add(groupedOperationsObj); - - return groupedOperationsList; - } - - private TelemetryAnalysis GetDrillingAnalysis(IEnumerable dataSaubPartOfPart) - { - var dataSaubFirst = dataSaubPartOfPart.First(); - var dataSaubLast = dataSaubPartOfPart.Last(); - - var saubWellDepths = dataSaubPartOfPart.Select(s => (Y: (double)s.WellDepth, - X: (s.Date - dataSaubFirst.Date).TotalSeconds)); - var saubBitDepths = dataSaubPartOfPart.Select(s => (Y: (double)s.BitDepth, - X: (s.Date - dataSaubFirst.Date).TotalSeconds)); - var saubBlockPositions = dataSaubPartOfPart.Select(s => (Y: (double)s.BlockPosition, - X: (s.Date - dataSaubFirst.Date).TotalSeconds)); - var saubRotorSpeeds = dataSaubPartOfPart.Select(s => (Y: (double)s.RotorSpeed, - X: (s.Date - dataSaubFirst.Date).TotalSeconds)); - var saubPressures = dataSaubPartOfPart.Select(s => (Y: (double)s.Pressure, - X: (s.Date - dataSaubFirst.Date).TotalSeconds)); - var saubHookWeights = dataSaubPartOfPart.Select(s => (Y: (double)s.HookWeight, - X: (s.Date - dataSaubFirst.Date).TotalSeconds)); - - var wellDepthLine = new InterpolationLine(saubWellDepths); - var bitPositionLine = new InterpolationLine(saubBitDepths); - var blockPositionLine = new InterpolationLine(saubBlockPositions); - var rotorSpeedLine = new InterpolationLine(saubRotorSpeeds); - var pressureLine = new InterpolationLine(saubPressures); - var hookWeightLine = new InterpolationLine(saubHookWeights); - - var drillingAnalysis = new TelemetryAnalysis - { - IdTelemetry = dataSaubFirst.IdTelemetry, - UnixDate = (long)(dataSaubFirst.Date - DateTime.UnixEpoch).TotalSeconds, - DurationSec = (int)(dataSaubLast.Date - dataSaubFirst.Date).TotalSeconds, - OperationStartDepth = null, - OperationEndDepth = null, - IsWellDepthDecreasing = wellDepthLine.IsYDecreases(-0.0001), - IsWellDepthIncreasing = wellDepthLine.IsYIncreases(0.0001), - IsBitPositionDecreasing = bitPositionLine.IsYDecreases(-0.0001), - IsBitPositionIncreasing = bitPositionLine.IsYIncreases(0.0001), - IsBitPositionLt20 = bitPositionLine.IsAverageYLessThanBound(20), - IsBlockPositionDecreasing = blockPositionLine.IsYDecreases(-0.0001), - IsBlockPositionIncreasing = blockPositionLine.IsYIncreases(0.0001), - IsRotorSpeedLt5 = rotorSpeedLine.IsAverageYLessThanBound(5), - IsRotorSpeedGt5 = rotorSpeedLine.IsAverageYMoreThanBound(5), - IsPressureLt20 = pressureLine.IsAverageYLessThanBound(20), - IsPressureGt20 = pressureLine.IsAverageYMoreThanBound(20), - IsHookWeightNotChanges = hookWeightLine.IsYNotChanges(0.0001, -0.0001), - IsHookWeightLt3 = hookWeightLine.IsAverageYLessThanBound(3), - IdOperation = default, - }; - - drillingAnalysis.IdOperation = - operationDetectorService.DetectOperation(drillingAnalysis).Id; - - return drillingAnalysis; - } - } -} diff --git a/AsbCloudInfrastructure/Services/Analysis/TelemetryOperationDetector.cs b/AsbCloudInfrastructure/Services/Analysis/TelemetryOperationDetector.cs deleted file mode 100644 index 8fad4f08..00000000 --- a/AsbCloudInfrastructure/Services/Analysis/TelemetryOperationDetector.cs +++ /dev/null @@ -1,12 +0,0 @@ -using AsbCloudDb.Model; -using System; - -namespace AsbCloudInfrastructure.Services.Analysis -{ - class TelemetryOperationDetector - { - public int Order { get; set; } - public WellOperationCategory Operation { get; set; } - public Func Detect { get; set; } - } -} diff --git a/AsbCloudInfrastructure/Services/Analysis/TelemetryOperationDetectorService.cs b/AsbCloudInfrastructure/Services/Analysis/TelemetryOperationDetectorService.cs deleted file mode 100644 index 2b0d3667..00000000 --- a/AsbCloudInfrastructure/Services/Analysis/TelemetryOperationDetectorService.cs +++ /dev/null @@ -1,205 +0,0 @@ -using AsbCloudDb.Model; -using System.Collections.Generic; -using System.Linq; - -namespace AsbCloudInfrastructure.Services.Analysis -{ - class TelemetryOperationDetectorService - { - private readonly IEnumerable detectors; - - public TelemetryOperationDetectorService(IEnumerable operations) - { - detectors = new List() - { - new TelemetryOperationDetector - { - Order = 1, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Роторное бурение")), - Detect = (data) => - { - return data.IsWellDepthIncreasing && data.IsBitPositionIncreasing && - data.IsBlockPositionIncreasing && data.IsRotorSpeedGt5; - } - }, - new TelemetryOperationDetector - { - Order = 2, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Слайдирование")), - Detect = (data) => - { - return data.IsWellDepthIncreasing && data.IsBitPositionIncreasing && - data.IsBlockPositionIncreasing && data.IsRotorSpeedLt5; - } - }, - new TelemetryOperationDetector - { - Order = 3, - Operation = operations.FirstOrDefault(o => o.Name.Equals("На поверхности")), - Detect = (data) => - { - return data.IsBitPositionLt20 && data.IsHookWeightLt3; - } - }, - new TelemetryOperationDetector - { - Order = 4, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Удержание в клиньях")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - !data.IsBitPositionIncreasing && !data.IsBitPositionDecreasing && - data.IsBlockPositionDecreasing && !data.IsBlockPositionIncreasing && - data.IsHookWeightLt3; - } - }, - new TelemetryOperationDetector - { - Order = 5, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Подъем с проработкой")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionDecreasing && data.IsBlockPositionIncreasing && - data.IsRotorSpeedGt5 && data.IsPressureGt20; - } - }, - new TelemetryOperationDetector - { - Order = 6, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Спуск с проработкой")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionIncreasing && data.IsBlockPositionDecreasing && - data.IsRotorSpeedGt5 && data.IsPressureGt20; - } - }, - new TelemetryOperationDetector - { - Order = 7, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Подъем с промывкой")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionDecreasing && data.IsBlockPositionIncreasing && - data.IsRotorSpeedLt5 && data.IsPressureGt20; - } - }, - new TelemetryOperationDetector - { - Order = 8, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Спуск с промывкой")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionIncreasing && data.IsBlockPositionDecreasing && - data.IsRotorSpeedLt5 && data.IsPressureGt20; - } - }, - new TelemetryOperationDetector - { - Order = 9, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Спуск в скважину")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionIncreasing && data.IsBlockPositionDecreasing && - data.IsRotorSpeedLt5 && data.IsPressureLt20; - } - }, - new TelemetryOperationDetector - { - Order = 10, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Спуск с вращением")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionIncreasing && data.IsBlockPositionDecreasing && - data.IsRotorSpeedGt5 && data.IsPressureLt20; - } - }, - new TelemetryOperationDetector - { - Order = 11, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Подъем из скважины")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionDecreasing && data.IsBlockPositionIncreasing && - data.IsRotorSpeedLt5 && data.IsPressureLt20; - } - }, - new TelemetryOperationDetector - { - Order = 12, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Подъем с вращением")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionDecreasing && data.IsBlockPositionIncreasing && - data.IsRotorSpeedGt5 && data.IsPressureLt20; - } - }, - new TelemetryOperationDetector - { - Order = 13, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Промывка в покое")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - !data.IsBitPositionDecreasing && !data.IsBitPositionIncreasing && - !data.IsBlockPositionDecreasing && !data.IsBlockPositionIncreasing && - data.IsRotorSpeedLt5 && data.IsPressureGt20; - } - }, - new TelemetryOperationDetector - { - Order = 14, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Промывка с вращением")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - !data.IsBitPositionDecreasing && !data.IsBitPositionIncreasing && - !data.IsBlockPositionDecreasing && !data.IsBlockPositionIncreasing && - data.IsRotorSpeedGt5 && data.IsPressureGt20; - } - }, - new TelemetryOperationDetector - { - Order = 15, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Неподвижное состояние")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - !data.IsBitPositionDecreasing && !data.IsBitPositionIncreasing && - !data.IsBlockPositionDecreasing && !data.IsBlockPositionIncreasing && - data.IsRotorSpeedLt5 && data.IsPressureLt20 && data.IsHookWeightNotChanges; - } - }, - new TelemetryOperationDetector - { - Order = 16, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Вращение без циркуляции")), - Detect = (data) => - { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - !data.IsBitPositionDecreasing && !data.IsBitPositionIncreasing && - !data.IsBlockPositionDecreasing && !data.IsBlockPositionIncreasing && - data.IsRotorSpeedGt5 && data.IsPressureLt20; - } - }, - new TelemetryOperationDetector - { - Order = 17, - Operation = operations.FirstOrDefault(o => o.Name.Equals("Невозможно определить операцию")), - Detect = (data) => true - } - }; - } - - public WellOperationCategory DetectOperation(TelemetryAnalysis data) => - detectors.OrderBy(d => d.Order).First(o => o.Detect(data)).Operation - ?? new WellOperationCategory { Id = 1, Name = "Невозможно определить операцию" }; - } -} diff --git a/AsbCloudWebApi/Controllers/SAUB/TelemetryAnalyticsController.cs b/AsbCloudWebApi/Controllers/SAUB/TelemetryAnalyticsController.cs deleted file mode 100644 index 0743061a..00000000 --- a/AsbCloudWebApi/Controllers/SAUB/TelemetryAnalyticsController.cs +++ /dev/null @@ -1,205 +0,0 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Services; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace AsbCloudWebApi.Controllers.SAUB -{ - [Route("api/well/{idWell}/telemetryAnalytics")] - [ApiController] - [Authorize] - public class TelemetryAnalyticsController : ControllerBase - { - private readonly ITelemetryAnalyticsService analyticsService; - private readonly IWellService wellService; - - public TelemetryAnalyticsController(ITelemetryAnalyticsService analyticsService, IWellService wellService) - { - this.analyticsService = analyticsService; - this.wellService = wellService; - } - - /// - /// Возвращает список операций на скважине за все время - /// - /// id скважины - /// список категорий - /// дата начала - /// окончание - /// для пагинации кол-во записей пропустить - /// для пагинации кол-во записей - /// Токен отмены задачи - /// Список операций на скважине за все время - [HttpGet] - [Route("operationsByWell")] - [Permission] - [ProducesResponseType(typeof(PaginationContainer), (int)System.Net.HttpStatusCode.OK)] - public async Task GetOperationsByWellAsync(int idWell, int skip = 0, int take = 32, - [FromQuery] IEnumerable categoryIds = default, DateTime begin = default, DateTime end = default, - CancellationToken token = default) - { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false)) - return Forbid(); - - var analytics = await analyticsService.GetOperationsByWellAsync(idWell, categoryIds, begin, end, skip, take, token) - .ConfigureAwait(false); - - if (analytics is null || analytics.Count == 0) - return NoContent(); - - return Ok(analytics); - } - - /// - /// Возвращает данные по скважине "глубина-день" - /// - /// id скважины - /// Токен отмены задачи - /// Коллекцию данных по скважине "глубина-день" - [HttpGet] - [Route("wellDepthToDay")] - [Permission] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetWellDepthToDayAsync(int idWell, - CancellationToken token = default) - { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false)) - return Forbid(); - - var wellDepthToDayData = await analyticsService.GetWellDepthToDayAsync(idWell, token) - .ConfigureAwait(false); - - if (wellDepthToDayData is null || !wellDepthToDayData.Any()) - return NoContent(); - - return Ok(wellDepthToDayData); - } - - /// - /// Возвращает данные по глубине скважины за период - /// - /// id скважины - /// количество секунд в необходимом интервале времени - /// количество секунд в времени начала смены - /// Токен отмены задачи - /// Коллекцию данных по глубине скважины за период - [HttpGet] - [Route("wellDepthToInterval")] - [Permission] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetWellDepthToIntervalAsync(int idWell, - int intervalSeconds, int shiftStartSec, CancellationToken token = default) - { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false)) - return Forbid(); - - var wellDepthToIntervalData = await analyticsService.GetWellDepthToIntervalAsync(idWell, - intervalSeconds, shiftStartSec, token).ConfigureAwait(false); - - if (wellDepthToIntervalData is null || !wellDepthToIntervalData.Any()) - return NoContent(); - - return Ok(wellDepthToIntervalData); - } - - /// - /// Возвращает данные по операциям на скважине "операции-время" - /// - /// id скважины - /// дата начала интервала - /// дата окончания интервала - /// Токен отмены задачи - /// Коллекцию операций на скважине - [HttpGet] - [Route("operationsSummary")] - [Permission] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetOperationsSummaryAsync(int idWell, DateTime begin = default, - DateTime end = default, CancellationToken token = default) - { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false)) - return Forbid(); - - var analytics = await analyticsService.GetOperationsSummaryAsync(idWell, - begin, end, token).ConfigureAwait(false); - - if (analytics is null || !analytics.Any()) - return NoContent(); - - return Ok(analytics); - } - - /// - /// Возвращает детальные данные по операциям на скважине за период - /// - /// id скважины - /// количество секунд в необходимом интервале времени - /// количество секунд в времени начала смены - /// Токен отмены задачи - /// Коллекцию операций на скважине - [HttpGet] - [Route("operationsToInterval")] - [Permission] - [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetOperationsToIntervalAsync(int idWell, - int intervalSeconds, int workBeginSeconds, CancellationToken token = default) - { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false)) - return Forbid(); - - var analytics = await analyticsService.GetOperationsToIntervalAsync(idWell, - intervalSeconds, workBeginSeconds, token).ConfigureAwait(false); - - if (analytics is null || !analytics.Any()) - return NoContent(); - - return Ok(analytics); - } - - /// - /// Возвращает даты первой и последней операций на скважине - /// - /// id скважины - /// Токен для отмены задачи - /// Даты самой первой и самой последней операций на скважине - [HttpGet] - [Route("datesRange")] - [Permission] - [ProducesResponseType(typeof(DatesRangeDto), (int)System.Net.HttpStatusCode.OK)] - public async Task GetOperationsDateRangeAsync(int idWell, CancellationToken token = default) - { - int? idCompany = User.GetCompanyId(); - - if (idCompany is null) - return Forbid(); - - if (!await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, - idWell, token).ConfigureAwait(false)) - return Forbid(); - - var wellOperationsDatesRange = await analyticsService.GetOperationsDateRangeAsync(idWell, token).ConfigureAwait(false); - - return Ok(wellOperationsDatesRange); - } - } -}