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.

PersistLiveMeasuresStep.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  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.ce.task.projectanalysis.step;
  21. import java.util.ArrayList;
  22. import java.util.HashSet;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import java.util.function.Predicate;
  27. import javax.annotation.Nonnull;
  28. import org.sonar.ce.task.projectanalysis.component.Component;
  29. import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
  30. import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
  31. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  32. import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
  33. import org.sonar.ce.task.projectanalysis.measure.BestValueOptimization;
  34. import org.sonar.ce.task.projectanalysis.measure.Measure;
  35. import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
  36. import org.sonar.ce.task.projectanalysis.measure.MeasureToMeasureDto;
  37. import org.sonar.ce.task.projectanalysis.metric.Metric;
  38. import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
  39. import org.sonar.ce.task.step.ComputationStep;
  40. import org.sonar.db.DbClient;
  41. import org.sonar.db.DbSession;
  42. import org.sonar.db.measure.LiveMeasureDto;
  43. import static java.util.Arrays.asList;
  44. import static java.util.Collections.unmodifiableSet;
  45. import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY;
  46. import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY;
  47. import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
  48. public class PersistLiveMeasuresStep implements ComputationStep {
  49. /**
  50. * List of metrics that should not be persisted on file measure.
  51. */
  52. private static final Set<String> NOT_TO_PERSIST_ON_FILE_METRIC_KEYS = Set.of(FILE_COMPLEXITY_DISTRIBUTION_KEY, FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
  53. private final DbClient dbClient;
  54. private final MetricRepository metricRepository;
  55. private final MeasureToMeasureDto measureToMeasureDto;
  56. private final TreeRootHolder treeRootHolder;
  57. private final MeasureRepository measureRepository;
  58. public PersistLiveMeasuresStep(DbClient dbClient, MetricRepository metricRepository, MeasureToMeasureDto measureToMeasureDto,
  59. TreeRootHolder treeRootHolder, MeasureRepository measureRepository) {
  60. this.dbClient = dbClient;
  61. this.metricRepository = metricRepository;
  62. this.measureToMeasureDto = measureToMeasureDto;
  63. this.treeRootHolder = treeRootHolder;
  64. this.measureRepository = measureRepository;
  65. }
  66. @Override
  67. public String getDescription() {
  68. return "Persist live measures";
  69. }
  70. @Override
  71. public void execute(ComputationStep.Context context) {
  72. boolean supportUpsert = dbClient.getDatabase().getDialect().supportsUpsert();
  73. try (DbSession dbSession = dbClient.openSession(true)) {
  74. Component root = treeRootHolder.getRoot();
  75. MeasureVisitor visitor = new MeasureVisitor(dbSession, supportUpsert);
  76. new DepthTraversalTypeAwareCrawler(visitor).visit(root);
  77. dbSession.commit();
  78. context.getStatistics()
  79. .add("insertsOrUpdates", visitor.insertsOrUpdates);
  80. }
  81. }
  82. private class MeasureVisitor extends TypeAwareVisitorAdapter {
  83. private final DbSession dbSession;
  84. private final boolean supportUpsert;
  85. private int insertsOrUpdates = 0;
  86. private MeasureVisitor(DbSession dbSession, boolean supportUpsert) {
  87. super(CrawlerDepthLimit.LEAVES, PRE_ORDER);
  88. this.supportUpsert = supportUpsert;
  89. this.dbSession = dbSession;
  90. }
  91. @Override
  92. public void visitAny(Component component) {
  93. List<String> metricUuids = new ArrayList<>();
  94. Map<String, Measure> measures = measureRepository.getRawMeasures(component);
  95. List<LiveMeasureDto> dtos = new ArrayList<>();
  96. for (Map.Entry<String, Measure> measuresByMetricKey : measures.entrySet()) {
  97. String metricKey = measuresByMetricKey.getKey();
  98. if (NOT_TO_PERSIST_ON_FILE_METRIC_KEYS.contains(metricKey) && component.getType() == Component.Type.FILE) {
  99. continue;
  100. }
  101. Metric metric = metricRepository.getByKey(metricKey);
  102. Predicate<Measure> notBestValueOptimized = BestValueOptimization.from(metric, component).negate();
  103. Measure m = measuresByMetricKey.getValue();
  104. if (!NonEmptyMeasure.INSTANCE.test(m) || !notBestValueOptimized.test(m)) {
  105. continue;
  106. }
  107. LiveMeasureDto lm = measureToMeasureDto.toLiveMeasureDto(m, metric, component);
  108. dtos.add(lm);
  109. metricUuids.add(metric.getUuid());
  110. }
  111. if (supportUpsert) {
  112. for (LiveMeasureDto dto : dtos) {
  113. dbClient.liveMeasureDao().upsert(dbSession, dto);
  114. }
  115. // The measures that no longer exist on the component must be deleted, for example
  116. // when the coverage on a file goes to the "best value" 100%.
  117. // The measures on deleted components are deleted by the step PurgeDatastoresStep
  118. dbClient.liveMeasureDao().deleteByComponentUuidExcludingMetricUuids(dbSession, component.getUuid(), metricUuids);
  119. } else {
  120. dbClient.liveMeasureDao().deleteByComponent(dbSession, component.getUuid());
  121. dtos.forEach(dto -> dbClient.liveMeasureDao().insert(dbSession, dto));
  122. }
  123. dbSession.commit();
  124. insertsOrUpdates += dtos.size();
  125. }
  126. }
  127. private enum NonEmptyMeasure implements Predicate<Measure> {
  128. INSTANCE;
  129. @Override
  130. public boolean test(@Nonnull Measure input) {
  131. return input.getValueType() != Measure.ValueType.NO_VALUE || input.hasVariation() || input.getData() != null;
  132. }
  133. }
  134. }