Skip to content

Commit

Permalink
Add instrumentation to api (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lehats authored Dec 18, 2023
2 parents dcc849f + feac004 commit 857285a
Show file tree
Hide file tree
Showing 12 changed files with 3,254 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/api/BdmsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class BdmsContext : DbContext
public DbSet<GroundwaterLevelMeasurement> GroundwaterLevelMeasurements { get; set; }
public DbSet<FieldMeasurement> FieldMeasurements { get; set; }
public DbSet<Completion> Completions { get; set; }
public DbSet<Instrumentation> Instrumentations { get; set; }

public BdmsContext(DbContextOptions options)
: base(options)
Expand Down
42 changes: 38 additions & 4 deletions src/api/BdmsContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public static void SeedData(this BdmsContext context)
List<int> qtInclinationDirectionIds = codelists.Where(c => c.Schema == "custom.qt_bore_inc_dir").Select(s => s.Id).ToList();
List<int> chronostratigraphyTopBedrockIds = codelists.Where(c => c.Schema == "custom.chronostratigraphy_top_bedrock").Select(s => s.Id).ToList();
List<int> lithostratigraphyTopBedrockIds = codelists.Where(c => c.Schema == "custom.lithostratigraphy_top_bedrock").Select(s => s.Id).ToList();
List<int> instrumentKindIds = codelists.Where(c => c.Schema == "inst100").Select(s => s.Id).ToList();
List<int> instrumentMaterialIds = codelists.Where(c => c.Schema == "inst101").Select(s => s.Id).ToList();
List<int> instrumentKindIds = codelists.Where(c => c.Schema == CompletionSchemas.InstrumentationKindSchema).Select(s => s.Id).ToList();
List<int> instrumentStatusIds = codelists.Where(c => c.Schema == CompletionSchemas.InstrumentationStatusSchema).Select(s => s.Id).ToList();
List<int> casingKindIds = codelists.Where(c => c.Schema == "casi200").Select(s => s.Id).ToList();
List<int> casingMaterialIds = codelists.Where(c => c.Schema == "casi201").Select(s => s.Id).ToList();
List<int> plasticityIds = codelists.Where(c => c.Schema == "mlpr101").Select(s => s.Id).ToList();
Expand All @@ -103,7 +103,7 @@ public static void SeedData(this BdmsContext context)
List<int> groundwaterLevelMeasurementKindIds = codelists.Where(c => c.Schema == HydrogeologySchemas.GroundwaterLevelMeasurementKindSchema).Select(s => s.Id).ToList();
List<int> fieldMeasurementSampleTypeIds = codelists.Where(c => c.Schema == HydrogeologySchemas.FieldMeasurementSampleTypeSchema).Select(s => s.Id).ToList();
List<int> fieldMeasurementParameterIds = codelists.Where(c => c.Schema == HydrogeologySchemas.FieldMeasurementParameterSchema).Select(s => s.Id).ToList();
List<int> completionKindIds = codelists.Where(c => c.Schema == "completion_kind").Select(s => s.Id).ToList();
List<int> completionKindIds = codelists.Where(c => c.Schema == CompletionSchemas.CompletionKindSchema).Select(s => s.Id).ToList();

// Seed Boreholes
var borehole_ids = 1_000_000;
Expand Down Expand Up @@ -354,7 +354,7 @@ int GetStratigraphyOrCasingId(int currentLayerId, int startId)
.RuleFor(o => o.Humidity, _ => default!)
.RuleFor(o => o.InstrumentKindId, f => f.PickRandom(instrumentKindIds).OrNull(f, .05f))
.RuleFor(o => o.InstrumentKind, _ => default!)
.RuleFor(o => o.InstrumentStatusId, f => f.PickRandom(instrumentMaterialIds).OrNull(f, .05f))
.RuleFor(o => o.InstrumentStatusId, f => f.PickRandom(instrumentStatusIds).OrNull(f, .05f))
.RuleFor(o => o.InstrumentStatus, _ => default!)
.RuleFor(o => o.InstrumentCasingId, f => GetStratigraphyOrCasingId(layer_ids, 7_000_000))
.RuleFor(o => o.InstrumentCasing, _ => default!)
Expand Down Expand Up @@ -723,6 +723,40 @@ FieldMeasurement SeededFieldMeasurements(Observation observation)

context.SaveChanges();

// Seed Instrumentation
var instrumentation_ids = 15_000_000;
var fakeInstrumentation = new Faker<Instrumentation>()
.RuleFor(i => i.CompletionId, f => f.PickRandom(completions.Select(c => c.Id)))
.RuleFor(i => i.Completion, _ => default!)
.RuleFor(i => i.FromDepth, f => (instrumentation_ids % 10) * 10)
.RuleFor(i => i.ToDepth, f => ((instrumentation_ids % 10) + 1) * 10)
.RuleFor(i => i.Name, f => f.Vehicle.Model())
.RuleFor(i => i.KindId, f => f.PickRandom(instrumentKindIds))
.RuleFor(i => i.Kind, _ => default!)
.RuleFor(i => i.StatusId, f => f.PickRandom(instrumentStatusIds))
.RuleFor(i => i.Status, _ => 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 => instrumentation_ids++);

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

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

context.BulkInsert(instrumentations, 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
72 changes: 72 additions & 0 deletions src/api/Controllers/InstrumentationController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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 InstrumentationController : BdmsControllerBase<Instrumentation>
{
private readonly BdmsContext context;

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

/// <summary>
/// Asynchronously gets the <see cref="Instrumentation"/>s, optionally filtered by <paramref name="completionId"/>.
/// </summary>
/// <param name="completionId">The id of the completion containing the <see cref="Instrumentation"/> to get.</param>
[HttpGet]
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<IEnumerable<Instrumentation>> GetAsync([FromQuery] int? completionId = null)
{
var instrumentations = context.Instrumentations.AsNoTracking();
if (completionId != null)
{
instrumentations = instrumentations.Where(i => i.CompletionId == completionId);
}

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

/// <summary>
/// Asynchronously gets the <see cref="Instrumentation"/> with the specified <paramref name="id"/>.
/// </summary>
[HttpGet("{id}")]
[Authorize(Policy = PolicyNames.Viewer)]
public async Task<ActionResult<Instrumentation>> GetByIdAsync(int id)
{
var instrumentation = await context.Instrumentations
.AsNoTracking()
.SingleOrDefaultAsync(i => i.Id == id)
.ConfigureAwait(false);

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

return Ok(instrumentation);
}

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

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

/// <inheritdoc />
[Authorize(Policy = PolicyNames.Viewer)]
public override Task<IActionResult> DeleteAsync(int id)
=> base.DeleteAsync(id);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 857285a

Please sign in to comment.