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

Make Inspection and MissionTask not owned entities #1155

Merged
merged 9 commits into from
Nov 10, 2023
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
4 changes: 2 additions & 2 deletions backend/api/Controllers/MissionDefinitionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ [FromQuery] MissionDefinitionQueryStringParameters parameters
}

/// <summary>
/// List all consended mission definitions in the Flotilla database
/// List all condensed mission definitions in the Flotilla database
/// </summary>
/// <remarks>
/// <para> This query gets all consended mission definitions </para>
/// <para> This query gets all condensed mission definitions </para>
/// </remarks>
[HttpGet("condensed")]
[Authorize(Roles = Role.Any)]
Expand Down
227 changes: 97 additions & 130 deletions backend/api/Database/Context/FlotillaDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,153 +2,120 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Api.Database.Context;

public class FlotillaDbContext : DbContext
namespace Api.Database.Context
{
public DbSet<Robot> Robots => Set<Robot>();
public DbSet<RobotModel> RobotModels => Set<RobotModel>();
public DbSet<MissionRun> MissionRuns => Set<MissionRun>();
public DbSet<MissionDefinition> MissionDefinitions => Set<MissionDefinition>();
public DbSet<Plant> Plants => Set<Plant>();
public DbSet<Installation> Installations => Set<Installation>();
public DbSet<Deck> Decks => Set<Deck>();
public DbSet<Area> Areas => Set<Area>();

public DbSet<Source> Sources => Set<Source>();
public DbSet<SafePosition> SafePositions => Set<SafePosition>();
public DbSet<DefaultLocalizationPose> DefaultLocalizationPoses => Set<DefaultLocalizationPose>();

// Timeseries:
public DbSet<RobotPressureTimeseries> RobotPressureTimeseries => Set<RobotPressureTimeseries>();
public DbSet<RobotBatteryTimeseries> RobotBatteryTimeseries => Set<RobotBatteryTimeseries>();
public DbSet<RobotPoseTimeseries> RobotPoseTimeseries => Set<RobotPoseTimeseries>();

public FlotillaDbContext(DbContextOptions options) : base(options) { }

protected override void OnModelCreating(ModelBuilder modelBuilder)
public class FlotillaDbContext : DbContext
{
bool isSqlLite = Database.ProviderName == "Microsoft.EntityFrameworkCore.Sqlite";
public FlotillaDbContext(DbContextOptions options) : base(options) { }
public DbSet<Robot> Robots => Set<Robot>();
public DbSet<RobotModel> RobotModels => Set<RobotModel>();
public DbSet<MissionRun> MissionRuns => Set<MissionRun>();
public DbSet<MissionTask> MissionTasks => Set<MissionTask>();
public DbSet<Inspection> Inspections => Set<Inspection>();
public DbSet<MissionDefinition> MissionDefinitions => Set<MissionDefinition>();
public DbSet<Plant> Plants => Set<Plant>();
public DbSet<Installation> Installations => Set<Installation>();
public DbSet<Deck> Decks => Set<Deck>();
public DbSet<Area> Areas => Set<Area>();
public DbSet<Source> Sources => Set<Source>();
public DbSet<SafePosition> SafePositions => Set<SafePosition>();
public DbSet<DefaultLocalizationPose> DefaultLocalizationPoses => Set<DefaultLocalizationPose>();

// Timeseries:
public DbSet<RobotPressureTimeseries> RobotPressureTimeseries => Set<RobotPressureTimeseries>();
public DbSet<RobotBatteryTimeseries> RobotBatteryTimeseries => Set<RobotBatteryTimeseries>();
public DbSet<RobotPoseTimeseries> RobotPoseTimeseries => Set<RobotPoseTimeseries>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
bool isSqlLite = Database.ProviderName == "Microsoft.EntityFrameworkCore.Sqlite";

// https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities
// https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities#collections-of-owned-types
modelBuilder.Entity<MissionRun>(
missionRunEntity =>
// https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities
// https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities#collections-of-owned-types
modelBuilder.Entity<MissionRun>(missionRunEntity =>
{
if (isSqlLite) { AddConverterForDateTimeOffsets(ref missionRunEntity); }
});
modelBuilder.Entity<MissionTask>(missionTaskEntity =>
{
if (isSqlLite)
AddConverterForDateTimeOffsets(ref missionRunEntity);
missionRunEntity.OwnsMany(
missionRun => missionRun.Tasks,
taskEntity =>
if (isSqlLite) { AddConverterForDateTimeOffsets(ref missionTaskEntity); }
missionTaskEntity.OwnsOne(task => task.InspectionTarget);
missionTaskEntity.OwnsOne(
task => task.RobotPose,
poseEntity =>
{
if (isSqlLite)
AddConverterForDateTimeOffsets(ref taskEntity);
taskEntity.OwnsMany(
task => task.Inspections,
inspectionEntity =>
{
if (isSqlLite)
AddConverterForDateTimeOffsets(ref inspectionEntity);

inspectionEntity.OwnsMany(i => i.InspectionFindings);

}

);
taskEntity.OwnsOne(task => task.InspectionTarget);
taskEntity.OwnsOne(
task => task.RobotPose,
poseEntity =>
{
poseEntity.OwnsOne(pose => pose.Position);
poseEntity.OwnsOne(pose => pose.Orientation);
}
);
poseEntity.OwnsOne(pose => pose.Position);
poseEntity.OwnsOne(pose => pose.Orientation);
}
);
}
);

modelBuilder.Entity<MissionDefinition>()
.Property(m => m.InspectionFrequency)
.HasConversion(new TimeSpanToTicksConverter());
});
modelBuilder.Entity<Inspection>(inspectionEntity =>
{
if (isSqlLite) { AddConverterForDateTimeOffsets(ref inspectionEntity); }
inspectionEntity.OwnsMany(i => i.InspectionFindings);
});

modelBuilder.Entity<MissionRun>().OwnsOne(m => m.Map).OwnsOne(t => t.TransformationMatrices);
modelBuilder.Entity<MissionRun>().OwnsOne(m => m.Map).OwnsOne(b => b.Boundary);
modelBuilder.Entity<Robot>().OwnsOne(r => r.Pose).OwnsOne(p => p.Orientation);
modelBuilder.Entity<Robot>().OwnsOne(r => r.Pose).OwnsOne(p => p.Position);
modelBuilder.Entity<Robot>().OwnsMany(r => r.VideoStreams);
modelBuilder.Entity<MissionDefinition>()
.Property(m => m.InspectionFrequency)
.HasConversion(new TimeSpanToTicksConverter());

modelBuilder.Entity<SafePosition>().OwnsOne(s => s.Pose, poseBuilder =>
{
poseBuilder.OwnsOne(pose => pose.Position);
poseBuilder.OwnsOne(pose => pose.Orientation);
});
modelBuilder.Entity<DefaultLocalizationPose>().OwnsOne(d => d.Pose, poseBuilder =>
{
poseBuilder.OwnsOne(pose => pose.Position);
poseBuilder.OwnsOne(pose => pose.Orientation);
});
modelBuilder.Entity<MissionRun>().OwnsOne(m => m.Map).OwnsOne(t => t.TransformationMatrices);
modelBuilder.Entity<MissionRun>().OwnsOne(m => m.Map).OwnsOne(b => b.Boundary);
modelBuilder.Entity<Robot>().OwnsOne(r => r.Pose).OwnsOne(p => p.Orientation);
modelBuilder.Entity<Robot>().OwnsOne(r => r.Pose).OwnsOne(p => p.Position);
modelBuilder.Entity<Robot>().OwnsMany(r => r.VideoStreams);

// There can only be one robot model per robot type
modelBuilder.Entity<RobotModel>().HasIndex(model => model.Type).IsUnique();

// There can only be one unique installation and plant shortname
modelBuilder.Entity<Installation>().HasIndex(a => new { a.InstallationCode }).IsUnique();
modelBuilder.Entity<Plant>().HasIndex(a => new { a.PlantCode }).IsUnique();
modelBuilder.Entity<SafePosition>().OwnsOne(s => s.Pose, poseBuilder =>
{
poseBuilder.OwnsOne(pose => pose.Position);
poseBuilder.OwnsOne(pose => pose.Orientation);
});
modelBuilder.Entity<DefaultLocalizationPose>().OwnsOne(d => d.Pose, poseBuilder =>
{
poseBuilder.OwnsOne(pose => pose.Position);
poseBuilder.OwnsOne(pose => pose.Orientation);
});

modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Plant).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Deck>().HasOne(d => d.Plant).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Deck>().HasOne(d => d.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Plant>().HasOne(p => p.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
}
// There can only be one robot model per robot type
modelBuilder.Entity<RobotModel>().HasIndex(model => model.Type).IsUnique();

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties(typeof(Enum)).HaveConversion<string>();
}
// There can only be one unique installation and plant shortname
modelBuilder.Entity<Installation>().HasIndex(a => new
{
a.InstallationCode
}).IsUnique();
modelBuilder.Entity<Plant>().HasIndex(a => new
{
a.PlantCode
}).IsUnique();

modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Plant).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Deck>().HasOne(d => d.Plant).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Deck>().HasOne(d => d.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Plant>().HasOne(p => p.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
}

// SQLite does not have proper support for DateTimeOffset via Entity Framework Core, see the limitations
// here: https://docs.microsoft.com/en-us/ef/core/providers/sqlite/limitations#query-limitations
// To work around this, when the Sqlite database provider is used, all model properties of type DateTimeOffset
// use the DateTimeOffsetToBinaryConverter
// Based on: https://github.com/aspnet/EntityFrameworkCore/issues/10784#issuecomment-415769754
// This only supports millisecond precision, but should be sufficient for most use cases.
private static void AddConverterForDateTimeOffsets<TOwnerEntity, TDependentEntity>(
ref OwnedNavigationBuilder<TOwnerEntity, TDependentEntity> entity
)
where TOwnerEntity : class
where TDependentEntity : class
{
var properties = entity.OwnedEntityType.ClrType
.GetProperties()
.Where(
p =>
p.PropertyType == typeof(DateTimeOffset)
|| p.PropertyType == typeof(DateTimeOffset?)
);
foreach (var property in properties)
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
entity.Property(property.Name).HasConversion(new DateTimeOffsetToBinaryConverter());
configurationBuilder.Properties(typeof(Enum)).HaveConversion<string>();
}
}

