Skip to content

Commit

Permalink
Store custom sources in SQL instead of blobs
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Aug 1, 2024
1 parent eeb8531 commit 810a79c
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 51 deletions.
2 changes: 2 additions & 0 deletions backend/api/Database/Models/Source.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Source

[Required]
public MissionSourceType Type { get; set; }

public string? CustomMissionTasks { get; set; }
}

public enum MissionSourceType
Expand Down
8 changes: 0 additions & 8 deletions backend/api/Options/StorageOptions.cs

This file was deleted.

1 change: 0 additions & 1 deletion backend/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@

builder.Services.Configure<AzureAdOptions>(builder.Configuration.GetSection("AzureAd"));
builder.Services.Configure<MapBlobOptions>(builder.Configuration.GetSection("Maps"));
builder.Services.Configure<StorageOptions>(builder.Configuration.GetSection("Blob"));

builder.Services
.AddControllers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,7 @@ public async Task<MissionDefinition> FindExistingOrCreateCustomMissionDefinition
{
try
{
string sourceUrl = await customMissionService.UploadSource(missionTasks);
source = await sourceService.Create(
new Source
{
SourceId = sourceUrl,
Type = MissionSourceType.Custom
}
);
source = await customMissionService.CreateSourceIfOneDoesNotExist(missionTasks);
}
catch (Exception e)
{
Expand Down
41 changes: 27 additions & 14 deletions backend/api/Services/CustomMissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,64 @@
using System.Text;
using System.Text.Json;
using Api.Database.Models;
using Api.Options;
using Microsoft.Extensions.Options;
namespace Api.Services
{

public interface ICustomMissionService
{
Task<string> UploadSource(List<MissionTask> tasks);
Task<Source> CreateSourceIfOneDoesNotExist(List<MissionTask> tasks);

Task<List<MissionTask>?> GetMissionTasksFromSourceId(string id);

string CalculateHashFromTasks(IList<MissionTask> tasks);
}

public class CustomMissionService(IOptions<StorageOptions> storageOptions, IBlobService blobService) : ICustomMissionService
public class CustomMissionService(ILogger<CustomMissionService> logger, ISourceService sourceService) : ICustomMissionService
{
public async Task<string> UploadSource(List<MissionTask> tasks)
public async Task<Source> CreateSourceIfOneDoesNotExist(List<MissionTask> tasks)
{
string json = JsonSerializer.Serialize(tasks);
string hash = CalculateHashFromTasks(tasks);
await blobService.UploadJsonToBlob(json, hash, storageOptions.Value.CustomMissionContainerName, storageOptions.Value.AccountName, false);

return hash;
var existingSource = await sourceService.ReadById(hash);

if (existingSource != null) return existingSource;

var newSource = await sourceService.Create(
new Source
{
SourceId = hash,
Type = MissionSourceType.Custom,
CustomMissionTasks = json
}
);

return newSource;
}

public async Task<List<MissionTask>?> GetMissionTasksFromSourceId(string id)
{
List<MissionTask>? content;
var existingSource = await sourceService.ReadById(id);
if (existingSource == null || existingSource.CustomMissionTasks == null) return null;

try
{
byte[] rawContent = await blobService.DownloadBlob(id, storageOptions.Value.CustomMissionContainerName, storageOptions.Value.AccountName);
var rawBinaryContent = new BinaryData(rawContent);
content = rawBinaryContent.ToObjectFromJson<List<MissionTask>>();
var content = JsonSerializer.Deserialize<List<MissionTask>>(existingSource.CustomMissionTasks);

if (content == null) return null;

foreach (var task in content)
{
task.Id = Guid.NewGuid().ToString(); // This is needed as tasks are owned by mission runs
task.IsarTaskId = Guid.NewGuid().ToString(); // This is needed to update the tasks for the correct mission run
}
return content;
}
catch (Exception)
catch (Exception e)
{
logger.LogWarning("Unable to deserialize custom mission tasks with ID {Id}. {ErrorMessage}", id, e);
return null;
}

return content;
}

public string CalculateHashFromTasks(IList<MissionTask> tasks)
Expand Down
4 changes: 0 additions & 4 deletions backend/api/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
"MaxRetryAttempts": 5,
"ShouldFailOnMaxRetries": false
},
"Blob": {
"CustomMissionContainerName": "custommission",
"AccountName": "flotilladevsa"
},
"Database": {
"UseInMemoryDatabase": false
}
Expand Down
4 changes: 0 additions & 4 deletions backend/api/appsettings.Local.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
"MaxRetryAttempts": 5,
"ShouldFailOnMaxRetries": false
},
"Blob": {
"CustomMissionContainerName": "custommission",
"AccountName": "flotilladevsa"
},
"ApplicationInsights": {
"ConnectionString": ""
},
Expand Down
4 changes: 0 additions & 4 deletions backend/api/appsettings.Production.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
},
"AllowedHosts": "*",
"AllowedOrigins": ["https://*.equinor.com/"],
"Blob": {
"CustomMissionContainerName": "custommission",
"AccountName": "flotillaprodsa"
},
"Mqtt": {
"Host": "localhost",
"Port": 1883,
Expand Down
4 changes: 0 additions & 4 deletions backend/api/appsettings.Staging.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
"http://localhost:3001",
"https://localhost:3001"
],
"Blob": {
"CustomMissionContainerName": "custommission",
"AccountName": "flotillastagingsa"
},
"Mqtt": {
"Host": "localhost",
"Port": 1883,
Expand Down
4 changes: 0 additions & 4 deletions backend/api/appsettings.Test.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
"http://localhost:3001",
"https://localhost:3001"
],
"Blob": {
"CustomMissionContainerName": "custommission",
"AccountName": "flotillatestsa"
},
"Mqtt": {
"Host": "localhost",
"Port": 1883,
Expand Down

0 comments on commit 810a79c

Please sign in to comment.