2021-09-27 11:47:39 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
2021-10-15 12:24:04 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2021-09-27 11:47:39 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2021-11-17 10:50:57 +05:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2021-09-27 11:47:39 +05:00
|
|
|
|
|
2021-10-01 15:44:56 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.Analysis
|
2021-09-27 11:47:39 +05:00
|
|
|
|
{
|
|
|
|
|
public class TelemetryAnalyticsBackgroundService : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
private readonly CacheDb cacheDb;
|
2021-10-15 12:24:04 +05:00
|
|
|
|
private readonly ITelemetryTracker telemetryTracker;
|
2021-11-17 10:50:57 +05:00
|
|
|
|
private readonly string connectionString;
|
2021-10-01 15:44:56 +05:00
|
|
|
|
private readonly TimeSpan period = TimeSpan.FromHours(1);
|
2021-09-27 11:47:39 +05:00
|
|
|
|
|
2021-11-17 10:50:57 +05:00
|
|
|
|
public TelemetryAnalyticsBackgroundService(CacheDb cacheDb, ITelemetryTracker telemetryTracker, IConfiguration configuration)
|
2021-09-27 11:47:39 +05:00
|
|
|
|
{
|
|
|
|
|
this.cacheDb = cacheDb;
|
2021-10-15 12:24:04 +05:00
|
|
|
|
this.telemetryTracker = telemetryTracker;
|
2021-11-17 10:50:57 +05:00
|
|
|
|
connectionString = configuration.GetConnectionString("DefaultConnection");
|
2021-09-27 11:47:39 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken token = default)
|
|
|
|
|
{
|
|
|
|
|
var timeToStartAnalysis = DateTime.Now;
|
2021-10-01 15:44:56 +05:00
|
|
|
|
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
2021-11-17 10:50:57 +05:00
|
|
|
|
.UseNpgsql(connectionString)
|
2021-10-01 15:44:56 +05:00
|
|
|
|
.Options;
|
2021-09-27 11:47:39 +05:00
|
|
|
|
|
2021-09-29 12:20:19 +05:00
|
|
|
|
while (!token.IsCancellationRequested)
|
2021-09-27 11:47:39 +05:00
|
|
|
|
{
|
|
|
|
|
if(DateTime.Now > timeToStartAnalysis)
|
|
|
|
|
{
|
2021-10-01 15:44:56 +05:00
|
|
|
|
timeToStartAnalysis = DateTime.Now + period;
|
2021-09-27 11:47:39 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-09-30 16:41:00 +05:00
|
|
|
|
using var context = new AsbCloudDbContext(options);
|
2021-11-22 11:30:08 +05:00
|
|
|
|
var timeZoneService = new TimeZoneService();
|
|
|
|
|
var telemetryService = new TelemetryService(context, telemetryTracker, timeZoneService, cacheDb);
|
2021-09-30 16:41:00 +05:00
|
|
|
|
var analyticsService = new TelemetryAnalyticsService(context,
|
|
|
|
|
telemetryService, cacheDb);
|
|
|
|
|
|
2021-10-01 15:44:56 +05:00
|
|
|
|
await analyticsService.AnalyzeAndSaveTelemetriesAsync(token).ConfigureAwait(false);
|
|
|
|
|
context.ChangeTracker.Clear();
|
|
|
|
|
context.Dispose();
|
2021-09-27 11:47:39 +05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Trace.TraceError(ex.Message);
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
2021-10-01 15:44:56 +05:00
|
|
|
|
GC.Collect();
|
2021-09-27 11:47:39 +05:00
|
|
|
|
}
|
2021-09-29 10:27:49 +05:00
|
|
|
|
|
|
|
|
|
var ms = (int)(timeToStartAnalysis - DateTime.Now).TotalMilliseconds;
|
|
|
|
|
ms = ms > 100 ? ms : 100;
|
|
|
|
|
await Task.Delay(ms, token).ConfigureAwait(false);
|
2021-09-27 11:47:39 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task StopAsync(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
await base.StopAsync(token).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|