From e7d6aef6a6f88bc6e1200f8bbb6a091a1846cc01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Mon, 27 Dec 2021 13:49:29 +0500 Subject: [PATCH] Move logic from AsbCloudDbContext.GetDepthToIntervalAsync() to TelemetryAnalyticsService.GetWellDepthToIntervalAsync() --- .../Services/ITelemetryAnalyticsService.cs | 2 +- AsbCloudDb/Model/AsbCloudDbContext.cs | 32 ------------------- AsbCloudDb/Model/IAsbCloudDbContext.cs | 2 -- .../Analysis/TelemetryAnalyticsService.cs | 20 +++++++++--- .../TelemetryAnalyticsController.cs | 6 ++-- 5 files changed, 19 insertions(+), 43 deletions(-) diff --git a/AsbCloudApp/Services/ITelemetryAnalyticsService.cs b/AsbCloudApp/Services/ITelemetryAnalyticsService.cs index 18994812..2580e3b3 100644 --- a/AsbCloudApp/Services/ITelemetryAnalyticsService.cs +++ b/AsbCloudApp/Services/ITelemetryAnalyticsService.cs @@ -15,7 +15,7 @@ namespace AsbCloudApp.Services Task> GetWellDepthToDayAsync(int idWell, CancellationToken token = default); Task> GetWellDepthToIntervalAsync(int idWell, - int intervalHoursTimestamp, int workBeginTimestamp, + int intervalHoursTimestamp, int shiftStartSec, CancellationToken token = default); Task> GetOperationsSummaryAsync(int idWell, DateTime begin = default, DateTime end = default, diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index ff3abb24..6bb8ba00 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -466,37 +466,5 @@ namespace AsbCloudDb.Model }); } - - public async Task> GetDepthToIntervalAsync(int telemetryId, - int intervalHoursTimestamp, int workStartTimestamp, double timezoneOffset, CancellationToken token) - { - //TODO: Сменить на LINQ группирование - using var command = Database.GetDbConnection().CreateCommand(); - - command.CommandText = $@"SELECT Min(t.bit_depth) AS MinDepth, Max(t.bit_depth) AS MaxDepth, Min(t.Date) AS dateStart - FROM t_telemetry_data_saub AS t - WHERE id_telemetry = {telemetryId} AND t.Id % 10 = 0 - GROUP BY floor((extract(epoch from t.date) - {workStartTimestamp} + {timezoneOffset}) / {intervalHoursTimestamp});"; - - Database.OpenConnection(); - using var reader = await command.ExecuteReaderAsync(token); - - var result = new List<(double? MinDepth, double? MaxDepth, DateTime BeginPeriodDate)>(); - - if (reader.HasRows) - { - while (reader.Read()) - { - result.Add( - ( - (double?)reader.GetValue(0), - (double?)reader.GetValue(1), - (DateTime)reader.GetValue(2) - )); - } - } - - return result; - } } } diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index 9aefc524..32c70df7 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -50,7 +50,5 @@ namespace AsbCloudDb.Model DbSet Set() where TEntity : class; - Task> GetDepthToIntervalAsync(int telemetryId, - int intervalHoursTimestamp, int workStartTimestamp, double timezoneOffset, CancellationToken token); } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsService.cs b/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsService.cs index 6fc7f037..16810da1 100644 --- a/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsService.cs +++ b/AsbCloudInfrastructure/Services/Analysis/TelemetryAnalyticsService.cs @@ -61,7 +61,7 @@ namespace AsbCloudInfrastructure.Services.Analysis } public async Task> GetWellDepthToIntervalAsync(int idWell, - int intervalSeconds, int workBeginSeconds, CancellationToken token = default) + int intervalSeconds, int shiftStartSec, CancellationToken token = default) { intervalSeconds = intervalSeconds == 0 ? 86400 : intervalSeconds; @@ -72,13 +72,23 @@ namespace AsbCloudInfrastructure.Services.Analysis var timezoneOffset = telemetryService.GetTimezoneOffsetByTelemetryId((int)telemetryId); - var drillingPeriodsInfo = await db.GetDepthToIntervalAsync((int)telemetryId, intervalSeconds, - workBeginSeconds, timezoneOffset, token).ConfigureAwait(false); + var drillingPeriodsInfo = await db.TelemetryDataSaub + .Where(t => t.IdTelemetry == telemetryId) + .GroupBy(t => Math.Floor((((t.Date.DayOfYear * 24 + t.Date.Hour) * 60 + t.Date.Minute) * 60 + t.Date.Second + timezoneOffset - shiftStartSec) / intervalSeconds)) + .Select(g => new { + WellDepthMin = g.Min(t => t.WellDepth), + WellDepthMax = g.Max(t => t.WellDepth), + DateMin = g.Min(t => t.Date), + DateMax = g.Max(t => t.Date), + }) + .OrderBy(g=>g.DateMin) + .ToListAsync(token); var wellDepthToIntervalData = drillingPeriodsInfo.Select(d => new WellDepthToIntervalDto { - IntervalStartDate = d.BeginPeriodDate, - IntervalDepthProgress = (d.MaxDepth - d.MinDepth) ?? 0.0 / intervalSeconds + IntervalStartDate = d.DateMin, + IntervalDepthProgress = (d.WellDepthMax - d.WellDepthMin) ?? 0.0 + // / (d.DateMax - d.DateMin).TotalHours, }).OrderBy(d => d.IntervalStartDate).ToList(); return wellDepthToIntervalData; diff --git a/AsbCloudWebApi/Controllers/TelemetryAnalyticsController.cs b/AsbCloudWebApi/Controllers/TelemetryAnalyticsController.cs index f6f86485..1cd5a764 100644 --- a/AsbCloudWebApi/Controllers/TelemetryAnalyticsController.cs +++ b/AsbCloudWebApi/Controllers/TelemetryAnalyticsController.cs @@ -89,14 +89,14 @@ namespace AsbCloudWebApi.Controllers /// /// id скважины /// количество секунд в необходимом интервале времени - /// количество секунд в времени начала смены + /// количество секунд в времени начала смены /// Токен отмены задачи /// Коллекцию данных по глубине скважины за период [HttpGet] [Route("wellDepthToInterval")] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetWellDepthToIntervalAsync(int idWell, - int intervalSeconds, int workBeginSeconds, CancellationToken token = default) + int intervalSeconds, int shiftStartSec, CancellationToken token = default) { int? idCompany = User.GetCompanyId(); @@ -105,7 +105,7 @@ namespace AsbCloudWebApi.Controllers return Forbid(); var wellDepthToIntervalData = await analyticsService.GetWellDepthToIntervalAsync(idWell, - intervalSeconds, workBeginSeconds, token).ConfigureAwait(false); + intervalSeconds, shiftStartSec, token).ConfigureAwait(false); if (wellDepthToIntervalData is null || !wellDepthToIntervalData.Any()) return NoContent();