Skip to content

Commit

Permalink
persist measures on server side - SONAR-6253
Browse files Browse the repository at this point in the history
  • Loading branch information
teryk committed Mar 20, 2015
1 parent 47fdcb9 commit ee58603
Show file tree
Hide file tree
Showing 47 changed files with 5,325 additions and 1,300 deletions.
Expand Up @@ -183,8 +183,8 @@ private MeasureDto coverageMeasure(Map<String, MeasureDto> measuresByMetricKey)
private Map<String, MeasureDto> measuresByMetricKey(ComponentDto component, DbSession session) {
Map<String, MeasureDto> measuresByMetricKey = newHashMap();
String fileKey = component.getKey();
for (MeasureDto measureDto : dbClient.measureDao().findByComponentKeyAndMetricKeys(fileKey, METRIC_KEYS, session)) {
measuresByMetricKey.put(measureDto.getKey().metricKey(), measureDto);
for (MeasureDto measureDto : dbClient.measureDao().findByComponentKeyAndMetricKeys(session, fileKey, METRIC_KEYS)) {
measuresByMetricKey.put(measureDto.getMetricKey(), measureDto);
}
return measuresByMetricKey;
}
Expand All @@ -199,29 +199,31 @@ private ComponentDto nullableComponentById(@Nullable Long componentId, DbSession

@CheckForNull
private String formatMeasure(@Nullable MeasureDto measure) {
if (measure != null) {
Metric metric = CoreMetrics.getMetric(measure.getKey().metricKey());
Metric.ValueType metricType = metric.getType();
Double value = measure.getValue();
String data = measure.getData();
if (BooleanUtils.isTrue(metric.isOptimizedBestValue()) && value == null) {
value = metric.getBestValue();
}
if (metricType.equals(Metric.ValueType.FLOAT) && value != null) {
return i18n.formatDouble(UserSession.get().locale(), value);
}
if (metricType.equals(Metric.ValueType.INT) && value != null) {
return i18n.formatInteger(UserSession.get().locale(), value.intValue());
}
if (metricType.equals(Metric.ValueType.PERCENT) && value != null) {
return i18n.formatDouble(UserSession.get().locale(), value) + "%";
}
if (metricType.equals(Metric.ValueType.WORK_DUR) && value != null) {
return durations.format(UserSession.get().locale(), durations.create(value.longValue()), Durations.DurationFormat.SHORT);
}
if ((metricType.equals(Metric.ValueType.STRING) || metricType.equals(Metric.ValueType.RATING)) && data != null) {
return data;
}
if (measure == null) {
return null;
}

Metric metric = CoreMetrics.getMetric(measure.getMetricKey());
Metric.ValueType metricType = metric.getType();
Double value = measure.getValue();
String data = measure.getData();
if (BooleanUtils.isTrue(metric.isOptimizedBestValue()) && value == null) {
value = metric.getBestValue();
}
if (metricType.equals(Metric.ValueType.FLOAT) && value != null) {
return i18n.formatDouble(UserSession.get().locale(), value);
}
if (metricType.equals(Metric.ValueType.INT) && value != null) {
return i18n.formatInteger(UserSession.get().locale(), value.intValue());
}
if (metricType.equals(Metric.ValueType.PERCENT) && value != null) {
return i18n.formatDouble(UserSession.get().locale(), value) + "%";
}
if (metricType.equals(Metric.ValueType.WORK_DUR) && value != null) {
return durations.format(UserSession.get().locale(), durations.create(value.longValue()), Durations.DurationFormat.SHORT);
}
if ((metricType.equals(Metric.ValueType.STRING) || metricType.equals(Metric.ValueType.RATING)) && data != null) {
return data;
}
return null;
}
Expand Down
Expand Up @@ -21,13 +21,8 @@

import org.sonar.api.platform.ComponentContainer;
import org.sonar.core.issue.db.UpdateConflictResolver;
import org.sonar.server.computation.issue.IssueCache;
import org.sonar.server.computation.issue.IssueComputation;
import org.sonar.server.computation.issue.RuleCache;
import org.sonar.server.computation.issue.RuleCacheLoader;
import org.sonar.server.computation.issue.ScmAccountCache;
import org.sonar.server.computation.issue.ScmAccountCacheLoader;
import org.sonar.server.computation.issue.SourceLinesCache;
import org.sonar.server.computation.issue.*;
import org.sonar.server.computation.measure.MetricCache;
import org.sonar.server.computation.step.ComputationSteps;
import org.sonar.server.platform.Platform;
import org.sonar.server.view.index.ViewIndex;
Expand All @@ -37,22 +32,6 @@

public class ComputationContainer {

public void execute(ReportQueue.Item item) {
ComponentContainer container = Platform.getInstance().getContainer();
ComponentContainer child = container.createChild();
child.addSingletons(componentClasses());
child.addSingletons(ComputationSteps.orderedStepClasses());
child.startComponents();
try {
child.getComponentByType(ComputationService.class).process(item);
} finally {
child.stopComponents();
// TODO not possible to have multiple children -> will be
// a problem when we will have multiple concurrent computation workers
container.removeChild();
}
}

/**
* List of all objects to be injected in the picocontainer dedicated to computation stack.
* Does not contain the steps declared in {@link org.sonar.server.computation.step.ComputationSteps#orderedStepClasses()}.
Expand All @@ -70,9 +49,26 @@ static List componentClasses() {
RuleCache.class,
RuleCacheLoader.class,
IssueCache.class,
MetricCache.class,
UpdateConflictResolver.class,

// views
ViewIndex.class);
}

public void execute(ReportQueue.Item item) {
ComponentContainer container = Platform.getInstance().getContainer();
ComponentContainer child = container.createChild();
child.addSingletons(componentClasses());
child.addSingletons(ComputationSteps.orderedStepClasses());
child.startComponents();
try {
child.getComponentByType(ComputationService.class).process(item);
} finally {
child.stopComponents();
// TODO not possible to have multiple children -> will be
// a problem when we will have multiple concurrent computation workers
container.removeChild();
}
}
}
@@ -0,0 +1,87 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.computation.measure;

import org.sonar.batch.protocol.output.BatchReport;

import javax.annotation.CheckForNull;

public class BatchReportMeasureUtils {
private BatchReportMeasureUtils() {
// static methods only
}

/**
* return the numerical value as a double. It's the type used in db.
* Returns null if no numerical value found
*/
@CheckForNull
public static Double valueAsDouble(BatchReport.Measure measure) {
switch (measure.getValueType()) {
case BOOLEAN:
return measure.getBooleanValue() ? 1.0d : 0.0d;
case INT:
return Double.valueOf(measure.getIntValue());
case LONG:
return Double.valueOf(measure.getLongValue());
case DOUBLE:
return measure.getDoubleValue();
default:
return null;
}
}

/**
* check that measure has a value (numerical or string) and a metric key
*/
public static void checkMeasure(BatchReport.Measure measure) {
if (!measure.hasValueType() || !measure.hasMetricKey()) {
throw newIllegalStateException(measure);
}

boolean hasValueOrData = false;
switch (measure.getValueType()) {
case DOUBLE:
hasValueOrData = measure.hasDoubleValue();
break;
case INT:
hasValueOrData = measure.hasIntValue();
break;
case LONG:
hasValueOrData = measure.hasLongValue();
break;
case STRING:
hasValueOrData = measure.hasStringValue();
break;
case BOOLEAN:
hasValueOrData = measure.hasBooleanValue();
break;
}

if (!hasValueOrData) {
throw newIllegalStateException(measure);
}
}

private static IllegalStateException newIllegalStateException(BatchReport.Measure measure) {
return new IllegalStateException(String.format("Measure %s does not have value", measure));
}
}
@@ -0,0 +1,56 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.computation.measure;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import org.sonar.core.measure.db.MetricDto;
import org.sonar.core.persistence.DbSession;
import org.sonar.server.db.DbClient;
import org.sonar.server.exceptions.NotFoundException;

import java.util.List;
import java.util.Map;

public class MetricCache {
private final Map<String, MetricDto> metrics;

public MetricCache(DbClient dbClient) {
try (DbSession dbSession = dbClient.openSession(false)) {
List<MetricDto> metricList = dbClient.metricDao().findEnabled(dbSession);
this.metrics = Maps.uniqueIndex(metricList, new Function<MetricDto, String>() {
@Override
public String apply(MetricDto metric) {
return metric.getKey();
}
});
}
}

public MetricDto get(String key) {
MetricDto metric = metrics.get(key);
if (metric == null) {
throw new NotFoundException(String.format("Not found: '%s'", key));
}

return metric;
}
}
Expand Up @@ -41,6 +41,7 @@ public static List<Class<? extends ComputationStep>> orderedStepClasses() {
ParseReportStep.class,

// Persist data
PersistMeasuresStep.class,
PersistIssuesStep.class,
PersistComponentLinksStep.class,
PersistEventsStep.class,
Expand Down

0 comments on commit ee58603

Please sign in to comment.