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.

NewEffortAggregator.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.issue;
  21. import com.google.common.base.MoreObjects;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.sonar.api.measures.CoreMetrics;
  25. import org.sonar.ce.task.projectanalysis.component.Component;
  26. import org.sonar.ce.task.projectanalysis.measure.Measure;
  27. import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
  28. import org.sonar.ce.task.projectanalysis.metric.Metric;
  29. import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
  30. import org.sonar.core.issue.DefaultIssue;
  31. import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_REMEDIATION_EFFORT_KEY;
  32. import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_REMEDIATION_EFFORT_KEY;
  33. import static org.sonar.api.measures.CoreMetrics.NEW_TECHNICAL_DEBT_KEY;
  34. /**
  35. * Compute new effort related measures :
  36. * {@link CoreMetrics#NEW_TECHNICAL_DEBT_KEY}
  37. * {@link CoreMetrics#NEW_RELIABILITY_REMEDIATION_EFFORT_KEY}
  38. * {@link CoreMetrics#NEW_SECURITY_REMEDIATION_EFFORT_KEY}
  39. */
  40. public class NewEffortAggregator extends IssueVisitor {
  41. private final Map<String, NewEffortCounter> counterByComponentUuid = new HashMap<>();
  42. private final MeasureRepository measureRepository;
  43. private final Metric newMaintainabilityEffortMetric;
  44. private final Metric newReliabilityEffortMetric;
  45. private final Metric newSecurityEffortMetric;
  46. private final NewIssueClassifier newIssueClassifier;
  47. private NewEffortCounter counter = null;
  48. public NewEffortAggregator(MetricRepository metricRepository, MeasureRepository measureRepository, NewIssueClassifier newIssueClassifier) {
  49. this.measureRepository = measureRepository;
  50. this.newMaintainabilityEffortMetric = metricRepository.getByKey(NEW_TECHNICAL_DEBT_KEY);
  51. this.newReliabilityEffortMetric = metricRepository.getByKey(NEW_RELIABILITY_REMEDIATION_EFFORT_KEY);
  52. this.newSecurityEffortMetric = metricRepository.getByKey(NEW_SECURITY_REMEDIATION_EFFORT_KEY);
  53. this.newIssueClassifier = newIssueClassifier;
  54. }
  55. @Override
  56. public void beforeComponent(Component component) {
  57. counter = new NewEffortCounter();
  58. counterByComponentUuid.put(component.getUuid(), counter);
  59. for (Component child : component.getChildren()) {
  60. NewEffortCounter childSum = counterByComponentUuid.remove(child.getUuid());
  61. if (childSum != null) {
  62. counter.add(childSum);
  63. }
  64. }
  65. }
  66. @Override
  67. public void onIssue(Component component, DefaultIssue issue) {
  68. if (issue.resolution() == null && issue.effortInMinutes() != null) {
  69. counter.add(component, issue);
  70. }
  71. }
  72. @Override
  73. public void afterComponent(Component component) {
  74. if (newIssueClassifier.isEnabled()) {
  75. computeMeasure(component, newMaintainabilityEffortMetric, counter.maintainabilitySum);
  76. computeMeasure(component, newReliabilityEffortMetric, counter.reliabilitySum);
  77. computeMeasure(component, newSecurityEffortMetric, counter.securitySum);
  78. }
  79. counter = null;
  80. }
  81. private void computeMeasure(Component component, Metric metric, EffortSum effortSum) {
  82. double variation = effortSum.isEmpty ? 0.0 : effortSum.newEffort;
  83. measureRepository.add(component, metric, Measure.newMeasureBuilder().setVariation(variation).createNoValue());
  84. }
  85. private class NewEffortCounter {
  86. private final EffortSum maintainabilitySum = new EffortSum();
  87. private final EffortSum reliabilitySum = new EffortSum();
  88. private final EffortSum securitySum = new EffortSum();
  89. void add(NewEffortCounter otherCounter) {
  90. maintainabilitySum.add(otherCounter.maintainabilitySum);
  91. reliabilitySum.add(otherCounter.reliabilitySum);
  92. securitySum.add(otherCounter.securitySum);
  93. }
  94. void add(Component component, DefaultIssue issue) {
  95. long newEffort = calculate(component, issue);
  96. switch (issue.type()) {
  97. case CODE_SMELL:
  98. maintainabilitySum.add(newEffort);
  99. break;
  100. case BUG:
  101. reliabilitySum.add(newEffort);
  102. break;
  103. case VULNERABILITY:
  104. securitySum.add(newEffort);
  105. break;
  106. case SECURITY_HOTSPOT:
  107. // Not counted
  108. break;
  109. default:
  110. throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
  111. }
  112. }
  113. long calculate(Component component, DefaultIssue issue) {
  114. if (newIssueClassifier.isNew(component, issue)) {
  115. return MoreObjects.firstNonNull(issue.effortInMinutes(), 0L);
  116. }
  117. return 0L;
  118. }
  119. }
  120. private static class EffortSum {
  121. private Double newEffort;
  122. private boolean isEmpty = true;
  123. void add(long newEffort) {
  124. double previous = MoreObjects.firstNonNull(this.newEffort, 0D);
  125. this.newEffort = previous + newEffort;
  126. isEmpty = false;
  127. }
  128. void add(EffortSum other) {
  129. Double otherValue = other.newEffort;
  130. if (otherValue != null) {
  131. add(otherValue.longValue());
  132. }
  133. }
  134. }
  135. }