Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

LiveQualityGateComputerImpl.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.server.measure.live;
  21. import java.util.Collection;
  22. import java.util.HashSet;
  23. import java.util.Map;
  24. import java.util.Objects;
  25. import java.util.Optional;
  26. import java.util.OptionalDouble;
  27. import java.util.Set;
  28. import org.sonar.api.measures.CoreMetrics;
  29. import org.sonar.api.measures.Metric;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.component.BranchDto;
  33. import org.sonar.db.component.BranchType;
  34. import org.sonar.db.component.ComponentDto;
  35. import org.sonar.db.measure.LiveMeasureDto;
  36. import org.sonar.db.metric.MetricDto;
  37. import org.sonar.db.organization.OrganizationDto;
  38. import org.sonar.db.qualitygate.QualityGateConditionDto;
  39. import org.sonar.db.qualitygate.QualityGateDto;
  40. import org.sonar.server.qualitygate.Condition;
  41. import org.sonar.server.qualitygate.EvaluatedQualityGate;
  42. import org.sonar.server.qualitygate.QualityGate;
  43. import org.sonar.server.qualitygate.QualityGateConverter;
  44. import org.sonar.server.qualitygate.QualityGateEvaluator;
  45. import org.sonar.server.qualitygate.QualityGateFinder;
  46. import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
  47. import static org.sonar.core.util.stream.MoreCollectors.toHashSet;
  48. import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
  49. public class LiveQualityGateComputerImpl implements LiveQualityGateComputer {
  50. private final DbClient dbClient;
  51. private final QualityGateFinder qGateFinder;
  52. private final QualityGateEvaluator evaluator;
  53. public LiveQualityGateComputerImpl(DbClient dbClient, QualityGateFinder qGateFinder, QualityGateEvaluator evaluator) {
  54. this.dbClient = dbClient;
  55. this.qGateFinder = qGateFinder;
  56. this.evaluator = evaluator;
  57. }
  58. @Override
  59. public QualityGate loadQualityGate(DbSession dbSession, OrganizationDto organization, ComponentDto project, BranchDto branch) {
  60. if (branch.getBranchType() == BranchType.SHORT || branch.getBranchType() == BranchType.PULL_REQUEST) {
  61. return ShortLivingBranchQualityGate.GATE;
  62. }
  63. ComponentDto mainProject = project.getMainBranchProjectUuid() == null ? project : dbClient.componentDao().selectOrFailByKey(dbSession, project.getKey());
  64. QualityGateDto gateDto = qGateFinder.getQualityGate(dbSession, organization, mainProject).getQualityGate();
  65. Collection<QualityGateConditionDto> conditionDtos = dbClient.gateConditionDao().selectForQualityGate(dbSession, gateDto.getId());
  66. Set<Integer> metricIds = conditionDtos.stream().map(c -> (int) c.getMetricId())
  67. .collect(toHashSet(conditionDtos.size()));
  68. Map<Integer, MetricDto> metricsById = dbClient.metricDao().selectByIds(dbSession, metricIds).stream()
  69. .collect(uniqueIndex(MetricDto::getId));
  70. Set<Condition> conditions = conditionDtos.stream().map(conditionDto -> {
  71. String metricKey = metricsById.get((int) conditionDto.getMetricId()).getKey();
  72. Condition.Operator operator = Condition.Operator.fromDbValue(conditionDto.getOperator());
  73. boolean onLeak = Objects.equals(conditionDto.getPeriod(), 1);
  74. return new Condition(metricKey, operator, conditionDto.getErrorThreshold(), conditionDto.getWarningThreshold(), onLeak);
  75. }).collect(toHashSet(conditionDtos.size()));
  76. return new QualityGate(String.valueOf(gateDto.getId()), gateDto.getName(), conditions);
  77. }
  78. @Override
  79. public EvaluatedQualityGate refreshGateStatus(ComponentDto project, QualityGate gate, MeasureMatrix measureMatrix) {
  80. QualityGateEvaluator.Measures measures = metricKey -> {
  81. Optional<LiveMeasureDto> liveMeasureDto = measureMatrix.getMeasure(project, metricKey);
  82. if (!liveMeasureDto.isPresent()) {
  83. return Optional.empty();
  84. }
  85. MetricDto metric = measureMatrix.getMetric(liveMeasureDto.get().getMetricId());
  86. return Optional.of(new LiveMeasure(liveMeasureDto.get(), metric));
  87. };
  88. EvaluatedQualityGate evaluatedGate = evaluator.evaluate(gate, measures);
  89. measureMatrix.setValue(project, CoreMetrics.ALERT_STATUS_KEY, evaluatedGate.getStatus().name());
  90. measureMatrix.setValue(project, CoreMetrics.QUALITY_GATE_DETAILS_KEY, QualityGateConverter.toJson(evaluatedGate));
  91. return evaluatedGate;
  92. }
  93. @Override
  94. public Set<String> getMetricsRelatedTo(QualityGate gate) {
  95. Set<String> metricKeys = new HashSet<>();
  96. metricKeys.add(CoreMetrics.ALERT_STATUS_KEY);
  97. metricKeys.add(CoreMetrics.QUALITY_GATE_DETAILS_KEY);
  98. metricKeys.addAll(evaluator.getMetricKeys(gate));
  99. return metricKeys;
  100. }
  101. private static class LiveMeasure implements QualityGateEvaluator.Measure {
  102. private final LiveMeasureDto dto;
  103. private final MetricDto metric;
  104. LiveMeasure(LiveMeasureDto dto, MetricDto metric) {
  105. this.dto = dto;
  106. this.metric = metric;
  107. }
  108. @Override
  109. public Metric.ValueType getType() {
  110. return Metric.ValueType.valueOf(metric.getValueType());
  111. }
  112. @Override
  113. public OptionalDouble getValue() {
  114. if (dto.getValue() == null) {
  115. return OptionalDouble.empty();
  116. }
  117. return OptionalDouble.of(dto.getValue());
  118. }
  119. @Override
  120. public Optional<String> getStringValue() {
  121. return Optional.ofNullable(dto.getTextValue());
  122. }
  123. @Override
  124. public OptionalDouble getLeakValue() {
  125. if (dto.getVariation() == null) {
  126. return OptionalDouble.empty();
  127. }
  128. return OptionalDouble.of(dto.getVariation());
  129. }
  130. }
  131. }