From 52cd33647d4a0b998d8da4281bd71e4e3bc1fe5c Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Tue, 17 Oct 2023 15:24:50 +0500 Subject: [PATCH] Fix TelemetryDataBaseService.GetRange(). Use cache. --- AsbCloudApp/Services/ITelemetryDataService.cs | 12 +++++-- .../Services/SAUB/TelemetryDataBaseService.cs | 35 ++++++------------- .../SAUB/TelemetryDataBaseController.cs | 2 +- .../SubsystemOperationTimeController.cs | 2 +- 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/AsbCloudApp/Services/ITelemetryDataService.cs b/AsbCloudApp/Services/ITelemetryDataService.cs index a2034c7f..58eeeb70 100644 --- a/AsbCloudApp/Services/ITelemetryDataService.cs +++ b/AsbCloudApp/Services/ITelemetryDataService.cs @@ -26,17 +26,25 @@ namespace AsbCloudApp.Services Task> GetAsync(int idWell, DateTime dateBegin = default, double intervalSec = 600d, int approxPointsCount = 1024, CancellationToken token = default); + + /// + /// Получить данные тех. процесса + /// + /// + /// + /// + /// Task> GetAsync(int idWell, TelemetryDataRequest request, CancellationToken token); /// - /// Получение статистики за период + /// Получение периода за период /// /// /// /// /// /// - Task GetRangeAsync(int idWell, DateTimeOffset start, DateTimeOffset end, CancellationToken token); + DatesRangeDto? GetRange(int idWell, DateTimeOffset start, DateTimeOffset end); /// /// добавить/изменить данные тех. процесса (используется панелью) diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs index bf735367..32fc88a2 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataBaseService.cs @@ -203,40 +203,27 @@ namespace AsbCloudInfrastructure.Services.SAUB } /// - public virtual async Task GetRangeAsync( - int idWell, - DateTimeOffset start, - DateTimeOffset end, - CancellationToken token) + public DatesRangeDto? GetRange(int idWell, DateTimeOffset start, DateTimeOffset end) { var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell); if (telemetry is null) return default; - var timezone = telemetryService.GetTimezone(telemetry.Id); - var startUtc = start.ToOffset(TimeSpan.Zero); - var endUtc = end.ToOffset(TimeSpan.Zero); + var datesRange = telemetryDataCache.GetOrDefaultDataDateRange(telemetry.Id); - var dbSet = db.Set(); - var query = dbSet - .Where(i => i.IdTelemetry == telemetry.Id) - .Where(i => i.DateTime >= startUtc) - .Where(i => i.DateTime <= endUtc) - .GroupBy(i => i.IdTelemetry) - .Select(g => new - { - DateStart = g.Min(i => i.DateTime), - DateEnd = g.Max(i => i.DateTime), - }); + if (datesRange is null) + return null; - var data = await query.FirstOrDefaultAsync(token); - if (data is null) - return default; + var from = datesRange.From > start.DateTime + ? datesRange.From : start.DateTime; + + var to = datesRange.To < end.DateTime + ? datesRange.To : end.DateTime; return new DatesRangeDto { - From = data.DateStart.ToRemoteDateTime(timezone.Hours), - To = data.DateEnd.ToRemoteDateTime(timezone.Hours), + From = from, + To = to, }; } diff --git a/AsbCloudWebApi/Controllers/SAUB/TelemetryDataBaseController.cs b/AsbCloudWebApi/Controllers/SAUB/TelemetryDataBaseController.cs index cd9eb7b5..4649d91d 100644 --- a/AsbCloudWebApi/Controllers/SAUB/TelemetryDataBaseController.cs +++ b/AsbCloudWebApi/Controllers/SAUB/TelemetryDataBaseController.cs @@ -153,7 +153,7 @@ namespace AsbCloudWebApi.Controllers.SAUB if (!isCompanyOwnsWell) return Forbid(); - var content = await telemetryDataService.GetRangeAsync(idWell, start, end, token); + var content = telemetryDataService.GetRange(idWell, start, end); return Ok(content); } diff --git a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs index efa60638..0cf8250a 100644 --- a/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs +++ b/AsbCloudWebApi/Controllers/Subsystems/SubsystemOperationTimeController.cs @@ -68,7 +68,7 @@ namespace AsbCloudWebApi.Controllers.Subsystems if (!await UserHasAccesToWellAsync(idWell, token)) return Forbid(); - var dateRange = await telemetryDataSaubService.GetRangeAsync(idWell, DateTimeOffset.MinValue, DateTimeOffset.MaxValue, token); + var dateRange = telemetryDataSaubService.GetRange(idWell, DateTimeOffset.MinValue, DateTimeOffset.MaxValue); return Ok(dateRange); }