Skip to content

Commit

Permalink
Merge pull request #192 from q-rapids/develop
Browse files Browse the repository at this point in the history
Release v2.0
  • Loading branch information
alejandravv authored Feb 18, 2021
2 parents b870aac + 0a6fd5d commit 44dd357
Show file tree
Hide file tree
Showing 50 changed files with 1,117 additions and 256 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ dependencies {
//API QR Generator
compile files('libs/qrapids-qr_generator-0.2.jar')

//Add qr-eval jar compilation
compile files('libs/qrapids-eval-2.0.jar')

testCompile('org.springframework.boot:spring-boot-starter-test')
compile('org.apache.logging.log4j:log4j-api:2.9.1')
Expand Down
194 changes: 128 additions & 66 deletions docs/asciidoc/index.html

Large diffs are not rendered by default.

Binary file added libs/qrapids-eval-2.0.jar
Binary file not shown.
62 changes: 50 additions & 12 deletions src/main/java/com/upc/gessi/qrapids/QrapidsApplication.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,81 @@
package com.upc.gessi.qrapids;

import com.upc.gessi.qrapids.app.domain.controllers.MetricsController;
import com.upc.gessi.qrapids.app.domain.controllers.FactorsController;
import com.upc.gessi.qrapids.app.domain.controllers.StrategicIndicatorsController;
import com.upc.gessi.qrapids.app.domain.controllers.*;
import com.upc.gessi.qrapids.app.domain.exceptions.CategoriesException;
import com.upc.gessi.qrapids.app.domain.exceptions.ProjectNotFoundException;
import com.upc.gessi.qrapids.app.domain.models.MetricCategory;
import com.upc.gessi.qrapids.app.domain.models.QFCategory;
import com.upc.gessi.qrapids.app.domain.models.SICategory;
import com.upc.gessi.qrapids.app.presentation.rest.services.Alerts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.text.ParseException;
import java.time.ZoneId;
import java.util.*;
import eval2.Eval;
import java.time.LocalDate;

@SpringBootApplication
@EnableScheduling
public class QrapidsApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(QrapidsApplication.class);
}

@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}

@Value("${projects.dir:}") // default -> empty string
private String projectsDir;

static ConfigurableApplicationContext context;

@Scheduled(cron = "${cron.expression:-}") // default -> disable scheduled task
public void scheduleTask() throws ParseException, ProjectNotFoundException, IOException, CategoriesException {
// ToDo: decide if we also copy this code to assessSI function
LocalDate evaluationLocalDate = LocalDate.now(); // we need LocalDate for assessStrategicIndicators
Date evaluationDate= Date.from(evaluationLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); // we need Date for evaluateQualityModel in qrapids-eval libs

// params config:
// projects dir path, evaluationDate, null
// projects dir path, fromDate, toDate
Eval.evaluateQualityModel(projectsDir, evaluationDate, null);

boolean correct = true;
// first assess factors for all projects
correct = context.getBean(FactorsController.class).assessQualityFactors(null, evaluationLocalDate);

if (correct) {
// then assess strategic indicator for all projects
correct = context.getBean(StrategicIndicatorsController.class).assessStrategicIndicators(null, evaluationLocalDate);
}

if (!correct) { // check if the assessment complete with error
Logger logger = LoggerFactory.getLogger(Alerts.class);
logger.error(evaluationLocalDate + ": factors or strategic indicators assessment complete with error.");
}
}


