Skip to content

Commit

Permalink
Merge pull request #134 from kbss-cvut/feature/133-evaluate-and-store…
Browse files Browse the repository at this point in the history
…-fha-based-failure-rate-on-fault-tree-evaluation

 evaluate and store fha based failure rate on fault tree evaluation
  • Loading branch information
kostobog authored Jul 3, 2024
2 parents 258951f + d8ebf8e commit 09ad988
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
public class OperationalDataConfig {

public static final String MIN_OPERATIONAL_TIME_PARAM = "minOperationalTime";
public static final String FHA_TYPES_PARAM = "fha";

protected Double minOperationalHours;
protected String operationalFailureRateService;
protected String fhaBasedoperationalFailureRateService;

@Autowired
public OperationalDataConfig(Environment env) {
operationalFailureRateService = env.getProperty("operationalFailureRateService");
fhaBasedoperationalFailureRateService = env.getProperty("fhaBasedoperationalFailureRateService");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public FaultTree evaluate(URI faultTreeUri, OperationalDataFilter filter) {

FaultTree faultTree = faultTreeService.findWithDetails(faultTreeUri);

faultTreeRepositoryService.updateFaultTreeOperationalFailureRates(faultTree, filter);
faultTreeRepositoryService.updateFailureRates(faultTree, filter);

faultTree = faultTreeRepositoryService.evaluate(faultTree);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,40 @@ public FaultTree performCutSetAnalysis(URI faultTreeUri){
return faultTree;
}

@Transactional
public void updateFailureRates(FaultTree faultTree, OperationalDataFilter filter){
updateFHABasedOperationalFailureRates(faultTree, filter);
updateFaultTreeOperationalFailureRates(faultTree, filter);
}

@Transactional
public void updateFHABasedOperationalFailureRates(FaultTree faultTree, OperationalDataFilter filter) {
FaultEventType fhaEvent = (FaultEventType)Optional.ofNullable(faultTree.getManifestingEvent())
.map(e -> e.getSupertypes())
.map(l -> l.stream().findFirst().orElse(null))
.orElse(null);

if(fhaEvent == null)
return;

ItemFailureRate[] estimates = operationalDataService
.fetchFhaFailureRates(filter, Collections.singleton(fhaEvent.getUri()));

if(estimates == null || estimates.length == 0)
return;
URI systemContext = systemRepositoryService.getToolContext(faultTree.getSystem().getUri());
FailureRate fr = fhaEvent.getFailureRate();
if(fr == null){
fr = new FailureRate();
fr.setContext(systemContext);
failureRateDao.persist(fr);
failureRateDao.setFailureRate(fhaEvent.getUri(), fr, systemContext);
fhaEvent.setFailureRate(fr);
}
updateOperationalFailureRate(systemContext, faultTree.getUri(), estimates[0],
faultTree.getManifestingEvent(), fr);
}

/**
* Updates the provided fault tree sns' failures with operational failure rate calculated based on filter. The update
* is reflected in the persistent storage and in the input fault tree.
Expand Down
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;
}
}
3 changes: 2 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ spring:
operational.data.filter:
min-operational-hours: 200

operationalFailureRateService: http://localhost:9998/stats/failure-rate
operationalFailureRateService: http://localhost:9998/stats/failure-rate
fhaBasedoperationalFailureRateService: http://localhost:9998/stats/fha-failure-rate

0 comments on commit 09ad988

Please sign in to comment.