Merge branch 'master-500-dev' of http://192.168.1.136:82/monitor_v3/anji-plus-re…
…port into master-500-dev-lushangqing
Showing
1 changed file
with
34 additions
and
27 deletions
1 | - | ||
2 | package com.anjiplus.template.gaea.business.modules.datasource.service.impl; | 1 | package com.anjiplus.template.gaea.business.modules.datasource.service.impl; |
3 | 2 | ||
3 | +import cn.hutool.core.date.DatePattern; | ||
4 | +import cn.hutool.core.date.DateUtil; | ||
4 | import com.alibaba.fastjson.JSONArray; | 5 | import com.alibaba.fastjson.JSONArray; |
5 | import com.alibaba.fastjson.JSONObject; | 6 | import com.alibaba.fastjson.JSONObject; |
6 | import com.anji.plus.gaea.constant.BaseOperationEnum; | 7 | import com.anji.plus.gaea.constant.BaseOperationEnum; |
@@ -35,6 +36,7 @@ import org.springframework.web.client.RestTemplate; | @@ -35,6 +36,7 @@ import org.springframework.web.client.RestTemplate; | ||
35 | 36 | ||
36 | import javax.annotation.Resource; | 37 | import javax.annotation.Resource; |
37 | import java.sql.*; | 38 | import java.sql.*; |
39 | +import java.time.LocalDateTime; | ||
38 | import java.util.*; | 40 | import java.util.*; |
39 | import java.util.Date; | 41 | import java.util.Date; |
40 | 42 | ||
@@ -67,13 +69,13 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -67,13 +69,13 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
67 | 69 | ||
68 | /** | 70 | /** |
69 | * 获取所有数据源 | 71 | * 获取所有数据源 |
72 | + * | ||
70 | * @return | 73 | * @return |
71 | */ | 74 | */ |
72 | @Override | 75 | @Override |
73 | public List<DataSource> queryAllDataSource() { | 76 | public List<DataSource> queryAllDataSource() { |
74 | LambdaQueryWrapper<DataSource> wrapper = Wrappers.lambdaQuery(); | 77 | LambdaQueryWrapper<DataSource> wrapper = Wrappers.lambdaQuery(); |
75 | - wrapper.select(DataSource::getSourceCode, DataSource::getSourceName) | ||
76 | - .eq(DataSource::getEnableFlag, Enabled.YES.getValue()); | 78 | + wrapper.select(DataSource::getSourceCode, DataSource::getSourceName).eq(DataSource::getEnableFlag, Enabled.YES.getValue()); |
77 | wrapper.orderByDesc(DataSource::getUpdateTime); | 79 | wrapper.orderByDesc(DataSource::getUpdateTime); |
78 | return dataSourceMapper.selectList(wrapper); | 80 | return dataSourceMapper.selectList(wrapper); |
79 | } | 81 | } |
@@ -158,11 +160,12 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -158,11 +160,12 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
158 | 160 | ||
159 | /** | 161 | /** |
160 | * 获取mysql count 和添加limit分页信息 | 162 | * 获取mysql count 和添加limit分页信息 |
163 | + * | ||
161 | * @param sourceDto | 164 | * @param sourceDto |
162 | * @param dto | 165 | * @param dto |
163 | * @return | 166 | * @return |
164 | */ | 167 | */ |
165 | - public long mysqlTotal(DataSourceDto sourceDto, DataSetDto dto){ | 168 | + public long mysqlTotal(DataSourceDto sourceDto, DataSetDto dto) { |
166 | String dynSentence = sourceDto.getDynSentence(); | 169 | String dynSentence = sourceDto.getDynSentence(); |
167 | String sql = "select count(1) as count from (" + dynSentence + ") as gaeaExecute"; | 170 | String sql = "select count(1) as count from (" + dynSentence + ") as gaeaExecute"; |
168 | sourceDto.setDynSentence(sql); | 171 | sourceDto.setDynSentence(sql); |
@@ -178,7 +181,6 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -178,7 +181,6 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
178 | } | 181 | } |
179 | 182 | ||
180 | 183 | ||
181 | - | ||
182 | public List<JSONObject> executeElasticsearchSql(DataSourceDto dto) { | 184 | public List<JSONObject> executeElasticsearchSql(DataSourceDto dto) { |
183 | analysisHttpConfig(dto); | 185 | analysisHttpConfig(dto); |
184 | HttpHeaders headers = new HttpHeaders(); | 186 | HttpHeaders headers = new HttpHeaders(); |
@@ -188,7 +190,7 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -188,7 +190,7 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
188 | try { | 190 | try { |
189 | exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, JSONObject.class); | 191 | exchange = restTemplate.exchange(dto.getApiUrl(), HttpMethod.valueOf(dto.getMethod()), entity, JSONObject.class); |
190 | } catch (Exception e) { | 192 | } catch (Exception e) { |
191 | - log.error("error",e); | 193 | + log.error("error", e); |
192 | throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); | 194 | throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); |
193 | } | 195 | } |
194 | if (exchange.getStatusCode().isError()) { | 196 | if (exchange.getStatusCode().isError()) { |
@@ -215,7 +217,7 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -215,7 +217,7 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
215 | result.add(jsonObject); | 217 | result.add(jsonObject); |
216 | } | 218 | } |
217 | } catch (Exception e) { | 219 | } catch (Exception e) { |
218 | - log.error("error",e); | 220 | + log.error("error", e); |
219 | throw BusinessExceptionBuilder.build(ResponseCode.ANALYSIS_DATA_ERROR, e.getMessage()); | 221 | throw BusinessExceptionBuilder.build(ResponseCode.ANALYSIS_DATA_ERROR, e.getMessage()); |
220 | } | 222 | } |
221 | return result; | 223 | return result; |
@@ -247,7 +249,7 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -247,7 +249,7 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
247 | Object result = dealResult(value); | 249 | Object result = dealResult(value); |
248 | jo.put(t, result); | 250 | jo.put(t, result); |
249 | } catch (SQLException throwable) { | 251 | } catch (SQLException throwable) { |
250 | - log.error("error",throwable); | 252 | + log.error("error", throwable); |
251 | throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_SQL_ERROR, throwable.getMessage()); | 253 | throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_SQL_ERROR, throwable.getMessage()); |
252 | } | 254 | } |
253 | }); | 255 | }); |
@@ -255,7 +257,7 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -255,7 +257,7 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
255 | } | 257 | } |
256 | return list; | 258 | return list; |
257 | } catch (Exception throwable) { | 259 | } catch (Exception throwable) { |
258 | - log.error("error",throwable); | 260 | + log.error("error", throwable); |
259 | throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_SQL_ERROR, throwable.getMessage()); | 261 | throw BusinessExceptionBuilder.build(ResponseCode.EXECUTE_SQL_ERROR, throwable.getMessage()); |
260 | } finally { | 262 | } finally { |
261 | try { | 263 | try { |
@@ -263,7 +265,7 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -263,7 +265,7 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
263 | pooledConnection.close(); | 265 | pooledConnection.close(); |
264 | } | 266 | } |
265 | } catch (SQLException throwable) { | 267 | } catch (SQLException throwable) { |
266 | - log.error("error",throwable); | 268 | + log.error("error", throwable); |
267 | throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, throwable.getMessage()); | 269 | throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, throwable.getMessage()); |
268 | } | 270 | } |
269 | } | 271 | } |
@@ -272,6 +274,7 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -272,6 +274,7 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
272 | /** | 274 | /** |
273 | * 解决sql返回值 类型问题 | 275 | * 解决sql返回值 类型问题 |
274 | * (through reference chain: java.util.HashMap["pageData"]->java.util.ArrayList[0]->java.util.HashMap["UPDATE_TIME"]->oracle.sql.TIMESTAMP["stream"]) | 276 | * (through reference chain: java.util.HashMap["pageData"]->java.util.ArrayList[0]->java.util.HashMap["UPDATE_TIME"]->oracle.sql.TIMESTAMP["stream"]) |
277 | + * | ||
275 | * @param result | 278 | * @param result |
276 | * @return | 279 | * @return |
277 | * @throws SQLException | 280 | * @throws SQLException |
@@ -284,8 +287,12 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -284,8 +287,12 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
284 | if ("oracle.sql.TIMESTAMP".equals(type)) { | 287 | if ("oracle.sql.TIMESTAMP".equals(type)) { |
285 | //oracle.sql.TIMESTAMP处理逻辑 | 288 | //oracle.sql.TIMESTAMP处理逻辑 |
286 | return new Date((Long) JSONObject.toJSON(result)); | 289 | return new Date((Long) JSONObject.toJSON(result)); |
290 | + } else if ("java.sql.Timestamp".equals(type)) { | ||
291 | + LocalDateTime localDateTime = DateUtil.toLocalDateTime((Timestamp) result); | ||
292 | + return DateUtil.format(localDateTime, DatePattern.NORM_DATETIME_PATTERN); | ||
287 | } | 293 | } |
288 | 294 | ||
295 | + | ||
289 | return result; | 296 | return result; |
290 | } | 297 | } |
291 | 298 | ||
@@ -298,17 +305,17 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -298,17 +305,17 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
298 | analysisHttpConfig(dto); | 305 | analysisHttpConfig(dto); |
299 | String apiUrl = dto.getApiUrl(); | 306 | String apiUrl = dto.getApiUrl(); |
300 | HttpMethod httpMethod = HttpMethod.valueOf(dto.getMethod()); | 307 | HttpMethod httpMethod = HttpMethod.valueOf(dto.getMethod()); |
301 | - if(Objects.equals(HttpMethod.GET, httpMethod)){ | 308 | + if (Objects.equals(HttpMethod.GET, httpMethod)) { |
302 | Map<String, Object> contextData = dto.getContextData(); | 309 | Map<String, Object> contextData = dto.getContextData(); |
303 | List<String> params = new ArrayList<>(); | 310 | List<String> params = new ArrayList<>(); |
304 | - contextData.forEach((k,v)->{ | ||
305 | - params.add(String.format("%s=%s",k,String.valueOf(v))); | 311 | + contextData.forEach((k, v) -> { |
312 | + params.add(String.format("%s=%s", k, String.valueOf(v))); | ||
306 | }); | 313 | }); |
307 | String element = "&"; | 314 | String element = "&"; |
308 | - if(apiUrl.indexOf('?') == -1){ | 315 | + if (apiUrl.indexOf('?') == -1) { |
309 | element = "?"; | 316 | element = "?"; |
310 | } | 317 | } |
311 | - apiUrl += element + String.join("&",params); | 318 | + apiUrl += element + String.join("&", params); |
312 | } | 319 | } |
313 | HttpHeaders headers = new HttpHeaders(); | 320 | HttpHeaders headers = new HttpHeaders(); |
314 | headers.setAll(JSONObject.parseObject(dto.getHeader(), Map.class)); | 321 | headers.setAll(JSONObject.parseObject(dto.getHeader(), Map.class)); |
@@ -316,17 +323,17 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -316,17 +323,17 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
316 | ResponseEntity<Object> exchange; | 323 | ResponseEntity<Object> exchange; |
317 | List<JSONObject> result = new ArrayList<>(); | 324 | List<JSONObject> result = new ArrayList<>(); |
318 | try { | 325 | try { |
319 | - exchange = restTemplate.exchange(apiUrl,httpMethod , entity, Object.class); | ||
320 | - } catch (HttpClientErrorException error){ | 326 | + exchange = restTemplate.exchange(apiUrl, httpMethod, entity, Object.class); |
327 | + } catch (HttpClientErrorException error) { | ||
321 | int rawStatusCode = error.getRawStatusCode(); | 328 | int rawStatusCode = error.getRawStatusCode(); |
322 | // 401 接口请求无权限 | 329 | // 401 接口请求无权限 |
323 | JSONObject jsonObject = new JSONObject(); | 330 | JSONObject jsonObject = new JSONObject(); |
324 | - jsonObject.put("code",rawStatusCode); | ||
325 | - jsonObject.put("data",error.getMessage()); | 331 | + jsonObject.put("code", rawStatusCode); |
332 | + jsonObject.put("data", error.getMessage()); | ||
326 | result.add(jsonObject); | 333 | result.add(jsonObject); |
327 | return result; | 334 | return result; |
328 | } catch (Exception e) { | 335 | } catch (Exception e) { |
329 | - log.error("error",e); | 336 | + log.error("error", e); |
330 | throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); | 337 | throw BusinessExceptionBuilder.build(ResponseCode.DATA_SOURCE_CONNECTION_FAILED, e.getMessage()); |
331 | } | 338 | } |
332 | if (exchange != null && exchange.getStatusCode().isError()) { | 339 | if (exchange != null && exchange.getStatusCode().isError()) { |
@@ -360,7 +367,7 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -360,7 +367,7 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
360 | log.info("数据库测试连接成功:{}", catalog); | 367 | log.info("数据库测试连接成功:{}", catalog); |
361 | unPooledConnection.close(); | 368 | unPooledConnection.close(); |
362 | } catch (SQLException e) { | 369 | } catch (SQLException e) { |
363 | - log.error("error",e); | 370 | + log.error("error", e); |
364 | if (e.getCause() instanceof ClassNotFoundException) { | 371 | if (e.getCause() instanceof ClassNotFoundException) { |
365 | throw BusinessExceptionBuilder.build(ResponseCode.CLASS_NOT_FOUND, e.getCause().getMessage()); | 372 | throw BusinessExceptionBuilder.build(ResponseCode.CLASS_NOT_FOUND, e.getCause().getMessage()); |
366 | } else { | 373 | } else { |
@@ -423,8 +430,8 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -423,8 +430,8 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
423 | 430 | ||
424 | public void analysisRelationalDbConfig(DataSourceDto dto) { | 431 | public void analysisRelationalDbConfig(DataSourceDto dto) { |
425 | JSONObject json = JSONObject.parseObject(dto.getSourceConfig()); | 432 | JSONObject json = JSONObject.parseObject(dto.getSourceConfig()); |
426 | - GaeaAssert.isFalse(json.containsKey("jdbcUrl"), ResponseCode.PARAM_IS_NULL,"jdbcUrl not empty"); | ||
427 | - GaeaAssert.isFalse(json.containsKey("driverName"), ResponseCode.PARAM_IS_NULL,"driverName not empty"); | 433 | + GaeaAssert.isFalse(json.containsKey("jdbcUrl"), ResponseCode.PARAM_IS_NULL, "jdbcUrl not empty"); |
434 | + GaeaAssert.isFalse(json.containsKey("driverName"), ResponseCode.PARAM_IS_NULL, "driverName not empty"); | ||
428 | String jdbcUrl = json.getString("jdbcUrl"); | 435 | String jdbcUrl = json.getString("jdbcUrl"); |
429 | String username = json.getString("username"); | 436 | String username = json.getString("username"); |
430 | String password = json.getString("password"); | 437 | String password = json.getString("password"); |
@@ -444,10 +451,10 @@ public class DataSourceServiceImpl implements DataSourceService { | @@ -444,10 +451,10 @@ public class DataSourceServiceImpl implements DataSourceService { | ||
444 | */ | 451 | */ |
445 | public void analysisHttpConfig(DataSourceDto dto) { | 452 | public void analysisHttpConfig(DataSourceDto dto) { |
446 | JSONObject json = JSONObject.parseObject(dto.getSourceConfig()); | 453 | JSONObject json = JSONObject.parseObject(dto.getSourceConfig()); |
447 | - GaeaAssert.isFalse(json.containsKey("apiUrl"), ResponseCode.PARAM_IS_NULL,"apiUrl not empty"); | ||
448 | - GaeaAssert.isFalse(json.containsKey("method"), ResponseCode.PARAM_IS_NULL,"method not empty"); | ||
449 | - GaeaAssert.isFalse(json.containsKey("header"), ResponseCode.PARAM_IS_NULL,"header not empty"); | ||
450 | - GaeaAssert.isFalse(json.containsKey("body"), ResponseCode.PARAM_IS_NULL,"body not empty"); | 454 | + GaeaAssert.isFalse(json.containsKey("apiUrl"), ResponseCode.PARAM_IS_NULL, "apiUrl not empty"); |
455 | + GaeaAssert.isFalse(json.containsKey("method"), ResponseCode.PARAM_IS_NULL, "method not empty"); | ||
456 | + GaeaAssert.isFalse(json.containsKey("header"), ResponseCode.PARAM_IS_NULL, "header not empty"); | ||
457 | + GaeaAssert.isFalse(json.containsKey("body"), ResponseCode.PARAM_IS_NULL, "body not empty"); | ||
451 | String apiUrl = json.getString("apiUrl"); | 458 | String apiUrl = json.getString("apiUrl"); |
452 | String method = json.getString("method"); | 459 | String method = json.getString("method"); |
453 | String header = json.getString("header"); | 460 | String header = json.getString("header"); |
-
Please register or login to post a comment