Update
This commit is contained in:
@@ -29,28 +29,17 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
|
||||
if (TodayNordPoolPrice?.Prices == null || TodayNordPoolPrice.Prices.Count == 0)
|
||||
{
|
||||
logger.LogInformation("Fetching today's prices from API for CET date: {Today}", today);
|
||||
TodayNordPoolPrice = await GetNordPoolPriceAsync(today);
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogDebug("Using existing today's prices (already have {Count} prices)", TodayNordPoolPrice.Prices.Count);
|
||||
TodayNordPoolPrice = await FetchPricesForDateWithRetryAsync(today, "today");
|
||||
}
|
||||
|
||||
if (TomorrowNordPoolPrice?.Prices == null || TomorrowNordPoolPrice.Prices.Count == 0)
|
||||
{
|
||||
logger.LogInformation("Fetching tomorrow's prices from API for CET date: {Tomorrow}", today.AddDays(1));
|
||||
TomorrowNordPoolPrice = await GetNordPoolPriceAsync(today.AddDays(1));
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogDebug("Already have tomorrow's prices ({Count} prices)", TomorrowNordPoolPrice.Prices.Count);
|
||||
TomorrowNordPoolPrice = await FetchPricesForDateWithRetryAsync(today.AddDays(1), "tomorrow");
|
||||
}
|
||||
|
||||
LastUpdatedDate = DateTime.UtcNow;
|
||||
LastFetchedDate = today;
|
||||
|
||||
logger.LogInformation("Prices ready for today ({Today}). Today prices count: {TodayCount}, Tomorrow prices count: {TomorrowCount}",
|
||||
today, TodayNordPoolPrice?.Prices?.Count ?? 0, TomorrowNordPoolPrice?.Prices?.Count ?? 0);
|
||||
}
|
||||
|
||||
public double UpdateCurrentPrice()
|
||||
@@ -68,7 +57,6 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
|
||||
if (price.HasValue)
|
||||
{
|
||||
CurrentPrice = price.Value;
|
||||
logger.LogInformation("Current price updated to: {Price} for quarter index {Index}", CurrentPrice, index);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -82,8 +70,6 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
|
||||
|
||||
DayQuartersIndex = index;
|
||||
}
|
||||
|
||||
logger.LogDebug("Returning current price: {Price}", CurrentPrice);
|
||||
|
||||
return CurrentPrice;
|
||||
}
|
||||
@@ -92,22 +78,16 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
|
||||
{
|
||||
if (!LastFetchedDate.HasValue)
|
||||
{
|
||||
logger.LogDebug("No cached prices, need to fetch");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (LastFetchedDate.Value < today)
|
||||
{
|
||||
logger.LogDebug("New day detected, need to fetch fresh prices");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (TodayNordPoolPrice?.Prices == null || TodayNordPoolPrice.Prices.Count == 0)
|
||||
{
|
||||
logger.LogDebug("Today's prices are missing, need to fetch");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -117,8 +97,6 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
|
||||
|
||||
if (currentHour >= 13)
|
||||
{
|
||||
logger.LogDebug("Checking for tomorrow's prices (after 13:00)");
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -128,26 +106,48 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
|
||||
|
||||
public void RollOverPrices(DateOnly today)
|
||||
{
|
||||
logger.LogInformation("New day detected in CET timezone! Rolling over tomorrow's prices to today. Previous date: {PreviousDate}, Current date: {CurrentDate}", LastFetchedDate, today);
|
||||
|
||||
if (TomorrowNordPoolPrice?.Prices is { Count: > 0 })
|
||||
{
|
||||
TodayNordPoolPrice = TomorrowNordPoolPrice;
|
||||
logger.LogInformation("Moved tomorrow's prices ({Count} prices) to today", TodayNordPoolPrice?.Prices?.Count ?? 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
TodayNordPoolPrice = new NordPoolPrice();
|
||||
logger.LogWarning("Tomorrow's prices were not available during rollover, clearing stale today's prices to trigger re-fetch");
|
||||
}
|
||||
|
||||
TomorrowNordPoolPrice = null;
|
||||
logger.LogInformation("Cleared tomorrow's prices");
|
||||
|
||||
DayQuartersIndex = -1;
|
||||
LastFetchedDate = today;
|
||||
}
|
||||
|
||||
private async Task<NordPoolPrice> FetchPricesForDateWithRetryAsync(DateOnly date, string label)
|
||||
{
|
||||
for (var attempt = 1; attempt <= NordPoolConsts.MaxFetchRetryAttempts; attempt++)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await GetNordPoolPriceAsync(date);
|
||||
}
|
||||
catch (InvalidOperationException ex) when (attempt < NordPoolConsts.MaxFetchRetryAttempts)
|
||||
{
|
||||
var delay = TimeSpan.FromSeconds(NordPoolConsts.RetryBaseDelaySeconds * Math.Pow(2, attempt - 1));
|
||||
logger.LogWarning(ex, "Attempt {Attempt}/{MaxAttempts} to fetch {Label} prices for {Date} failed. Retrying in {Delay}", attempt, NordPoolConsts.MaxFetchRetryAttempts, label, date, delay);
|
||||
|
||||
await Task.Delay(delay);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Failed to fetch {Label} prices for {Date} on attempt {Attempt} with unexpected error", label, date, attempt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
logger.LogWarning("All {MaxAttempts} attempts to fetch {Label} prices for {Date} exhausted; returning empty prices", NordPoolConsts.MaxFetchRetryAttempts, label, date);
|
||||
|
||||
return new NordPoolPrice();
|
||||
}
|
||||
|
||||
private async Task<NordPoolPrice> GetNordPoolPriceAsync(DateOnly date)
|
||||
{
|
||||
var nordPoolPricesDto = await _nordPoolServices.GetPricesForDateAsync(
|
||||
@@ -160,7 +160,7 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
|
||||
});
|
||||
|
||||
var nordPoolResult = ParseNordPoolResult(nordPoolPricesDto);
|
||||
|
||||
|
||||
return AdjustedNordPoolPrice(nordPoolResult);
|
||||
}
|
||||
|
||||
@@ -211,11 +211,12 @@ public class NordPoolPriceService(Services services, IMapper mapper, ILogger<Nor
|
||||
return Math.Round(cntPrice, ConversionConsts.PriceDecimalPlaces);
|
||||
}
|
||||
|
||||
internal static int GetTimeQuarterIndex(DateTime date)
|
||||
internal static int GetTimeQuarterIndex(DateTime utcDate)
|
||||
{
|
||||
var relativeHour = (date.Hour + 1) % 24;
|
||||
var totalMinutes = (relativeHour * 60) + date.Minute;
|
||||
|
||||
return totalMinutes / 15;
|
||||
var cetTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time");
|
||||
var cetDate = TimeZoneInfo.ConvertTimeFromUtc(utcDate, cetTimeZone);
|
||||
var totalMinutes = (cetDate.Hour * 60) + cetDate.Minute;
|
||||
|
||||
return totalMinutes / NordPoolConsts.MinutesPerInterval;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user