Skip to content

Commit

Permalink
Merge pull request #12 from Nesteo/sample-data-generator
Browse files Browse the repository at this point in the history
Sample data generator added
  • Loading branch information
MarcusWichelmann authored Oct 15, 2019
2 parents 73bc4f2 + a3c6f9b commit 4b53ee7
Show file tree
Hide file tree
Showing 12 changed files with 1,067 additions and 170 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ COPY . .
# Compile and pack server as self contained app
RUN dotnet publish /src/Nesteo.Server/Nesteo.Server.csproj -c Release -o /app --self-contained --runtime debian-x64

# Compile and pack sample data generation tool as self contained app
RUN dotnet publish /src/Nesteo.Server.SampleDataGenerator/Nesteo.Server.SampleDataGenerator.csproj -c Release -o /app --runtime debian-x64


### Build final image

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Nesteo.Server\Nesteo.Server.csproj" />
</ItemGroup>

</Project>
127 changes: 127 additions & 0 deletions Nesteo.Server.SampleDataGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Nesteo.Server.Data;
using Nesteo.Server.Data.Entities;
using Nesteo.Server.Data.Entities.Identity;
using Nesteo.Server.Data.Enums;

namespace Nesteo.Server.SampleDataGenerator
{
public static class Program
{
public static async Task Main(string[] args)
{
// Create host
IHost host = Server.Program.CreateHostBuilder(args).Build();
await Server.Program.PrepareHostAsync(host).ConfigureAwait(false);

// Request database context
NesteoDbContext dbContext = host.Services.GetRequiredService<NesteoDbContext>();

// Safety check
Console.Write("Do you want to fill the database with sample data? Please type 'yes': ");
if (Console.ReadLine()?.ToLower() != "yes")
return;
Console.Write("Do you really want to do this? This will DELETE all existing data! Please type 'yes': ");
if (Console.ReadLine()?.ToLower() != "yes")
return;

Console.WriteLine("Clearing database...");

// Clear database
dbContext.Regions.RemoveRange(dbContext.Regions);
dbContext.Owners.RemoveRange(dbContext.Owners);
dbContext.Species.RemoveRange(dbContext.Species);
dbContext.NestingBoxes.RemoveRange(dbContext.NestingBoxes);
dbContext.ReservedIdSpaces.RemoveRange(dbContext.ReservedIdSpaces);
dbContext.Inspections.RemoveRange(dbContext.Inspections);
await dbContext.SaveChangesAsync().ConfigureAwait(false);

Console.WriteLine("Generating sample data...");

var random = new Random();
UserEntity user = await dbContext.Users.FirstOrDefaultAsync().ConfigureAwait(false);
if (user == null)
{
Console.WriteLine("Please run the server first to make sure that at least one user exists.");
return;
}

// Generate regions
Console.WriteLine("Generating regions...");
await dbContext.Regions.AddRangeAsync(
Enumerable.Range(1, 20).Select(i => new RegionEntity { Name = $"Owner {i}", NestingBoxIdPrefix = ((char)('A' + (i - 1))).ToString() }))
.ConfigureAwait(false);

// Generate owners
Console.WriteLine("Generating owners...");
await dbContext.Owners.AddRangeAsync(Enumerable.Range(1, 15).Select(i => new OwnerEntity { Name = $"Owner {i}" })).ConfigureAwait(false);

// Generate species
Console.WriteLine("Generating species...");
await dbContext.Species.AddRangeAsync(Enumerable.Range(1, 50).Select(i => new SpeciesEntity { Name = $"Species {i}" })).ConfigureAwait(false);

// Generate nesting boxes
Console.WriteLine("Generating nesting boxes...");
await dbContext.NestingBoxes.AddRangeAsync(Enumerable.Range(1, 400).Select(i => {
RegionEntity region = dbContext.Regions.Local.GetRandomOrDefault();
return new NestingBoxEntity {
Id = $"{region.NestingBoxIdPrefix}{i:00000}",
Region = region,
OldId = random.Next(10) >= 5 ? "Old ID" : null,
ForeignId = random.Next(10) >= 5 ? "Foreign ID" : null,
CoordinateLongitude = 9.724372 + region.Id + (random.NextDouble() - 0.5) / 100,
CoordinateLatitude = 52.353092 + (random.NextDouble() - 0.5) / 100,
HangUpDate = DateTime.UtcNow.AddMonths(-6 - random.Next(24)),
HangUpUser = user,
Owner = dbContext.Owners.Local.GetRandomOrDefault(),
Material = (Material)random.Next(4),
HoleSize = (HoleSize)random.Next(7),
ImageFileName = null,
Comment = "This is a generated nesting box for testing purposes."
};
})).ConfigureAwait(false);

// Generate inspections
Console.WriteLine("Generating inspections...");
await dbContext.Inspections.AddRangeAsync(Enumerable.Range(1, 2000).Select(i => {
NestingBoxEntity nestingBox = dbContext.NestingBoxes.Local.GetRandomOrDefault();
return new InspectionEntity {
NestingBox = nestingBox,
InspectionDate = nestingBox.HangUpDate.GetValueOrDefault().AddMonths(random.Next(6)),
InspectedByUser = user,
HasBeenCleaned = false,
Condition = (Condition)random.Next(3),
JustRepaired = false,
Occupied = true,
ContainsEggs = true,
EggCount = random.Next(6),
ChickCount = random.Next(1, 4),
RingedChickCount = 1,
AgeInDays = random.Next(6),
FemaleParentBirdDiscovery = (ParentBirdDiscovery)random.Next(4),
MaleParentBirdDiscovery = (ParentBirdDiscovery)random.Next(4),
Species = dbContext.Species.Local.GetRandomOrDefault(),
ImageFileName = null,
Comment = "This is a generated inspection for testing purposes."
};
})).ConfigureAwait(false);

Console.WriteLine("Saving sample data...");

// Save changes
await dbContext.SaveChangesAsync().ConfigureAwait(false);

Console.WriteLine("Done.");
}

private static T GetRandomOrDefault<T>(this LocalView<T> localView) where T : class => localView.OrderBy(r => Guid.NewGuid()).FirstOrDefault();
}
}
6 changes: 6 additions & 0 deletions Nesteo.Server.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ ProjectSection(SolutionItems) = preProject
sample.env = sample.env
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nesteo.Server.SampleDataGenerator", "Nesteo.Server.SampleDataGenerator\Nesteo.Server.SampleDataGenerator.csproj", "{CB1814DC-8726-4158-8C7C-D7B86F93ABE8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -31,5 +33,9 @@ Global
{A58836B0-4FCA-46A2-BDDF-4FB8D67C2162}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A58836B0-4FCA-46A2-BDDF-4FB8D67C2162}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A58836B0-4FCA-46A2-BDDF-4FB8D67C2162}.Release|Any CPU.Build.0 = Release|Any CPU
{CB1814DC-8726-4158-8C7C-D7B86F93ABE8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB1814DC-8726-4158-8C7C-D7B86F93ABE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB1814DC-8726-4158-8C7C-D7B86F93ABE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB1814DC-8726-4158-8C7C-D7B86F93ABE8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
2 changes: 0 additions & 2 deletions Nesteo.Server/Data/Entities/NestingBoxEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ public class NestingBoxEntity : IEntity<string>
[Required]
public RegionEntity Region { get; set; }

[Required]
[StringLength(100, MinimumLength = 2)]
public string OldId { get; set; }

[Required]
[StringLength(100, MinimumLength = 2)]
public string ForeignId { get; set; }

Expand Down
6 changes: 3 additions & 3 deletions Nesteo.Server/Data/Enums/Condition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Nesteo.Server.Data.Enums
[JsonConverter(typeof(StringEnumConverter))]
public enum Condition
{
Good,
NeedsRepair,
NeedsReplacement
Good = 0,
NeedsRepair = 1,
NeedsReplacement = 2
}
}
Loading

0 comments on commit 4b53ee7

Please sign in to comment.