use of io.gravitee.rest.api.service.quality.ApiQualityMetric in project gravitee-management-rest-api by gravitee-io.
the class QualityMetricsServiceImpl method getMetrics.
@Override
public ApiQualityMetricsEntity getMetrics(ApiEntity apiEntity) {
if (!isApiMetricsEnabled()) {
throw new ApiQualityMetricsDisableException();
}
Map<String, Integer> weights = getWeights().entrySet().stream().filter(e -> e.getValue() > 0).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
ApiQualityMetricsEntity result = new ApiQualityMetricsEntity();
result.setMetricsPassed(new HashMap<>(weights.size()));
double score = 0;
double maxScore = 0;
if (weights.isEmpty()) {
result.setScore(1);
} else {
Map<String, ApiQualityMetric> apiMetrics = getApiMetricsMap();
for (Map.Entry<String, Integer> weight : weights.entrySet()) {
boolean passed = apiMetrics.get(weight.getKey()).isValid(apiEntity);
result.getMetricsPassed().put(weight.getKey(), passed);
score += weight.getValue() * (passed ? 1 : 0);
maxScore += weight.getValue();
}
result.setScore((int) ((score / maxScore) * 100) / 100d);
}
// manual quality rules
final List<QualityRuleEntity> qualityRules = qualityRuleService.findAll();
if (qualityRules != null && !qualityRules.isEmpty()) {
final List<ApiQualityRuleEntity> apiQualityRules = apiQualityRuleService.findByApi(apiEntity.getId());
for (final QualityRuleEntity qualityRule : qualityRules) {
if (qualityRule.getWeight() > 0) {
final ApiQualityRuleEntity apiQualityRule = apiQualityRules.stream().filter(aqr -> apiEntity.getId().equals(aqr.getApi()) && qualityRule.getId().equals(aqr.getQualityRule())).findFirst().orElse(null);
final boolean checked = apiQualityRule != null && apiQualityRule.isChecked();
result.getMetricsPassed().put(qualityRule.getId(), checked);
score += qualityRule.getWeight() * (checked ? 1 : 0);
maxScore += qualityRule.getWeight();
result.setScore((int) ((score / maxScore) * 100) / 100d);
}
}
}
return result;
}
Aggregations