DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/TelemetryTracker.cs

51 lines
1.9 KiB
C#
Raw Normal View History

2021-07-21 15:29:19 +05:00
using AsbCloudApp.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Services
{
public class TelemetryTracker : ITelemetryTracker
{
private readonly Dictionary<string, DateTime> requests;
public TelemetryTracker(CacheDb cacheDb)
{
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
.Options;
using var db = new AsbCloudDbContext(options);
2021-10-27 17:48:19 +05:00
var cacheTelemetry = cacheDb.GetCachedTable<Telemetry>(db);
var lastTelemetriesDates = (from d in db.TelemetryDataSaub
group d by d.IdTelemetry into g
select new
{
2021-10-27 17:48:19 +05:00
IdTelemetry = g.Key,
Date = g.Max(t => t.Date)
2021-10-27 17:48:19 +05:00
})
.AsNoTracking()
.ToList();
requests = lastTelemetriesDates.Select(t => new
{
Uid = cacheTelemetry.FirstOrDefault(c => c.Id == t.IdTelemetry).RemoteUid,
t.Date
}).ToDictionary(t => t.Uid, u => u.Date);
}
public void SaveRequestDate(string uid) =>
requests[uid] = DateTime.Now;
public DateTime GetLastTelemetryDateByUid(string uid) =>
requests.GetValueOrDefault(uid, DateTime.MinValue);
public IEnumerable<string> GetTransmittingTelemetryUids() =>
requests.Keys;
}
}