You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PersistMeasuresStep.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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.
  10. *
  11. * SonarQube 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.
  15. *
  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.
  19. */
  20. package org.sonar.server.computation.step;
  21. import com.google.common.base.Predicate;
  22. import com.google.common.base.Predicates;
  23. import com.google.common.collect.ImmutableList;
  24. import com.google.common.collect.Multimap;
  25. import java.util.Collection;
  26. import java.util.List;
  27. import java.util.Map;
  28. import javax.annotation.Nonnull;
  29. import org.sonar.api.measures.CoreMetrics;
  30. import org.sonar.db.measure.MeasureDto;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.server.computation.component.Component;
  33. import org.sonar.server.computation.component.DbIdsRepository;
  34. import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
  35. import org.sonar.server.computation.component.TreeRootHolder;
  36. import org.sonar.server.computation.measure.BestValueOptimization;
  37. import org.sonar.server.computation.measure.Measure;
  38. import org.sonar.server.computation.measure.MeasureRepository;
  39. import org.sonar.server.computation.measure.MeasureToMeasureDto;
  40. import org.sonar.server.computation.metric.Metric;
  41. import org.sonar.server.computation.metric.MetricRepository;
  42. import org.sonar.server.db.DbClient;
  43. import static com.google.common.collect.FluentIterable.from;
  44. import static org.sonar.server.computation.component.ComponentVisitor.Order.PRE_ORDER;
  45. public class PersistMeasuresStep implements ComputationStep {
  46. /**
  47. * List of metrics that should not be received from the report, as they should only be fed by the compute engine
  48. */
  49. private static final List<String> FORBIDDEN_METRIC_KEYS = ImmutableList.of(CoreMetrics.DUPLICATIONS_DATA_KEY);
  50. /**
  51. * List of metrics that should be persisted on file measure (Waiting for SONAR-6688 to be implemented)
  52. */
  53. private static final List<String> NOT_TO_PERSIST_ON_FILE_METRIC_KEYS = ImmutableList.of(
  54. CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY,
  55. CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY
  56. );
  57. private final DbClient dbClient;
  58. private final MetricRepository metricRepository;
  59. private final DbIdsRepository dbIdsRepository;
  60. private final TreeRootHolder treeRootHolder;
  61. private final MeasureRepository measureRepository;
  62. public PersistMeasuresStep(DbClient dbClient, MetricRepository metricRepository, DbIdsRepository dbIdsRepository,
  63. TreeRootHolder treeRootHolder, MeasureRepository measureRepository) {
  64. this.dbClient = dbClient;
  65. this.metricRepository = metricRepository;
  66. this.dbIdsRepository = dbIdsRepository;
  67. this.treeRootHolder = treeRootHolder;
  68. this.measureRepository = measureRepository;
  69. }
  70. @Override
  71. public String getDescription() {
  72. return "Persist measures";
  73. }
  74. @Override
  75. public void execute() {
  76. DbSession dbSession = dbClient.openSession(true);
  77. try {
  78. new MeasureVisitor(dbSession).visit(treeRootHolder.getRoot());
  79. dbSession.commit();
  80. } finally {
  81. dbSession.close();
  82. }
  83. }
  84. private class MeasureVisitor extends DepthTraversalTypeAwareVisitor {
  85. private final DbSession session;
  86. private MeasureVisitor(DbSession session) {
  87. super(Component.Type.FILE, PRE_ORDER);
  88. this.session = session;
  89. }
  90. @Override
  91. public void visitAny(Component component) {
  92. Multimap<String, Measure> measures = measureRepository.getRawMeasures(component);
  93. long componentId = dbIdsRepository.getComponentId(component);
  94. long snapshotId = dbIdsRepository.getSnapshotId(component);
  95. persistMeasures(component, measures, componentId, snapshotId);
  96. }
  97. private void persistMeasures(Component component, Multimap<String, Measure> batchReportMeasures, long componentId, long snapshotId) {
  98. for (Map.Entry<String, Collection<Measure>> measures : batchReportMeasures.asMap().entrySet()) {
  99. String metricKey = measures.getKey();
  100. if (FORBIDDEN_METRIC_KEYS.contains(metricKey)) {
  101. throw new IllegalStateException(String.format("Measures on metric '%s' cannot be send in the report", metricKey));
  102. }
  103. if (NOT_TO_PERSIST_ON_FILE_METRIC_KEYS.contains(metricKey) && component.getType() == Component.Type.FILE) {
  104. continue;
  105. }
  106. Metric metric = metricRepository.getByKey(metricKey);
  107. Predicate<Measure> notBestValueOptimized = Predicates.not(BestValueOptimization.from(metric, component));
  108. for (Measure measure : from(measures.getValue()).filter(NonEmptyMeasure.INSTANCE).filter(notBestValueOptimized)) {
  109. MeasureDto measureDto = MeasureToMeasureDto.INSTANCE.toMeasureDto(measure, metric, componentId, snapshotId);
  110. dbClient.measureDao().insert(session, measureDto);
  111. }
  112. }
  113. }
  114. }
  115. private enum NonEmptyMeasure implements Predicate<Measure> {
  116. INSTANCE;
  117. @Override
  118. public boolean apply(@Nonnull Measure input) {
  119. return input.getValueType() != Measure.ValueType.NO_VALUE || input.hasVariations() || input.getData() != null;
  120. }
  121. }
  122. }