diff --git a/AsbCloudApp/Services/IWitsRepository.cs b/AsbCloudApp/Services/IWitsRepository.cs
index 17ae9d69..cc9bbbfa 100644
--- a/AsbCloudApp/Services/IWitsRepository.cs
+++ b/AsbCloudApp/Services/IWitsRepository.cs
@@ -37,7 +37,7 @@ namespace AsbCloudApp.Services
/// The id telemetry.
/// The token.
/// A Task.
- Task> GetLastAsync(int idTelemetry, CancellationToken token);
+ TDto? GetLastOrDefault(int idTelemetry);
///
/// получить статистику по всему архиву: дата самой ранней записи, самой поздней и общее количество
diff --git a/AsbCloudInfrastructure/Repository/WitsRecordRepository.cs b/AsbCloudInfrastructure/Repository/WitsRecordRepository.cs
index 2e0d5626..4b0906ec 100644
--- a/AsbCloudInfrastructure/Repository/WitsRecordRepository.cs
+++ b/AsbCloudInfrastructure/Repository/WitsRecordRepository.cs
@@ -3,6 +3,7 @@ using AsbCloudDb.Model;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
+using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@@ -10,16 +11,17 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Repository
{
-
public class WitsRecordRepository : IWitsRecordRepository
where TEntity : AsbCloudDb.Model.WITS.RecordBase, ITelemetryData
where TDto : AsbCloudApp.Data.ITelemetryData
{
- private static Random random = new Random((int)(DateTime.Now.Ticks % 0xFFFFFFFF));
+ private static readonly Random random = new Random((int)(DateTime.Now.Ticks % 0xFFFFFFFF));
private readonly DbSet dbset;
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
+ private static readonly ConcurrentDictionary cache = new ();
+
public WitsRecordRepository(IAsbCloudDbContext db, ITelemetryService telemetryService)
{
dbset = db.Set();
@@ -56,25 +58,16 @@ namespace AsbCloudInfrastructure.Repository
return data.Select(d => Convert(d, timezoneHours));
}
- public async Task> GetLastAsync(int idTelemetry, CancellationToken token)
- {
- var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
- var query = dbset
- .Where(d => d.IdTelemetry == idTelemetry)
- .OrderBy(d => d.DateTime)
- .AsNoTracking();
- var data = await query.LastOrDefaultAsync(token);
- if(data is not null)
- return new TDto[] { Convert(data, timezoneHours) };
-
- return Enumerable.Empty();
- }
+ public TDto? GetLastOrDefault(int idTelemetry)
+ => cache.GetValueOrDefault(idTelemetry);
public async Task SaveDataAsync(int idTelemetry, IEnumerable dtos, CancellationToken token)
{
- if (dtos?.Any() != true)
+ if (!dtos.Any())
return;
+ cache.AddOrUpdate(idTelemetry, dtos.Last(), (_,_) => dtos.OrderBy(r => r.DateTime).Last());
+
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
var entities = dtos
.DistinctBy(d => d.DateTime)
@@ -144,7 +137,5 @@ namespace AsbCloudInfrastructure.Repository
data.DateTime = entity.DateTime.ToRemoteDateTime(timezoneHours);
return data;
}
-
}
-
}
diff --git a/AsbCloudWebApi/Controllers/WITS/WitsControllerAbstract.cs b/AsbCloudWebApi/Controllers/WITS/WitsControllerAbstract.cs
index 3e6a46ef..41bfa725 100644
--- a/AsbCloudWebApi/Controllers/WITS/WitsControllerAbstract.cs
+++ b/AsbCloudWebApi/Controllers/WITS/WitsControllerAbstract.cs
@@ -67,16 +67,15 @@ namespace AsbCloudWebApi.Controllers.WITS
///
///
[HttpGet("{idWell}/last")]
- public async virtual Task>> GetLastDataAsync(
+ public virtual ActionResult> GetLastData(
int idWell,
- [FromServices] IWitsRecordRepository witsRecordRepository,
- CancellationToken token)
+ [FromServices] IWitsRecordRepository witsRecordRepository)
{
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
if (telemetry is null)
return NoContent();
- var dtos = await witsRecordRepository.GetLastAsync(telemetry.Id, token);
- return Ok(dtos);
+ var dto = witsRecordRepository.GetLastOrDefault(telemetry.Id);
+ return Ok(new[] { dto });
}
///