CS2-88: Changed telemetry analysis start event.

This commit is contained in:
KharchenkoVV 2021-09-27 11:47:39 +05:00
parent 99ec0dedaf
commit 40b83826a4
9 changed files with 111 additions and 36 deletions

View File

@ -2,7 +2,7 @@
namespace AsbCloudApp.Services
{
public interface IBackgroundQueue
public interface IReportsBackgroundQueue
{
int EnqueueTask(Action<int> action);

View File

@ -23,7 +23,7 @@ namespace AsbCloudApp.Services
Task<IEnumerable<TelemetryOperationInfoDto>> GetOperationsToIntervalAsync(int idWell,
int intervalHoursTimestamp, int workBeginTimestamp,
CancellationToken token = default);
void SaveAnalytics(TelemetryDataSaubDto dataSaub);
void SaveAnalytics();
Task<DatesRangeDto> GetOperationsDateRangeAsync(int idWell,
CancellationToken token = default);
}

View File

@ -18,11 +18,12 @@ namespace AsbCloudInfrastructure
services.AddScoped<IAsbCloudDbContext>(provider => provider.GetService<AsbCloudDbContext>());
services.AddHostedService<BackgroundWorkerService>();
services.AddHostedService<ReportsBackgroundService>();
services.AddHostedService<TelemetryAnalyticsBackgroundService>();
services.AddSingleton(new CacheDb());
services.AddSingleton<ITelemetryTracker, TelemetryTracker>();
services.AddSingleton<IBackgroundQueue, BackgroundQueue>();
services.AddSingleton<IReportsBackgroundQueue, ReportsBackgroundQueue>();
services.AddSingleton<ISaubDataCache, SaubDataCache>();
services.AddTransient<IAuthService, AuthService>();

View File

@ -17,17 +17,15 @@ namespace AsbCloudInfrastructure.Services
{
private readonly IAsbCloudDbContext db;
private readonly ITelemetryService telemetryService;
//private readonly ITelemetryAnalyticsService analyticsService;
private readonly CacheTable<Telemetry> cacheTelemetry;
private readonly CacheTable<TelemetryUser> cacheTelemetryUsers;
private readonly CacheTable<Well> cacheWells;
public DataService(IAsbCloudDbContext db, ITelemetryService telemetryService,
ITelemetryAnalyticsService analyticsService, CacheDb cacheDb)
CacheDb cacheDb)
{
this.db = db;
this.telemetryService = telemetryService;
//this.analyticsService = analyticsService;
cacheTelemetry = cacheDb.GetCachedTable<Telemetry>((AsbCloudDbContext)db);
cacheTelemetryUsers = cacheDb.GetCachedTable<TelemetryUser>((AsbCloudDbContext)db);
cacheWells = cacheDb.GetCachedTable<Well>((AsbCloudDbContext)db);
@ -106,9 +104,6 @@ namespace AsbCloudInfrastructure.Services
dataSaub.IdUser = telemetryUser?.IdUser;
dataSaub.IdTelemetry = telemetryId;
db.TelemetryDataSaub.Add(dataSaub);
//dto.IdTelemetry = telemetryId;
//analyticsService.SaveAnalytics(dto);
}
return await db.SaveChangesAsync(token);

View File

@ -20,11 +20,11 @@ namespace AsbCloudInfrastructure.Services
private readonly IConfiguration configuration;
private readonly ITelemetryService telemetryService;
private readonly IFileService fileService;
private readonly IBackgroundQueue queue;
private readonly IReportsBackgroundQueue queue;
public ReportService(IAsbCloudDbContext db, IConfiguration configuration,
ITelemetryService telemetryService, IFileService fileService,
IBackgroundQueue queue)
IReportsBackgroundQueue queue)
{
this.db = db;
this.configuration = configuration;

View File

@ -4,7 +4,7 @@ using System.Collections.Concurrent;
namespace AsbCloudInfrastructure.Services
{
class BackgroundQueue : IBackgroundQueue
class ReportsBackgroundQueue : IReportsBackgroundQueue
{
private readonly ConcurrentQueue<(Action<int> action, int id)> tasks =
new ConcurrentQueue<(Action<int> action, int id)>();

View File

@ -7,11 +7,11 @@ using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class BackgroundWorkerService : BackgroundService
public class ReportsBackgroundService : BackgroundService
{
private readonly IBackgroundQueue tasksQueue;
private readonly IReportsBackgroundQueue tasksQueue;
public BackgroundWorkerService(IBackgroundQueue tasksQueue)
public ReportsBackgroundService(IReportsBackgroundQueue tasksQueue)
{
this.tasksQueue = tasksQueue;
}

View File

@ -0,0 +1,64 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using AsbCloudApp.Services;
using AsbCloudInfrastructure.Services.Cache;
using AsbCloudDb.Model;
namespace AsbCloudInfrastructure.Services
{
public class TelemetryAnalyticsBackgroundService : BackgroundService
{
private static readonly DbContextOptions<AsbCloudDbContext> options =
new DbContextOptionsBuilder<AsbCloudDbContext>()
.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True")
.Options;
private readonly AsbCloudDbContext context = new AsbCloudDbContext(options);
private readonly CacheDb cacheDb;
private readonly ISaubDataCache saubDataCache;
public TelemetryAnalyticsBackgroundService(ISaubDataCache saubDataCache, CacheDb cacheDb)
{
this.saubDataCache = saubDataCache;
this.cacheDb = cacheDb;
}
protected override async Task ExecuteAsync(CancellationToken token = default)
{
var telemetryService = new TelemetryService(context, cacheDb);
var analyticsService = new TelemetryAnalyticsService(context,
telemetryService, saubDataCache, cacheDb);
var timeToStartAnalysis = DateTime.Now;
while (!token.IsCancellationRequested)
{
if(DateTime.Now > timeToStartAnalysis)
{
try
{
timeToStartAnalysis = DateTime.Now.AddHours(1);
await Task.Run(() => analyticsService.SaveAnalytics(), token)
.ConfigureAwait(false);
}
catch (Exception ex)
{
Trace.TraceError(ex.Message);
Console.WriteLine(ex.Message);
}
}
}
}
public override async Task StopAsync(CancellationToken token)
{
await base.StopAsync(token).ConfigureAwait(false);
}
}
}

View File

@ -231,32 +231,47 @@ namespace AsbCloudInfrastructure.Services
return groupedOperationsList;
}
public void SaveAnalytics(TelemetryDataSaubDto dataSaubDto)
public void SaveAnalytics()
{
saubDataCache.AddData(dataSaubDto);
var alltelemetryIds = db.TelemetryDataSaub.Select(t => t.IdTelemetry).Distinct().ToList();
if (saubDataCache.GetOrCreateCache(dataSaubDto.IdTelemetry).Count() > 1)
foreach(var idTelemetry in alltelemetryIds)
{
var dataSaubs = saubDataCache.GetOrCreateCache(dataSaubDto.IdTelemetry)
.OrderBy(d => d.Date);
var telemetries = db.TelemetryDataSaub.Where(t => t.IdTelemetry == idTelemetry &&
t.Date.Hour == (DateTime.UtcNow.Hour - 1)).ToList();
var telemetryAnalysisDto = GetDrillingAnalysis(dataSaubs);
if (saubDataCache.CurrentAnalysis is null)
saubDataCache.CurrentAnalysis = telemetryAnalysisDto;
if (saubDataCache.CurrentAnalysis.IdOperation == telemetryAnalysisDto.IdOperation)
foreach(var telemetry in telemetries)
{
saubDataCache.CurrentAnalysis.DurationSec +=
telemetryAnalysisDto.DurationSec;
saubDataCache.CurrentAnalysis.OperationEndDepth = dataSaubDto.WellDepth;
}
else
{
db.TelemetryAnalysis.Add(saubDataCache.CurrentAnalysis.Adapt<TelemetryAnalysis>());
saubDataCache.CurrentAnalysis = telemetryAnalysisDto;
saubDataCache.CurrentAnalysis.OperationStartDepth = dataSaubDto.WellDepth;
var dataSaubDto = telemetry.Adapt<TelemetryDataSaubDto>();
saubDataCache.AddData(dataSaubDto);
if (saubDataCache.GetOrCreateCache(dataSaubDto.IdTelemetry).Count() > 1)
{
var dataSaubs = saubDataCache.GetOrCreateCache(dataSaubDto.IdTelemetry)
.OrderBy(d => d.Date);
var telemetryAnalysisDto = GetDrillingAnalysis(dataSaubs);
if (saubDataCache.CurrentAnalysis is null)
saubDataCache.CurrentAnalysis = telemetryAnalysisDto;
if (saubDataCache.CurrentAnalysis.IdOperation == telemetryAnalysisDto.IdOperation)
{
saubDataCache.CurrentAnalysis.DurationSec +=
telemetryAnalysisDto.DurationSec;
saubDataCache.CurrentAnalysis.OperationEndDepth = dataSaubDto.WellDepth;
}
else
{
db.TelemetryAnalysis.Add(saubDataCache.CurrentAnalysis.Adapt<TelemetryAnalysis>());
saubDataCache.CurrentAnalysis = telemetryAnalysisDto;
saubDataCache.CurrentAnalysis.OperationStartDepth = dataSaubDto.WellDepth;
}
}
}
db.SaveChanges();
}
}