Skip to content

Commit

Permalink
Refactor and cleanup InspectionFinding Service
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadUsama-afk-equinor authored and UsamaEquinorAFK committed Dec 1, 2023
1 parent 178ee12 commit e5fd502
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
83 changes: 43 additions & 40 deletions backend/api/EventHandlers/InspectionFindingEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using Api.Database.Models;
Expand All @@ -19,68 +20,69 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
await Task.Delay(_interval, stoppingToken);
await Task.Delay(_interval, stoppingToken);

var lastReportingTime = DateTime.UtcNow - _timeSpan;
var lastReportingTime = DateTime.UtcNow - _timeSpan;

var inspectionFindings = await InspectionFindingService.RetrieveInspectionFindings(lastReportingTime);
var inspectionFindings = await InspectionFindingService.RetrieveInspectionFindings(lastReportingTime);

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

if (inspectionFindings.Count > 0)
{
var findingsList = await GenerateFindingsList(inspectionFindings);
if (inspectionFindings.Count > 0)
{
var findingsList = await GenerateFindingsList(inspectionFindings);

string messageString = GenerateReportFromFindingsReportsList(findingsList);
string messageString = GenerateReportFromFindingsReportsList(findingsList);

string adaptiveCardJson = GenerateAdaptiveCard(messageString);
string adaptiveCardJson = GenerateAdaptiveCard(messageString);

string url = GetWebhookURL("TeamsInspectionFindingsWebhook");
string url = GetWebhookURL("TeamsInspectionFindingsWebhook");

var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(adaptiveCardJson, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content, stoppingToken);
//if (response.StatusCode == StatusCodes.Status201Created) ;
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(adaptiveCardJson, Encoding.UTF8, "application/json");
var response = await client.PostAsync(url, content, stoppingToken);
if (response.StatusCode == HttpStatusCode.OK)
{
logger.LogInformation("Post request via teams incomming webhook was successful, Status Code: {response.StatusCode}", response.StatusCode);
}
else
{
logger.LogInformation("Post request via teams incomming webhook was not successful, Status Code: {response.StatusCode}", response.StatusCode);
}
}
catch (OperationCanceledException) { throw; }
}
}

private async Task<List<Finding>> GenerateFindingsList(List<InspectionFinding> inspectionFindings)
{
var findingsList = new List<Finding>();

foreach (var inspectionFinding in inspectionFindings)
{
try
var missionRun = await InspectionFindingService.GetMissionRunByIsarStepId(inspectionFinding);
var task = await InspectionFindingService.GetMissionTaskByIsarStepId(inspectionFinding);

if (task?.TagId != null && missionRun?.Area?.Plant?.Name != null && missionRun?.Area?.Name != null)
{
var missionRun = await InspectionFindingService.GetMissionRunByIsarStepId(inspectionFinding);
var task = await InspectionFindingService.GetMissionTaskByIsarStepId(inspectionFinding);
var finding = new Finding(
task.TagId,
missionRun.Area.Plant.Name,
missionRun.Area.Name,
inspectionFinding.Finding,
inspectionFinding.InspectionDate,
missionRun.Robot.Name
);

if (task?.TagId != null && missionRun?.Area?.Plant?.Name != null && missionRun?.Area?.Name != null)
{
var finding = new Finding(
task.TagId,
missionRun.Area.Plant.Name,
missionRun.Area.Name,
inspectionFinding.Finding,
inspectionFinding.InspectionDate,
missionRun.Robot.Name
);

findingsList.Add(finding);
}
else
{
logger.LogInformation("Failed to generate a finding since TagId in missionTask or Area in MissionRun is null");
continue;
}
findingsList.Add(finding);
}
else
{
logger.LogInformation("Failed to generate a finding since TagId in missionTask or Area in MissionRun is null");
continue;
}
catch { throw; }
}
logger.LogInformation("Findings List sucessfully generated");
return findingsList;
}

Expand Down Expand Up @@ -133,6 +135,7 @@ public static string GenerateAdaptiveCard(string messageContent)
}}";
return adaptiveCardJson;
}

public static string GetWebhookURL(string secretName)
{
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")!;
Expand Down
16 changes: 6 additions & 10 deletions backend/api/Services/InspectionFindingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@ public async Task<List<InspectionFinding>> RetrieveInspectionFindings(DateTime l
public async Task<MissionRun?> GetMissionRunByIsarStepId(InspectionFinding inspectionFinding)
{
return await context.MissionRuns
.Include(mr => mr.Area).ThenInclude(area => area != null ? area.Plant : null)
.Include(mr => mr.Robot)
.Where(mr => mr.Tasks.Any(mt => mt.Inspections.Any(i => i.IsarStepId == inspectionFinding.IsarStepId)))
.FirstOrDefaultAsync()
?? null;
.Include(missionRun => missionRun.Area).ThenInclude(area => area != null ? area.Plant : null)
.Include(missionRun => missionRun.Robot)
.Where(missionRun => missionRun.Tasks.Any(missionTask => missionTask.Inspections.Any(inspection => inspection.IsarStepId == inspectionFinding.IsarStepId)))
.FirstOrDefaultAsync();
}

public async Task<MissionTask?> GetMissionTaskByIsarStepId(InspectionFinding inspectionFinding)
{
return await context.MissionTasks
.Where(mt => mt.Inspections.Any(i => i.IsarStepId == inspectionFinding.IsarStepId))
.FirstOrDefaultAsync()
?? null;
.Where(missionTask => missionTask.Inspections.Any(inspection => inspection.IsarStepId == inspectionFinding.IsarStepId))
.FirstOrDefaultAsync();
}

}

}

0 comments on commit e5fd502

Please sign in to comment.