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="idTelemetry">The id telemetry.</param>
|
||||||
/// <param name="token">The token.</param>
|
/// <param name="token">The token.</param>
|
||||||
/// <returns>A Task.</returns>
|
/// <returns>A Task.</returns>
|
||||||
Task<IEnumerable<TDto>> GetLastAsync(int idTelemetry, CancellationToken token);
|
TDto? GetLastOrDefault(int idTelemetry);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// получить статистику по всему архиву: дата самой ранней записи, самой поздней и общее количество
|
/// получить статистику по всему архиву: дата самой ранней записи, самой поздней и общее количество
|
||||||
|
@ -3,6 +3,7 @@ using AsbCloudDb.Model;
|
|||||||
using Mapster;
|
using Mapster;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -10,16 +11,17 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace AsbCloudInfrastructure.Repository
|
namespace AsbCloudInfrastructure.Repository
|
||||||
{
|
{
|
||||||
|
|
||||||
public class WitsRecordRepository<TDto, TEntity> : IWitsRecordRepository<TDto>
|
public class WitsRecordRepository<TDto, TEntity> : IWitsRecordRepository<TDto>
|
||||||
where TEntity : AsbCloudDb.Model.WITS.RecordBase, ITelemetryData
|
where TEntity : AsbCloudDb.Model.WITS.RecordBase, ITelemetryData
|
||||||
where TDto : AsbCloudApp.Data.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 DbSet<TEntity> dbset;
|
||||||
private readonly IAsbCloudDbContext db;
|
private readonly IAsbCloudDbContext db;
|
||||||
private readonly ITelemetryService telemetryService;
|
private readonly ITelemetryService telemetryService;
|
||||||
|
|
||||||
|
private static readonly ConcurrentDictionary<int, TDto> cache = new ();
|
||||||
|
|
||||||
public WitsRecordRepository(IAsbCloudDbContext db, ITelemetryService telemetryService)
|
public WitsRecordRepository(IAsbCloudDbContext db, ITelemetryService telemetryService)
|
||||||
{
|
{
|
||||||
dbset = db.Set<TEntity>();
|
dbset = db.Set<TEntity>();
|
||||||
@ -56,25 +58,16 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
return data.Select(d => Convert(d, timezoneHours));
|
return data.Select(d => Convert(d, timezoneHours));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<TDto>> GetLastAsync(int idTelemetry, CancellationToken token)
|
public TDto? GetLastOrDefault(int idTelemetry)
|
||||||
{
|
=> cache.GetValueOrDefault(idTelemetry);
|
||||||
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 async Task SaveDataAsync(int idTelemetry, IEnumerable<TDto> dtos, CancellationToken token)
|
public async Task SaveDataAsync(int idTelemetry, IEnumerable<TDto> dtos, CancellationToken token)
|
||||||
{
|
{
|
||||||
if (dtos?.Any() != true)
|
if (!dtos.Any())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
cache.AddOrUpdate(idTelemetry, dtos.Last(), (_,_) => dtos.OrderBy(r => r.DateTime).Last());
|
||||||
|
|
||||||
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
||||||
var entities = dtos
|
var entities = dtos
|
||||||
.DistinctBy(d => d.DateTime)
|
.DistinctBy(d => d.DateTime)
|
||||||
@ -144,7 +137,5 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
data.DateTime = entity.DateTime.ToRemoteDateTime(timezoneHours);
|
data.DateTime = entity.DateTime.ToRemoteDateTime(timezoneHours);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -67,16 +67,15 @@ namespace AsbCloudWebApi.Controllers.WITS
|
|||||||
/// <param name="token"></param>
|
/// <param name="token"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("{idWell}/last")]
|
[HttpGet("{idWell}/last")]
|
||||||
public async virtual Task<ActionResult<IEnumerable<TDto>>> GetLastDataAsync(
|
public virtual ActionResult<IEnumerable<TDto>> GetLastData(
|
||||||
int idWell,
|
int idWell,
|
||||||
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository,
|
[FromServices] IWitsRecordRepository<TDto> witsRecordRepository)
|
||||||
CancellationToken token)
|
|
||||||
{
|
{
|
||||||
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
||||||
if (telemetry is null)
|
if (telemetry is null)
|
||||||
return NoContent();
|
return NoContent();
|
||||||
var dtos = await witsRecordRepository.GetLastAsync(telemetry.Id, token);
|
var dto = witsRecordRepository.GetLastOrDefault(telemetry.Id);
|
||||||
return Ok(dtos);
|
return Ok(new[] { dto });
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user