Files
HomeAssistant/NordPool.Manager/CheapestIntervalsFinder.cs
T
gitea_admin 77458383d1 Add braces to single-statement guards and refactor NordPool price fetching
- Expand inline guard, return and logger blocks across the monitor services, init classes and tests
- Have INordPoolPriceService take a per-date fetch delegate and expose ParseAndAdjust, moving the HA fetch call into NordPoolInit so the service can be registered singleton
- Record the NordPool and boiler service lifetimes in AGENTS.md
- Drop the unused persisted-state argument from the boiler test data helper
2026-12-01 11:59:31 +02:00

52 lines
1.7 KiB
C#

using Microsoft.Extensions.Logging;
using NordPool.Models;
using NordPoolManager.Interface;
namespace NordPoolManager;
public class CheapestIntervalsFinder(ILogger<CheapestIntervalsFinder> logger) : ICheapestIntervalsFinder
{
public IReadOnlyList<CheapestIntervalResult> FindCheapestIntervals(NordPoolPrice prices, int numberOfIntervals, double priceCap, DateTime deadline)
{
if (prices.Prices is not { Count: > 0 })
{
logger.LogWarning("No prices available for cheapest interval calculation");
return [];
}
if (numberOfIntervals <= 0)
{
logger.LogWarning("Number of intervals requested is {Count}, returning empty", numberOfIntervals);
return [];
}
var eligible = prices.Prices
.Where(p => p is { Start: not null, End: not null, Price: not null })
.Where(p => p.End!.Value <= deadline)
.Where(p => p.Price!.Value <= priceCap)
.OrderBy(p => p.Price!.Value)
.ThenBy(p => p.Start!.Value)
.Take(numberOfIntervals)
.ToList();
var ranked = eligible
.Select((p, index) => new CheapestIntervalResult
{
Start = p.Start!.Value,
End = p.End!.Value,
Price = p.Price!.Value,
Rank = index + 1
})
.OrderBy(r => r.Start)
.ToList();
return ranked;
}
public bool IsCurrentIntervalCheap(IReadOnlyList<CheapestIntervalResult> cheapestIntervals, DateTime utcNow)
{
return cheapestIntervals.Any(interval => utcNow >= interval.Start && utcNow < interval.End);
}
}