3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.step;
22 import com.google.common.base.Predicate;
23 import com.google.common.base.Predicates;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.Multimap;
26 import java.util.Collection;
27 import java.util.List;
29 import javax.annotation.Nonnull;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.measure.MeasureDao;
33 import org.sonar.db.measure.MeasureDto;
34 import org.sonar.server.computation.task.projectanalysis.component.Component;
35 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
36 import org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
37 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolder;
38 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
39 import org.sonar.server.computation.task.projectanalysis.measure.BestValueOptimization;
40 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
41 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
42 import org.sonar.server.computation.task.projectanalysis.measure.MeasureToMeasureDto;
43 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
44 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
45 import org.sonar.server.computation.task.step.ComputationStep;
47 import static com.google.common.collect.FluentIterable.from;
48 import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY_DISTRIBUTION_KEY;
49 import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY;
50 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY;
51 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
53 public class PersistMeasuresStep implements ComputationStep {
56 * List of metrics that should not be persisted on file measure (Waiting for SONAR-6688 to be implemented)
58 private static final List<String> NOT_TO_PERSIST_ON_FILE_METRIC_KEYS = ImmutableList.of(
59 FILE_COMPLEXITY_DISTRIBUTION_KEY,
60 FUNCTION_COMPLEXITY_DISTRIBUTION_KEY,
61 CLASS_COMPLEXITY_DISTRIBUTION_KEY);
63 private final DbClient dbClient;
64 private final MetricRepository metricRepository;
65 private final MeasureToMeasureDto measureToMeasureDto;
66 private final TreeRootHolder treeRootHolder;
67 private final MeasureRepository measureRepository;
69 public PersistMeasuresStep(DbClient dbClient, MetricRepository metricRepository, MeasureToMeasureDto measureToMeasureDto,
70 TreeRootHolder treeRootHolder, MeasureRepository measureRepository) {
71 this.dbClient = dbClient;
72 this.metricRepository = metricRepository;
73 this.measureToMeasureDto = measureToMeasureDto;
74 this.treeRootHolder = treeRootHolder;
75 this.measureRepository = measureRepository;
79 public String getDescription() {
80 return "Persist measures";
84 public void execute() {
85 DbSession dbSession = dbClient.openSession(true);
87 new DepthTraversalTypeAwareCrawler(new MeasureVisitor(dbSession)).visit(treeRootHolder.getRoot());
94 private class MeasureVisitor extends TypeAwareVisitorAdapter {
95 private final DbSession session;
97 private MeasureVisitor(DbSession session) {
98 super(CrawlerDepthLimit.LEAVES, PRE_ORDER);
99 this.session = session;
103 public void visitAny(Component component) {
104 Multimap<String, Measure> measures = measureRepository.getRawMeasures(component);
105 persistMeasures(component, measures);
108 private void persistMeasures(Component component, Multimap<String, Measure> batchReportMeasures) {
109 for (Map.Entry<String, Collection<Measure>> measures : batchReportMeasures.asMap().entrySet()) {
110 String metricKey = measures.getKey();
111 if (NOT_TO_PERSIST_ON_FILE_METRIC_KEYS.contains(metricKey) && component.getType() == Component.Type.FILE) {
115 Metric metric = metricRepository.getByKey(metricKey);
116 Predicate<Measure> notBestValueOptimized = Predicates.not(BestValueOptimization.from(metric, component));
117 MeasureDao measureDao = dbClient.measureDao();
118 for (Measure measure : from(measures.getValue()).filter(NonEmptyMeasure.INSTANCE).filter(notBestValueOptimized)) {
119 MeasureDto measureDto = measureToMeasureDto.toMeasureDto(measure, metric, component);
120 measureDao.insert(session, measureDto);
127 private enum NonEmptyMeasure implements Predicate<Measure> {
131 public boolean apply(@Nonnull Measure input) {
132 return input.getValueType() != Measure.ValueType.NO_VALUE || input.hasVariations() || input.getData() != null;