fix: 处理原来nezha自监控提供指标变更图表无法展示问题

This commit is contained in:
tanghao
2021-06-10 17:13:50 +08:00
parent 4deff29620
commit 8817ee5533

View File

@@ -38,6 +38,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
@@ -129,10 +131,29 @@ public class ChartServiceImpl extends ServiceImpl<ChartDao, Chart> implements Ch
// 同步chartElement
List<VisualChartElement> visualChartElements = new ArrayList<VisualChartElement>();
List<ChartElement> chartElements = chartElementService.list();
Pattern pattern = Pattern.compile("\"(.*?)\"");
for(ChartElement chartElement:chartElements) {
VisualChartElement visualChartElement = new VisualChartElement();
String expression = chartElement.getExpression();
expression = expression.replaceAll("endpoint=", "endpoint_id=");
if(expression.startsWith("sum(nz_asset_nums)")&&expression.endsWith("sum(nz_asset_online_nums)")) {
expression = "sum(nz_asset_nums{state='Not in storage'})";
}
if(expression.startsWith("sum(nz_asset_nums)-count(nz_asset_ping)")) {
expression = "count(probe_success==0)";
}
if(expression.startsWith("nz_asset_ping{host=")) {
Matcher matcher = pattern.matcher(expression);
String host="";
if(matcher.find()) {
host = matcher.group();
}
if(StrUtil.isNotBlank(host)) {
expression="probe_icmp_duration_seconds{phase='rtt',host="+host+"} * 1000";
}
}
chartElement.setExpression(expression);
BeanUtil.copyProperties(chartElement, visualChartElement);
visualChartElements.add(visualChartElement);