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.

EvaluationResultTextConverterImpl.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.qualitygate;
  21. import java.util.Locale;
  22. import java.util.Map;
  23. import javax.annotation.CheckForNull;
  24. import org.sonar.api.utils.Duration;
  25. import org.sonar.api.utils.Durations;
  26. import org.sonar.ce.task.projectanalysis.measure.Measure;
  27. import org.sonar.ce.task.projectanalysis.metric.Metric;
  28. import org.sonar.core.i18n.I18n;
  29. import static java.util.Objects.requireNonNull;
  30. public final class EvaluationResultTextConverterImpl implements EvaluationResultTextConverter {
  31. private static final Map<Condition.Operator, String> OPERATOR_LABELS = Map.of(
  32. Condition.Operator.GREATER_THAN, ">",
  33. Condition.Operator.LESS_THAN, "<");
  34. private final I18n i18n;
  35. private final Durations durations;
  36. public EvaluationResultTextConverterImpl(I18n i18n, Durations durations) {
  37. this.i18n = i18n;
  38. this.durations = durations;
  39. }
  40. @Override
  41. @CheckForNull
  42. public String asText(Condition condition, EvaluationResult evaluationResult) {
  43. requireNonNull(condition);
  44. if (evaluationResult.getLevel() == Measure.Level.OK) {
  45. return null;
  46. }
  47. return getAlertLabel(condition);
  48. }
  49. private String getAlertLabel(Condition condition) {
  50. String metric = i18n.message(Locale.ENGLISH, "metric." + condition.getMetric().getKey() + ".name", condition.getMetric().getName());
  51. return metric +
  52. " " + OPERATOR_LABELS.get(condition.getOperator()) + " " +
  53. alertValue(condition);
  54. }
  55. private String alertValue(Condition condition) {
  56. if (condition.getMetric().getType() == Metric.MetricType.WORK_DUR) {
  57. return formatDuration(condition.getErrorThreshold());
  58. }
  59. return condition.getErrorThreshold();
  60. }
  61. private String formatDuration(String value) {
  62. return durations.format(Duration.create(Long.parseLong(value)));
  63. }
  64. }