forked from ddrilling/AsbCloudServer
Wits repository cache last received value.
This commit is contained in:
parent
cf0ba06b2c
commit
968f6db348
@ -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>
|
||||
/// получить статистику по всему архиву: дата самой ранней записи, самой поздней и общее количество
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user