From 0721effb2beb94175d4bfc1b3b62d0c37a2bb3c5 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Thu, 27 Jun 2024 14:30:03 +0200 Subject: [PATCH] [Fix partially 23ava-distribution#102] Simplify configuration for communication with analytics module - use only one configuration parameter - implement OperationalDataService.checkConnection using header method - Call checkConnection on startup to log connection status --- .../config/conf/OperationalDataConfig.java | 8 ++-- .../exception/ExternalServiceException.java | 7 ++++ .../external/OperationalDataService.java | 38 ++++++++++--------- src/main/resources/application.yml | 4 +- 4 files changed, 31 insertions(+), 26 deletions(-) create mode 100644 src/main/java/cz/cvut/kbss/analysis/exception/ExternalServiceException.java diff --git a/src/main/java/cz/cvut/kbss/analysis/config/conf/OperationalDataConfig.java b/src/main/java/cz/cvut/kbss/analysis/config/conf/OperationalDataConfig.java index 7e665ff8..f2400c61 100644 --- a/src/main/java/cz/cvut/kbss/analysis/config/conf/OperationalDataConfig.java +++ b/src/main/java/cz/cvut/kbss/analysis/config/conf/OperationalDataConfig.java @@ -15,15 +15,13 @@ @ConfigurationProperties("operational.data.filter") public class OperationalDataConfig { + public static final String MIN_OPERATIONAL_TIME_PARAM = "minOperationalTime"; + protected Double minOperationalHours; - protected String operationalDataServer; - protected String operationaDataServerCheck; protected String operationalFailureRateService; @Autowired public OperationalDataConfig(Environment env) { - operationalDataServer = env.getProperty("operationalDataServer"); - operationaDataServerCheck = env.getProperty("operationaDataServerCheck", "check"); - operationalFailureRateService = env.getProperty("operationalFailureRateService", "failure-rate"); + operationalFailureRateService = env.getProperty("operationalFailureRateService"); } } diff --git a/src/main/java/cz/cvut/kbss/analysis/exception/ExternalServiceException.java b/src/main/java/cz/cvut/kbss/analysis/exception/ExternalServiceException.java new file mode 100644 index 00000000..bbaed96e --- /dev/null +++ b/src/main/java/cz/cvut/kbss/analysis/exception/ExternalServiceException.java @@ -0,0 +1,7 @@ +package cz.cvut.kbss.analysis.exception; + +public class ExternalServiceException extends FtaFmeaException { + public ExternalServiceException(String message) { + super(message); + } +} diff --git a/src/main/java/cz/cvut/kbss/analysis/service/external/OperationalDataService.java b/src/main/java/cz/cvut/kbss/analysis/service/external/OperationalDataService.java index 1752b632..51438fc9 100644 --- a/src/main/java/cz/cvut/kbss/analysis/service/external/OperationalDataService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/external/OperationalDataService.java @@ -1,9 +1,11 @@ package cz.cvut.kbss.analysis.service.external; import cz.cvut.kbss.analysis.config.conf.OperationalDataConfig; +import cz.cvut.kbss.analysis.exception.ExternalServiceException; import cz.cvut.kbss.analysis.model.opdata.ItemFailureRate; import cz.cvut.kbss.analysis.model.opdata.OperationalDataFilter; import cz.cvut.kbss.analysis.service.OperationalDataFilterService; +import jakarta.annotation.PostConstruct; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @@ -22,44 +24,44 @@ public class OperationalDataService { private final OperationalDataFilterService service; private final RestTemplate restTemplate; - public OperationalDataService(OperationalDataConfig operationalDataConfig, OperationalDataFilterService service, @Qualifier("customRestTemplate") RestTemplate restTemplate) { this.operationalDataConfig = operationalDataConfig; this.service = service; this.restTemplate = restTemplate; } - protected String getApi(String api){ - String serverUri = operationalDataConfig.getOperationalDataServer(); - return api == null || serverUri == null ? null : serverUri + api; - } - - - protected String getCheckApi(){ - return getApi(operationalDataConfig.getOperationaDataServerCheck()); + @PostConstruct + public void checkConnectionOnStartUp(){ + checkConnection(); } protected String getFailureRateApi(){ - return getApi(operationalDataConfig.getOperationalFailureRateService()); + String path = operationalDataConfig.getOperationalFailureRateService(); + if(path == null) + throw new ExternalServiceException("Configuration parameter operationalFailureRateService not set."); + return path; } public String checkConnection(){ - String apiURI = getCheckApi(); - if(apiURI == null) return "not connected"; + String apiURI = null; try { - return restTemplate.getForObject(apiURI, String.class); + apiURI = getFailureRateApi(); + restTemplate.headForHeaders(apiURI); + log.warn("connection to {} available", apiURI); + return "ok"; } catch (Exception e){ - log.warn("Failed to fetch failure rates from " + apiURI, e); + log.warn("checkConnection failed - {} ", e.getMessage()); } - return "not working"; + return apiURI != null ? "bad configuration " : "connection not working"; } public ItemFailureRate[] fetchFailureRates(OperationalDataFilter filter, Collection components){ - String apiURI = getFailureRateApi(); - if(apiURI == null) return null; + String apiURI = null; try { + apiURI = getFailureRateApi(); + Map uriParams = new HashMap<>(); - uriParams.put("minOperationalTime", filter.getMinOperationalHours()); + uriParams.put(OperationalDataConfig.MIN_OPERATIONAL_TIME_PARAM, filter.getMinOperationalHours()); return restTemplate.postForObject(apiURI, components, ItemFailureRate[].class, uriParams); } catch (Exception e){ log.warn("Failed to fetch failure rates from \"{}\" \nerror message: {}", apiURI, e.getMessage()); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index bd4d475b..6fc8dc27 100755 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -41,6 +41,4 @@ spring: operational.data.filter: min-operational-hours: 200 -operationalDataServer: https://kbss.felk.cvut.cz/ava/services/ava-analytics/stats/ -operationaDataServerCheck: check -operationalFailureRateService: failure-rate \ No newline at end of file +operationalFailureRateService: http://localhost:9998/stats/failure-rate \ No newline at end of file