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

Store custom sources in SQL instead of blobs #1693

Merged
merged 3 commits into from
Aug 1, 2024
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
5 changes: 3 additions & 2 deletions backend/api.test/EventHandlers/TestMissionEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public TestMissionEventHandler(DatabaseFixture fixture)
var returnToHomeServiceLogger = new Mock<ILogger<ReturnToHomeService>>().Object;
var missionDefinitionServiceLogger = new Mock<ILogger<MissionDefinitionService>>().Object;
var lastMissionRunServiceLogger = new Mock<ILogger<LastMissionRunService>>().Object;
var sourceServiceLogger = new Mock<ILogger<SourceService>>().Object;

var configuration = WebApplication.CreateBuilder().Configuration;

Expand All @@ -66,8 +67,8 @@ public TestMissionEventHandler(DatabaseFixture fixture)

var echoServiceMock = new MockEchoService();
var stidServiceMock = new MockStidService(context);
var customMissionServiceMock = new MockCustomMissionService();
var missionDefinitionService = new MissionDefinitionService(context, echoServiceMock, customMissionServiceMock, signalRService, accessRoleService, missionDefinitionServiceLogger, _missionRunService);
var sourceService = new SourceService(context, echoServiceMock, sourceServiceLogger);
var missionDefinitionService = new MissionDefinitionService(context, echoServiceMock, sourceService, signalRService, accessRoleService, missionDefinitionServiceLogger, _missionRunService);
var robotModelService = new RobotModelService(context);
var taskDurationServiceMock = new MockTaskDurationService();
var isarServiceMock = new MockIsarService();
Expand Down
65 changes: 0 additions & 65 deletions backend/api.test/Mocks/CustomMissionServiceMock.cs

This file was deleted.

1 change: 0 additions & 1 deletion backend/api.test/TestWebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
services.AddScoped<IMapService, MockMapService>();
services.AddScoped<IBlobService, MockBlobService>();
services.AddScoped<IStidService, MockStidService>();
services.AddScoped<ICustomMissionService, MockCustomMissionService>();
services.AddAuthorizationBuilder().AddFallbackPolicy(
TestAuthHandler.AuthenticationScheme, policy => policy.RequireAuthenticatedUser()
);
Expand Down
66 changes: 61 additions & 5 deletions backend/api/Controllers/MissionSchedulingController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Api.Controllers.Models;
using Api.Database.Models;
using Api.Services;
using Api.Services.ActionServices;
using Api.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -14,7 +13,6 @@ namespace Api.Controllers
[Route("missions")]
public class MissionSchedulingController(
IMissionDefinitionService missionDefinitionService,
ICustomMissionSchedulingService customMissionSchedulingService,
IMissionRunService missionRunService,
IInstallationService installationService,
IEchoService echoService,
Expand All @@ -23,7 +21,8 @@ public class MissionSchedulingController(
IStidService stidService,
ILocalizationService localizationService,
IRobotService robotService,
ISourceService sourceService
ISourceService sourceService,
IAreaService areaService
) : ControllerBase

{
Expand Down Expand Up @@ -371,7 +370,41 @@ [FromBody] CustomMissionQuery customMissionQuery
var missionTasks = customMissionQuery.Tasks.Select(task => new MissionTask(task)).ToList();

MissionDefinition? customMissionDefinition;
try { customMissionDefinition = await customMissionSchedulingService.FindExistingOrCreateCustomMissionDefinition(customMissionQuery, missionTasks); }
try
{
Area? area = null;
if (customMissionQuery.AreaName != null) { area = await areaService.ReadByInstallationAndName(customMissionQuery.InstallationCode, customMissionQuery.AreaName); }

if (area == null)
{
throw new AreaNotFoundException($"No area with name {customMissionQuery.AreaName} in installation {customMissionQuery.InstallationCode} was found");
}

var source = await sourceService.CheckForExistingCustomSource(missionTasks);

MissionDefinition? existingMissionDefinition = null;
if (source == null)
{
source = await sourceService.CreateSourceIfDoesNotExist(missionTasks);
}
else
{
var missionDefinitions = await missionDefinitionService.ReadBySourceId(source.SourceId);
if (missionDefinitions.Count > 0) { existingMissionDefinition = missionDefinitions.First(); }
}

customMissionDefinition = existingMissionDefinition ?? new MissionDefinition
{
Id = Guid.NewGuid().ToString(),
Source = source,
Name = customMissionQuery.Name,
InspectionFrequency = customMissionQuery.InspectionFrequency,
InstallationCode = customMissionQuery.InstallationCode,
Area = area
};

if (existingMissionDefinition == null) { await missionDefinitionService.Create(customMissionDefinition); }
}
catch (SourceException e) { return StatusCode(StatusCodes.Status502BadGateway, e.Message); }
catch (AreaNotFoundException) { return NotFound($"No area with name {customMissionQuery.AreaName} in installation {customMissionQuery.InstallationCode} was found"); }

Expand All @@ -380,7 +413,30 @@ [FromBody] CustomMissionQuery customMissionQuery
catch (RobotNotInSameInstallationAsMissionException e) { return Conflict(e.Message); }

MissionRun? newMissionRun;
try { newMissionRun = await customMissionSchedulingService.QueueCustomMissionRun(customMissionQuery, customMissionDefinition.Id, robot.Id, missionTasks); }
try
{
var scheduledMission = new MissionRun
{
Name = customMissionQuery.Name,
Description = customMissionQuery.Description,
MissionId = customMissionDefinition.Id,
Comment = customMissionQuery.Comment,
Robot = robot,
Status = MissionStatus.Pending,
MissionRunType = MissionRunType.Normal,
DesiredStartTime = customMissionQuery.DesiredStartTime ?? DateTime.UtcNow,
Tasks = missionTasks,
InstallationCode = customMissionQuery.InstallationCode,
Area = customMissionDefinition.Area,
Map = new MapMetadata()
};

await mapService.AssignMapToMission(scheduledMission);

if (scheduledMission.Tasks.Any()) { scheduledMission.CalculateEstimatedDuration(); }

newMissionRun = await missionRunService.Create(scheduledMission);
}
catch (Exception e) when (e is UnsupportedRobotCapabilityException) { return BadRequest(e.Message); }
catch (Exception e) when (e is MissionNotFoundException) { return NotFound(e.Message); }
catch (Exception e) when (e is RobotNotFoundException) { return NotFound(e.Message); }
Expand Down
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
Loading
Loading