public static void main(String[] args) throws Exception {

ConfigurableApplicationContext context = SpringApplication.run(QrapidsApplication.class, args);
context = SpringApplication.run(QrapidsApplication.class, args);

// Check the categories in the SQL database and if they are empty create the default ones
List<SICategory> siCategoryList = context.getBean(StrategicIndicatorsController.class).getStrategicIndicatorCategories();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AssesSI {
@Autowired
private StrategicIndicatorsController strategicIndicatorsController;

@Value("${assessSI.url}")
@Value("${assessSI.url:}")
private String url;

public List<DTOAssessment> assesSI(String siId, Map<String, String> mapFactors, File network) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.upc.gessi.qrapids.app.domain.controllers;

import com.upc.gessi.qrapids.app.domain.models.Alert;
import com.upc.gessi.qrapids.app.domain.models.AlertStatus;
import com.upc.gessi.qrapids.app.domain.models.AlertType;
import com.upc.gessi.qrapids.app.domain.models.Project;
import com.upc.gessi.qrapids.app.domain.exceptions.ProjectNotFoundException;
import com.upc.gessi.qrapids.app.domain.models.*;
import com.upc.gessi.qrapids.app.domain.repositories.Alert.AlertRepository;
import com.upc.gessi.qrapids.app.domain.exceptions.AlertNotFoundException;
import com.upc.gessi.qrapids.app.domain.repositories.Metric.MetricRepository;
import com.upc.gessi.qrapids.app.domain.repositories.Profile.ProfileProjectStrategicIndicatorsRepository;
import com.upc.gessi.qrapids.app.domain.repositories.Project.ProjectRepository;
import com.upc.gessi.qrapids.app.domain.repositories.QualityFactor.QualityFactorRepository;
import com.upc.gessi.qrapids.app.domain.repositories.StrategicIndicator.StrategicIndicatorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service;
Expand All @@ -21,9 +24,30 @@ public class AlertsController {
@Autowired
private AlertRepository alertRepository;

@Autowired
private ProjectRepository projectRepository;

@Autowired
private MetricRepository metricRepository;

@Autowired
private QualityFactorRepository factorRepository;

@Autowired
private StrategicIndicatorRepository strategicIndicatorRepository;

@Autowired
private ProfileProjectStrategicIndicatorsRepository profileProjectStrategicIndicatorsRepository;

@Autowired
private QRPatternsController qrPatternsController;

@Autowired
private ProjectsController projectsController;

@Autowired
private ProfilesController profilesController;

public Alert getAlertById(long alertId) throws AlertNotFoundException {
Optional<Alert> alertOptional = alertRepository.findById(alertId);
if (alertOptional.isPresent()) {
Expand Down Expand Up @@ -52,10 +76,109 @@ public Pair<Long, Long> countNewAlerts(Project project) {
return Pair.of(newAlerts, newAlertsWithQR);
}

public Pair<Long, Long> countNewAlertsByProfile(Project project, String profileId) throws ProjectNotFoundException {
long newAlertsCount = alertRepository.countByProject_IdAndStatus(project.getId(), AlertStatus.NEW);
long newAlertsWithQRCount = alertRepository.countByProject_IdAndReqAssociatIsTrueAndStatusEquals(project.getId(), AlertStatus.NEW);
if ((profileId != null) && (!profileId.equals("null"))) { // if profile not null
Profile profile = profilesController.findProfileById(profileId);
if (profile.getAllSIByProject(project)) { // if allSI true --> return all new alerts count
return Pair.of(newAlertsCount, newAlertsWithQRCount);
} else { // if allSI false --> return filtered new alerts
List<Alert> allNewAlerts = alertRepository.getByProject_IdAndStatus(project.getId(), AlertStatus.NEW);
List<Alert> allNewAlertsWithQR = alertRepository.getByProject_IdAndReqAssociatIsTrueAndStatusEquals(project.getId(), AlertStatus.NEW);
List<Alert> filteredNewAlerts = filterByProfile(project, profile, allNewAlerts);
List<Alert> filteredNewAlertsWithQR = filterByProfile(project, profile, allNewAlertsWithQR);
return Pair.of(Long.valueOf(filteredNewAlerts.size()), Long.valueOf(filteredNewAlertsWithQR.size()));
}
} else { // if profile is null --> return all alerts
return Pair.of(newAlertsCount, newAlertsWithQRCount);
}
}

public void createAlert (String id, String name, AlertType type, float value, float threshold, String category, Project project) {
Alert alert = new Alert(id, name, type, value, threshold, category, new Date(), AlertStatus.NEW, false, project);
boolean hasReq = qrPatternsController.existsPatternForAlert(alert);
alert.setReqAssociat(hasReq);
alertRepository.save(alert);
}

public void checkMetricAlert(String externalId, float value, String prj){
// get project from data base
Project p = projectRepository.findByExternalId(prj);
// get metric threshold from data base
Metric m = metricRepository.findByExternalIdAndProjectId(externalId, p.getId());
// check if the value is below the threshold then create new alert for this metric
if (m.getThreshold() != null && value < m.getThreshold()) {
// createAlert( id, name, type, value, threshold, category, project)
createAlert(externalId, m.getName(), AlertType.METRIC, value, m.getThreshold(), externalId, p);
}
}

public void checkFactorAlert(String externalId, float value, String prj){
// get project from data base
Project p = projectRepository.findByExternalId(prj);
// get factor threshold from data base
Factor f = factorRepository.findByExternalIdAndProjectId(externalId, p.getId());
// check if the value is below the threshold then create new alert for this factor
if (f.getThreshold() != null && value < f.getThreshold()) {
// createAlert( id, name, type, value, threshold, category, project)
createAlert(externalId, f.getName(), AlertType.FACTOR, value, f.getThreshold(), externalId, p);
}
}

public void checkStrategicIndicatorAlert(String externalId, float value, String prj){
// get project from data base
Project p = projectRepository.findByExternalId(prj);
// get factor threshold from data base
Strategic_Indicator si = strategicIndicatorRepository.findByExternalIdAndProjectId(externalId, p.getId());
// check if the value is below the threshold then create new alert for this factor
if (si.getThreshold() != null && value < si.getThreshold()) {
// createAlert( id, name, type, value, threshold, category, project)
createAlert(externalId, si.getName(), AlertType.STRATEGIC_INDICATOR, value, si.getThreshold(), externalId, p);
}
}

public List<Alert> getAlertsByProjectAndProfile(Project project, String profileId) throws ProjectNotFoundException {
List<Alert> alerts = alertRepository.findByProject_IdOrderByDateDesc(project.getId());
if ((profileId != null) && (!profileId.equals("null"))) { // if profile not null
Profile profile = profilesController.findProfileById(profileId);
if (profile.getAllSIByProject(project)) { // allSI true --> filter only by profile quality level
if(profile.getQualityLevel().equals(Profile.QualityLevel.METRICS_FACTORS))
alerts.removeIf(a -> a.getType().equals(AlertType.STRATEGIC_INDICATOR));
else if (profile.getQualityLevel().equals(Profile.QualityLevel.METRICS)) {
alerts.removeIf(a -> a.getType().equals(AlertType.STRATEGIC_INDICATOR));
alerts.removeIf(a -> a.getType().equals(AlertType.FACTOR));
}
return alerts;
} else { // if allSI false --> return alerts filtered by profile quality level and SIs
return filterByProfile(project,profile,alerts);
}
} else { // if profile is null --> return all alerts
return alerts;
}
}

public List <Alert> filterByProfile (Project project, Profile profile, List<Alert> alerts) {
List<ProfileProjectStrategicIndicators> ppsiList =
profileProjectStrategicIndicatorsRepository.findByProfileAndProject(profile,project);
List<Alert> result = new ArrayList<>();
Profile.QualityLevel ql = profile.getQualityLevel();
for (Alert a : alerts) {
for (ProfileProjectStrategicIndicators ppsi : ppsiList) {
if (a.getType().equals(AlertType.STRATEGIC_INDICATOR) && a.getId_element().equals(ppsi.getStrategicIndicator().getExternalId()) && !result.contains(a)
&& ql.equals(Profile.QualityLevel.ALL)) result.add(a);
List<StrategicIndicatorQualityFactors> siqfList = ppsi.getStrategicIndicator().getStrategicIndicatorQualityFactorsList();
for (StrategicIndicatorQualityFactors siqf : siqfList) {
if (a.getType().equals(AlertType.FACTOR) && a.getId_element().equals(siqf.getFactor().getExternalId()) && !result.contains(a)
&& (ql.equals(Profile.QualityLevel.ALL) || ql.equals(Profile.QualityLevel.METRICS_FACTORS))) result.add(a);
List<String> metrics = siqf.getFactor().getMetrics();
for (String m : metrics) {
if (a.getType().equals(AlertType.METRIC) && a.getId_element().equals(m) && !result.contains(a))
result.add(a);
}
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class FactorsController {
@Autowired
private ProfileProjectStrategicIndicatorsRepository profileProjectStrategicIndicatorsRepository;

@Autowired
private AlertsController alertsController;

@Autowired
private MetricsController metricsController;

Expand Down Expand Up @@ -182,10 +185,14 @@ public Factor getQualityFactorById (Long qualityFactorId) throws QualityFactorNo
}
}

public Factor saveQualityFactor(String name, String description, List<String> qualityMetrics, Project project) throws MetricNotFoundException {
public Factor saveQualityFactor(String name, String description, String threshold, List<String> qualityMetrics, Project project) throws MetricNotFoundException {
Factor qualityFactor;
// create Quality Factor minim (without quality factors and weighted)
qualityFactor = new Factor (name, description, project);
if (!threshold.isEmpty()) // check if threshold is specified and then set it
qualityFactor.setThreshold(Float.parseFloat(threshold));
else
qualityFactor.setThreshold(null);
qualityFactorRepository.save(qualityFactor);
boolean weighted = assignQualityMetricsToQualityFactor (qualityMetrics, qualityFactor);
qualityFactor.setWeighted(weighted);
Expand Down Expand Up @@ -220,10 +227,14 @@ private boolean assignQualityMetricsToQualityFactor (List<String> qualityMetrics
return weighted;
}

public Factor editQualityFactor (Long factorId, String name, String description, List<String> qualityMetrics) throws QualityFactorNotFoundException, QualityFactorMetricsNotFoundException, MetricNotFoundException {
public Factor editQualityFactor(Long factorId, String name, String description, String threshold, List<String> qualityMetrics) throws QualityFactorNotFoundException, QualityFactorMetricsNotFoundException, MetricNotFoundException {
Factor factor = getQualityFactorById(factorId);
factor.setName(name);
factor.setDescription(description);
if (!threshold.isEmpty()) // check if threshold is specified and then set it
factor.setThreshold(Float.parseFloat(threshold));
else
factor.setThreshold(null);
// Actualize Quality Metrics
boolean weighted = reassignQualityMetricsToQualityFactor (qualityMetrics, factor);
factor.setWeighted(weighted);
Expand Down Expand Up @@ -326,6 +337,11 @@ private boolean assessDateProjectQualityFactors(String project, LocalDate evalua
metricList = metricsController.getAllMetricsHistoricalEvaluation(project, null, evaluationDate, evaluationDate);
metricEvaluationQma.setMetrics(metricList);

// CHECK METRICS ALERTS
for (DTOMetricEvaluation m : metricList) {
alertsController.checkMetricAlert(m.getId(), m.getValue(), project);
}

return assessProjectQualityFactors(evaluationDate, project, metricEvaluationQma);
}

Expand Down Expand Up @@ -507,6 +523,8 @@ private String assessQualityFactors(LocalDate evaluationDate, String project, Fa
indicators
))
throw new AssessmentErrorException();
// CHECK FACTORS ALERTS
alertsController.checkFactorAlert(qualityFactor.getExternalId(),value,project);
}
return assessmentValueOrLabel;
}
Expand All @@ -526,7 +544,6 @@ public boolean assessQualityFactor(String name, String prj) throws IOException,
MetricEvaluation metricEvaluationQma = new MetricEvaluation();

// We will compute the evaluation values for the QF for THIS CONCRETE component

// 1.- We need to remove old data from metric evaluations in the quality_factors relationship attribute
metricEvaluationQma.setMetrics(metricsController.getAllMetricsEvaluation(prj, null));
metricEvaluationQma.clearQualityFactorsRelations(qf.getExternalId());
Expand Down Expand Up @@ -583,16 +600,6 @@ public void setFactorStrategicIndicatorRelation (List<DTOFactorEvaluation> facto
qmaQualityFactors.setFactorStrategicIndicatorRelation(factorList, projectExternalId);
}

/*public static String getFactorLabelFromValue(Float f) {
List <QFCategory> qfCategoryList = factorCategoryRepository.findAllByOrderByUpperThresholdAsc();
if (f != null) {
for (QFCategory qfCategory : qfCategoryList) {
if (f <= qfCategory.getUpperThreshold())
return qfCategory.getName();
}
}
return "No Category";
}*/
public String getFactorLabelFromValue (Float f) {
List <QFCategory> qfCategoryList = factorCategoryRepository.findAllByOrderByUpperThresholdAsc();
if (f != null) {
Expand Down
Loading

0 comments on commit 44dd357

Please sign in to comment.