private static void AddConverterForDateTimeOffsets<T>(ref EntityTypeBuilder<T> entity)
where T : class
{
var properties = entity.Metadata.ClrType
.GetProperties()
.Where(
p =>
p.PropertyType == typeof(DateTimeOffset)
|| p.PropertyType == typeof(DateTimeOffset?)
);
foreach (var property in properties)
private static void AddConverterForDateTimeOffsets<T>(ref EntityTypeBuilder<T> entity)
where T : class
{
entity.Property(property.Name).HasConversion(new DateTimeOffsetToBinaryConverter());
var properties = entity.Metadata.ClrType
.GetProperties()
.Where(
p =>
p.PropertyType == typeof(DateTimeOffset)
|| p.PropertyType == typeof(DateTimeOffset?)
);
foreach (var property in properties)
{
entity.Property(property.Name).HasConversion(new DateTimeOffsetToBinaryConverter());
}
}
}
}
2 changes: 0 additions & 2 deletions backend/api/Database/Models/Inspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
using System.ComponentModel.DataAnnotations.Schema;
using Api.Controllers.Models;
using Api.Services.Models;
using Microsoft.EntityFrameworkCore;
#pragma warning disable CS8618
namespace Api.Database.Models
{
[Owned]
public class Inspection
{

Expand Down
Loading
Loading