feat: 增加数据缺失比较器和规则

dev
huangfeng 12 months ago
parent 1677aacaf8
commit ac83b310cb

@ -1,5 +1,6 @@
package com.xydl.cac.comparator;
import java.util.Date;
import java.util.LinkedHashMap;
/**
@ -10,12 +11,13 @@ import java.util.LinkedHashMap;
*/
public abstract class Comparator {
protected String operator;
protected long hours;
public Comparator() {
}
// 比较
public abstract boolean compare(Object source, String threshold);
public abstract boolean compare(Object source, String threshold, Date lastDTime);
// 支持的比较符
public abstract LinkedHashMap<String, String> supportedOperator();
@ -30,4 +32,12 @@ public abstract class Comparator {
public void setOperator(String operator) {
this.operator = operator;
}
public long getHours() {
return hours;
}
public void setHours(long hours) {
this.hours = hours;
}
}

@ -3,6 +3,7 @@ package com.xydl.cac.comparator;
import com.xydl.cac.exception.BusinessException;
import java.util.Date;
import java.util.LinkedHashMap;
public class FloatCompare extends Comparator {
@ -11,7 +12,7 @@ public class FloatCompare extends Comparator {
private float IgnoreValue = 0.00001f;
@Override
public boolean compare(Object source, String threshold) {
public boolean compare(Object source, String threshold, Date lastDTime) {
if (source != null) {
float s1 = (float) source;

@ -3,12 +3,13 @@ package com.xydl.cac.comparator;
import com.xydl.cac.exception.BusinessException;
import java.util.Date;
import java.util.LinkedHashMap;
public class IntCompare extends Comparator {
@Override
public boolean compare(Object source, String threshold) {
public boolean compare(Object source, String threshold, Date lastDTime) {
if (source != null) {
int s1 = (int) source;

@ -0,0 +1,33 @@
package com.xydl.cac.comparator;
import java.util.Date;
import java.util.LinkedHashMap;
public class MissCompare extends Comparator {
@Override
public boolean compare(Object source, String threshold, Date lastDTime) {
if (source == null && lastDTime != null) {
long t2 = Long.parseLong(threshold);
long last = System.currentTimeMillis() - lastDTime.getTime();
hours = last / 1000 / 60 / 60;
if (hours >= t2) {
return true;
}
}
return false;
}
@Override
public LinkedHashMap<String, String> supportedOperator() {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
map.put("GTR", "超过规定小时无数据");
return map;
}
@Override
public void valid(String operator, String threshold) throws Exception {
}
}

@ -3,7 +3,9 @@ package com.xydl.cac.controller;
import com.xydl.cac.comparator.Comparator;
import com.xydl.cac.comparator.FloatCompare;
import com.xydl.cac.comparator.IntCompare;
import com.xydl.cac.comparator.MissCompare;
import com.xydl.cac.entity.WarnRule;
import com.xydl.cac.entity.constants.Constants;
import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.ColumnModel;
import com.xydl.cac.model.Response;
@ -18,8 +20,6 @@ import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.*;
import static com.xydl.cac.entity.constants.Constants.FloatCompare;
import static com.xydl.cac.entity.constants.Constants.IntCompare;
@RestController
@Api(tags = {"告警规则相关接口"})
@ -28,6 +28,7 @@ import static com.xydl.cac.entity.constants.Constants.IntCompare;
public class WarnRuleController extends BasicController {
FloatCompare floatCompare = new FloatCompare();
IntCompare intCompare = new IntCompare();
MissCompare missCompare = new MissCompare();
@Resource
WarnRuleService service;
@ -37,13 +38,17 @@ public class WarnRuleController extends BasicController {
public Response<List<ColumnModel>> listComparator() throws Exception {
List<ColumnModel> result = new ArrayList<>();
ColumnModel item = new ColumnModel();
item.setType(FloatCompare);
item.setType(Constants.FloatCompare);
item.setComment("浮点型数据比较器");
result.add(item);
item = new ColumnModel();
item.setType(IntCompare);
item.setType(Constants.IntCompare);
item.setComment("整型数据比较器");
result.add(item);
item = new ColumnModel();
item.setType(Constants.MissCompare);
item.setComment("数据缺失比较器");
result.add(item);
return Response.success(result);
}
@ -55,10 +60,12 @@ public class WarnRuleController extends BasicController {
}
private Comparator getComparator(String name) throws BusinessException {
if (FloatCompare.equalsIgnoreCase(name)) {
if (Constants.FloatCompare.equalsIgnoreCase(name)) {
return floatCompare;
} else if (IntCompare.equalsIgnoreCase(name)) {
} else if (Constants.IntCompare.equalsIgnoreCase(name)) {
return intCompare;
} else if (Constants.MissCompare.equalsIgnoreCase(name)) {
return missCompare;
} else {
throw new BusinessException("未找到该比较器");
}
@ -69,14 +76,18 @@ public class WarnRuleController extends BasicController {
public Response<List<WarnRule>> listAll(Integer sensorId) throws Exception {
List<WarnRule> result = service.listAll(sensorId);
for (WarnRule item : result) {
if (FloatCompare.equalsIgnoreCase(item.getComparator())) {
if (Constants.FloatCompare.equalsIgnoreCase(item.getComparator())) {
item.setComparatorDesc("浮点型数据比较器");
String name = floatCompare.supportedOperator().get(item.getOperator());
item.setOperatorDesc(name);
} else if (IntCompare.equalsIgnoreCase(item.getComparator())) {
} else if (Constants.IntCompare.equalsIgnoreCase(item.getComparator())) {
item.setComparatorDesc("整型数据比较器");
String name = intCompare.supportedOperator().get(item.getOperator());
item.setOperatorDesc(name);
} else if (Constants.MissCompare.equalsIgnoreCase(item.getComparator())) {
item.setComparatorDesc("数据缺失比较器");
String name = intCompare.supportedOperator().get(item.getOperator());
item.setOperatorDesc(name);
}
}
return Response.success(result);

@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.xydl.cac.comparator.Comparator;
import com.xydl.cac.comparator.FloatCompare;
import com.xydl.cac.comparator.IntCompare;
import com.xydl.cac.comparator.MissCompare;
import com.xydl.cac.entity.constants.Constants;
import com.xydl.cac.model.TriggerModel;
import com.xydl.cac.util.DateUtil;
@ -106,6 +107,9 @@ public class WarnRule {
} else if (Constants.IntCompare.equalsIgnoreCase(comparator)) {
actualComp = new IntCompare();
actualComp.setOperator(operator);
} else if (Constants.MissCompare.equalsIgnoreCase(comparator)) {
actualComp = new MissCompare();
actualComp.setOperator(operator);
} else {
actualComp = null;
log.error("该规则(" + id + ")比较器(" + comparator + ")异常");
@ -126,14 +130,23 @@ public class WarnRule {
// 单个数据是否触发
public boolean triggerRule(Object value) {
return actualComp.compare(value, threshold);
return actualComp.compare(value, threshold, lastDTime);
}
// 多数据触发返回告警数据
public List<TriggerModel> checkData(List<Map<String, Object>> list) throws Exception {
List<TriggerModel> result = null;
if (CollectionUtils.isEmpty(list)) {
if (Constants.MissCompare.equalsIgnoreCase(comparator)) {
boolean r = this.triggerRule(null);
if (r) {
result = new ArrayList<>();
TriggerModel model = new TriggerModel();
model.setDate(new Date());
model.setValue(String.valueOf(actualComp.getHours()));
result.add(model);
}
}
} else {
result = new ArrayList<>();
for (Map<String, Object> map : list) {

@ -12,4 +12,5 @@ public class Constants {
public static String FloatCompare = "float";
public static String IntCompare = "int";
public static String MissCompare = "miss";
}

Loading…
Cancel
Save