From dd43a53efce230f3ac595d961af6459540c29308 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Fri, 21 Jun 2024 09:41:51 +0200 Subject: [PATCH] [Fix partially kbss-cvut/fta-fmea-ui#452] Continue evaluation of fault tree if operationalDataService.fetchFailureRates fails. - implement rest api to check if operationalDataService works properly --- .../config/conf/OperationalDataConfig.java | 7 +++- .../OperationalDataFilterController.java | 10 ++++- .../service/FaultTreeRepositoryService.java | 2 + .../external/OperationalDataService.java | 39 +++++++++++++++++-- src/main/resources/application.yml | 4 +- 5 files changed, 54 insertions(+), 8 deletions(-) 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 d897788..7e665ff 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 @@ -1,6 +1,5 @@ package cz.cvut.kbss.analysis.config.conf; -import cz.cvut.kbss.analysis.util.ConfigParam; import lombok.Getter; import lombok.Setter; import org.springframework.beans.factory.annotation.Autowired; @@ -17,10 +16,14 @@ public class OperationalDataConfig { protected Double minOperationalHours; + protected String operationalDataServer; + protected String operationaDataServerCheck; protected String operationalFailureRateService; @Autowired public OperationalDataConfig(Environment env) { - operationalFailureRateService = env.getProperty("operationalFailureRateService"); + operationalDataServer = env.getProperty("operationalDataServer"); + operationaDataServerCheck = env.getProperty("operationaDataServerCheck", "check"); + operationalFailureRateService = env.getProperty("operationalFailureRateService", "failure-rate"); } } diff --git a/src/main/java/cz/cvut/kbss/analysis/controller/OperationalDataFilterController.java b/src/main/java/cz/cvut/kbss/analysis/controller/OperationalDataFilterController.java index b6da332..18dbf49 100644 --- a/src/main/java/cz/cvut/kbss/analysis/controller/OperationalDataFilterController.java +++ b/src/main/java/cz/cvut/kbss/analysis/controller/OperationalDataFilterController.java @@ -3,6 +3,7 @@ import cz.cvut.kbss.analysis.model.opdata.OperationalDataFilter; import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.service.OperationalDataFilterService; +import cz.cvut.kbss.analysis.service.external.OperationalDataService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jsonld.JsonLd; import lombok.extern.slf4j.Slf4j; @@ -20,10 +21,12 @@ public class OperationalDataFilterController { private final OperationalDataFilterService filterService; private final IdentifierService identifierService; + private final OperationalDataService operationalDataService; - public OperationalDataFilterController(OperationalDataFilterService filterService, IdentifierService identifierService) { + public OperationalDataFilterController(OperationalDataFilterService filterService, IdentifierService identifierService, OperationalDataService operationalDataService) { this.filterService = filterService; this.identifierService = identifierService; + this.operationalDataService = operationalDataService; } @PutMapping(path="reset", produces = {JsonLd.MEDIA_TYPE, MediaType.APPLICATION_JSON_VALUE}) @@ -46,4 +49,9 @@ public void updateFaultTreeFilter(@PathVariable(name = "faultTreeFragment") Stri URI faultTreeUri = identifierService.composeIdentifier(Vocabulary.s_c_fault_tree, faultTreeFragment); filterService.updateFaultTreeFilter(faultTreeUri, filter); } + + @GetMapping(value = "/check-service", produces = {MediaType.TEXT_PLAIN_VALUE}) + public String checkOperationalDataService(){ + return operationalDataService.checkConnection(); + } } diff --git a/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java index 9207466..7bf745a 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryService.java @@ -540,6 +540,8 @@ public void updateFaultTreeOperationalFailureRates(FaultTree faultTree, Operatio return; ItemFailureRate[] operationalFailureRateEstimates = operationalDataService.fetchFailureRates(filter, map.keySet()); + if(operationalFailureRateEstimates == null) + return; URI systemContext = getToolContext(faultTree.getSystem().getUri()); for(ItemFailureRate estimate : operationalFailureRateEstimates){ 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 91184c0..1752b63 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 @@ -29,10 +29,41 @@ public OperationalDataService(OperationalDataConfig operationalDataConfig, Opera 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()); + } + + protected String getFailureRateApi(){ + return getApi(operationalDataConfig.getOperationalFailureRateService()); + } + + public String checkConnection(){ + String apiURI = getCheckApi(); + if(apiURI == null) return "not connected"; + try { + return restTemplate.getForObject(apiURI, String.class); + } catch (Exception e){ + log.warn("Failed to fetch failure rates from " + apiURI, e); + } + return "not working"; + } + public ItemFailureRate[] fetchFailureRates(OperationalDataFilter filter, Collection components){ - String apiURI = operationalDataConfig.getOperationalFailureRateService(); - Map uriParams = new HashMap<>(); - uriParams.put("minOperationalTime", filter.getMinOperationalHours()); - return restTemplate.postForObject(apiURI, components, ItemFailureRate[].class, uriParams); + String apiURI = getFailureRateApi(); + if(apiURI == null) return null; + try { + Map uriParams = new HashMap<>(); + uriParams.put("minOperationalTime", 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()); + } + return null; } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 971cf68..bd4d475 100755 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -41,4 +41,6 @@ spring: operational.data.filter: min-operational-hours: 200 -operationalFailureRateService: https://kbss.onto.fel.cvut.cz/ava/services/ava-analytics/stats/failire-rate \ No newline at end of file +operationalDataServer: https://kbss.felk.cvut.cz/ava/services/ava-analytics/stats/ +operationaDataServerCheck: check +operationalFailureRateService: failure-rate \ No newline at end of file