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.

CustomMeasuresCopyStep.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 com.google.common.annotations.VisibleForTesting;
  22. import java.util.List;
  23. import org.apache.commons.lang.math.NumberUtils;
  24. import org.sonar.ce.task.projectanalysis.component.Component;
  25. import org.sonar.ce.task.projectanalysis.component.ComponentVisitor;
  26. import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
  27. import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
  28. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  29. import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
  30. import org.sonar.ce.task.projectanalysis.measure.Measure;
  31. import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
  32. import org.sonar.ce.task.projectanalysis.metric.Metric;
  33. import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
  34. import org.sonar.ce.task.step.ComputationStep;
  35. import org.sonar.db.DbClient;
  36. import org.sonar.db.DbSession;
  37. import org.sonar.db.measure.custom.CustomMeasureDto;
  38. public class CustomMeasuresCopyStep implements ComputationStep {
  39. private final TreeRootHolder treeRootHolder;
  40. private final DbClient dbClient;
  41. private final MetricRepository metricRepository;
  42. private final MeasureRepository measureRepository;
  43. public CustomMeasuresCopyStep(TreeRootHolder treeRootHolder, DbClient dbClient,
  44. MetricRepository metricRepository, MeasureRepository measureRepository) {
  45. this.treeRootHolder = treeRootHolder;
  46. this.dbClient = dbClient;
  47. this.metricRepository = metricRepository;
  48. this.measureRepository = measureRepository;
  49. }
  50. @Override
  51. public void execute(ComputationStep.Context context) {
  52. try (DbSession session = dbClient.openSession(false)) {
  53. CrawlerDepthLimit depthLimit = new CrawlerDepthLimit.Builder(Component.Type.PROJECT)
  54. .withViewsMaxDepth(Component.Type.PROJECT_VIEW);
  55. new DepthTraversalTypeAwareCrawler(
  56. new TypeAwareVisitorAdapter(depthLimit, ComponentVisitor.Order.PRE_ORDER) {
  57. @Override
  58. public void visitAny(Component component) {
  59. copy(component, session);
  60. }
  61. }).visit(treeRootHolder.getRoot());
  62. }
  63. }
  64. private void copy(Component component, DbSession session) {
  65. for (CustomMeasureDto dto : loadCustomMeasures(component, session)) {
  66. Metric metric = metricRepository.getById(dto.getMetricId());
  67. // else metric is not found and an exception is raised
  68. Measure measure = dtoToMeasure(dto, metric);
  69. measureRepository.add(component, metric, measure);
  70. }
  71. }
  72. private List<CustomMeasureDto> loadCustomMeasures(Component component, DbSession session) {
  73. return dbClient.customMeasureDao().selectByComponentUuid(session, component.getUuid());
  74. }
  75. @VisibleForTesting
  76. static Measure dtoToMeasure(CustomMeasureDto dto, Metric metric) {
  77. switch (metric.getType()) {
  78. case INT:
  79. case RATING:
  80. return Measure.newMeasureBuilder().create((int) dto.getValue());
  81. case MILLISEC:
  82. case WORK_DUR:
  83. return Measure.newMeasureBuilder().create((long) dto.getValue());
  84. case FLOAT:
  85. case PERCENT:
  86. return Measure.newMeasureBuilder().create(dto.getValue(), metric.getDecimalScale());
  87. case BOOL:
  88. return Measure.newMeasureBuilder().create(NumberUtils.compare(dto.getValue(), 1.0) == 0);
  89. case LEVEL:
  90. return Measure.newMeasureBuilder().create(Measure.Level.valueOf(dto.getTextValue()));
  91. case STRING:
  92. case DISTRIB:
  93. case DATA:
  94. String textValue = dto.getTextValue();
  95. if (textValue == null) {
  96. return Measure.newMeasureBuilder().createNoValue();
  97. }
  98. return Measure.newMeasureBuilder().create(textValue);
  99. default:
  100. throw new IllegalArgumentException(String.format("Custom measures do not support the metric type [%s]", metric.getType()));
  101. }
  102. }
  103. @Override
  104. public String getDescription() {
  105. return "Copy custom measures";
  106. }
  107. }