Authored by 鲁尚清

Merge branch 'master-500-dev' of http://192.168.1.136:82/monitor_v3/anji-plus-re…

…port into master-500-dev-lushangqing
package com.anjiplus.template.gaea.business.modules.datasource.service.impl;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anji.plus.gaea.constant.BaseOperationEnum;
... ... @@ -35,6 +36,7 @@ import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.*;
import java.util.Date;
... ... @@ -67,13 +69,13 @@ public class DataSourceServiceImpl implements DataSourceService {
/**
* 获取所有数据源
*
* @return
*/
@Override
public List<DataSource> queryAllDataSource() {
LambdaQueryWrapper<DataSource> wrapper = Wrappers.lambdaQuery();
wrapper.select(DataSource::getSourceCode, DataSource::getSourceName)
.eq(DataSource::getEnableFlag, Enabled.YES.getValue());
wrapper.select(DataSource::getSourceCode, DataSource::getSourceName).eq(DataSource::getEnableFlag, Enabled.YES.getValue());
wrapper.orderByDesc(DataSource::getUpdateTime);
return dataSourceMapper.selectList(wrapper);
}
... ... @@ -158,11 +160,12 @@ public class DataSourceServiceImpl implements DataSourceService {
/**
* 获取mysql count 和添加limit分页信息
*
* @param sourceDto
* @param dto
* @return
*/
public long mysqlTotal(DataSourceDto sourceDto, DataSetDto dto){
public long mysqlTotal(DataSourceDto sourceDto, DataSetDto dto) {
String dynSentence = sourceDto.getDynSentence();
String sql = "select count(1) as count from (" + dynSentence + ") as gaeaExecute";
sourceDto.setDynSentence(sql);
... ... @@ -178,7 +181,6 @@ public class DataSourceServiceImpl implements DataSourceService {
}
public List<JSONObject> executeElasticsearchSql(DataSourceDto dto) {
analysisHttpConfig(dto);
HttpHeaders headers = new HttpHeaders();
... ... @@ -188,7 +190,7 @@ public class DataSourceServiceImpl implements DataSourceService {
try {
exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, JSONObject.class);
} catch (Exception e) {
log.error("error",e);
log.error("error", e);
throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage());
}
if (exchange.getStatusCode().isError()) {
... ... @@ -215,7 +217,7 @@ public class DataSourceServiceImpl implements DataSourceService {
result.add(jsonObject);
}
} catch (Exception e) {
log.error("error",e);
log.error("error", e);
throw BusinessExceptionBuilder.build(ResponseCode.ANALYSIS_DATA_ERROR, e.getMessage());
}
return result;
... ... @@ -247,7 +249,7 @@ public class DataSourceServiceImpl implements DataSourceService {
Object result = dealResult(value);
jo.put(t, result);
} catch (SQLException throwable) {
log.error("error",throwable);
log.error("error", throwable);
throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_SQL_ERROR, throwable.getMessage());
}
});
... ... @@ -255,7 +257,7 @@ public class DataSourceServiceImpl implements DataSourceService {
}
return list;
} catch (Exception throwable) {
log.error("error",throwable);
log.error("error", throwable);
throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_SQL_ERROR, throwable.getMessage());
} finally {
try {
... ... @@ -263,7 +265,7 @@ public class DataSourceServiceImpl implements DataSourceService {
pooledConnection.close();
}
} catch (SQLException throwable) {
log.error("error",throwable);
log.error("error", throwable);
throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, throwable.getMessage());
}
}
... ... @@ -272,6 +274,7 @@ public class DataSourceServiceImpl implements DataSourceService {
/**
* 解决sql返回值 类型问题
* (through reference chain: java.util.HashMap["pageData"]->java.util.ArrayList[0]->java.util.HashMap["UPDATE_TIME"]->oracle.sql.TIMESTAMP["stream"])
*
* @param result
* @return
* @throws SQLException
... ... @@ -284,8 +287,12 @@ public class DataSourceServiceImpl implements DataSourceService {
if ("oracle.sql.TIMESTAMP".equals(type)) {
//oracle.sql.TIMESTAMP处理逻辑
return new Date((Long) JSONObject.toJSON(result));
} else if ("java.sql.Timestamp".equals(type)) {
LocalDateTime localDateTime = DateUtil.toLocalDateTime((Timestamp) result);
return DateUtil.format(localDateTime, DatePattern.NORM_DATETIME_PATTERN);
}
return result;
}
... ... @@ -298,17 +305,17 @@ public class DataSourceServiceImpl implements DataSourceService {
analysisHttpConfig(dto);
String apiUrl = dto.getApiUrl();
HttpMethod httpMethod = HttpMethod.valueOf(dto.getMethod());
if(Objects.equals(HttpMethod.GET, httpMethod)){
if (Objects.equals(HttpMethod.GET, httpMethod)) {
Map<String, Object> contextData = dto.getContextData();
List<String> params = new ArrayList<>();
contextData.forEach((k,v)->{
params.add(String.format("%s=%s",k,String.valueOf(v)));
contextData.forEach((k, v) -> {
params.add(String.format("%s=%s", k, String.valueOf(v)));
});
String element = "&";
if(apiUrl.indexOf('?') == -1){
if (apiUrl.indexOf('?') == -1) {
element = "?";
}
apiUrl += element + String.join("&",params);
apiUrl += element + String.join("&", params);
}
HttpHeaders headers = new HttpHeaders();
headers.setAll(JSONObject.parseObject(dto.getHeader(), Map.class));
... ... @@ -316,17 +323,17 @@ public class DataSourceServiceImpl implements DataSourceService {
ResponseEntity<Object> exchange;
List<JSONObject> result = new ArrayList<>();
try {
exchange = restTemplate.exchange(apiUrl,httpMethod , entity, Object.class);
} catch (HttpClientErrorException error){
exchange = restTemplate.exchange(apiUrl, httpMethod, entity, Object.class);
} catch (HttpClientErrorException error) {
int rawStatusCode = error.getRawStatusCode();
// 401 接口请求无权限
JSONObject jsonObject = new JSONObject();
jsonObject.put("code",rawStatusCode);
jsonObject.put("data",error.getMessage());
jsonObject.put("code", rawStatusCode);
jsonObject.put("data", error.getMessage());
result.add(jsonObject);
return result;
} catch (Exception e) {
log.error("error",e);
log.error("error", e);
throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage());
}
if (exchange != null && exchange.getStatusCode().isError()) {
... ... @@ -360,7 +367,7 @@ public class DataSourceServiceImpl implements DataSourceService {
log.info("数据库测试连接成功:{}", catalog);
unPooledConnection.close();
} catch (SQLException e) {
log.error("error",e);
log.error("error", e);
if (e.getCause() instanceof ClassNotFoundException) {
throw BusinessExceptionBuilder.build(ResponseCode.CLASS_NOT_FOUND, e.getCause().getMessage());
} else {
... ... @@ -423,8 +430,8 @@ public class DataSourceServiceImpl implements DataSourceService {
public void analysisRelationalDbConfig(DataSourceDto dto) {
JSONObject json = JSONObject.parseObject(dto.getSourceConfig());
GaeaAssert.isFalse(json.containsKey("jdbcUrl"), ResponseCode.PARAM_IS_NULL,"jdbcUrl not empty");
GaeaAssert.isFalse(json.containsKey("driverName"), ResponseCode.PARAM_IS_NULL,"driverName not empty");
GaeaAssert.isFalse(json.containsKey("jdbcUrl"), ResponseCode.PARAM_IS_NULL, "jdbcUrl not empty");
GaeaAssert.isFalse(json.containsKey("driverName"), ResponseCode.PARAM_IS_NULL, "driverName not empty");
String jdbcUrl = json.getString("jdbcUrl");
String username = json.getString("username");
String password = json.getString("password");
... ... @@ -444,10 +451,10 @@ public class DataSourceServiceImpl implements DataSourceService {
*/
public void analysisHttpConfig(DataSourceDto dto) {
JSONObject json = JSONObject.parseObject(dto.getSourceConfig());
GaeaAssert.isFalse(json.containsKey("apiUrl"), ResponseCode.PARAM_IS_NULL,"apiUrl not empty");
GaeaAssert.isFalse(json.containsKey("method"), ResponseCode.PARAM_IS_NULL,"method not empty");
GaeaAssert.isFalse(json.containsKey("header"), ResponseCode.PARAM_IS_NULL,"header not empty");
GaeaAssert.isFalse(json.containsKey("body"), ResponseCode.PARAM_IS_NULL,"body not empty");
GaeaAssert.isFalse(json.containsKey("apiUrl"), ResponseCode.PARAM_IS_NULL, "apiUrl not empty");
GaeaAssert.isFalse(json.containsKey("method"), ResponseCode.PARAM_IS_NULL, "method not empty");
GaeaAssert.isFalse(json.containsKey("header"), ResponseCode.PARAM_IS_NULL, "header not empty");
GaeaAssert.isFalse(json.containsKey("body"), ResponseCode.PARAM_IS_NULL, "body not empty");
String apiUrl = json.getString("apiUrl");
String method = json.getString("method");
String header = json.getString("header");
... ...