-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit adding ability to update the canvas_paintings table wi…
…th asset details from the DLCS
- Loading branch information
1 parent
cba835e
commit 3bc4607
Showing
21 changed files
with
782 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...resentation/BackgroundHandler.Tests/BatchCompletion/BatchCompletionMessageHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace BackgroundHandler.Tests.BatchCompletion; | ||
|
||
public class BatchCompletionMessageHandlerTests | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/IIIFPresentation/BackgroundHandler/BatchCompletion/BatchCompletionMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace BackgroundHandler.BatchCompletion; | ||
|
||
public record BatchCompletionMessage( | ||
int Id, | ||
int CustomerId, | ||
int Total, | ||
int Success, | ||
int Errors, | ||
bool Superseded, | ||
DateTime Started, | ||
DateTime Finished); |
122 changes: 122 additions & 0 deletions
122
src/IIIFPresentation/BackgroundHandler/BatchCompletion/BatchCompletionMessageHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using System.Text.Json; | ||
using AWS.SQS; | ||
using BackgroundHandler.Helpers; | ||
using Core.Helpers; | ||
using DLCS; | ||
using DLCS.API; | ||
using DLCS.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Options; | ||
using Models.Database.General; | ||
using Repository; | ||
using Batch = Models.Database.General.Batch; | ||
|
||
namespace BackgroundHandler.BatchCompletion; | ||
|
||
public class BatchCompletionMessageHandler( | ||
PresentationContext dbContext, | ||
IDlcsApiClient dlcsApiClient, | ||
IOptions<DlcsSettings> dlcsOptions, | ||
ILogger<BatchCompletionMessageHandler> logger) | ||
: IMessageHandler | ||
{ | ||
private static readonly JsonSerializerOptions JsonSerializerOptions = new(JsonSerializerDefaults.Web); | ||
|
||
private readonly DlcsSettings dlcsSettings = dlcsOptions.Value; | ||
|
||
public async Task<bool> HandleMessage(QueueMessage message, CancellationToken cancellationToken) | ||
{ | ||
using (LogContextHelpers.SetServiceName(nameof(BatchCompletionMessageHandler))) | ||
{ | ||
try | ||
{ | ||
var batchCompletionMessage = DeserializeMessage(message); | ||
|
||
await UpdateAssetsIfRequired(batchCompletionMessage, cancellationToken); | ||
return true; | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "Error handling batch-completion message {MessageId}", message.MessageId); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private async Task UpdateAssetsIfRequired(BatchCompletionMessage batchCompletionMessage, CancellationToken cancellationToken) | ||
{ | ||
var batch = await dbContext.Batches.Include(b => b.Manifest) | ||
.ThenInclude(m => m.CanvasPaintings) | ||
.FirstOrDefaultAsync(b => b.Id == batchCompletionMessage.Id, cancellationToken); | ||
|
||
// batch isn't tracked by presentation, so nothing to do | ||
if (batch == null) return; | ||
|
||
// Other batches haven't completed, so no point populating items until all are complete | ||
if (await dbContext.Batches.AnyAsync(b => b.ManifestId == batch.ManifestId && b.Status != BatchStatus.Completed, | ||
cancellationToken)) | ||
{ | ||
return; | ||
} | ||
|
||
logger.LogInformation( | ||
"Attempting to complete assets in batch {BatchId} for customer {CustomerId} with the manifest {ManifestId}", | ||
batch.Id, batch.CustomerId, batch.ManifestId); | ||
|
||
var assets = await RetrieveImages(batch, cancellationToken); | ||
|
||
UpdateCanvasPaintings(assets, batch); | ||
CompleteBatch(batch); | ||
|
||
await dbContext.SaveChangesAsync(cancellationToken); | ||
|
||
logger.LogTrace("updating batch {BatchId} has been completed", batch.Id); | ||
} | ||
|
||
private void CompleteBatch(Batch batch) | ||
{ | ||
batch.Finished = DateTime.UtcNow; //todo: change to actual batch completion time? | ||
batch.Status = BatchStatus.Completed; | ||
} | ||
|
||
private void UpdateCanvasPaintings(HydraCollection<Asset> assets, Batch batch) | ||
{ | ||
if (batch.Manifest?.CanvasPaintings == null) return; | ||
|
||
foreach (var canvasPainting in batch.Manifest.CanvasPaintings) | ||
{ | ||
if (canvasPainting.CanvasOriginalId == null) // Trying to figure out an asset that hasn't been updated | ||
{ | ||
var assetId = AssetId.FromString(canvasPainting.AssetId!); | ||
|
||
var asset = assets.Members.FirstOrDefault(a => a.ResourceId!.Contains($"{assetId.Space}/images/{assetId.Asset}")); | ||
if (asset == null || asset.Ingesting) continue; | ||
|
||
canvasPainting.CanvasOriginalId = | ||
new Uri( | ||
$"{dlcsSettings.OrchestratorUri}/iiif-img/{assetId.Customer}/{assetId.Space}/{assetId.Asset}/full/max/0/default.jpg"); //todo: do we need this? Supposed to be null for an asset really | ||
canvasPainting.Thumbnail = | ||
new Uri( | ||
$"{dlcsSettings.OrchestratorUri}/thumbs/{assetId.Customer}/{assetId.Space}/{assetId.Asset}/100,/max/0/default.jpg"); //todo: move this to class | ||
canvasPainting.Modified = DateTime.UtcNow; | ||
canvasPainting.StaticHeight = asset.Height; | ||
canvasPainting.StaticWidth = asset.Width; | ||
} | ||
} | ||
} | ||
|
||
private async Task<HydraCollection<Asset>> RetrieveImages(Batch batch, CancellationToken cancellationToken) | ||
{ | ||
var assetsRequest = | ||
batch.Manifest?.CanvasPaintings?.Where(c => c.AssetId != null).Select(c => c.AssetId!).ToList() ?? []; | ||
|
||
return await dlcsApiClient.RetrieveAllImages(batch.CustomerId, assetsRequest, cancellationToken); | ||
} | ||
|
||
private static BatchCompletionMessage DeserializeMessage(QueueMessage message) | ||
{ | ||
var deserialized = JsonSerializer.Deserialize<BatchCompletionMessage>(message.Body, JsonSerializerOptions); | ||
return deserialized.ThrowIfNull(nameof(deserialized)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/IIIFPresentation/DLCS/Handlers/AmbientAuthLocalHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Net.Http.Headers; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace DLCS.Handlers; | ||
|
||
public class AmbientAuthLocalHandler() : DelegatingHandler | ||
Check warning on line 6 in src/IIIFPresentation/DLCS/Handlers/AmbientAuthLocalHandler.cs GitHub Actions / test-dotnet
|
||
{ | ||
private readonly DlcsSettings dlcsSettings; | ||
|
||
public AmbientAuthLocalHandler(IOptions<DlcsSettings> dlcsOptions) : this() | ||
{ | ||
dlcsSettings = dlcsOptions.Value; | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, | ||
CancellationToken cancellationToken) | ||
{ | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Basic", dlcsSettings.ApiLocalAuth); | ||
return base.SendAsync(request, cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace DLCS.Models; | ||
|
||
public class AllImages : JsonLdBase | ||
{ | ||
public AllImages(List<string> members) | ||
{ | ||
Members = members.Select(m => new AllImagesMember(m)).ToList(); | ||
Type = "Collection" ; | ||
} | ||
|
||
[JsonPropertyOrder(3)] | ||
[JsonPropertyName("member")] | ||
public List<AllImagesMember> Members { get; set; } | ||
} | ||
|
||
public class AllImagesMember(string id) | ||
{ | ||
public string Id { get; set; } = id; | ||
} | ||
|
||
|
Oops, something went wrong.