Skip to content

Commit

Permalink
[Fix partially 23ava-distribution#102] Simplify configuration for com…
Browse files Browse the repository at this point in the history
…munication with analytics module

- use only one configuration parameter
- implement OperationalDataService.checkConnection using header method
- Call checkConnection on startup to log connection status
  • Loading branch information
kostobog committed Jun 27, 2024
1 parent a8d6eca commit 0721eff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cz.cvut.kbss.analysis.exception;

public class ExternalServiceException extends FtaFmeaException {
public ExternalServiceException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<URI> components){
String apiURI = getFailureRateApi();
if(apiURI == null) return null;
String apiURI = null;
try {
apiURI = getFailureRateApi();

Map<String, Object> 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());
Expand Down
4 changes: 1 addition & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
operationalFailureRateService: http://localhost:9998/stats/failure-rate

0 comments on commit 0721eff

Please sign in to comment.