Wits repository cache last received value.

This commit is contained in:
ngfrolov 2023-06-29 10:37:21 +05:00
parent cf0ba06b2c
commit 968f6db348
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
3 changed files with 14 additions and 24 deletions

View File

@ -37,7 +37,7 @@ namespace AsbCloudApp.Services
/// <param name="idTelemetry">The id telemetry.</param>
/// <param name="token">The token.</param>
/// <returns>A Task.</returns>
Task<IEnumerable<TDto>> GetLastAsync(int idTelemetry, CancellationToken token);
TDto? GetLastOrDefault(int idTelemetry);
/// <summary>
/// получить статистику по всему архиву: дата самой ранней записи, самой поздней и общее количество

View File

@ -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<TDto, TEntity> : IWitsRecordRepository<TDto>
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<TEntity> dbset;
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
private static readonly ConcurrentDictionary<int, TDto> cache = new ();
public WitsRecordRepository(IAsbCloudDbContext db, ITelemetryService telemetryService)
{
dbset = db.Set<TEntity>();
@ -56,25 +58,16 @@ namespace AsbCloudInfrastructure.Repository
return data.Select(d => Convert(d, timezoneHours));
}
public async Task<IEnumerable<TDto>> 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<TDto>();
}
public TDto? GetLastOrDefault(int idTelemetry)
=> cache.GetValueOrDefault(idTelemetry);
public async Task SaveDataAsync(int idTelemetry, IEnumerable<TDto> 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;
}
}
}

View File

@ -67,16 +67,15 @@ namespace AsbCloudWebApi.Controllers.WITS
/// <param name="token"></param>
/// <returns></returns>
[HttpGet("{idWell}/last")]
public async virtual Task<ActionResult<IEnumerable<TDto>>> GetLastDataAsync(
public virtual ActionResult<IEnumerable<TDto>> GetLastData(
int idWell,
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
CancellationToken token)
[FromServices] IWitsRecordRepository<TDto> 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 });
}
/// <summary>