Skip to content

Commit

Permalink
Refactor InspectionFindingService to do more processing in eventhandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadUsama-afk-equinor authored and UsamaEquinorAFK committed Nov 27, 2023
1 parent 9ece5c6 commit 3b93684
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
14 changes: 5 additions & 9 deletions backend/api/EventHandlers/InspectionFindingsEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
using Api.Database.Context;
using Api.Services;
using Api.Services;
namespace Api.EventHandlers
{
public class InspectionFindingEventHandler(IConfiguration configuration,
IServiceScopeFactory scopeFactory,
ILogger<InspectionFindingEventHandler> logger) : BackgroundService
{
private readonly TimeSpan _interval = configuration.GetValue<TimeSpan>("InspectionFindingEventHandler:Interval");
private IInspectionFindingService InspectionFindingService => scopeFactory.CreateScope().ServiceProvider.GetRequiredService<IInspectionFindingService>();
private InspectionFindingService InspectionFindingService => scopeFactory.CreateScope().ServiceProvider.GetRequiredService<InspectionFindingService>();
private readonly TimeSpan _timeSpan = configuration.GetValue<TimeSpan>("InspectionFindingEventHandler:TimeSpan");

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{


while (!stoppingToken.IsCancellationRequested)
{
try
{
await Task.Delay(_interval, stoppingToken);

using var scope = scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<FlotillaDbContext>();
var inspectionFindings = await InspectionFindingService.RetrieveInspectionFindings(_timeSpan, context);
var lastReportingTime = DateTime.UtcNow - _timeSpan;

var inspectionFindings = await InspectionFindingService.RetrieveInspectionFindings(lastReportingTime);

logger.LogInformation("Found {count} inspection findings in the last {interval}.", inspectionFindings.Count, _timeSpan);

Expand All @@ -33,6 +30,5 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
}
}

}
}
4 changes: 1 addition & 3 deletions backend/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
builder.Services.AddScoped<IPoseTimeseriesService, PoseTimeseriesService>();
builder.Services.AddScoped<ILastMissionRunService, LastMissionRunService>();


bool useInMemoryDatabase = builder.Configuration
.GetSection("Database")
.GetValue<bool>("UseInMemoryDatabase");
Expand All @@ -89,11 +88,10 @@
builder.Services.AddScoped<RobotController>();
builder.Services.AddScoped<EmergencyActionController>();
builder.Services.AddScoped<ICustomMissionService, CustomMissionService>();
builder.Services.AddScoped<IInspectionFindingService>();
builder.Services.AddScoped<InspectionFindingService>();

builder.Services.AddTransient<ISignalRService, SignalRService>();


builder.Services.AddHostedService<InspectionFindingEventHandler>();
builder.Services.AddHostedService<MqttEventHandler>();
builder.Services.AddHostedService<MissionEventHandler>();
Expand Down
8 changes: 2 additions & 6 deletions backend/api/Services/InspectionFindingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
using Api.Database.Models;
using Microsoft.EntityFrameworkCore;


namespace Api.Services
{
public class IInspectionFindingService()
public class InspectionFindingService(FlotillaDbContext context)
{

public async Task<List<InspectionFinding>> RetrieveInspectionFindings(TimeSpan timeSpan, FlotillaDbContext context)
public async Task<List<InspectionFinding>> RetrieveInspectionFindings(DateTime lastReportingTime)
{
var lastReportingTime = DateTime.UtcNow - timeSpan;
var inspectionFindings = await context.InspectionFindings
.Where(f => f.InspectionDate > lastReportingTime)
.ToListAsync();
return inspectionFindings;
}

}
}

0 comments on commit 3b93684

Please sign in to comment.