feat: add OtherPowerCalculationService with EMA smoothing and TDD tests
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using PowerMonitorManager;
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
namespace PowerMonitor.Manager.Tests;
|
||||
|
||||
public class OtherPowerCalculationServiceTests
|
||||
{
|
||||
private static OtherPowerCalculationService CreateSut(int smoothingWindowSize = 10)
|
||||
{
|
||||
var options = Options.Create(new PowerMonitorOptions
|
||||
{
|
||||
SmoothingWindowSize = smoothingWindowSize
|
||||
});
|
||||
return new OtherPowerCalculationService(options);
|
||||
}
|
||||
|
||||
#region Basic calculation
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ShouldReturnOtherAsTotalMinusDevices()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = new Dictionary<string, double>
|
||||
{
|
||||
["sensor.device_a_power"] = 500,
|
||||
["sensor.device_b_power"] = 300
|
||||
};
|
||||
|
||||
var result = sut.Calculate(1000, devices);
|
||||
|
||||
result.TotalPowerWatts.Should().Be(1000);
|
||||
result.KnownDevicesSumWatts.Should().Be(800);
|
||||
result.RawOtherPowerWatts.Should().Be(200);
|
||||
result.DataQuality.Should().Be("ok");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_WhenDevicesSumExceedsTotal_ShouldClampToZero()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = new Dictionary<string, double>
|
||||
{
|
||||
["sensor.device_a_power"] = 800,
|
||||
["sensor.device_b_power"] = 500
|
||||
};
|
||||
|
||||
var result = sut.Calculate(1000, devices);
|
||||
|
||||
result.RawOtherPowerWatts.Should().Be(0);
|
||||
result.SmoothedOtherPowerWatts.Should().Be(0);
|
||||
result.DataQuality.Should().Be("device_sum_exceeds_total");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_WithNoDevices_ShouldReturnTotalAsOther()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
var result = sut.Calculate(1500, devices);
|
||||
|
||||
result.RawOtherPowerWatts.Should().Be(1500);
|
||||
result.KnownDevicesSumWatts.Should().Be(0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_ShouldIncludeAllDeviceReadingsInResult()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 1);
|
||||
var devices = new Dictionary<string, double>
|
||||
{
|
||||
["sensor.a"] = 100,
|
||||
["sensor.b"] = 200
|
||||
};
|
||||
|
||||
var result = sut.Calculate(500, devices);
|
||||
|
||||
result.DevicePowerReadings.Should().HaveCount(2);
|
||||
result.DevicePowerReadings["sensor.a"].Should().Be(100);
|
||||
result.DevicePowerReadings["sensor.b"].Should().Be(200);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region EMA smoothing
|
||||
|
||||
[Fact]
|
||||
public void Calculate_WithSmoothing_ShouldApplyExponentialMovingAverage()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 4);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
var result1 = sut.Calculate(1000, devices);
|
||||
result1.SmoothedOtherPowerWatts.Should().BeApproximately(1000, 0.1);
|
||||
|
||||
var result2 = sut.Calculate(500, devices);
|
||||
result2.SmoothedOtherPowerWatts.Should().BeLessThan(1000);
|
||||
result2.SmoothedOtherPowerWatts.Should().BeGreaterThan(500);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Calculate_MultipleCallsWithSameValue_ShouldConverge()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 5);
|
||||
var devices = new Dictionary<string, double>();
|
||||
|
||||
PowerCalculationResult? result = null;
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
result = sut.Calculate(300, devices);
|
||||
}
|
||||
|
||||
result!.SmoothedOtherPowerWatts.Should().BeApproximately(300, 0.1);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Reset
|
||||
|
||||
[Fact]
|
||||
public void Reset_ShouldClearSmoothedState()
|
||||
{
|
||||
var sut = CreateSut(smoothingWindowSize: 10);
|
||||
var devices = new Dictionary<string, double>();
|
||||
sut.Calculate(1000, devices);
|
||||
sut.Calculate(1000, devices);
|
||||
|
||||
sut.Reset();
|
||||
var result = sut.Calculate(100, devices);
|
||||
|
||||
result.SmoothedOtherPowerWatts.Should().BeApproximately(100, 0.1);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
namespace PowerMonitorManager.Interface;
|
||||
|
||||
public interface IOtherPowerCalculationService
|
||||
{
|
||||
PowerCalculationResult Calculate(double totalPowerWatts, IReadOnlyDictionary<string, double> deviceReadings);
|
||||
void Reset();
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using PowerMonitorManager.Interface;
|
||||
using PowerMonitorManager.Models;
|
||||
|
||||
namespace PowerMonitorManager;
|
||||
|
||||
public sealed class OtherPowerCalculationService : IOtherPowerCalculationService
|
||||
{
|
||||
private readonly double _alpha;
|
||||
private double? _previousSmoothed;
|
||||
private readonly object _lock = new();
|
||||
|
||||
public OtherPowerCalculationService(IOptions<PowerMonitorOptions> options)
|
||||
{
|
||||
var windowSize = options.Value.SmoothingWindowSize;
|
||||
_alpha = 2.0 / (windowSize + 1);
|
||||
}
|
||||
|
||||
public PowerCalculationResult Calculate(double totalPowerWatts, IReadOnlyDictionary<string, double> deviceReadings)
|
||||
{
|
||||
var knownSum = deviceReadings.Values.Sum();
|
||||
var rawOther = totalPowerWatts - knownSum;
|
||||
var dataQuality = "ok";
|
||||
|
||||
if (rawOther < 0)
|
||||
{
|
||||
rawOther = 0;
|
||||
dataQuality = "device_sum_exceeds_total";
|
||||
}
|
||||
|
||||
double smoothed;
|
||||
lock (_lock)
|
||||
{
|
||||
smoothed = _previousSmoothed.HasValue
|
||||
? (_alpha * rawOther) + ((1 - _alpha) * _previousSmoothed.Value)
|
||||
: rawOther;
|
||||
_previousSmoothed = smoothed;
|
||||
}
|
||||
|
||||
return new PowerCalculationResult
|
||||
{
|
||||
TotalPowerWatts = totalPowerWatts,
|
||||
KnownDevicesSumWatts = knownSum,
|
||||
RawOtherPowerWatts = rawOther,
|
||||
SmoothedOtherPowerWatts = smoothed,
|
||||
DataQuality = dataQuality,
|
||||
DevicePowerReadings = new Dictionary<string, double>(deviceReadings)
|
||||
};
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
_previousSmoothed = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user