Refactor UpdateCheapestIntervals logic to return a result object and improve code readability. Introduce CheapestIntervalsUpdateResult model for encapsulating update details and simplify inline tuples.
This commit is contained in:
@@ -79,13 +79,10 @@ public class NordPoolInit : INordPoolInit
|
||||
|
||||
private void ScheduleEndOfDayReminder()
|
||||
{
|
||||
_scheduler.ScheduleCron("0 20 * * *", () =>
|
||||
{
|
||||
_ = CheckSkipPriceCapReminderAsync();
|
||||
});
|
||||
_scheduler.ScheduleCron("0 20 * * *", CheckSkipPriceCapReminderAsync);
|
||||
}
|
||||
|
||||
private async Task CheckSkipPriceCapReminderAsync()
|
||||
private void CheckSkipPriceCapReminderAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -111,7 +108,11 @@ public class NordPoolInit : INordPoolInit
|
||||
BoilerSkipPriceCapEntity.StateChanges()
|
||||
.Subscribe(e =>
|
||||
{
|
||||
if (e.Old?.State == e.New?.State) return;
|
||||
if (e.Old?.State == e.New?.State)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Skip price cap switch changed, recalculating cheapest intervals");
|
||||
_ = RecalculateOnConfigChangeAsync();
|
||||
});
|
||||
@@ -121,8 +122,8 @@ public class NordPoolInit : INordPoolInit
|
||||
{
|
||||
try
|
||||
{
|
||||
var (intervalCount, priceCap, skipPriceCap) = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(intervalCount, priceCap, skipPriceCap);
|
||||
var result = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(result.IntervalCount, result.PriceCap, result.SkipPriceCap);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -152,8 +153,8 @@ public class NordPoolInit : INordPoolInit
|
||||
_logger.LogWarning("Current price is 0, which may indicate an issue with price retrieval");
|
||||
}
|
||||
|
||||
var (intervalCount, priceCap, skipPriceCap) = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(intervalCount, priceCap, skipPriceCap);
|
||||
var result = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(result.IntervalCount, result.PriceCap, result.SkipPriceCap);
|
||||
|
||||
_logger.LogInformation("NordPool price initialized with current price: {CurrentPrice} {Unit}", _priceService.CurrentPrice, UnitOfMeasurement);
|
||||
}
|
||||
@@ -218,8 +219,8 @@ public class NordPoolInit : INordPoolInit
|
||||
|
||||
_priceService.UpdateCurrentPrice();
|
||||
|
||||
var (intervalCount, priceCap, skipPriceCap) = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(intervalCount, priceCap, skipPriceCap);
|
||||
var result = UpdateCheapestIntervals();
|
||||
await UpdateEntitiesAsync(result.IntervalCount, result.PriceCap, result.SkipPriceCap);
|
||||
|
||||
_logger.LogInformation("NordPool prices updated successfully. Current price: {CurrentPrice} {Unit}", _priceService.CurrentPrice, UnitOfMeasurement);
|
||||
}
|
||||
@@ -234,7 +235,7 @@ public class NordPoolInit : INordPoolInit
|
||||
await _entityService.CleanupEntitiesAsync();
|
||||
}
|
||||
|
||||
private (int IntervalCount, double PriceCap, bool SkipPriceCap) UpdateCheapestIntervals()
|
||||
private CheapestIntervalsUpdateResult UpdateCheapestIntervals()
|
||||
{
|
||||
var boilerConfig = ReadBoilerConfig();
|
||||
var effectivePriceCap = boilerConfig.SkipPriceCap ? double.MaxValue : boilerConfig.PriceCap;
|
||||
@@ -246,7 +247,12 @@ public class NordPoolInit : INordPoolInit
|
||||
{
|
||||
_logger.LogDebug("No today prices available, skipping cheapest intervals calculation");
|
||||
CheapestIntervals = [];
|
||||
return (boilerConfig.IntervalCount, boilerConfig.PriceCap, boilerConfig.SkipPriceCap);
|
||||
return new CheapestIntervalsUpdateResult
|
||||
{
|
||||
IntervalCount = boilerConfig.IntervalCount,
|
||||
PriceCap = boilerConfig.PriceCap,
|
||||
SkipPriceCap = boilerConfig.SkipPriceCap
|
||||
};
|
||||
}
|
||||
|
||||
CheapestIntervals = _cheapestIntervalsFinder.FindCheapestIntervals(_priceService.TodayNordPoolPrice, effectiveIntervalCount, effectivePriceCap, boilerConfig.Deadline);
|
||||
@@ -260,7 +266,12 @@ public class NordPoolInit : INordPoolInit
|
||||
CheapestIntervals = [];
|
||||
}
|
||||
|
||||
return (boilerConfig.IntervalCount, boilerConfig.PriceCap, boilerConfig.SkipPriceCap);
|
||||
return new CheapestIntervalsUpdateResult
|
||||
{
|
||||
IntervalCount = boilerConfig.IntervalCount,
|
||||
PriceCap = boilerConfig.PriceCap,
|
||||
SkipPriceCap = boilerConfig.SkipPriceCap
|
||||
};
|
||||
}
|
||||
|
||||
private BoilerConfig ReadBoilerConfig()
|
||||
|
||||
Reference in New Issue
Block a user