Добавлено кеширование данных ГТИ

This commit is contained in:
ngfrolov 2023-06-30 15:48:24 +05:00
parent 69f5639571
commit 821fe270f6
Signed by untrusted user who does not match committer: ng.frolov
GPG Key ID: E99907A0357B29A7
4 changed files with 143 additions and 63 deletions

View File

@ -38,8 +38,14 @@ namespace AsbCloudApp.Repositories
/// </summary>
/// <param name="idWell"></param>
/// <param name="idRecord"></param>
/// <param name="token"></param>
/// <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);
}
}

View 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();
}
}
}

View File

@ -5,6 +5,7 @@ using AsbCloudDb.Model;
using AsbCloudDb.Model.GTR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@ -16,12 +17,12 @@ namespace AsbCloudInfrastructure.Repository
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
private static ConcurrentDictionary<int, ConcurrentDictionary<(int, int), WitsItemRecordDto>> cache = new();
public GtrWitsRepository(
IAsbCloudDbContext db,
ITelemetryService telemetryService)
{
this.db = db;
this.telemetryService = telemetryService;
}
@ -69,62 +70,21 @@ namespace AsbCloudInfrastructure.Repository
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);
if (telemetry is null)
return Enumerable.Empty<WitsItemRecordDto>();
var timezone = telemetryService.GetTimezone(telemetry.Id);
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;
var lastData = cache.GetValueOrDefault(telemetry.Id);
return lastData?.Values ?? Enumerable.Empty<WitsItemRecordDto>();
}
private async Task<IEnumerable<WitsItemRecordDto>> GetItemsOrDefaultAsync<TEntity, TValue>(
@ -187,30 +147,93 @@ namespace AsbCloudInfrastructure.Repository
{
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)
{
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);
}
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);
}
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);
}
}
}
try
{
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)
where TEntity : WitsItemBase<TValue>, new()
@ -224,6 +247,18 @@ namespace AsbCloudInfrastructure.Repository
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
{
public int IdTelemetry { get; set; }

View File

@ -77,7 +77,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
/// <returns></returns>
[HttpGet("{idWell}/{idRecord}")]
[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();
@ -90,7 +90,7 @@ namespace AsbCloudWebApi.Controllers.SAUB
if (!isCompanyOwnsWell)
return Forbid();
var content = await gtrRepository.GetLastDataByRecordIdAsync(idWell, idRecord, token).ConfigureAwait(false);
var content = gtrRepository.GetLastDataByRecordId(idWell, idRecord);
return Ok(content);
}