Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify inspection controller #1898

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 18 additions & 58 deletions backend/api/Controllers/InspectionController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Globalization;
using Api.Controllers.Models;
using Api.Controllers.Models;
using Api.Database.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 @@ -53,82 +53,42 @@ [FromBody] IsarZoomDescription zoom
}

/// <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(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,
[FromRoute] string taskId
public async Task<ActionResult<Inspection>> 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 {taskId}", 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
);

if (inspectionStream == null)
return NotFound($"Could not retrieve inspection with task Id {taskId}");
return NotFound(
$"Could not fetch inspection with ISAR Inspection ID {isarInspectionId}"
);

return File(inspectionStream, "image/png");
}
catch (Azure.RequestFailedException)
catch (InspectionNotFoundException e)
{
logger.LogError(e, "{ErrorMessage}", e.Message);
return NotFound(
$"Could not find inspection blob {inspectionData.BlobName} in container {inspectionData.BlobContainer} and storage account {inspectionData.StorageAccount}."
$"Could not find inspection image with ISAR Inspection ID {isarInspectionId}"
);
}
}
Expand Down
76 changes: 51 additions & 25 deletions backend/api/Services/InspectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ 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
Expand All @@ -28,7 +24,6 @@ IsarTaskStatus isarTaskStatus
InspectionFindingQuery inspectionFindingsQuery,
string isarTaskId
);
public Task<IDAInspectionDataResponse?> GetInspectionStorageInfo(string inspectionId);
}

[SuppressMessage(
Expand All @@ -46,13 +41,18 @@ IBlobService blobService
{
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(
Expand Down Expand Up @@ -155,18 +155,35 @@ string isarTaskId
return inspection;
}

public async Task<IDAInspectionDataResponse?> GetInspectionStorageInfo(string inspectionId)
private async Task<IDAInspectionDataResponse?> GetInspectionStorageInfo(string inspectionId)
oysand marked this conversation as resolved.
Show resolved Hide resolved
{
string relativePath = $"InspectionData/{inspectionId}/inspection-data-storage-location";

var response = await idaApi.CallApiForAppAsync(
ServiceName,
options =>
{
options.HttpMethod = HttpMethod.Get.Method;
options.RelativePath = relativePath;
}
);
HttpResponseMessage response;
try
{
response = await idaApi.CallApiForAppAsync(
ServiceName,
options =>
{
options.HttpMethod = HttpMethod.Get.Method;
options.RelativePath = relativePath;
}
);
}
catch (Exception e)
{
logger.LogError(e, "{ErrorMessage}", e.Message);
return null;
}

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)
{
Expand Down Expand Up @@ -195,11 +212,20 @@ string isarTaskId
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;
}

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