feat: 增加比较器和比较符

dev
huangfeng 1 year ago
parent 37d435c952
commit b215354046

@ -0,0 +1,39 @@
package com.xydl.cac.comparator;
import java.util.HashMap;
import java.util.Map;
/**
* Created by hf
* desc:
* true:
* false:
*/
public abstract class Comparator {
protected String operator;
public Comparator() {
}
// 比较
public abstract boolean compare(Object source, String threshold);
// 支持的比较符
public Map<String, String> supportedOperator() {
Map<String, String> map = new HashMap<String, String>();
map.put("LSS", "小于");
map.put("LEQ", "小等于");
map.put("GTR", "大于");
map.put("GEQ", "大等于");
return map;
}
public String getOperator() {
return operator;
}
public void setOperator(String operator) {
this.operator = operator;
}
}

@ -0,0 +1,54 @@
package com.xydl.cac.comparator;
public class FloatCompare extends Comparator {
private float IgnoreValueN = -0.00001f;
private float IgnoreValue = 0.00001f;
@Override
public boolean compare(Object source, String threshold) {
if (source != null) {
float s1 = (float) source;
if ("BTW".equalsIgnoreCase(operator)) {
String[] strs = threshold.split(",");
float t1 = Float.parseFloat(strs[0]);
float t3 = Float.parseFloat(strs[1]);
if (t1 < s1 && s1 < t3) {
return true;
}
} else {
float t2 = Float.parseFloat(threshold);
float dif = s1 - t2;
if ("EQU".equalsIgnoreCase(operator)) {
if (IgnoreValueN < dif && dif < IgnoreValue) {
return true;
}
} else if ("NEQ".equalsIgnoreCase(operator)) {
if (IgnoreValueN > dif || dif > IgnoreValue) {
return true;
}
} else if ("LSS".equalsIgnoreCase(operator)) {
if (IgnoreValueN > dif) {
return true;
}
} else if ("LEQ".equalsIgnoreCase(operator)) {
if (IgnoreValue > dif) {
return true;
}
} else if ("GTR".equalsIgnoreCase(operator)) {
if (dif > IgnoreValue) {
return true;
}
} else if ("GEQ".equalsIgnoreCase(operator)) {
if (dif > IgnoreValueN) {
return true;
}
}
}
}
return false;
}
}

@ -0,0 +1,38 @@
package com.xydl.cac.comparator;
public class IntCompare extends Comparator {
@Override
public boolean compare(Object source, String threshold) {
if (source != null) {
int s1 = (int) source;
if ("BTW".equalsIgnoreCase(operator)) {
String[] strs = threshold.split(",");
int t1 = Integer.parseInt(strs[0]);
int t3 = Integer.parseInt(strs[1]);
if (t1 < s1 && s1 < t3) {
return true;
}
} else {
int t2 = Integer.parseInt(threshold);
if ("EQU".equalsIgnoreCase(operator) && s1 == t2) {
return true;
} else if ("NEQ".equalsIgnoreCase(operator) && s1 != t2) {
return true;
} else if ("LSS".equalsIgnoreCase(operator) && s1 < t2) {
return true;
} else if ("LEQ".equalsIgnoreCase(operator) && s1 <= t2) {
return true;
} else if ("GTR".equalsIgnoreCase(operator) && s1 > t2) {
return true;
} else if ("GEQ".equalsIgnoreCase(operator) && s1 >= t2) {
return true;
}
}
}
return false;
}
}

@ -2,8 +2,10 @@ package com.xydl.cac.controller;
import com.xydl.cac.entity.WarnRule; import com.xydl.cac.entity.WarnRule;
import com.xydl.cac.exception.BusinessException; import com.xydl.cac.exception.BusinessException;
import com.xydl.cac.model.ColumnModel;
import com.xydl.cac.model.Response; import com.xydl.cac.model.Response;
import com.xydl.cac.service.WarnRuleService; import com.xydl.cac.service.WarnRuleService;
import com.xydl.cac.task.RuleCheckTask;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -12,7 +14,10 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.List; import java.util.*;
import static com.xydl.cac.entity.constants.Constants.FloatCompare;
import static com.xydl.cac.entity.constants.Constants.IntCompare;
@RestController @RestController
@Api(tags = {"告警规则相关接口"}) @Api(tags = {"告警规则相关接口"})
@ -23,9 +28,36 @@ public class WarnRuleController extends BasicController {
@Resource @Resource
WarnRuleService service; WarnRuleService service;
@GetMapping("listComparator")
@ApiOperation("查询比较器")
public Response<List<ColumnModel>> listComparator() throws Exception {
List<ColumnModel> result = new ArrayList<>();
ColumnModel item = new ColumnModel();
item.setName(FloatCompare);
item.setComment("浮点型数据比较器");
result.add(item);
item = new ColumnModel();
item.setName(IntCompare);
item.setComment("整型数据比较器");
result.add(item);
return Response.success(result);
}
@GetMapping("listOperator")
@ApiOperation("查询比较符")
public Response<Map<String, String>> listOperator(String name) throws Exception {
Map<String, String> result = new HashMap<>();
if (FloatCompare.equalsIgnoreCase(name)) {
result = RuleCheckTask.floatCompare.supportedOperator();
} else if (IntCompare.equalsIgnoreCase(name)) {
result = RuleCheckTask.intCompare.supportedOperator();
}
return Response.success(result);
}
@GetMapping("listAll") @GetMapping("listAll")
@ApiOperation("查询全部列表") @ApiOperation("查询全部列表")
public Response<List<WarnRule>> listAll() { public Response<List<WarnRule>> listAll() throws Exception {
List<WarnRule> result = service.listAll(); List<WarnRule> result = service.listAll();
return Response.success(result); return Response.success(result);
} }

@ -48,6 +48,11 @@ public class WarnRule {
@Column(name = "comparator") @Column(name = "comparator")
private String comparator; private String comparator;
@NotBlank(message = "比较符")
@ApiModelProperty("比较符")
@Column(name = "operator")
private String operator;
@NotNull(message = "告警级别不能为空") @NotNull(message = "告警级别不能为空")
@ApiModelProperty("告警级别 0:高 1:中 2:低") @ApiModelProperty("告警级别 0:高 1:中 2:低")
@Column(name = "level") @Column(name = "level")

@ -9,4 +9,7 @@ public class Constants {
public static final Integer TRUE = 1; public static final Integer TRUE = 1;
public static final Integer FALSE = 0; public static final Integer FALSE = 0;
public static String FloatCompare = "FloatCompare";
public static String IntCompare = "IntCompare";
} }

@ -73,6 +73,7 @@ public class WarnRuleServiceImpl implements WarnRuleService {
} }
WarnRule rule = optional.get(); WarnRule rule = optional.get();
rule.setComparator(item.getComparator()); rule.setComparator(item.getComparator());
rule.setOperator(item.getOperator());
rule.setThreshold(item.getThreshold()); rule.setThreshold(item.getThreshold());
rule.setLevel(item.getLevel()); rule.setLevel(item.getLevel());
repository.save(rule); repository.save(rule);

@ -0,0 +1,13 @@
package com.xydl.cac.task;
import com.xydl.cac.comparator.FloatCompare;
import com.xydl.cac.comparator.IntCompare;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class RuleCheckTask {
public static FloatCompare floatCompare = new FloatCompare();
public static IntCompare intCompare = new IntCompare();
}
Loading…
Cancel
Save