forked from ddrilling/AsbCloudServer
Добавлено кеширование данных ГТИ
This commit is contained in:
parent
69f5639571
commit
821fe270f6
@ -38,8 +38,14 @@ namespace AsbCloudApp.Repositories
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="idWell"></param>
|
/// <param name="idWell"></param>
|
||||||
/// <param name="idRecord"></param>
|
/// <param name="idRecord"></param>
|
||||||
/// <param name="token"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
Task<IEnumerable<WitsItemRecordDto>> GetLastDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default);
|
IEnumerable<WitsItemRecordDto> GetLastDataByRecordId(int idWell, int idRecord);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Последние полученные параметры
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="idWell"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
IEnumerable<WitsItemRecordDto> GetLastData(int idWell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
39
AsbCloudInfrastructure/LinqExtensions.cs
Normal file
39
AsbCloudInfrastructure/LinqExtensions.cs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
// Ignore Spelling: Linq
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace AsbCloudInfrastructure
|
||||||
|
{
|
||||||
|
public static class LinqExtensions
|
||||||
|
{
|
||||||
|
public static TProp? MaxOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
|
||||||
|
where TProp : struct
|
||||||
|
{
|
||||||
|
var value = MaxByOrDefault(enumerable, getter);
|
||||||
|
if (value is null)
|
||||||
|
return null;
|
||||||
|
return getter(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TObj? MaxByOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
|
||||||
|
{
|
||||||
|
return enumerable.OrderByDescending(getter).FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TProp? MinOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
|
||||||
|
where TProp : struct
|
||||||
|
{
|
||||||
|
var value = MinByOrDefault(enumerable, getter);
|
||||||
|
if (value is null)
|
||||||
|
return null;
|
||||||
|
return getter(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TObj? MinByOrDefault<TObj, TProp>(this IEnumerable<TObj> enumerable, Func<TObj, TProp> getter)
|
||||||
|
{
|
||||||
|
return enumerable.OrderBy(getter).FirstOrDefault();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ using AsbCloudDb.Model;
|
|||||||
using AsbCloudDb.Model.GTR;
|
using AsbCloudDb.Model.GTR;
|
||||||
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;
|
||||||
@ -16,12 +17,12 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
{
|
{
|
||||||
private readonly IAsbCloudDbContext db;
|
private readonly IAsbCloudDbContext db;
|
||||||
private readonly ITelemetryService telemetryService;
|
private readonly ITelemetryService telemetryService;
|
||||||
|
private static ConcurrentDictionary<int, ConcurrentDictionary<(int, int), WitsItemRecordDto>> cache = new();
|
||||||
|
|
||||||
public GtrWitsRepository(
|
public GtrWitsRepository(
|
||||||
IAsbCloudDbContext db,
|
IAsbCloudDbContext db,
|
||||||
ITelemetryService telemetryService)
|
ITelemetryService telemetryService)
|
||||||
{
|
{
|
||||||
|
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.telemetryService = telemetryService;
|
this.telemetryService = telemetryService;
|
||||||
}
|
}
|
||||||
@ -69,62 +70,21 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
return dtos;
|
return dtos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<WitsItemRecordDto>> GetLastDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default)
|
public IEnumerable<WitsItemRecordDto> GetLastDataByRecordId(int idWell, int idRecord)
|
||||||
|
{
|
||||||
|
var result = GetLastData(idWell)
|
||||||
|
.Where(item => item.IdRecord == idRecord);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerable<WitsItemRecordDto> GetLastData(int idWell)
|
||||||
{
|
{
|
||||||
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
var telemetry = telemetryService.GetOrDefaultTelemetryByIdWell(idWell);
|
||||||
if (telemetry is null)
|
if (telemetry is null)
|
||||||
return Enumerable.Empty<WitsItemRecordDto>();
|
return Enumerable.Empty<WitsItemRecordDto>();
|
||||||
|
|
||||||
var timezone = telemetryService.GetTimezone(telemetry.Id);
|
var lastData = cache.GetValueOrDefault(telemetry.Id);
|
||||||
|
return lastData?.Values ?? Enumerable.Empty<WitsItemRecordDto>();
|
||||||
var witsRequest = new WitsRequest()
|
|
||||||
{
|
|
||||||
IdTelemetry = telemetry.Id,
|
|
||||||
TimezoneHours = timezone.Hours,
|
|
||||||
IdRecord = idRecord,
|
|
||||||
};
|
|
||||||
|
|
||||||
var recordAllInt = await GetGroupedItemsOrDefaultAsync<WitsItemInt, int>(witsRequest, token);
|
|
||||||
var recordAllFloat = await GetGroupedItemsOrDefaultAsync<WitsItemFloat, float>(witsRequest, token);
|
|
||||||
var recordAllString = await GetGroupedItemsOrDefaultAsync<WitsItemString, string>(witsRequest, token);
|
|
||||||
|
|
||||||
var dtos = recordAllFloat.Union(recordAllInt).Union(recordAllString);
|
|
||||||
return dtos;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task<IEnumerable<WitsItemRecordDto>> GetGroupedItemsOrDefaultAsync<TEntity, TValue>(WitsRequest request, CancellationToken token)
|
|
||||||
where TEntity : WitsItemBase<TValue>
|
|
||||||
where TValue : notnull
|
|
||||||
{
|
|
||||||
var query = BuildQuery<TEntity, TValue>(request);
|
|
||||||
var groupedQuery = query.GroupBy(g => new
|
|
||||||
{
|
|
||||||
g.IdRecord,
|
|
||||||
g.IdTelemetry,
|
|
||||||
g.IdItem
|
|
||||||
})
|
|
||||||
.Select(g => new
|
|
||||||
{
|
|
||||||
g.Key.IdRecord,
|
|
||||||
g.Key.IdItem,
|
|
||||||
Data = g.OrderByDescending(i => i.DateTime)
|
|
||||||
.FirstOrDefault()
|
|
||||||
});
|
|
||||||
|
|
||||||
var groupedEntities = await groupedQuery
|
|
||||||
.ToArrayAsync(token)
|
|
||||||
.ConfigureAwait(false);
|
|
||||||
|
|
||||||
var dtos = groupedEntities
|
|
||||||
.Select(e => new WitsItemRecordDto()
|
|
||||||
{
|
|
||||||
IdRecord = e.IdRecord,
|
|
||||||
IdItem = e.IdItem,
|
|
||||||
Date = e.Data!.DateTime.ToRemoteDateTime(request.TimezoneHours),
|
|
||||||
Value = new JsonValue(e.Data!.Value)
|
|
||||||
});
|
|
||||||
|
|
||||||
return dtos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<IEnumerable<WitsItemRecordDto>> GetItemsOrDefaultAsync<TEntity, TValue>(
|
private async Task<IEnumerable<WitsItemRecordDto>> GetItemsOrDefaultAsync<TEntity, TValue>(
|
||||||
@ -187,30 +147,93 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
{
|
{
|
||||||
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
var timezoneHours = telemetryService.GetTimezone(idTelemetry).Hours;
|
||||||
|
|
||||||
foreach (var dto in dtos)
|
var cacheTelemetryItems = cache.GetValueOrDefault(idTelemetry);
|
||||||
|
|
||||||
|
foreach (var record in dtos)
|
||||||
{
|
{
|
||||||
foreach (var item in dto.Items)
|
var dateTime = record.Date.ToUtcDateTimeOffset(timezoneHours);
|
||||||
|
foreach (var item in record.Items)
|
||||||
{
|
{
|
||||||
var dateTime = dto.Date.ToUtcDateTimeOffset(timezoneHours);
|
if (cacheTelemetryItems?.TryGetValue((record.Id, item.Key), out var cacheItem) == true)
|
||||||
|
if (Math.Abs((dateTime - cacheItem.Date).TotalSeconds) < 1)
|
||||||
|
continue;
|
||||||
|
|
||||||
if (item.Value.Value is string valueString)
|
if (item.Value.Value is string valueString)
|
||||||
{
|
{
|
||||||
var entity = MakeEntity<WitsItemString, string>(dto.Id, item.Key, idTelemetry, dateTime, valueString);
|
var entity = MakeEntity<WitsItemString, string>(record.Id, item.Key, idTelemetry, dateTime, valueString);
|
||||||
db.WitsItemString.Add(entity);
|
db.WitsItemString.Add(entity);
|
||||||
}
|
}
|
||||||
if (item.Value.Value is float valueFloat)
|
if (item.Value.Value is float valueFloat)
|
||||||
{
|
{
|
||||||
var entity = MakeEntity<WitsItemFloat, float>(dto.Id, item.Key, idTelemetry, dateTime, valueFloat);
|
var entity = MakeEntity<WitsItemFloat, float>(record.Id, item.Key, idTelemetry, dateTime, valueFloat);
|
||||||
db.WitsItemFloat.Add(entity);
|
db.WitsItemFloat.Add(entity);
|
||||||
}
|
}
|
||||||
if (item.Value.Value is int valueInt)
|
if (item.Value.Value is int valueInt)
|
||||||
{
|
{
|
||||||
var entity = MakeEntity<WitsItemInt, int>(dto.Id, item.Key, idTelemetry, dateTime, valueInt);
|
var entity = MakeEntity<WitsItemInt, int>(record.Id, item.Key, idTelemetry, dateTime, valueInt);
|
||||||
db.WitsItemInt.Add(entity);
|
db.WitsItemInt.Add(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
await db.SaveChangesAsync(token);
|
await db.SaveChangesAsync(token);
|
||||||
}
|
}
|
||||||
|
catch(DbUpdateException ex)
|
||||||
|
{
|
||||||
|
var isRelational = ex.Source == "Microsoft.EntityFrameworkCore.Relational" && ex.Entries.Any();
|
||||||
|
if (!isRelational)
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.AddOrUpdate(idTelemetry,
|
||||||
|
(_) => MakeNewCache(dtos),
|
||||||
|
(_, oldItemsDictionary) => {
|
||||||
|
foreach (var record in dtos)
|
||||||
|
foreach (var item in record.Items)
|
||||||
|
{
|
||||||
|
oldItemsDictionary.AddOrUpdate(
|
||||||
|
(record.Id, item.Key),
|
||||||
|
(_) => new WitsItemRecordDto
|
||||||
|
{
|
||||||
|
IdRecord = record.Id,
|
||||||
|
IdItem = item.Key,
|
||||||
|
Date = record.Date,
|
||||||
|
Value = item.Value
|
||||||
|
},
|
||||||
|
(_, _) => new WitsItemRecordDto
|
||||||
|
{
|
||||||
|
IdRecord = record.Id,
|
||||||
|
IdItem = item.Key,
|
||||||
|
Date = record.Date,
|
||||||
|
Value = item.Value
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return oldItemsDictionary;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ConcurrentDictionary<(int, int), WitsItemRecordDto> MakeNewCache(IEnumerable<WitsRecordDto> dtos)
|
||||||
|
{
|
||||||
|
var items = dtos.SelectMany(record =>
|
||||||
|
record.Items.Select(
|
||||||
|
item => new WitsItemRecordDto {
|
||||||
|
IdItem = item.Key,
|
||||||
|
IdRecord = record.Id,
|
||||||
|
Date = record.Date,
|
||||||
|
Value = item.Value,
|
||||||
|
}));
|
||||||
|
|
||||||
|
var groups = items
|
||||||
|
.GroupBy(item => (item.IdRecord, item.IdItem));
|
||||||
|
|
||||||
|
var pairs = groups.Select(group => new KeyValuePair<(int, int), WitsItemRecordDto>(
|
||||||
|
group.Key,
|
||||||
|
group.OrderByDescending(item => item.Date).First()));
|
||||||
|
|
||||||
|
return new ConcurrentDictionary<(int, int), WitsItemRecordDto>(pairs);
|
||||||
|
}
|
||||||
|
|
||||||
private static TEntity MakeEntity<TEntity, TValue>(int idRecord, int idItem, int idTelemetry, DateTimeOffset dateTime, TValue value)
|
private static TEntity MakeEntity<TEntity, TValue>(int idRecord, int idItem, int idTelemetry, DateTimeOffset dateTime, TValue value)
|
||||||
where TEntity : WitsItemBase<TValue>, new()
|
where TEntity : WitsItemBase<TValue>, new()
|
||||||
@ -224,6 +247,18 @@ namespace AsbCloudInfrastructure.Repository
|
|||||||
Value = value,
|
Value = value,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static TEntity MakeEntity<TEntity, TValue>(WitsItemRecordDto dto, int idTelemetry, DateTimeOffset dateTime)
|
||||||
|
where TEntity : WitsItemBase<TValue>, new()
|
||||||
|
where TValue : notnull
|
||||||
|
=> new TEntity()
|
||||||
|
{
|
||||||
|
IdRecord = dto.IdRecord,
|
||||||
|
IdItem = dto.IdItem,
|
||||||
|
IdTelemetry = idTelemetry,
|
||||||
|
DateTime = dateTime,
|
||||||
|
Value = (TValue)dto.Value.Value,
|
||||||
|
};
|
||||||
|
|
||||||
private class WitsRequest
|
private class WitsRequest
|
||||||
{
|
{
|
||||||
public int IdTelemetry { get; set; }
|
public int IdTelemetry { get; set; }
|
||||||
|
@ -77,7 +77,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpGet("{idWell}/{idRecord}")]
|
[HttpGet("{idWell}/{idRecord}")]
|
||||||
[Permission]
|
[Permission]
|
||||||
public async Task<ActionResult<IEnumerable<WitsItemRecordDto>>> GetLastDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token = default)
|
public async Task<ActionResult<IEnumerable<WitsItemRecordDto>>> GetLastDataByRecordIdAsync(int idWell, int idRecord, CancellationToken token)
|
||||||
{
|
{
|
||||||
int? idCompany = User.GetCompanyId();
|
int? idCompany = User.GetCompanyId();
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
|
|||||||
if (!isCompanyOwnsWell)
|
if (!isCompanyOwnsWell)
|
||||||
return Forbid();
|
return Forbid();
|
||||||
|
|
||||||
var content = await gtrRepository.GetLastDataByRecordIdAsync(idWell, idRecord, token).ConfigureAwait(false);
|
var content = gtrRepository.GetLastDataByRecordId(idWell, idRecord);
|
||||||
|
|
||||||
return Ok(content);
|
return Ok(content);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user