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

Populating more realistic data #35

Merged
merged 4 commits into from
May 28, 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
168 changes: 0 additions & 168 deletions Infrastructure/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,172 +54,4 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

base.OnModelCreating(modelBuilder);
}

public async Task SeedDevelopmentDataAsync(IServiceScope scope, string defaultPlantImage)
{
var userRepository = scope.ServiceProvider.GetRequiredService<UserRepository>();
await userRepository.CreateUser(new RegisterUserDto
{
Email = "[email protected]",
Password = "password",
Username = "bob"
});

var collectionsRepository = scope.ServiceProvider.GetRequiredService<CollectionsRepository>();
var collection1 = await collectionsRepository.CreateCollection(
new Collection
{
CollectionId = Guid.NewGuid(),
Name = "Succulents",
UserEmail = "[email protected]",
}
);
var collection2 = await collectionsRepository.CreateCollection(
new Collection
{
CollectionId = Guid.NewGuid(),
Name = "Cacti",
UserEmail = "[email protected]",
}
);

var plantRepository = scope.ServiceProvider.GetRequiredService<PlantRepository>();
await plantRepository.CreatePlant(
new Plant
{
PlantId = Guid.NewGuid(),
Nickname = "Aloe Vera",
UserEmail = "[email protected]",
ImageUrl = defaultPlantImage,
CollectionId = collection1.CollectionId,
LatestChange = DateTime.UtcNow.Subtract(TimeSpan.FromDays(5))
}
);

await plantRepository.CreatePlant(
new Plant
{
PlantId = Guid.NewGuid(),
Nickname = "Prickly Pear",
UserEmail = "[email protected]",
ImageUrl = defaultPlantImage,
CollectionId = collection2.CollectionId,
LatestChange = DateTime.UtcNow.Subtract(TimeSpan.FromDays(7))
}
);

await plantRepository.CreatePlant(
new Plant
{
PlantId = Guid.NewGuid(),
Nickname = "Dying plant",
UserEmail = "[email protected]",
ImageUrl = defaultPlantImage,
CollectionId = collection2.CollectionId,
LatestChange = DateTime.UtcNow
}
);

var plants = await plantRepository.GetPlantsForUser("[email protected]", 1, 5);

var requirementsRepository = scope.ServiceProvider.GetRequiredService<RequirementsRepository>();
await requirementsRepository.CreateRequirements(
new Requirements
{
RequirementsId = Guid.NewGuid(),
PlantId = plants.First(p => p.Nickname == "Aloe Vera").PlantId,
LightLevel = RequirementLevel.Low,
SoilMoistureLevel = RequirementLevel.Medium,
HumidityLevel = RequirementLevel.High,
TemperatureLevel = 22,
}
);

await requirementsRepository.CreateRequirements(
new Requirements
{
RequirementsId = Guid.NewGuid(),
PlantId = plants.First(p => p.Nickname == "Prickly Pear").PlantId,
LightLevel = RequirementLevel.High,
SoilMoistureLevel = RequirementLevel.Low,
HumidityLevel = RequirementLevel.Low,
TemperatureLevel = 27,
}
);

await requirementsRepository.CreateRequirements(
new Requirements
{
RequirementsId = Guid.NewGuid(),
PlantId = plants.First(p => p.Nickname == "Dying plant").PlantId,
LightLevel = RequirementLevel.High,
SoilMoistureLevel = RequirementLevel.Low,
HumidityLevel = RequirementLevel.Medium,
TemperatureLevel = 24,
}
);

var conditionsLogRepository = scope.ServiceProvider.GetRequiredService<ConditionsLogsRepository>();

for (var i = 0; i < 100; i++)
{
await conditionsLogRepository.CreateConditionsLogAsync(
GetRandomConditionsLog(plants.First(p => p.Nickname == "Prickly Pear").PlantId, i * 6)
);
await conditionsLogRepository.CreateConditionsLogAsync(
GetRandomConditionsLog(plants.First(p => p.Nickname == "Aloe Vera").PlantId, i * 6)
);
}

await conditionsLogRepository.CreateConditionsLogAsync(
new ConditionsLog()
{
ConditionsId = Guid.NewGuid(),
PlantId = plants.First(p => p.Nickname == "Dying plant").PlantId,
TimeStamp = DateTime.UtcNow,
Mood = 0,
SoilMoisture = 55,
Light = 13,
Humidity = 68,
Temperature = 25,
}
);

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Seeded development data");
Console.ResetColor();
}

private double GetRandomLevelValue()
{
var random = new Random();
return random.NextDouble() * 100;
}

private int GetRandomMood()
{
var random = new Random();
return random.Next(0, 5);
}

private int GetRandomTemperature()
{
var random = new Random();
return random.Next(-20, 45);
}

private ConditionsLog GetRandomConditionsLog(Guid plantId, int hoursAgo = 0)
{
return new ConditionsLog
{
ConditionsId = Guid.NewGuid(),
PlantId = plantId,
TimeStamp = DateTime.UtcNow.Subtract(TimeSpan.FromHours(hoursAgo)),
Mood = GetRandomMood(),
SoilMoisture = GetRandomLevelValue(),
Light = GetRandomLevelValue(),
Temperature = GetRandomTemperature(),
Humidity = GetRandomLevelValue(),
};
}
}
3 changes: 2 additions & 1 deletion Infrastructure/Repositories/PlantRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
await using var context = await dbContextFactory.CreateDbContextAsync();
return await context.Plants
.Include(plant => plant.Requirements)
.Include(plant => plant.ConditionsLogs)
.Include(plant => plant.ConditionsLogs.OrderByDescending(log => log.TimeStamp).Take(1))
.Where(p => p.UserEmail == requesterEmail && p.ConditionsLogs.Count != 0)
.Select(p => new
{
Expand All @@ -108,12 +108,13 @@
.ThenByDescending(log => log.TimeStamp)
.FirstOrDefault()
})
.OrderBy(p => p.WorstMood.Mood)

Check warning on line 111 in Infrastructure/Repositories/PlantRepository.cs

View workflow job for this annotation

GitHub Actions / tests

Dereference of a possibly null reference.
.ThenByDescending(p => p.WorstMood.TimeStamp)

Check warning on line 112 in Infrastructure/Repositories/PlantRepository.cs

View workflow job for this annotation

GitHub Actions / tests

Dereference of a possibly null reference.
.Take(3)
.Select(p => p.Plant)
.ToListAsync();
}

public async Task<Stats> GetStats(string userEmail)
{
await using var context = await dbContextFactory.CreateDbContextAsync();
Expand Down
2 changes: 1 addition & 1 deletion Shared/Dtos/CreateConditionsLogDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class CreateConditionsLogDto
public double Light { get; set; }
public double Temperature { get; set; }
public double Humidity { get; set; }
public long DeviceId { get; set; }
public required string DeviceId { get; set; }
}
3 changes: 1 addition & 2 deletions api/Core/Services/MqttPublisherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public MqttPublisherService(IOptions<MqttOptions> options)
if (string.IsNullOrEmpty(_options.Value.Username) || _options.Value.Username == "FILL_ME_IN")
throw new Exception("MQTT username not set in appsettings.json");
}
public async Task PublishAsync(MoodDto mood, long deviceId)
public async Task PublishAsync(MoodDto mood, string deviceId)
{
var mqttFactory = new MqttFactory();


using var mqttClient = mqttFactory.CreateMqttClient();
var mqttClientOptions = new MqttClientOptionsBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Fleck;
using Shared;
using Shared.Wrappers;

namespace api;
Expand Down
Loading
Loading