Adjust retry policy for extended spot price unavailability (#16653)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2024-04-14 21:48:34 +02:00 committed by GitHub
parent 815ada9afc
commit f4fada5036
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 24 additions and 27 deletions

View File

@ -122,14 +122,10 @@ public class ApiController {
try { try {
String responseContent = sendRequest(request, properties); String responseContent = sendRequest(request, properties);
ElspotpriceRecords records = gson.fromJson(responseContent, ElspotpriceRecords.class); ElspotpriceRecords records = gson.fromJson(responseContent, ElspotpriceRecords.class);
if (records == null) { if (records == null || Objects.isNull(records.records())) {
throw new DataServiceException("Error parsing response"); throw new DataServiceException("Error parsing response");
} }
if (records.total() == 0 || Objects.isNull(records.records()) || records.records().length == 0) {
throw new DataServiceException("No records");
}
return Arrays.stream(records.records()).filter(Objects::nonNull).toArray(ElspotpriceRecord[]::new); return Arrays.stream(records.records()).filter(Objects::nonNull).toArray(ElspotpriceRecord[]::new);
} catch (JsonSyntaxException e) { } catch (JsonSyntaxException e) {
throw new DataServiceException("Error parsing response", e); throw new DataServiceException("Error parsing response", e);

View File

@ -250,12 +250,15 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
updateTimeSeries(); updateTimeSeries();
if (isLinked(CHANNEL_SPOT_PRICE)) { if (isLinked(CHANNEL_SPOT_PRICE)) {
if (cacheManager.getNumberOfFutureSpotPrices() < 13) { long numberOfFutureSpotPrices = cacheManager.getNumberOfFutureSpotPrices();
logger.warn("Spot prices are not yet available, retry scheduled (see details in Thing properties)"); LocalTime now = LocalTime.now(NORD_POOL_TIMEZONE);
retryPolicy = RetryPolicyFactory.whenExpectedSpotPriceDataMissing(DAILY_REFRESH_TIME_CET,
NORD_POOL_TIMEZONE); if (numberOfFutureSpotPrices >= 13 || (numberOfFutureSpotPrices == 12
} else { && now.isAfter(DAILY_REFRESH_TIME_CET.minusHours(1)) && now.isBefore(DAILY_REFRESH_TIME_CET))) {
retryPolicy = RetryPolicyFactory.atFixedTime(DAILY_REFRESH_TIME_CET, NORD_POOL_TIMEZONE); retryPolicy = RetryPolicyFactory.atFixedTime(DAILY_REFRESH_TIME_CET, NORD_POOL_TIMEZONE);
} else {
logger.warn("Spot prices are not available, retry scheduled (see details in Thing properties)");
retryPolicy = RetryPolicyFactory.whenExpectedSpotPriceDataMissing();
} }
} else { } else {
retryPolicy = RetryPolicyFactory.atFixedTime(LocalTime.MIDNIGHT, timeZoneProvider.getTimeZone()); retryPolicy = RetryPolicyFactory.atFixedTime(LocalTime.MIDNIGHT, timeZoneProvider.getTimeZone());
@ -293,10 +296,13 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
Duration.ofHours(-CacheManager.NUMBER_OF_HISTORIC_HOURS)); Duration.ofHours(-CacheManager.NUMBER_OF_HISTORIC_HOURS));
} }
Map<String, String> properties = editProperties(); Map<String, String> properties = editProperties();
ElspotpriceRecord[] spotPriceRecords = apiController.getSpotPrices(config.priceArea, config.getCurrency(), try {
start, properties); ElspotpriceRecord[] spotPriceRecords = apiController.getSpotPrices(config.priceArea, config.getCurrency(),
cacheManager.putSpotPrices(spotPriceRecords, config.getCurrency()); start, properties);
updateProperties(properties); cacheManager.putSpotPrices(spotPriceRecords, config.getCurrency());
} finally {
updateProperties(properties);
}
} }
private void downloadTariffs(DatahubTariff datahubTariff) throws InterruptedException, DataServiceException { private void downloadTariffs(DatahubTariff datahubTariff) throws InterruptedException, DataServiceException {
@ -325,11 +331,11 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
private Collection<DatahubPricelistRecord> downloadPriceLists(GlobalLocationNumber globalLocationNumber, private Collection<DatahubPricelistRecord> downloadPriceLists(GlobalLocationNumber globalLocationNumber,
DatahubTariffFilter filter) throws InterruptedException, DataServiceException { DatahubTariffFilter filter) throws InterruptedException, DataServiceException {
Map<String, String> properties = editProperties(); Map<String, String> properties = editProperties();
Collection<DatahubPricelistRecord> records = apiController.getDatahubPriceLists(globalLocationNumber, try {
ChargeType.Tariff, filter, properties); return apiController.getDatahubPriceLists(globalLocationNumber, ChargeType.Tariff, filter, properties);
updateProperties(properties); } finally {
updateProperties(properties);
return records; }
} }
private DatahubTariffFilter getGridTariffFilter() { private DatahubTariffFilter getGridTariffFilter() {

View File

@ -76,15 +76,10 @@ public class RetryPolicyFactory {
/** /**
* Determine {@link RetryStrategy} when expected spot price data is missing. * Determine {@link RetryStrategy} when expected spot price data is missing.
* *
* @param localTime the time of daily data request
* @param zoneId time-zone
* @return retry strategy * @return retry strategy
*/ */
public static RetryStrategy whenExpectedSpotPriceDataMissing(LocalTime localTime, ZoneId zoneId) { public static RetryStrategy whenExpectedSpotPriceDataMissing() {
LocalTime now = LocalTime.now(zoneId); return new ExponentialBackoff().withMinimum(Duration.ofMinutes(10)).withMaximum(Duration.ofHours(1))
if (now.isAfter(localTime)) { .withJitter(0.2);
return new ExponentialBackoff().withMinimum(Duration.ofMinutes(10)).withJitter(0.2);
}
return atFixedTime(localTime, zoneId);
} }
} }