use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.
the class LiveMeasureDtoToMeasureTest method toMeasure_creates_MeasureVariation_and_maps_the_right_one.
@Test
public void toMeasure_creates_MeasureVariation_and_maps_the_right_one() {
LiveMeasureDto LiveMeasureDto = new LiveMeasureDto().setData("1").setVariation(2d);
Optional<Measure> measure = underTest.toMeasure(LiveMeasureDto, SOME_STRING_METRIC);
assertThat(measure.get().getVariation()).isEqualTo(2);
}
use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.
the class MeasureToMeasureDtoTest method toLiveMeasureDto.
@Test
public void toLiveMeasureDto() {
treeRootHolder.setRoot(SOME_COMPONENT);
LiveMeasureDto liveMeasureDto = underTest.toLiveMeasureDto(Measure.newMeasureBuilder().create(Measure.Level.OK), SOME_LEVEL_METRIC, SOME_COMPONENT);
assertThat(liveMeasureDto.getTextValue()).isEqualTo(Measure.Level.OK.name());
}
use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.
the class LiveMeasureComputerImpl method refreshComponentsOnSameProject.
private Optional<QGChangeEvent> refreshComponentsOnSameProject(DbSession dbSession, List<ComponentDto> touchedComponents) {
// load all the components to be refreshed, including their ancestors
List<ComponentDto> components = loadTreeOfComponents(dbSession, touchedComponents);
ComponentDto branchComponent = findBranchComponent(components);
BranchDto branch = loadBranch(dbSession, branchComponent);
ProjectDto project = loadProject(dbSession, branch.getProjectUuid());
Optional<SnapshotDto> lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, branchComponent.uuid());
if (!lastAnalysis.isPresent()) {
return Optional.empty();
}
QualityGate qualityGate = qGateComputer.loadQualityGate(dbSession, project, branch);
Collection<String> metricKeys = getKeysOfAllInvolvedMetrics(qualityGate);
List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, metricKeys);
Map<String, MetricDto> metricsPerId = metrics.stream().collect(uniqueIndex(MetricDto::getUuid));
List<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(toArrayList(components.size()));
List<LiveMeasureDto> dbMeasures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricUuids(dbSession, componentUuids, metricsPerId.keySet());
// previous status must be load now as MeasureMatrix mutate the LiveMeasureDto which are passed to it
Metric.Level previousStatus = loadPreviousStatus(metrics, dbMeasures);
Configuration config = projectConfigurationLoader.loadProjectConfiguration(dbSession, branchComponent);
DebtRatingGrid debtRatingGrid = new DebtRatingGrid(config);
MeasureMatrix matrix = new MeasureMatrix(components, metricsPerId.values(), dbMeasures);
FormulaContextImpl context = new FormulaContextImpl(matrix, debtRatingGrid);
long beginningOfLeak = getBeginningOfLeakPeriod(lastAnalysis, branch);
components.forEach(c -> {
IssueCounter issueCounter = new IssueCounter(dbClient.issueDao().selectIssueGroupsByBaseComponent(dbSession, c, beginningOfLeak));
for (IssueMetricFormula formula : formulaFactory.getFormulas()) {
// use formulas when the leak period is defined, it's a PR, or the formula is not about the leak period
if (shouldUseLeakFormulas(lastAnalysis.get(), branch) || !formula.isOnLeak()) {
context.change(c, formula);
try {
formula.compute(context, issueCounter);
} catch (RuntimeException e) {
throw new IllegalStateException("Fail to compute " + formula.getMetric().getKey() + " on " + context.getComponent().getDbKey(), e);
}
}
}
});
EvaluatedQualityGate evaluatedQualityGate = qGateComputer.refreshGateStatus(branchComponent, qualityGate, matrix, config);
// persist the measures that have been created or updated
matrix.getChanged().sorted(LiveMeasureComparator.INSTANCE).forEach(m -> dbClient.liveMeasureDao().insertOrUpdate(dbSession, m));
projectIndexer.commitAndIndexComponents(dbSession, singleton(branchComponent), ProjectIndexer.Cause.MEASURE_CHANGE);
return Optional.of(new QGChangeEvent(project, branch, lastAnalysis.get(), config, previousStatus, () -> Optional.of(evaluatedQualityGate)));
}
use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.
the class MeasureMatrix method changeCell.
private void changeCell(ComponentDto component, String metricKey, Predicate<LiveMeasureDto> changer) {
MeasureCell cell = table.get(component.uuid(), metricKey);
if (cell == null) {
LiveMeasureDto measure = new LiveMeasureDto().setComponentUuid(component.uuid()).setProjectUuid(component.projectUuid()).setMetricUuid(metricsByKeys.get(metricKey).getUuid());
cell = new MeasureCell(measure, true);
table.put(component.uuid(), metricKey, cell);
changer.test(cell.getMeasure());
} else if (changer.test(cell.getMeasure())) {
cell.setChanged(true);
}
}
use of org.sonar.db.measure.LiveMeasureDto in project sonarqube by SonarSource.
the class ComponentAction method getMeasuresByMetric.
private static Map<MetricDto, LiveMeasureDto> getMeasuresByMetric(List<LiveMeasureDto> measures, Collection<MetricDto> metrics) {
Map<String, MetricDto> metricsByUuid = Maps.uniqueIndex(metrics, MetricDto::getUuid);
Map<MetricDto, LiveMeasureDto> measuresByMetric = new HashMap<>();
for (LiveMeasureDto measure : measures) {
MetricDto metric = metricsByUuid.get(measure.getMetricUuid());
measuresByMetric.put(metric, measure);
}
return measuresByMetric;
}
Aggregations