This commit is contained in:
panbaolin 2023-12-07 08:44:52 +08:00
commit 7a1aa66027
5 changed files with 117 additions and 4 deletions

View File

@ -19,6 +19,8 @@ public interface DBSQL {
String DBSTATUS_CONN_PG = "SELECT COUNT(*) FROM pg_stat_activity WHERE query_start >= now() - interval '1 minute'";
String DBSTATUS_DATASIZE_PG = "SELECT ROUND(SUM(pg_database_size(datname)) / 1024 / 1024, 0) FROM pg_database";
/* Oracle */
String DBNAMES_OR = "SELECT username FROM all_users";
String DBINFO_OR = "SELECT a.table_name AS tableName, a.num_rows AS numRow," +
@ -54,6 +56,10 @@ public interface DBSQL {
String DBSTATUS_CONN_OR = "SELECT COUNT(*) FROM V$SESSION WHERE LOGON_TIME >= SYSDATE - 1 / 24 / 60";
String DBSTATUS_LOGRESIDUE_OR = "SELECT SUM(BYTES) / 1024 / 1024 FROM V$log WHERE ARCHIVED = 'NO'";
String DBSTATUS_DATASIZE_OR = "SELECT SUM(BYTES) / 1024 / 1024 AS dataSize FROM dba_data_files";
/* MySQL */
String DBNAMES_MY = "SHOW DATABASES";
String DBINFO_MY = "SELECT TABLE_NAME AS tableName, TABLE_ROWS AS numRow," +

View File

@ -0,0 +1,103 @@
package org.jeecg.common.handler;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
public class HttpClient {
private static final String URL_ADDRESS = "http://127.0.0.1:7080/";
public static String fitPeakFull(String phdStr, List<Integer> Af, List<Integer> Cf, List<Integer> Ff) {
//接收算法返回内容
String resultJson = "";
//拼接请求路径
String url = URL_ADDRESS + "fitPeakFull";
HttpURLConnection connection = null;
OutputStream out = null;
BufferedReader in = null;
try {
//创建请求参数 以json的形式
JsonObject requestJson = new JsonObject();
requestJson.addProperty("phd", phdStr);
JsonArray AfJson = new JsonArray();
for (int i = 0; i < Af.size(); i++) {
AfJson.add(Af.get(i));
}
requestJson.add("Af", AfJson);
JsonArray CfJson = new JsonArray();
for (int i = 0; i < Cf.size(); i++) {
CfJson.add(Cf.get(i));
}
requestJson.add("Cf", CfJson);
JsonArray FfJson = new JsonArray();
for (int i = 0; i < Ff.size(); i++) {
FfJson.add(Ff.get(i));
}
requestJson.add("Ff", FfJson);
//请求的json参数转换为字符串
String requestParam = requestJson.toString();
//请求的json字符串转为字节数组的方式
byte[] requestParamBytes = requestParam.getBytes(StandardCharsets.UTF_8);
//建立Http连接
connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", String.valueOf(requestParamBytes.length));
connection.setDoOutput(true);
connection.setConnectTimeout(300000);
connection.setReadTimeout(300000);
//获取http连接的输出流
out = connection.getOutputStream();
//向输出流写入传递的参数字节数组信息
out.write(requestParamBytes);
//接收http请求的响应编码
int responseCode = connection.getResponseCode();
//如果是成功的响应编码
if (responseCode == HttpURLConnection.HTTP_OK) {
StringBuilder responseBuilder = new StringBuilder();
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null) {
responseBuilder.append(line);
}
resultJson = responseBuilder.toString();
} else {
System.out.println("Error while sending request to C++ server, response code: " + responseCode);
}
} catch (ProtocolException e) {
throw new RuntimeException(e);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (Objects.nonNull(out)) {
out.close();
}
if (Objects.nonNull(connection)) {
connection.disconnect();
}
if (Objects.nonNull(in)) {
in.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return resultJson;
}
}

View File

@ -1,6 +1,8 @@
package org.jeecg.modules.databaseStatus;
import cn.hutool.core.collection.ArrayIter;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.DBSQL;
@ -39,8 +41,9 @@ public class ConnFetcher implements Runnable{
public void run() {
if (CollUtil.isEmpty(databases)) return;
List<DatabaseStatusConn> statusList = new ArrayList<>();
Iterator<SysDatabase> iterator = databases.iterator();
while (true){
Iterator<SysDatabase> iterator = CollUtil
.addAll(new ArrayList<>() ,databases).iterator();
LocalDateTime now = LocalDateTime.now();
while (iterator.hasNext()) {
try {
@ -88,7 +91,7 @@ public class ConnFetcher implements Runnable{
databaseStatus.setCollectTime(now);
// 数据达到一定数量,进行批量保存
statusList.add(databaseStatus);
if (statusList.size() == 5){
if (statusList.size() == 100){
connService.saveBatch(statusList);
statusList = new ArrayList<>();
}

View File

@ -23,6 +23,7 @@ import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.cache.LocalCache;
import org.jeecg.common.constant.DateConstant;
import org.jeecg.common.handler.HttpClient;
import org.jeecg.common.properties.ParameterProperties;
import org.jeecg.common.properties.SpectrumPathProperties;
import org.jeecg.common.system.util.JwtUtil;
@ -1491,7 +1492,7 @@ public class GammaServiceImpl extends AbstractLogOrReport implements IGammaServi
}
ObjectMapper mapper = new ObjectMapper();
String phdStr = mapper.writeValueAsString(phd);
String strValue = CalValuesHandler.fitPeakFull(phdStr, Af, Cf, Ff);
String strValue = HttpClient.fitPeakFull(phdStr, Af, Cf, Ff);
Map<String, Object> parseMap = JSON.parseObject(strValue, Map.class);
for (Map.Entry<String, Object> entry : parseMap.entrySet()) {
if (entry.getKey().equalsIgnoreCase("vPeak")) {

View File

@ -63,7 +63,7 @@ public class JeecgAbnormalAlarmApplication extends SpringBootServletInitializer
// 启动监测服务器连接状态的线程
serverStatusManager.start();
// 启动采集数据库状态信息线程组
databaseStatusFetcher.start();
// databaseStatusFetcher.start();
}
}