提交 eaf3de7d 编写于 作者: L leon.li

refactor RuleType

上级 2c6c1ced
package com.dianping.cat.report.task.alert;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.unidal.tuple.Pair;
......@@ -14,8 +11,6 @@ import com.dianping.cat.home.rule.entity.SubCondition;
public class DefaultDataChecker implements DataChecker {
private DecimalFormat m_df = new DecimalFormat("0.0");
private static final Long ONE_MINUTE_MILLSEC = 60000L;
private double[] buildLastMinutesDoubleArray(double[] doubleList, int remainCount) {
......@@ -54,32 +49,27 @@ public class DefaultDataChecker implements DataChecker {
}
private Pair<Boolean, String> checkDataByCondition(double[] value, double[] baseline, Condition condition) {
int length = value.length;
StringBuilder baselines = new StringBuilder();
StringBuilder values = new StringBuilder();
double valueSum = 0;
double baselineSum = 0;
for (int i = 0; i < length; i++) {
baselines.append(m_df.format(baseline[i])).append(" ");
values.append(m_df.format(value[i])).append(" ");
valueSum = valueSum + value[i];
baselineSum = baselineSum + baseline[i];
if (!checkDataByMinute(condition, value[i], baseline[i])) {
StringBuilder builder = new StringBuilder();
for (SubCondition subCondition : condition.getSubConditions()) {
try {
String ruleType = subCondition.getType();
double ruleValue = parseSubConditionText(subCondition.getText());
RuleType rule = RuleType.getByTypeId(ruleType);
Pair<Boolean, String> subResult = rule.executeRule(value, baseline, ruleValue);
if (!subResult.getKey()) {
return new Pair<Boolean, String>(false, "");
}
builder.append(subResult.getValue()).append("\n");
} catch (Exception ex) {
Cat.logError(condition.toString(), ex);
return new Pair<Boolean, String>(false, "");
}
}
double percent = (1 - valueSum / baselineSum) * 100;
StringBuilder sb = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sb.append("[基线值:").append(baselines.toString()).append("] ");
sb.append("[实际值:").append(values.toString()).append("] ");
sb.append("[下降:").append(m_df.format(percent)).append("%").append("]");
sb.append("[告警时间:").append(sdf.format(new Date()) + "]");
return new Pair<Boolean, String>(true, sb.toString());
return new Pair<Boolean, String>(true, builder.toString());
}
private Pair<Boolean, String> checkDataByConfig(double[] value, double[] baseline, Config config) {
......@@ -102,26 +92,6 @@ public class DefaultDataChecker implements DataChecker {
return new Pair<Boolean, String>(false, "");
}
private boolean checkDataByMinute(Condition condition, double value, double baseline) {
for (SubCondition subCondition : condition.getSubConditions()) {
try {
String ruleType = subCondition.getType();
double ruleValue = parseSubConditionText(subCondition.getText());
RuleType rule = RuleType.getByTypeId(ruleType);
boolean isSubRuleTriggered = rule.executeRule(value, baseline, ruleValue);
if (!isSubRuleTriggered) {
return false;
}
} catch (Exception ex) {
Cat.logError(condition.toString(), ex);
return false;
}
}
return true;
}
private double parseSubConditionText(String text) {
if (text.contains("Mb")) {
double value = Double.parseDouble(text.replaceAll("Mb", ""));
......
package com.dianping.cat.report.task.alert;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import org.unidal.tuple.Pair;
public enum RuleType {
DecreasePercentage {
private double[] buildDescPers(double[] values, double[] baselines) {
int length = values.length;
double[] descPers = new double[length];
for (int i = 0; i < length; i++) {
descPers[i] = (1 - values[i] / baselines[i]) * 100;
}
return descPers;
}
@Override
protected String buildRuleMessage(double[] values, double[] baselines, double ruleValue) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sb.append("[基线值:").append(convertDoublesToString(baselines)).append("] ");
sb.append("[实际值:").append(convertDoublesToString(values)).append("] ");
sb.append("[下降比:").append(convertPercentsToString(buildDescPers(values, baselines))).append("]");
sb.append("[下降百分比阈值: " + m_df.format(ruleValue) + "% ]");
sb.append("[告警时间:").append(sdf.format(new Date()) + "]");
return sb.toString();
}
@Override
public boolean executeRule(double value, double baseline, double ruleValue) {
if (baseline > 0) {
return value / baseline <= (1 - ruleValue / 100);
} else {
return false;
public Pair<Boolean, String> executeRule(double[] values, double[] baselines, double ruleValue) {
int length = values.length;
for (int i = 0; i < length; i++) {
if (baselines[i] <= 0 || values[i] / baselines[i] > (1 - ruleValue / 100)) {
return new Pair<Boolean, String>(false, "");
}
}
return new Pair<Boolean, String>(true, buildRuleMessage(values, baselines, ruleValue));
}
@Override
public String getId() {
return "DescPer";
}
},
DecreaseValue {
private double[] buildDescVals(double[] values, double[] baselines) {
int length = values.length;
double[] descVals = new double[length];
for (int i = 0; i < length; i++) {
descVals[i] = baselines[i] - values[i];
}
return descVals;
}
@Override
protected String buildRuleMessage(double[] values, double[] baselines, double ruleValue) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sb.append("[基线值:").append(convertDoublesToString(baselines)).append("] ");
sb.append("[实际值:").append(convertDoublesToString(values)).append("] ");
sb.append("[下降值:").append(convertDoublesToString(buildDescVals(values, baselines))).append("]");
sb.append("[下降阈值: " + m_df.format(ruleValue) + " ]");
sb.append("[告警时间:").append(sdf.format(new Date()) + "]");
return sb.toString();
}
@Override
public boolean executeRule(double value, double baseline, double ruleValue) {
return baseline - value >= ruleValue;
public Pair<Boolean, String> executeRule(double[] values, double[] baselines, double ruleValue) {
int length = values.length;
for (int i = 0; i < length; i++) {
if (baselines[i] - values[i] < ruleValue) {
return new Pair<Boolean, String>(false, "");
}
}
return new Pair<Boolean, String>(true, buildRuleMessage(values, baselines, ruleValue));
}
@Override
......@@ -34,13 +102,42 @@ public enum RuleType {
},
IncreasePercentage {
private double[] buildAscPers(double[] values, double[] baselines) {
int length = values.length;
double[] ascPers = new double[length];
for (int i = 0; i < length; i++) {
ascPers[i] = (values[i] / baselines[i] - 1) * 100;
}
return ascPers;
}
@Override
public boolean executeRule(double value, double baseline, double ruleValue) {
if (baseline > 0) {
return value / baseline >= (1 + ruleValue / 100);
} else {
return false;
protected String buildRuleMessage(double[] values, double[] baselines, double ruleValue) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sb.append("[基线值:").append(convertDoublesToString(baselines)).append("] ");
sb.append("[实际值:").append(convertDoublesToString(values)).append("] ");
sb.append("[上升比:").append(convertPercentsToString(buildAscPers(values, baselines))).append("]");
sb.append("[上升百分比阈值: " + m_df.format(ruleValue) + "% ]");
sb.append("[告警时间:").append(sdf.format(new Date()) + "]");
return sb.toString();
}
@Override
public Pair<Boolean, String> executeRule(double[] values, double[] baselines, double ruleValue) {
int length = values.length;
for (int i = 0; i < length; i++) {
if (baselines[i] <= 0 || values[i] / baselines[i] < (1 + ruleValue / 100)) {
return new Pair<Boolean, String>(false, "");
}
}
return new Pair<Boolean, String>(true, buildRuleMessage(values, baselines, ruleValue));
}
@Override
......@@ -50,9 +147,42 @@ public enum RuleType {
},
IncreaseValue {
private double[] buildAscVals(double[] values, double[] baselines) {
int length = values.length;
double[] ascVals = new double[length];
for (int i = 0; i < length; i++) {
ascVals[i] = values[i] - baselines[i];
}
return ascVals;
}
@Override
public boolean executeRule(double value, double baseline, double ruleValue) {
return value - baseline >= ruleValue;
protected String buildRuleMessage(double[] values, double[] baselines, double ruleValue) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sb.append("[基线值:").append(convertDoublesToString(baselines)).append("] ");
sb.append("[实际值:").append(convertDoublesToString(values)).append("] ");
sb.append("[上升值:").append(convertDoublesToString(buildAscVals(values, baselines))).append("]");
sb.append("[上升阈值: " + m_df.format(ruleValue) + " ]");
sb.append("[告警时间:").append(sdf.format(new Date()) + "]");
return sb.toString();
}
@Override
public Pair<Boolean, String> executeRule(double[] values, double[] baselines, double ruleValue) {
int length = values.length;
for (int i = 0; i < length; i++) {
if (values[i] - baselines[i] < ruleValue) {
return new Pair<Boolean, String>(false, "");
}
}
return new Pair<Boolean, String>(true, buildRuleMessage(values, baselines, ruleValue));
}
@Override
......@@ -63,8 +193,28 @@ public enum RuleType {
absoluteMaxValue {
@Override
public boolean executeRule(double value, double baseline, double ruleValue) {
return value >= ruleValue;
protected String buildRuleMessage(double[] values, double[] baselines, double ruleValue) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sb.append("[实际值:").append(convertDoublesToString(values)).append("] ");
sb.append("[最大阈值: " + m_df.format(ruleValue) + " ]");
sb.append("[告警时间:").append(sdf.format(new Date()) + "]");
return sb.toString();
}
@Override
public Pair<Boolean, String> executeRule(double[] values, double[] baselines, double ruleValue) {
int length = values.length;
for (int i = 0; i < length; i++) {
if (values[i] < ruleValue) {
return new Pair<Boolean, String>(false, "");
}
}
return new Pair<Boolean, String>(true, buildRuleMessage(values, baselines, ruleValue));
}
@Override
......@@ -75,8 +225,28 @@ public enum RuleType {
absoluteMinValue {
@Override
public boolean executeRule(double value, double baseline, double ruleValue) {
return value <= ruleValue;
protected String buildRuleMessage(double[] values, double[] baselines, double ruleValue) {
StringBuilder sb = new StringBuilder();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sb.append("[实际值:").append(convertDoublesToString(values)).append("] ");
sb.append("[最小阈值: " + m_df.format(ruleValue) + " ]");
sb.append("[告警时间:").append(sdf.format(new Date()) + "]");
return sb.toString();
}
@Override
public Pair<Boolean, String> executeRule(double[] values, double[] baselines, double ruleValue) {
int length = values.length;
for (int i = 0; i < length; i++) {
if (values[i] > ruleValue) {
return new Pair<Boolean, String>(false, "");
}
}
return new Pair<Boolean, String>(true, buildRuleMessage(values, baselines, ruleValue));
}
@Override
......@@ -93,12 +263,36 @@ public enum RuleType {
}
}
public static RuleType getByTypeId(String typeId) {
return s_map.get(typeId);
protected DecimalFormat m_df = new DecimalFormat("0.0");
protected abstract String buildRuleMessage(double[] values, double[] baselines, double ruleValue);
protected String convertDoublesToString(double[] values) {
StringBuilder builder = new StringBuilder();
for (double value : values) {
builder.append(m_df.format(value)).append(" ");
}
return builder.toString();
}
protected String convertPercentsToString(double[] values) {
StringBuilder builder = new StringBuilder();
for (double value : values) {
builder.append(m_df.format(value)).append("% ");
}
return builder.toString();
}
public abstract boolean executeRule(double value, double baseline, double ruleValue);
public abstract Pair<Boolean, String> executeRule(double[] values, double[] baselines, double ruleValue);
public abstract String getId();
public static RuleType getByTypeId(String typeId) {
return s_map.get(typeId);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册