Skip to content

Commit

Permalink
[Fix partially #133] Implement call to fha based failure rate service
Browse files Browse the repository at this point in the history
- fix missing url query parameters in OperationalDataService calls
  • Loading branch information
kostobog committed Jul 2, 2024
1 parent 241041c commit 9e61016
Showing 1 changed file with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
Expand All @@ -35,17 +35,24 @@ public void checkConnectionOnStartUp(){
checkConnection();
}

protected String getFailureRateApi(){
String path = operationalDataConfig.getOperationalFailureRateService();
protected String getFailureRateApi(String path){
if(path == null)
throw new ExternalServiceException("Configuration parameter operationalFailureRateService not set.");
return path;
}

protected String getOperationalFailureRateService(){
return getFailureRateApi(operationalDataConfig.getOperationalFailureRateService());
}

protected String getFhaBasedOperationalFailureRateService(){
return getFailureRateApi(operationalDataConfig.getFhaBasedoperationalFailureRateService());
}

public String checkConnection(){
String apiURI = null;
try {
apiURI = getFailureRateApi();
apiURI = getOperationalFailureRateService();
restTemplate.headForHeaders(apiURI);
log.warn("connection to {} available", apiURI);
return "ok";
Expand All @@ -58,14 +65,32 @@ public String checkConnection(){
public ItemFailureRate[] fetchFailureRates(OperationalDataFilter filter, Collection<URI> components){
String apiURI = null;
try {
apiURI = getFailureRateApi();
apiURI = getOperationalFailureRateService();

Map<String, Object> uriParams = new HashMap<>();
uriParams.put(OperationalDataConfig.MIN_OPERATIONAL_TIME_PARAM, filter.getMinOperationalHours());
return restTemplate.postForObject(apiURI, components, ItemFailureRate[].class, uriParams);
apiURI = UriComponentsBuilder.fromHttpUrl(apiURI)
.queryParam(OperationalDataConfig.MIN_OPERATIONAL_TIME_PARAM, filter.getMinOperationalHours())
.toUriString();
return restTemplate.postForObject(apiURI, components, ItemFailureRate[].class);
} catch (Exception e){
log.warn("Failed to fetch failure rates from \"{}\" \nerror message: {}", apiURI, e.getMessage());
}
return null;
}

public ItemFailureRate[] fetchFhaFailureRates(OperationalDataFilter filter, Collection<URI> fhaTypes){
String apiURI = null;
try {
apiURI = getFhaBasedOperationalFailureRateService();

apiURI = UriComponentsBuilder.fromHttpUrl(apiURI)
.queryParam(OperationalDataConfig.MIN_OPERATIONAL_TIME_PARAM, filter.getMinOperationalHours())
.queryParam(OperationalDataConfig.FHA_TYPES_PARAM, fhaTypes)
.toUriString();

return restTemplate.getForObject(apiURI, ItemFailureRate[].class);
} catch (Exception e){
log.warn("Failed to fetch fha based failure rates from \"{}\" \nerror message: {}", apiURI, e.getMessage());
}
return null;
}
}

0 comments on commit 9e61016

Please sign in to comment.