瀏覽代碼

修改指标获取时的BUG

wukai 11 月之前
父節點
當前提交
ebcf606fea

+ 14 - 9
jjt-biz/src/main/java/com/jjt/biz/controller/BizObjController.java

@@ -4,10 +4,7 @@ import com.jjt.biz.domain.BizObj;
 import com.jjt.biz.domain.BizObjMetrics;
 import com.jjt.biz.domain.MetricsDef;
 import com.jjt.biz.domain.MetricsTpl;
-import com.jjt.biz.service.IBizObjMetricsService;
-import com.jjt.biz.service.IBizObjPpService;
-import com.jjt.biz.service.IBizObjService;
-import com.jjt.biz.service.IBizObjTplService;
+import com.jjt.biz.service.*;
 import com.jjt.common.annotation.Log;
 import com.jjt.common.core.controller.BaseController;
 import com.jjt.common.core.domain.AjaxResult;
@@ -51,6 +48,8 @@ public class BizObjController extends BaseController {
     private IBizObjPpService ppService;
     @Resource
     private IBizObjTplService tplService;
+    @Resource
+    private IPrometheusService prometheusService;
 
     @ApiOperation("选择模板")
     @GetMapping("/tpl/select/{objId}")
@@ -85,11 +84,17 @@ public class BizObjController extends BaseController {
     @GetMapping("/validate")
     public AjaxResult validate(@ApiParam(value = "公式内容", required = true) String exp) {
         //TODO 开发时,返回验证通过
-        System.err.println(exp);
-        DecimalFormat df = new DecimalFormat("#0.00");
-        Random r = new Random();
-        float f = r.nextFloat() * 100;
-        return AjaxResult.success(Float.parseFloat(df.format(f)));
+//        System.err.println(exp);
+//        DecimalFormat df = new DecimalFormat("#0.00");
+//        Random r = new Random();
+//        float f = r.nextFloat() * 100;
+//        return AjaxResult.success(Float.parseFloat(df.format(f)));
+        Float value=prometheusService.query(exp);
+        if (value != null) {
+            return success(value);
+        } else {
+            return error();
+        }
     }
 
     @ApiOperation("数据接口配置进度")

+ 4 - 5
jjt-biz/src/main/java/com/jjt/biz/service/impl/BizObjMetricsServiceImpl.java

@@ -17,10 +17,7 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -334,12 +331,12 @@ public class BizObjMetricsServiceImpl implements IBizObjMetricsService {
             //非pinpoint
             Float value = prometheusService.query(om.getDataExp());
             String alarmLevel = getAlarmLevel(value, om);
+            om.setDValue(BigDecimal.valueOf(value));
 
             if (alarmLevel != null) {
                 insertAlarm(om, alarmLevel);
             }
 
-            om.setDValue(BigDecimal.valueOf(value));
             metricsToUpdate.add(om);
         });
         // 更新所有需要更新的BizObjMetrics对象
@@ -376,6 +373,8 @@ public class BizObjMetricsServiceImpl implements IBizObjMetricsService {
         record.setObjId(om.getObjId());
         record.setObjMetricsId(om.getObjMetricsId());
         record.setAlarmLevel(alarmLevel);
+        record.setAlarmValue(om.getDValue());
+        record.setAlarmTime(new Date());
         alarmRecordService.insertAlarmRecord(record);
     }
 

+ 15 - 12
jjt-biz/src/main/java/com/jjt/biz/service/impl/PinpointServiceImpl.java

@@ -162,23 +162,26 @@ public class PinpointServiceImpl implements IPinpointService {
         JSONArray noHeapUsed = jsonObject.getJSONObject("charts").getJSONObject("y").getJSONArray("JVM_MEMORY_HEAP_MAX");
         JSONArray noHeapMax = jsonObject.getJSONObject("charts").getJSONObject("y").getJSONArray("JVM_MEMORY_HEAP_MAX");
         JSONArray gc = jsonObject.getJSONObject("charts").getJSONObject("y").getJSONArray("JVM_GC_OLD_COUNT");
+
         Float used = 0F;
         Float max = 0F;
+        float usage = 0;
         int gcTimes = 0;
-        for (int i = 0; i < heapUsed.size(); i++) {
-            float v = heapUsed.getJSONArray(i).getFloat(1);
-            int gcV = gc.getJSONArray(i).getInteger(1);
-            if (gcV > 0) {
-                //如果有gc则增加
-                gcTimes += gcV;
-            }
-            if (v != 0f && v > used) {
-                used = v;
-                max = heapMax.getJSONArray(i).getFloat(1);
+        if (heapUsed.size() > 0) {
+            for (int i = 0; i < heapUsed.size(); i++) {
+                float v = heapUsed.getJSONArray(i).getFloat(1);
+                int gcV = gc.getJSONArray(i).getInteger(1);
+                if (gcV > 0) {
+                    //如果有gc则增加
+                    gcTimes += gcV;
+                }
+                if (v != 0f && v > used) {
+                    used = v;
+                    max = heapMax.getJSONArray(i).getFloat(1);
+                }
             }
+            usage = BigDecimal.valueOf(used).divide(BigDecimal.valueOf(max), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).floatValue();
         }
-
-        float usage = BigDecimal.valueOf(used).divide(BigDecimal.valueOf(max), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).floatValue();
         Map<String, Object> map = new HashMap<>(16);
         map.put("usage", usage);
         map.put("gc", gcTimes);

+ 17 - 13
jjt-biz/src/main/java/com/jjt/biz/service/impl/PrometheusServiceImpl.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.jjt.biz.service.IPrometheusService;
 import com.jjt.common.core.redis.RedisCache;
+import com.jjt.common.utils.StringUtils;
 import com.jjt.common.utils.http.HttpUtils;
 import com.jjt.system.service.ISysConfigService;
 import org.springframework.stereotype.Service;
@@ -11,7 +12,6 @@ import org.yaml.snakeyaml.util.UriEncoder;
 
 import javax.annotation.Resource;
 import java.text.DecimalFormat;
-import java.util.Date;
 
 /**
  * 告警记录Service业务层处理
@@ -34,6 +34,7 @@ public class PrometheusServiceImpl implements IPrometheusService {
     private String baseUri() {
         return configService.selectConfigByKey("pm.url");
     }
+
     /**
      * 即时查询
      *
@@ -42,20 +43,23 @@ public class PrometheusServiceImpl implements IPrometheusService {
      */
     @Override
     public Float query(String param) {
-        float v = 0f;
-        String uri = baseUri() + "/api/v1/query?query=";
-        uri += UriEncoder.encode(param);
-        String result = HttpUtils.sendGet(uri);
-        JSONObject jsonObject = JSONObject.parseObject(result);
-        JSONArray array = jsonObject.getJSONObject("data").getJSONArray("result");
-        try {
-            JSONArray value = array.getJSONObject(0).getJSONArray("value");
-            DecimalFormat df = new DecimalFormat("#0.00");
-            v = Float.parseFloat(df.format(value.getFloat(1)));
+        if (StringUtils.isNotEmpty(param)) {
 
-        } catch (Exception ignored) {
+            String uri = baseUri() + "/api/v1/query?query=";
+            uri += UriEncoder.encode(param);
+            String result = HttpUtils.sendGet(uri);
+            JSONObject jsonObject = JSONObject.parseObject(result);
+            JSONArray array = jsonObject.getJSONObject("data").getJSONArray("result");
+            try {
+                JSONArray value = array.getJSONObject(0).getJSONArray("value");
+                DecimalFormat df = new DecimalFormat("#0.00");
+                return Float.parseFloat(df.format(value.getFloat(1)));
+            } catch (Exception ignored) {
+                return null;
+            }
+        } else {
+            return null;
         }
-        return v;
     }
 
     /**

+ 4 - 0
jjt-biz/src/main/java/com/jjt/task/AlarmTask.java

@@ -26,6 +26,10 @@ public class AlarmTask {
             metricsService.prometheusMetricsValue(obj.getObjId());
         });
     }
+    public void test() {
+        metricsService.pinpointMetricsValue(2L);
+        metricsService.prometheusMetricsValue(15L);
+    }
 
     public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
         System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));