Search in sources :

Example 1 with ApiQualityMetric

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;
}
Also used : ApiQualityMetricsEntity(io.gravitee.rest.api.model.ApiQualityMetricsEntity) ParameterService(io.gravitee.rest.api.service.ParameterService) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) QualityMetricsService(io.gravitee.rest.api.service.QualityMetricsService) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) QualityRuleService(io.gravitee.rest.api.service.QualityRuleService) ApiQualityMetric(io.gravitee.rest.api.service.quality.ApiQualityMetric) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ApiQualityMetricsDisableException(io.gravitee.rest.api.service.exceptions.ApiQualityMetricsDisableException) Collectors(java.util.stream.Collectors) Key(io.gravitee.rest.api.model.parameters.Key) List(java.util.List) Component(org.springframework.stereotype.Component) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) Map(java.util.Map) QualityRuleEntity(io.gravitee.rest.api.model.quality.QualityRuleEntity) ApiQualityMetricLoader(io.gravitee.rest.api.service.quality.ApiQualityMetricLoader) Collections(java.util.Collections) ApiQualityRuleService(io.gravitee.rest.api.service.ApiQualityRuleService) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) QualityRuleEntity(io.gravitee.rest.api.model.quality.QualityRuleEntity) ApiQualityMetric(io.gravitee.rest.api.service.quality.ApiQualityMetric) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) ApiQualityMetricsDisableException(io.gravitee.rest.api.service.exceptions.ApiQualityMetricsDisableException) ApiQualityMetricsEntity(io.gravitee.rest.api.model.ApiQualityMetricsEntity) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ApiQualityMetricsEntity (io.gravitee.rest.api.model.ApiQualityMetricsEntity)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 Key (io.gravitee.rest.api.model.parameters.Key)1 ParameterReferenceType (io.gravitee.rest.api.model.parameters.ParameterReferenceType)1 ApiQualityRuleEntity (io.gravitee.rest.api.model.quality.ApiQualityRuleEntity)1 QualityRuleEntity (io.gravitee.rest.api.model.quality.QualityRuleEntity)1 ApiQualityRuleService (io.gravitee.rest.api.service.ApiQualityRuleService)1 ParameterService (io.gravitee.rest.api.service.ParameterService)1 QualityMetricsService (io.gravitee.rest.api.service.QualityMetricsService)1 QualityRuleService (io.gravitee.rest.api.service.QualityRuleService)1 ApiQualityMetricsDisableException (io.gravitee.rest.api.service.exceptions.ApiQualityMetricsDisableException)1 ApiQualityMetric (io.gravitee.rest.api.service.quality.ApiQualityMetric)1 ApiQualityMetricLoader (io.gravitee.rest.api.service.quality.ApiQualityMetricLoader)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1