3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.qualitygate;
22 import com.google.common.collect.ImmutableMap;
23 import java.util.Locale;
25 import javax.annotation.CheckForNull;
26 import org.sonar.api.i18n.I18n;
27 import org.sonar.api.utils.DateUtils;
28 import org.sonar.api.utils.Duration;
29 import org.sonar.api.utils.Durations;
30 import org.sonar.core.timemachine.Periods;
31 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
32 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
33 import org.sonar.server.computation.task.projectanalysis.period.Period;
34 import org.sonar.server.computation.task.projectanalysis.period.PeriodsHolder;
36 import static java.util.Objects.requireNonNull;
37 import static org.sonar.server.computation.task.projectanalysis.measure.Measure.Level.ERROR;
39 public final class EvaluationResultTextConverterImpl implements EvaluationResultTextConverter {
40 private static final String VARIATION_METRIC_PREFIX = "new_";
41 private static final String VARIATION = "variation";
42 private static final Map<Condition.Operator, String> OPERATOR_LABELS = ImmutableMap.of(
43 Condition.Operator.EQUALS, "=",
44 Condition.Operator.NOT_EQUALS, "!=",
45 Condition.Operator.GREATER_THAN, ">",
46 Condition.Operator.LESS_THAN, "<");
48 private final I18n i18n;
49 private final Durations durations;
50 private final Periods periods;
51 private final PeriodsHolder periodsHolder;
53 public EvaluationResultTextConverterImpl(I18n i18n, Durations durations, Periods periods, PeriodsHolder periodsHolder) {
55 this.durations = durations;
56 this.periods = periods;
57 this.periodsHolder = periodsHolder;
62 public String asText(Condition condition, EvaluationResult evaluationResult) {
63 requireNonNull(condition);
64 if (evaluationResult.getLevel() == Measure.Level.OK) {
67 return getAlertLabel(condition, evaluationResult.getLevel());
70 private String getAlertLabel(Condition condition, Measure.Level level) {
71 Integer alertPeriod = condition.getPeriod();
72 String metric = i18n.message(Locale.ENGLISH, "metric." + condition.getMetric().getKey() + ".name", condition.getMetric().getName());
74 StringBuilder stringBuilder = new StringBuilder();
75 stringBuilder.append(metric);
77 if (alertPeriod != null && !condition.getMetric().getKey().startsWith(VARIATION_METRIC_PREFIX)) {
78 String variation = i18n.message(Locale.ENGLISH, VARIATION, VARIATION).toLowerCase(Locale.ENGLISH);
79 stringBuilder.append(" ").append(variation);
83 .append(" ").append(OPERATOR_LABELS.get(condition.getOperator())).append(" ")
84 .append(alertValue(condition, level));
86 if (alertPeriod != null) {
87 Period period = periodsHolder.getPeriod(alertPeriod);
88 stringBuilder.append(" ").append(periods.label(period.getMode(), period.getModeParameter(), DateUtils.longToDate(period.getSnapshotDate())));
91 return stringBuilder.toString();
94 private String alertValue(Condition condition, Measure.Level level) {
95 String value = level == ERROR ? condition.getErrorThreshold() : condition.getWarningThreshold();
96 if (condition.getMetric().getType() == Metric.MetricType.WORK_DUR) {
97 return formatDuration(value);
102 private String formatDuration(String value) {
103 return durations.format(Duration.create(Long.parseLong(value)));