Skip to content

Commit

Permalink
Merge pull request #20 from q-rapids/fix_qf_label
Browse files Browse the repository at this point in the history
Fix QF labels and Detailed QF
  • Loading branch information
marcoriol authored Jan 16, 2023
2 parents 4c2588f + a3f106d commit 1b26b28
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.exceptions.ProjectNotFoundException;
import com.upc.gessi.qrapids.app.domain.exceptions.QualityFactorNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -360,12 +361,13 @@ private void buildFactor(List<DTOFactorEvaluation> result, JsonArray lower80, Js
if (lower80.size() > 0) {
for (int j = 0; j < lower80.size(); ++j) {
float aux = mean.get(j).getAsFloat();
result.add(new DTOFactorEvaluation(f.getId(), f.getName(),
f.getDescription(),
f.getDatasource(),
f.getRationale(),
current.plusDays((long) j + 1), Pair.of(aux,factorsController.getFactorLabelFromValue(aux)), Pair.of(upper80.get(j).getAsFloat(), lower80.get(j).getAsFloat()), Pair.of(upper95.get(j).getAsFloat(), lower95.get(j).getAsFloat())));
}
String cat_name = factorsController.getCategoryFromRationale(f.getRationale());
result.add(new DTOFactorEvaluation(f.getId(), f.getName(),
f.getDescription(),
f.getDatasource(),
f.getRationale(),
current.plusDays((long) j + 1), Pair.of(aux,factorsController.getFactorLabelFromNameAndValue(cat_name,aux)), Pair.of(upper80.get(j).getAsFloat(), lower80.get(j).getAsFloat()), Pair.of(upper95.get(j).getAsFloat(), lower95.get(j).getAsFloat())));
}
} else {
result.add(new DTOFactorEvaluation(f.getId(), f.getName(),
f.getDescription(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,12 @@ public List<DTOFactorEvaluation> FactorEvaluationDTOListToDTOFactorList(String s
}

public DTOFactorEvaluation FactorEvaluationDTOToDTOFactor(FactorEvaluationDTO factor, EvaluationDTO evaluation) {
String cat_name = factorsController.getCategoryFromRationale(evaluation.getRationale());
DTOFactorEvaluation factorEval = new DTOFactorEvaluation(
factor.getID(),
factor.getName(),
factor.getDescription(),
Pair.of(evaluation.getValue(), factorsController.getFactorLabelFromValue(evaluation.getValue())), evaluation.getEvaluationDate(),
Pair.of(evaluation.getValue(), factorsController.getFactorLabelFromNameAndValue(cat_name, evaluation.getValue())), evaluation.getEvaluationDate(),
evaluation.getDatasource(),evaluation.getRationale(),
factor.getStrategicIndicators());
factorEval.setMismatchDays(evaluation.getMismatchDays());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,12 @@ private List<DTODetailedFactorEvaluation> FactorMetricEvaluationDTOListToDTOQual
} catch (QualityFactorNotFoundException e) {
logger.error(e.getMessage(), e);
}

DTODetailedFactorEvaluation df = new DTODetailedFactorEvaluation(qualityFactor.getID(), qualityFactor.getDescription(), qualityFactor.getName(), QMAMetrics.MetricEvaluationDTOListToDTOMetricList(factorExternalID, qualityFactor.getMetrics(), project.getExternalId() ,profileId),type);
EvaluationDTO evaluation = qualityFactor.getEvaluations().get(0);
String cat_name = factorsController.getCategoryFromRationale(evaluation.getRationale());
df.setDate(evaluation.getEvaluationDate());
df.setValue(Pair.of(evaluation.getValue(), factorsController.getFactorLabelFromValue(evaluation.getValue())));
df.setValue(Pair.of(evaluation.getValue(), factorsController.getFactorLabelFromNameAndValue(cat_name, evaluation.getValue())));
df.setMismatchDays(evaluation.getMismatchDays());
if (evaluation.getMissingElements() == null)
df.setMissingMetrics(new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,9 @@ private String assessQualityFactors(LocalDate evaluationDate, String project, Fa
info += metricInfo;
}
if (weighted) {
info += " }, formula: weighted average, value: " + value + ", category: " + getFactorLabelFromValue(value);
info += " }, formula: weighted average, value: " + value + ", category: " + qualityFactor.getCategoryName();
} else {
info += " }, formula: average, value: " + value + ", category: " + getFactorLabelFromValue(value);
info += " }, formula: average, value: " + value + ", category: " + qualityFactor.getCategoryName();
}
// saving the QF's assessment
// in case of new factor -> indicators list is empty
Expand Down Expand Up @@ -726,6 +726,31 @@ public String getFactorLabelFromValue (Float f) {
return "No Category";
}

public String getFactorLabelFromNameAndValue (String nameCategory, Float val_metric) {
List <QFCategory> qfCategoryList = factorCategoryRepository.findAllByOrderByUpperThresholdAsc();
//List <QFCategory> qfCategoryList = factorCategoryRepository.findAllByName(nameCategory); TODO: no funciona por lo que he cambiado la implementacion
Float distance = 1.1f; //distancia demasiado grande, para que se recalcule
String type = "No Category";
for (QFCategory qfCategory : qfCategoryList) {
System.out.println(qfCategory.getType() + " " + qfCategory.getName() + " " + qfCategory.getUpperThreshold() + " "+ distance);
System.out.println(qfCategory.getUpperThreshold()-val_metric);
if (val_metric <= qfCategory.getUpperThreshold() && //si la metrica puede ser de ese thresh
distance>(qfCategory.getUpperThreshold()-val_metric) // y la distancia hasta ahora es MAYOR a la nueva
&& qfCategory.getName().equals(nameCategory)){ //y la categoria es la correcta
distance = qfCategory.getUpperThreshold()-val_metric;
type = qfCategory.getType();
}

}
if (type == "No Category") type = nameCategory;
return type;
}

public String getCategoryFromRationale (String rationale){
String category = rationale.substring(rationale.indexOf("category") + 10, rationale.length()); //+9 porque te da el indice de c: hay que sumarle toda la palabra y el espacio
return category;
}

public List<DTOFactorEvaluation> getFactorsPrediction(List<DTOFactorEvaluation> currentEvaluation, String prj, String technique, String freq, String horizon) throws IOException {
return qmaForecast.ForecastFactor(currentEvaluation, technique, freq, horizon, prj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface QFCategoryRepository extends CrudRepository<QFCategory, Long> {

List<QFCategory> findAllByName(String name);

List<QFCategory> findAllByType(String type);

QFCategory findByName(String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class DTOFactorEvaluation {
private int mismatchDays;
private List<String> missingMetrics;
private String type;
//todo: add maybe a categoryname att to calculate the label

/**
* Constructor of the DTO of Factors
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/js/gaugeChartMet.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const DEFAULT_CATEGORY = "Default";
var url;
if (getParameterByName('id').length !== 0) {
id = true;
url = parseURLComposed("../api/qualityFactors/metrics/currentcurrent");
url = parseURLComposed("../api/qualityFactors/metrics/current");
} else {
var profileId = sessionStorage.getItem("profile_id");
url = parseURLComposed("../api/metrics/current?profile="+profileId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.upc.gessi.qrapids.app.domain.adapters.QMA.QMAQualityFactors;
import com.upc.gessi.qrapids.app.domain.adapters.QMA.QMASimulation;
import com.upc.gessi.qrapids.app.domain.exceptions.ProjectNotFoundException;
import com.upc.gessi.qrapids.app.domain.exceptions.QualityFactorNotFoundException;
import com.upc.gessi.qrapids.app.domain.models.QFCategory;
import com.upc.gessi.qrapids.app.domain.repositories.QFCategory.QFCategoryRepository;
import com.upc.gessi.qrapids.app.presentation.rest.dto.DTOFactorCategory;
import com.upc.gessi.qrapids.app.presentation.rest.dto.DTOFactorEvaluation;
import com.upc.gessi.qrapids.app.presentation.rest.dto.DTODetailedFactorEvaluation;
import com.upc.gessi.qrapids.app.domain.exceptions.CategoriesException;
Expand All @@ -17,6 +19,8 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;

import java.io.IOException;
import java.time.LocalDate;
Expand All @@ -42,6 +46,12 @@ public class FactorEvaluationControllerTest {
@Mock
private QFCategoryRepository factorCategoryRepository;

@Autowired
private QFCategoryRepository factorCategoryRepositoryold;

@Autowired
private TestEntityManager entityManager;

@InjectMocks
private FactorsController factorsController;

Expand Down Expand Up @@ -104,7 +114,7 @@ public void newFactorCategoriesNotEnough() throws CategoriesException {
}

@Test
public void getSingleFactorEvaluation() throws IOException {
public void getSingleFactorEvaluation() throws IOException, QualityFactorNotFoundException {
// Given
DTOFactorEvaluation dtoFactorEvaluation = domainObjectsBuilder.buildDTOFactor();
String projectExternalId = "test";
Expand Down Expand Up @@ -264,6 +274,7 @@ public void simulate() throws IOException {
assertEquals(dtoFactorEvaluationList.size(), factorsSimulationList.size());
assertEquals(dtoFactorEvaluation, factorsSimulationList.get(0));
}
/*
@Test
public void getFactorLabelFromValueGood() {
Expand Down Expand Up @@ -312,4 +323,47 @@ public void getFactorLabelFromValueBad() {
String expectedLabel = "Bad";
assertEquals(expectedLabel, label);
}
*/
@Test
public void getFactorLabelFromValueAndName() {
// Given
List<QFCategory> qfCategoryList = domainObjectsBuilder.buildFactorCategoryList();
Collections.reverse(qfCategoryList);
when(factorCategoryRepository.findAllByOrderByUpperThresholdAsc()).thenReturn(qfCategoryList);

// When
String label_up_6mem = factorsController.getFactorLabelFromNameAndValue("6 members contribution", 0.7f);
String label_low_6mem = factorsController.getFactorLabelFromNameAndValue("6 members contribution", 0.10f);
String label_low2_6mem = factorsController.getFactorLabelFromNameAndValue("6 members contribution", 0.15f);
String label_low3_6mem = factorsController.getFactorLabelFromNameAndValue("6 members contribution", 0f);
String label_good_6mem = factorsController.getFactorLabelFromNameAndValue("6 members contribution", 0.37f);
String label_up2_6mem = factorsController.getFactorLabelFromNameAndValue("6 members contribution", 0.8f);
String label_high2_6mem = factorsController.getFactorLabelFromNameAndValue("6 members contribution", 1f);

// Then
assertEquals("Up", label_up_6mem);
assertEquals("Good enough", label_good_6mem);
assertEquals("Up", label_up2_6mem);
assertEquals("High", label_high2_6mem);
assertEquals("Low", label_low_6mem);
assertEquals("Low", label_low2_6mem);
assertEquals("Low", label_low3_6mem);

}

@Test
public void getCategoryFromRationale (){
String rationale_example = "metrics: { commits_anonymous (value: 0.13736264, no weighted); }, formula: average, value: 0.13736264, category: Default";
String cat_def = factorsController.getCategoryFromRationale(rationale_example);
rationale_example = "metrics: { commits_anonymous (value: 0.6264, no weighted); }, formula: average, value: 0.6264, category: 6 members contribution";
String cat_6m = factorsController.getCategoryFromRationale(rationale_example);
rationale_example = "metrics: { commits_anonymous (value: 0.482, no weighted); }, formula: average, value: 0.482, category: Reversed default";
String cat_rev = factorsController.getCategoryFromRationale(rationale_example);

assertEquals("Default", cat_def);
assertEquals("6 members contribution", cat_6m);
assertEquals("Reversed default", cat_rev);
}


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.upc.gessi.qrapids.app.domain.repositories.QFCategory;

import com.upc.gessi.qrapids.app.domain.controllers.FactorsController;
import com.upc.gessi.qrapids.app.domain.models.Factor;
import com.upc.gessi.qrapids.app.domain.models.QFCategory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
Expand All @@ -11,6 +14,7 @@
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

@RunWith(SpringRunner.class)
@DataJpaTest
Expand All @@ -21,18 +25,20 @@ public class QFCategoryRepositoryTest {

@Autowired
private QFCategoryRepository qfCategoryRepository;
@Mock
private FactorsController qualityFactorsDomainController;

@Test
public void findAllByOrderByUpperThresholdAsc() {
// Given
QFCategory qfCategoryBad = new QFCategory("Bad", "#ff0000", 0.33f, "Default");
QFCategory qfCategoryBad = new QFCategory("Default", "#ff0000", 0.33f, "Bad");
entityManager.persist(qfCategoryBad);

QFCategory qfCategoryGood = new QFCategory("Good", "#00ff00", 1f,"Default");
QFCategory qfCategoryGood = new QFCategory("Default", "#00ff00", 1f,"Good");
entityManager.persist(qfCategoryGood);

QFCategory qfCategoryNeutral = new QFCategory("Neutral", "#ff8000", 0.67f,"Default");
entityManager.persistAndFlush(qfCategoryNeutral);
QFCategory qfCategoryNeutral = new QFCategory("Default", "#ff8000", 0.67f,"Neutral");
entityManager.persist(qfCategoryNeutral);

// When
List<QFCategory> categoryList = qfCategoryRepository.findAllByOrderByUpperThresholdAsc();
Expand All @@ -41,5 +47,84 @@ public void findAllByOrderByUpperThresholdAsc() {
assertEquals(qfCategoryBad, categoryList.get(0));
assertEquals(qfCategoryNeutral, categoryList.get(1));
assertEquals(qfCategoryGood, categoryList.get(2));

}

@Test
public void findAllByCategoryName() {
// Given
QFCategory qfCategoryDefaultBad = new QFCategory("Default", "#ff0000", 0.33f, "Bad");
entityManager.persist(qfCategoryDefaultBad);

QFCategory qfCategoryDefaultGood = new QFCategory("Default", "#00ff00", 1f,"Good");
entityManager.persist(qfCategoryDefaultGood);

QFCategory qfCategoryDefaultNeutral = new QFCategory("Default", "#ff8000", 0.67f,"Neutral");
entityManager.persist(qfCategoryDefaultNeutral);

QFCategory qfCategory6memUp = new QFCategory("6 members contribution", "#ff0000", 1f, "Up");
entityManager.persist(qfCategory6memUp);

QFCategory qfCategory6memHigh = new QFCategory("6 members contribution", "#00ff00", 0.8f,"High");
entityManager.persist(qfCategory6memHigh);

QFCategory qfCategory6memGood = new QFCategory("6 members contribution", "#00ff00", 0.5f,"Neutral");
entityManager.persist(qfCategory6memGood);

QFCategory qfCategory6memLow = new QFCategory("6 members contribution", "#ff8000", 0.3f,"Low");
entityManager.persistAndFlush(qfCategory6memLow);

QFCategory qfCategory6memDown = new QFCategory("6 members contribution", "#ff8000", 0.15f,"Down");
entityManager.persistAndFlush(qfCategory6memDown);


// When
List<QFCategory> categoryList = qfCategoryRepository.findAllByName("6 members contribution");

//System.out.println(categoryList.get(0).getUpperThreshold());
//System.out.println(categoryList.get(0).getName());
//System.out.println(categoryList.get(0).getType());

// Then
assertEquals(qfCategory6memLow.getName(), categoryList.get(0).getName());
assertEquals(5, categoryList.size());

}

@Test
public void findAllByCategoryType() {

QFCategory qfCategoryDefaultBad = new QFCategory("Default", "#ff0000", 0.33f, "Bad");
entityManager.persist(qfCategoryDefaultBad);

QFCategory qfCategoryDefaultGood = new QFCategory("Default", "#00ff00", 1f,"Good");
entityManager.persist(qfCategoryDefaultGood);

QFCategory qfCategoryDefaultNeutral = new QFCategory("Default", "#ff8000", 0.67f,"Neutral");
entityManager.persist(qfCategoryDefaultNeutral);

QFCategory qfCategory6memUp = new QFCategory("6 members contribution", "#ff0000", 1f, "Up");
entityManager.persist(qfCategory6memUp);

QFCategory qfCategory6memHigh = new QFCategory("6 members contribution", "#00ff00", 0.8f,"High");
entityManager.persist(qfCategory6memHigh);

QFCategory qfCategory6memGood = new QFCategory("6 members contribution", "#00ff00", 0.5f,"Neutral");
entityManager.persist(qfCategory6memGood);

QFCategory qfCategory6memLow = new QFCategory("6 members contribution", "#ff8000", 0.3f,"Low");
entityManager.persist(qfCategory6memLow);

QFCategory qfCategory6memDown = new QFCategory("6 members contribution", "#ff8000", 0.15f,"Down");
entityManager.persist(qfCategory6memDown);

// When
List<QFCategory> categoryList = qfCategoryRepository.findAllByType("Down");

// Then
assertEquals("Down", categoryList.get(0).getType());
assertEquals("6 members contribution", categoryList.get(0).getName());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void getFactorsCategories () throws Exception {

this.mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
//.andExpect(jsonPath("$", hasSize(3))) Because now there is more than 3 categoreis
.andExpect(jsonPath("$[0].id", is(factorCategoryList.get(0).getId().intValue())))
.andExpect(jsonPath("$[0].name", is(factorCategoryList.get(0).getName())))
.andExpect(jsonPath("$[0].color", is(factorCategoryList.get(0).getColor())))
Expand Down
Loading

0 comments on commit 1b26b28

Please sign in to comment.