Skip to content

Commit

Permalink
Simplify inspection controller
Browse files Browse the repository at this point in the history
by moving more functionality to the inspection service
and avoiding a call to the Flotilla database
by using the ISAR Inspection ID directly from the frontend
to make the call to the IDA API
  • Loading branch information
tsundvoll committed Dec 18, 2024
1 parent 95b9347 commit 30a34ae
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 46 deletions.
48 changes: 12 additions & 36 deletions backend/api/Controllers/InspectionController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Globalization;
using Api.Controllers.Models;
using Api.Database.Models;

using Api.Controllers.Models;
using Api.Services;
using Api.Services.MissionLoaders;
using Api.Services.Models;
using Api.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -55,53 +53,31 @@ public async Task<ActionResult<TagInspectionMetadata>> Create([FromRoute] string
}

/// <summary>
/// Lookup the inspection image for task with specified isarTaskId
/// Lookup the inspection image for task with specified isarInspectionId
/// </summary>
/// <remarks>
/// Retrieves the inspection image associated with the given ISAR Inspection ID.
/// </remarks>
[HttpGet]
[HttpGet("{isarInspectionId}")]
[Authorize(Roles = Role.User)]
[Route("{installationCode}/{taskId}/taskId")]
[ProducesResponseType(typeof(Inspection), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<Inspection>> GetInspectionImageById([FromRoute] string installationCode, string taskId)
public async Task<IActionResult> GetInspectionImageByIsarInspectionId([FromRoute] string isarInspectionId)
{
Inspection? inspection;
try
{
inspection = await inspectionService.ReadByIsarTaskId(taskId, readOnly: true);
if (inspection == null) return NotFound($"Could not find inspection for task with Id {taskId}.");

}
catch (Exception e)
{
logger.LogError(e, $"Error while finding an inspection with task Id {taskId}");
return StatusCode(StatusCodes.Status500InternalServerError);
}

if (inspection.IsarInspectionId == null) return NotFound($"Could not find isar inspection Id {inspection.IsarInspectionId} for Inspection with task ID {taskId}.");

var inspectionData = await inspectionService.GetInspectionStorageInfo(inspection.IsarInspectionId);

if (inspectionData == null) return NotFound($"Could not find inspection data for inspection with isar Id {inspection.IsarInspectionId}.");

if (!inspectionData.BlobContainer.ToLower(CultureInfo.CurrentCulture).Equals(installationCode.ToLower(CultureInfo.CurrentCulture), StringComparison.Ordinal))
{
return NotFound($"Could not find inspection data for inspection with isar Id {inspection.IsarInspectionId} because blob name {inspectionData.BlobName} does not match installation {installationCode}.");
}

try
{
byte[] inspectionStream = await inspectionService.FetchInpectionImage(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount);
byte[] inspectionStream = await inspectionService.FetchInpectionImageFromIsarInspectionId(isarInspectionId);
return File(inspectionStream, "image/png");
}
catch (Azure.RequestFailedException)
catch (InspectionNotFoundException e)
{
return NotFound($"Could not find inspection blob {inspectionData.BlobName} in container {inspectionData.BlobContainer} and storage account {inspectionData.StorageAccount}.");
logger.LogError(e, "{ErrorMessage}", e.Message);
return NotFound($"Could not find inspection image with ISAR Inspection ID {isarInspectionId}");
}
}
}
Expand Down
36 changes: 26 additions & 10 deletions backend/api/Services/InspectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
using Api.Utilities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Identity.Abstractions;




namespace Api.Services
{
public interface IInspectionService
{
public Task<byte[]> FetchInpectionImage(string inpectionName, string installationCode, string storageAccount);
public Task<byte[]> FetchInpectionImageFromIsarInspectionId(string isarInspectionId);
public Task<Inspection> UpdateInspectionStatus(string isarTaskId, IsarTaskStatus isarTaskStatus);
public Task<Inspection?> ReadByIsarTaskId(string id, bool readOnly = true);
public Task<Inspection?> AddFinding(InspectionFindingQuery inspectionFindingsQuery, string isarTaskId);
public Task<IDAInspectionDataResponse?> GetInspectionStorageInfo(string inspectionId);

}

Expand All @@ -31,9 +34,12 @@ public class InspectionService(FlotillaDbContext context, ILogger<InspectionServ
{
public const string ServiceName = "IDA";

public async Task<byte[]> FetchInpectionImage(string inpectionName, string installationCode, string storageAccount)
public async Task<byte[]> FetchInpectionImageFromIsarInspectionId(string isarInspectionId)
{
return await blobService.DownloadBlob(inpectionName, installationCode, storageAccount);

var inspectionData = await GetInspectionStorageInfo(isarInspectionId)
?? throw new InspectionNotFoundException($"Could not find inspection data for inspection with ISAR Inspection Id {isarInspectionId}.");
return await blobService.DownloadBlob(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount);
}

public async Task<Inspection> UpdateInspectionStatus(string isarTaskId, IsarTaskStatus isarTaskStatus)
Expand Down Expand Up @@ -110,7 +116,7 @@ private IQueryable<Inspection> GetInspections(bool readOnly = true)
return inspection;
}

public async Task<IDAInspectionDataResponse?> GetInspectionStorageInfo(string inspectionId)
private async Task<IDAInspectionDataResponse?> GetInspectionStorageInfo(string inspectionId)
{
string relativePath = $"InspectionData/{inspectionId}/inspection-data-storage-location";

Expand All @@ -123,6 +129,12 @@ private IQueryable<Inspection> GetInspections(bool readOnly = true)
}
);

if (response.StatusCode == HttpStatusCode.OK)
{
var inspectionData = await response.Content.ReadFromJsonAsync<IDAInspectionDataResponse>() ?? throw new JsonException("Failed to deserialize inspection data from IDA.");
return inspectionData;
}

if (response.StatusCode == HttpStatusCode.Accepted)
{
logger.LogInformation("Inspection data storage location for inspection with Id {inspectionId} is not yet available", inspectionId);
Expand All @@ -131,7 +143,7 @@ private IQueryable<Inspection> GetInspections(bool readOnly = true)

if (response.StatusCode == HttpStatusCode.InternalServerError)
{
logger.LogError("Inetrnal server error when trying to get inspection data for inspection with Id {inspectionId}", inspectionId);
logger.LogError("Internal server error when trying to get inspection data for inspection with Id {inspectionId}", inspectionId);
return null;
}

Expand All @@ -141,11 +153,15 @@ private IQueryable<Inspection> GetInspections(bool readOnly = true)
return null;
}

var inspectionData = await response.Content.ReadFromJsonAsync<
IDAInspectionDataResponse
>() ?? throw new JsonException("Failed to deserialize inspection data from IDA.");
if (response.StatusCode == HttpStatusCode.UnprocessableEntity)
{
logger.LogError("Anonymization workflow failed for inspection with Id {inspectionId}", inspectionId);
return null;
}

logger.LogError("Unexpected error when trying to get inspection data for inspection with Id {inspectionId}", inspectionId);
return null;

return inspectionData;
}
}
}

0 comments on commit 30a34ae

Please sign in to comment.