Skip to content

Commit

Permalink
Merge branch 'main' into update-test-seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
danjov authored Jan 12, 2024
2 parents 5ab14b8 + d0fb8c6 commit 9e0e33b
Show file tree
Hide file tree
Showing 12 changed files with 3,318 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/api/BdmsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class BdmsContext : DbContext
public DbSet<FieldMeasurement> FieldMeasurements { get; set; }
public DbSet<Completion> Completions { get; set; }
public DbSet<Instrumentation> Instrumentations { get; set; }
public DbSet<Backfill> Backfills { get; set; }

public BdmsContext(DbContextOptions options)
: base(options)
Expand Down
45 changes: 39 additions & 6 deletions src/api/BdmsContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public static void SeedData(this BdmsContext context)
List<int> humidityIds = codelists.Where(c => c.Schema == "mlpr105").Select(s => s.Id).ToList();
List<int> alterationIds = codelists.Where(c => c.Schema == "mlpr106").Select(s => s.Id).ToList();
List<int> cohesionIds = codelists.Where(c => c.Schema == "mlpr116").Select(s => s.Id).ToList();
List<int> fillKindIds = codelists.Where(c => c.Schema == "fill100").Select(s => s.Id).ToList();
List<int> fillMaterialIds = codelists.Where(c => c.Schema == "fill200").Select(s => s.Id).ToList();
List<int> backfillKindIds = codelists.Where(c => c.Schema == CompletionSchemas.BackfillKindSchema).Select(s => s.Id).ToList();
List<int> backfillMaterialIds = codelists.Where(c => c.Schema == CompletionSchemas.BackfillMaterialSchema).Select(s => s.Id).ToList();
List<int> uscsIds = codelists.Where(c => c.Schema == "mcla101").Select(s => s.Id).ToList();
List<int> uscsDeterminationIds = codelists.Where(c => c.Schema == "mcla104").Select(s => s.Id).ToList();
List<int> gradationIds = codelists.Where(c => c.Schema == "gradation").Select(s => s.Id).ToList();
Expand Down Expand Up @@ -340,9 +340,9 @@ int GetStratigraphyOrCasingId(int currentLayerId, int startId)
.RuleFor(o => o.CreatedBy, _ => default!)
.RuleFor(o => o.UpdatedById, f => f.PickRandom(userRange))
.RuleFor(o => o.UpdatedBy, _ => default!)
.RuleFor(o => o.FillKindId, f => f.PickRandom(fillKindIds).OrNull(f, .05f))
.RuleFor(o => o.FillKindId, f => f.PickRandom(backfillKindIds).OrNull(f, .05f))
.RuleFor(o => o.FillKind, _ => default!)
.RuleFor(o => o.FillMaterialId, f => f.PickRandom(fillMaterialIds).OrNull(f, .05f))
.RuleFor(o => o.FillMaterialId, f => f.PickRandom(backfillMaterialIds).OrNull(f, .05f))
.RuleFor(o => o.FillMaterial, _ => default!)
.RuleFor(o => o.GradationId, f => f.PickRandom(gradationIds).OrNull(f, .05f))
.RuleFor(o => o.Gradation, f => default!)
Expand Down Expand Up @@ -744,19 +744,52 @@ FieldMeasurement SeededFieldMeasurements(Observation observation)
.RuleFor(i => i.UpdatedBy, _ => default!)
.RuleFor(i => i.Id, f => instrumentation_ids++);

Instrumentation SeedeInstrumentation(Completion completion)
Instrumentation SeededInstrumentation(Completion completion)
{
return fakeInstrumentation
.UseSeed(completion.Id)
.Generate();
}

var instrumentations = completions.Select(c => SeedeInstrumentation(c)).ToList();
var instrumentations = completions.Select(c => SeededInstrumentation(c)).ToList();

context.BulkInsert(instrumentations, bulkConfig);

context.SaveChanges();

// Seed Backfill
var backfill_ids = 16_000_000;
var fakeBackfill = new Faker<Backfill>()
.RuleFor(b => b.CompletionId, f => f.PickRandom(completions.Select(c => c.Id)))
.RuleFor(b => b.Completion, _ => default!)
.RuleFor(b => b.FromDepth, f => (backfill_ids % 10) * 10)
.RuleFor(b => b.ToDepth, f => ((backfill_ids % 10) + 1) * 10)
.RuleFor(b => b.KindId, f => f.PickRandom(backfillKindIds))
.RuleFor(b => b.Kind, _ => default!)
.RuleFor(b => b.MaterialId, f => f.PickRandom(backfillMaterialIds))
.RuleFor(b => b.Material, _ => default!)
.RuleFor(i => i.Notes, f => f.Random.Words(4))
.RuleFor(i => i.Created, f => f.Date.Past().ToUniversalTime())
.RuleFor(i => i.CreatedById, f => f.PickRandom(userRange))
.RuleFor(i => i.CreatedBy, _ => default!)
.RuleFor(i => i.Updated, f => f.Date.Past().ToUniversalTime())
.RuleFor(i => i.UpdatedById, f => f.PickRandom(userRange))
.RuleFor(i => i.UpdatedBy, _ => default!)
.RuleFor(i => i.Id, f => backfill_ids++);

