2021-07-21 15:29:19 +05:00
|
|
|
|
using AsbCloudApp.Comparators;
|
2022-04-08 13:10:06 +05:00
|
|
|
|
using AsbCloudApp.Data.SAUB;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2022-06-10 17:36:03 +05:00
|
|
|
|
using AsbCloudDb;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2022-11-18 12:58:53 +05:00
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
using System;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2022-06-10 17:36:03 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.SAUB
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2022-11-18 12:58:53 +05:00
|
|
|
|
#nullable enable
|
2021-04-23 10:21:25 +05:00
|
|
|
|
public class TelemetryUserService : ITelemetryUserService
|
|
|
|
|
{
|
2022-11-18 12:58:53 +05:00
|
|
|
|
private const string CacheTag = "TelemetryUserCacheTag";
|
|
|
|
|
private readonly TimeSpan CacheOlescence = TimeSpan.FromMinutes(5);
|
|
|
|
|
|
2022-06-10 17:36:03 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
private readonly ITelemetryService telemetryService;
|
2022-11-18 12:58:53 +05:00
|
|
|
|
private readonly IMemoryCache memoryCache;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2022-11-18 12:58:53 +05:00
|
|
|
|
public TelemetryUserService(IAsbCloudDbContext db,
|
|
|
|
|
ITelemetryService telemetryService,
|
|
|
|
|
IMemoryCache memoryCache)
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2022-06-10 17:36:03 +05:00
|
|
|
|
this.db = db;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
this.telemetryService = telemetryService;
|
2022-11-18 12:58:53 +05:00
|
|
|
|
this.memoryCache = memoryCache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TelemetryUserDto? GetOrDefault(int idTelemetry, int idUser)
|
|
|
|
|
{
|
|
|
|
|
var entity = GetCache()
|
|
|
|
|
.FirstOrDefault(u => u.IdTelemetry == idTelemetry && u.IdUser == idUser);
|
|
|
|
|
|
|
|
|
|
if(entity is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return Convert(entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<TelemetryUserDto> GetUsers(int idTelemetry, Func<TelemetryUserDto, bool>? predicate = null)
|
|
|
|
|
{
|
|
|
|
|
var entities = GetCache()
|
|
|
|
|
.Where(u => u.IdTelemetry == idTelemetry);
|
|
|
|
|
|
|
|
|
|
foreach (var entity in entities)
|
|
|
|
|
{
|
|
|
|
|
var dto = Convert(entity);
|
|
|
|
|
if(predicate?.Invoke(dto)??true)
|
|
|
|
|
yield return dto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield break;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-10 17:36:03 +05:00
|
|
|
|
public async Task UpsertAsync(string uid, IEnumerable<TelemetryUserDto> dtos, CancellationToken token = default)
|
2021-04-23 10:21:25 +05:00
|
|
|
|
{
|
2021-05-20 11:17:55 +05:00
|
|
|
|
if (!dtos.Any())
|
2021-04-23 10:21:25 +05:00
|
|
|
|
return;
|
|
|
|
|
|
2021-12-07 18:27:52 +05:00
|
|
|
|
var telemetryId = telemetryService.GetOrCreateTelemetryIdByUid(uid);
|
2021-04-23 10:21:25 +05:00
|
|
|
|
|
2022-11-18 12:58:53 +05:00
|
|
|
|
var entities = dtos.Distinct(new TelemetryUserDtoComparer()).Select(dto => {
|
|
|
|
|
var entity = dto.Adapt<TelemetryUser>();
|
|
|
|
|
entity.IdUser = dto.Id;
|
|
|
|
|
entity.IdTelemetry = telemetryId;
|
|
|
|
|
return entity;
|
2021-07-16 09:15:10 +05:00
|
|
|
|
});
|
2022-06-10 17:36:03 +05:00
|
|
|
|
var result = await db.Database.ExecInsertOrUpdateAsync(db.TelemetryUsers, entities, token);
|
2022-11-18 12:58:53 +05:00
|
|
|
|
DropCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<TelemetryUser> GetCache()
|
|
|
|
|
{
|
|
|
|
|
var cache = memoryCache.GetOrCreate(CacheTag, cacheEntry => {
|
|
|
|
|
cacheEntry.AbsoluteExpirationRelativeToNow = CacheOlescence;
|
|
|
|
|
cacheEntry.SlidingExpiration = CacheOlescence;
|
|
|
|
|
|
|
|
|
|
var entities = db.Set<TelemetryUser>().ToArray();
|
|
|
|
|
return entities;
|
|
|
|
|
});
|
|
|
|
|
return cache;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DropCache()
|
|
|
|
|
=> memoryCache.Remove(CacheTag);
|
|
|
|
|
|
|
|
|
|
private static TelemetryUserDto Convert(TelemetryUser entity)
|
|
|
|
|
{
|
|
|
|
|
var dto = entity.Adapt<TelemetryUserDto>();
|
|
|
|
|
dto.Id = entity.IdUser;
|
|
|
|
|
return dto;
|
2021-04-23 10:21:25 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-18 12:58:53 +05:00
|
|
|
|
#nullable disable
|
2021-04-23 10:21:25 +05:00
|
|
|
|
}
|