Merge "Ensure threshold is always printed as a decimal"

This commit is contained in:
Jenkins 2015-08-20 17:26:31 +00:00 committed by Gerrit Code Review
commit ca45f80fbb
2 changed files with 13 additions and 2 deletions

View File

@ -17,6 +17,7 @@
package monasca.common.model.alarm;
import java.io.Serializable;
import java.text.DecimalFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.antlr.v4.runtime.ANTLRInputStream;
@ -43,6 +44,9 @@ public class AlarmSubExpression implements Serializable {
private double threshold;
private int period;
private int periods;
// Use a DecimalFormatter for threshold because the standard double format starts using scientific notation when
// threshold is very large and that scientific notation can't be parsed when recreating the SubExpression
private static final DecimalFormat formatter = new DecimalFormat("0.0##############");
public AlarmSubExpression(AggregateFunction function, MetricDefinition metricDefinition,
AlarmOperator operator, double threshold, int period, int periods) {
@ -119,7 +123,7 @@ public class AlarmSubExpression implements Serializable {
sb.append(function).append('(').append(metricDefinition.toExpression());
if (period != 60)
sb.append(", ").append(period);
sb.append(") ").append(operator).append(' ').append(threshold);
sb.append(") ").append(operator).append(' ').append(formatter.format(threshold));
if (periods != 1)
sb.append(" times ").append(periods);
return sb.toString();

View File

@ -161,7 +161,14 @@ public class AlarmSubExpressionTest {
AlarmSubExpression.of("avg(hpcs.compute{metric_name=cpu, device=1}, 45) > 5 times 4")
.getExpression(), "avg(hpcs.compute{device=1, metric_name=cpu}, 45) > 5.0 times 4");
}
public void toStringAndBack() {
AlarmExpression expr = new AlarmExpression("elasticsearch.store.size>21474836480");
AlarmSubExpression subExpr1 = expr.getSubExpressions().get(0);
AlarmSubExpression subExpr2 = AlarmSubExpression.of(subExpr1.getExpression());
assertEquals(subExpr2, subExpr1);
}
public void shouldAllowDecimalThresholds() {
assertEquals(AlarmSubExpression.of("avg(hpcs.compute) > 2.375").getThreshold(), 2.375);
}