Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
persist measures on server side - SONAR-6253
- Loading branch information
Showing
47 changed files
with
5,325 additions
and
1,300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...ar-server/src/main/java/org/sonar/server/computation/measure/BatchReportMeasureUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
server/sonar-server/src/main/java/org/sonar/server/computation/measure/MetricCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.