Skip to content

Commit

Permalink
Sample switch consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
just-seba committed Sep 25, 2024
1 parent b3873ec commit 67d54d2
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public string GetSamplingSql(Func<Type, string> tableNameProvider, string timest
Producer.TotalEnergyProduction.Value
)}",
"{nameof(Producer.OwnedBy)}"
FROM "{tableNameProvider(typeof(Producer))}"
FROM "{tableNameProvider(typeof(Producer))}";
""";

return sql;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using OpenEMS.Analytics;
using OpenEMS.Domain.Consumers;

namespace OpenEMS.Infrastructure.Analytics;

public class PostgreSqlSwitchConsumerSamplingSqlFactory : IDbContextDeviceSamplingSqlFactory
{
public string GetSamplingSql(Func<Type, string> tableNameProvider, string timestampParam)
{
var sql = $"""
INSERT INTO "{tableNameProvider(typeof(SwitchConsumerSample))}" (
"{nameof(SwitchConsumerSample.SwitchConsumerId)}",
"{nameof(SwitchConsumerSample.Timestamp)}",
"{nameof(SwitchConsumerSample.CurrentPowerConsumption)}",
"{nameof(SwitchConsumerSample.TotalEnergyConsumption)}",
"{nameof(SwitchConsumerSample.OwnedBy)}"
)
SELECT
"{nameof(SwitchConsumer.Id)}",
{timestampParam},
"{nameof(SwitchConsumer.CurrentPowerConsumption)}",
"{nameof(SwitchConsumer.TotalEnergyConsumption)}_{nameof(
SwitchConsumer.TotalEnergyConsumption.Value
)}",
"{nameof(SwitchConsumer.OwnedBy)}"
FROM "{tableNameProvider(typeof(SwitchConsumer))}";
""";

return sql;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
numberOfSamples: 1,
switchConsumerSamples: [
{
SwitchConsumerId: switch-consumer-id,
Timestamp: 2024-08-31 13:34:59 +0,
CurrentPowerConsumption: 200,
TotalEnergyConsumption: 400,
OwnedBy: test-user
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NSubstitute;
using OpenEMS.Application.Shared.Identity;
using OpenEMS.Domain;
using OpenEMS.Domain.Consumers;
using OpenEMS.Domain.Producers;
using OpenEMS.Domain.Units;
using OpenEMS.Infrastructure.Analytics;
Expand All @@ -12,7 +13,7 @@ namespace OpenEMS.Infrastructure.Test.Analytics;
public class DbContextDeviceSamplerTest
{
[Fact]
public async Task Creates_Samples()
public async Task Creates_Producer_Samples()
{
// Arrange
var userId = UserId.From("test-user");
Expand Down Expand Up @@ -61,6 +62,58 @@ public async Task Creates_Samples()
await Verify(result).DontScrubDateTimes().AddNamedGuid(producer.Id.Value, "producer-id");
}

[Fact]
public async Task Creates_Switch_Consumer_Samples()
{
// Arrange
var userId = UserId.From("test-user");
var switchConsumer = new SwitchConsumer
{
Name = DeviceName.From("switch-consumer-1"),
Integration = new IntegrationIdentifier(
IntegrationId.From("switch-consumer"),
IntegrationDeviceId.From("1")
),
OwnedBy = userId,
};

// Maximum power: 5000W and current power: 200W
switchConsumer.ReportCurrentPowerConsumption(Watt.From(5000));
switchConsumer.ReportCurrentPowerConsumption(Watt.From(200));

// TotalEnergy.Value: 400Wh and TotalEnergy.LastReported: 100Wh
switchConsumer.ReportTotalEnergyConsumption(WattHours.From(300));
switchConsumer.ReportTotalEnergyConsumption(WattHours.From(100));

using var arrangeContext = await TestDbContext.CreateNew();
await AddAndSave(arrangeContext, switchConsumer);

var utcNow = new DateTimeOffset(2024, 08, 31, 13, 34, 59, TimeSpan.Zero);

// Act
using var actContext = TestDbContext.FromExisting(arrangeContext);
var sampler = CreateSampler(actContext);
var numberOfSamples = await sampler.TakeSamples(utcNow);

// Assert
var currentUserReader = Substitute.For<ICurrentUserReader>();
currentUserReader.GetUserIdOrThrow().Returns(userId);
await using var assertContext = TestDbContext.FromExisting(
arrangeContext,
currentUserReader
);

var result = new
{
numberOfSamples,
switchConsumerSamples = await assertContext.SwitchConsumerSamples.ToArrayAsync(),
};

await Verify(result)
.DontScrubDateTimes()
.AddNamedGuid(switchConsumer.Id.Value, "switch-consumer-id");
}

private static async Task AddAndSave(TestDbContext context, params object[] devices)
{
context.AddRange(devices);
Expand Down

0 comments on commit 67d54d2

Please sign in to comment.