Backfill SeededBackfill(Completion completion)
{
return fakeBackfill
.UseSeed(completion.Id)
.Generate();
}

var backfills = completions.Select(c => SeededBackfill(c)).ToList();

context.BulkInsert(backfills, bulkConfig);

context.SaveChanges();

// Sync all database sequences
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.workgroups', 'id_wgp'), {workgroup_ids - 1})");
context.Database.ExecuteSqlInterpolated($"SELECT setval(pg_get_serial_sequence('bdms.borehole', 'id_bho'), {borehole_ids - 1})");
Expand Down
75 changes: 75 additions & 0 deletions src/api/Controllers/BackfillController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using BDMS.Authentication;
using BDMS.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace BDMS.Controllers;

[ApiController]
[Route("api/v{version:apiVersion}/[controller]")]
public class BackfillController : BdmsControllerBase<Backfill>
{
public BackfillController(BdmsContext context, ILogger<Backfill> logger)
: base(context, logger)
{
}

/// <summary>
/// Asynchronously gets the <see cref="Backfill"/>s, optionally filtered by <paramref name="completionId"/>.
/// </summary>
/// <param name="completionId">The id of the completion containing the <see cref="Backfill"/> to get.</param>
[HttpGet]
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<IEnumerable<Backfill>> GetAsync([FromQuery] int? completionId = null)
{
var backfills = Context.Backfills
.Include(i => i.Material)
.Include(i => i.Kind)
.AsNoTracking();

if (completionId != null)
{
backfills = backfills.Where(i => i.CompletionId == completionId);
}

return await backfills.ToListAsync().ConfigureAwait(false);
}

/// <summary>
/// Asynchronously gets the <see cref="Backfill"/> with the specified <paramref name="id"/>.
/// </summary>
[HttpGet("{id}")]
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<ActionResult<Backfill>> GetByIdAsync(int id)
{
var backfill = await Context.Backfills
.Include(i => i.Material)
.Include(i => i.Kind)
.AsNoTracking()
.SingleOrDefaultAsync(i => i.Id == id)
.ConfigureAwait(false);

if (backfill == null)
{
return NotFound();
}

return Ok(backfill);
}

/// <inheritdoc />
[Authorize(Policy = PolicyNames.Viewer)]
public override Task<ActionResult<Backfill>> CreateAsync(Backfill entity)
=> base.CreateAsync(entity);

/// <inheritdoc />
[Authorize(Policy = PolicyNames.Viewer)]
public override Task<ActionResult<Backfill>> EditAsync(Backfill entity)
=> base.EditAsync(entity);

/// <inheritdoc />
[Authorize(Policy = PolicyNames.Viewer)]
public override Task<IActionResult> DeleteAsync(int id)
=> base.DeleteAsync(id);
}
7 changes: 2 additions & 5 deletions src/api/Controllers/InstrumentationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ namespace BDMS.Controllers;
[Route("api/v{version:apiVersion}/[controller]")]
public class InstrumentationController : BdmsControllerBase<Instrumentation>
{
private readonly BdmsContext context;

public InstrumentationController(BdmsContext context, ILogger<Instrumentation> logger)
: base(context, logger)
{
this.context = context;
}

/// <summary>
Expand All @@ -26,7 +23,7 @@ public InstrumentationController(BdmsContext context, ILogger<Instrumentation> l
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<IEnumerable<Instrumentation>> GetAsync([FromQuery] int? completionId = null)
{
var instrumentations = context.Instrumentations
var instrumentations = Context.Instrumentations
.Include(i => i.Status)
.Include(i => i.Kind)
.AsNoTracking();
Expand All @@ -46,7 +43,7 @@ public async Task<IEnumerable<Instrumentation>> GetAsync([FromQuery] int? comple
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<ActionResult<Instrumentation>> GetByIdAsync(int id)
{
var instrumentation = await context.Instrumentations
var instrumentation = await Context.Instrumentations
.Include(i => i.Status)
.Include(i => i.Kind)
.AsNoTracking()
Expand Down
Loading

0 comments on commit 9e0e33b

Please sign in to comment.