|
@@ -1,17 +1,25 @@
|
|
package com.jjt.biz.service.impl;
|
|
package com.jjt.biz.service.impl;
|
|
|
|
|
|
-import com.alibaba.fastjson2.JSONArray;
|
|
|
|
-import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
|
+import cn.hutool.http.Method;
|
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
import com.jjt.biz.service.IPrometheusService;
|
|
import com.jjt.biz.service.IPrometheusService;
|
|
-import com.jjt.common.core.redis.RedisCache;
|
|
|
|
import com.jjt.common.utils.StringUtils;
|
|
import com.jjt.common.utils.StringUtils;
|
|
-import com.jjt.common.utils.http.HttpUtils;
|
|
|
|
|
|
+import com.jjt.common.utils.sign.Base64;
|
|
import com.jjt.system.service.ISysConfigService;
|
|
import com.jjt.system.service.ISysConfigService;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.yaml.snakeyaml.util.UriEncoder;
|
|
import org.yaml.snakeyaml.util.UriEncoder;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
import java.text.DecimalFormat;
|
|
import java.text.DecimalFormat;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 告警记录Service业务层处理
|
|
* 告警记录Service业务层处理
|
|
@@ -20,11 +28,10 @@ import java.text.DecimalFormat;
|
|
* @date 2024-08-08
|
|
* @date 2024-08-08
|
|
*/
|
|
*/
|
|
@Service
|
|
@Service
|
|
|
|
+@Slf4j
|
|
public class PrometheusServiceImpl implements IPrometheusService {
|
|
public class PrometheusServiceImpl implements IPrometheusService {
|
|
@Resource
|
|
@Resource
|
|
private ISysConfigService configService;
|
|
private ISysConfigService configService;
|
|
- @Resource
|
|
|
|
- private RedisCache redisCache;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取接口地址
|
|
* 获取接口地址
|
|
@@ -44,30 +51,40 @@ public class PrometheusServiceImpl implements IPrometheusService {
|
|
@Override
|
|
@Override
|
|
public Float query(String param) {
|
|
public Float query(String param) {
|
|
if (StringUtils.isNotEmpty(param)) {
|
|
if (StringUtils.isNotEmpty(param)) {
|
|
- String uri = baseUri() + "/api/v1/query?query=";
|
|
|
|
- uri += UriEncoder.encode(param);
|
|
|
|
- String result = HttpUtils.sendGet(uri);
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
|
- try {
|
|
|
|
- JSONArray array = jsonObject.getJSONObject("data").getJSONArray("result");
|
|
|
|
- JSONArray value = array.getJSONObject(0).getJSONArray("value");
|
|
|
|
- DecimalFormat df = new DecimalFormat("#0.00");
|
|
|
|
- return Float.parseFloat(df.format(value.getFloat(1)));
|
|
|
|
- } catch (Exception ignored) {
|
|
|
|
|
|
+ if (StringUtils.isNotEmpty(param)) {
|
|
|
|
+ String uri = baseUri() + "/api/v1/query";
|
|
|
|
+ param = UriEncoder.encode(param);
|
|
|
|
+
|
|
|
|
+ Map<String, Object> map = new HashMap<>(16);
|
|
|
|
+ map.put("query", param);
|
|
|
|
+ HttpRequest request = HttpUtil.createPost(uri);
|
|
|
|
+ request.setMethod(Method.GET);
|
|
|
|
+ String auth = configService.selectConfigByKey("pm.param");
|
|
|
|
+ if (StringUtils.isNotEmpty(auth)) {
|
|
|
|
+ String baseAuth = "Basic " + Base64.encode(auth.getBytes());
|
|
|
|
+ request.header("Authorization", baseAuth);
|
|
|
|
+ }
|
|
|
|
+ request.form(map);
|
|
|
|
+ request.setConnectionTimeout(2000);
|
|
|
|
+ try (HttpResponse execute = request.execute()) {
|
|
|
|
+ if (!execute.isOk()) {
|
|
|
|
+ throw new RuntimeException("status:" + execute.getStatus() + "\tres:" + execute.body());
|
|
|
|
+ }
|
|
|
|
+ String res = new String(execute.body().getBytes(), StandardCharsets.UTF_8);
|
|
|
|
+ JSONObject object = JSONUtil.parseObj(res, true);
|
|
|
|
+ JSONArray array = object.getJSONObject("data").getJSONArray("result");
|
|
|
|
+ JSONArray value = array.getJSONObject(0).getJSONArray("value");
|
|
|
|
+ DecimalFormat df = new DecimalFormat("#0.00");
|
|
|
|
+ return Float.parseFloat(df.format(value.getFloat(1)));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("调用prometheus接口失败, url=" + uri + ",param=" + param, e);
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 普罗米修斯接口
|
|
|
|
- *
|
|
|
|
- * @return 结果
|
|
|
|
- */
|
|
|
|
- @Override
|
|
|
|
- public JSONObject range() {
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|