Files
HomeAssistant/NordPool.Manager/CheapestIntervalsFinder.cs
gitea_admin f919d6b25b Apply code formatting cleanup across the monitor projects
- Wrap using directives in `#region` blocks across the Battery, Boiler and NordPool sources and tests
- Expand single-statement guards and loops into braced blocks
- Realign wrapped method arguments, chained calls and continuations
- Replace target-typed `new()` with explicit generic construction and empty string literals with `string.Empty`
- Flatten the boiler config expression and reindent the remaining touched files
2026-12-01 12:33:31 +02:00

56 lines
1.9 KiB
C#

#region
using Microsoft.Extensions.Logging;
using NordPool.Models;
using NordPoolManager.Interface;
#endregion
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);
}
}