Skip to content

Commit 6388ad8

Browse files
author
yuluozyf
committed
修复部分BUG
1 parent 0287275 commit 6388ad8

File tree

12 files changed

+301
-86
lines changed

12 files changed

+301
-86
lines changed

backend/services/data-collection-service/src/main/java/com/dataengine/collection/interfaces/facade/DataSourceController.java

Lines changed: 147 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import com.dataengine.collection.domain.model.DataSource;
55
import com.dataengine.collection.interfaces.api.DataSourceApi;
66
import com.dataengine.collection.interfaces.dto.*;
7+
import com.fasterxml.jackson.core.type.TypeReference;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
79
import lombok.RequiredArgsConstructor;
10+
import lombok.extern.slf4j.Slf4j;
811
import org.springframework.http.ResponseEntity;
912
import org.springframework.validation.annotation.Validated;
1013
import org.springframework.web.bind.annotation.RestController;
@@ -17,21 +20,72 @@
1720
@RestController
1821
@RequiredArgsConstructor
1922
@Validated
23+
@Slf4j
2024
public class DataSourceController implements DataSourceApi {
2125

2226
private final DataSourceService dataSourceService;
27+
private final ObjectMapper objectMapper;
2328

2429
private DataSourceResponse toResponse(DataSource ds) {
30+
if (ds == null) {
31+
return null;
32+
}
33+
2534
DataSourceResponse r = new DataSourceResponse();
2635
r.setId(ds.getId());
2736
r.setName(ds.getName());
2837
r.setDescription(ds.getDescription());
29-
r.setType(DataSourceType.fromValue(ds.getType().name()));
30-
r.setStatus(DataSourceResponse.StatusEnum.fromValue(ds.getStatus().name()));
31-
r.setConfig(Map.of());
38+
39+
// 安全处理枚举转换
40+
if (ds.getType() != null) {
41+
try {
42+
r.setType(DataSourceType.fromValue(ds.getType().name()));
43+
} catch (Exception e) {
44+
log.error("Failed to convert data source type: {} for datasource id: {}, error: {}",
45+
ds.getType(), ds.getId(), e.getMessage());
46+
// 设置一个默认值或者抛出异常
47+
r.setType(DataSourceType.MYSQL);
48+
}
49+
}
50+
51+
if (ds.getStatus() != null) {
52+
try {
53+
r.setStatus(DataSourceResponse.StatusEnum.fromValue(ds.getStatus().name()));
54+
} catch (Exception e) {
55+
log.error("Failed to convert data source status: {} for datasource id: {}, error: {}",
56+
ds.getStatus(), ds.getId(), e.getMessage());
57+
r.setStatus(DataSourceResponse.StatusEnum.ACTIVE);
58+
}
59+
}
60+
61+
r.setConfig(parseConfigJson(ds.getConfig()));
3262
return r;
3363
}
3464

65+
private Map<String, Object> parseConfigJson(String configJson) {
66+
if (configJson == null || configJson.trim().isEmpty()) {
67+
return Map.of();
68+
}
69+
70+
String trimmedJson = configJson.trim();
71+
if (trimmedJson.equals("{}")) {
72+
return Map.of();
73+
}
74+
75+
try {
76+
return objectMapper.readValue(trimmedJson, new TypeReference<Map<String, Object>>() {});
77+
} catch (Exception e) {
78+
// 记录详细的错误信息用于调试
79+
log.warn("Failed to parse config JSON: {}, error: {}", trimmedJson, e.getMessage());
80+
// 返回包含错误信息的Map,便于前端处理
81+
return Map.of(
82+
"error", "Invalid JSON format",
83+
"message", e.getMessage(),
84+
"raw", trimmedJson
85+
);
86+
}
87+
}
88+
3589
@Override
3690
public ResponseEntity<PagedDataSources> datasourcesGet(Integer page, Integer size, DataSourceType type, String status) {
3791
List<DataSource> list = dataSourceService.list(page, size,
@@ -56,24 +110,59 @@ public ResponseEntity<Void> datasourcesIdDelete(String id) {
56110

57111
@Override
58112
public ResponseEntity<DataSourceResponse> datasourcesIdGet(String id) {
113+
log.debug("Getting data source by id: {}", id);
59114
DataSource ds = dataSourceService.get(id);
60115
return ds == null ? ResponseEntity.notFound().build() : ResponseEntity.ok(toResponse(ds));
61116
}
62117

63118
@Override
64119
public ResponseEntity<DataSourceResponse> datasourcesIdPut(String id, UpdateDataSourceRequest body) {
65-
DataSource ds = new DataSource();
66-
ds.setId(id);
67-
ds.setName(body.getName());
68-
ds.setDescription(body.getDescription());
69-
ds.setType(com.dataengine.collection.domain.model.DataSourceType.MYSQL);
70-
ds.setConfig("{}");
71-
ds = dataSourceService.update(ds);
72-
return ResponseEntity.ok(toResponse(ds));
120+
log.debug("Updating data source: {}", id);
121+
122+
// 1. 先从数据库查询现有数据源
123+
DataSource existingDs = dataSourceService.get(id);
124+
if (existingDs == null) {
125+
log.warn("Data source not found for update: {}", id);
126+
return ResponseEntity.notFound().build();
127+
}
128+
129+
// 2. 只更新传入的非空字段,保持其他字段不变
130+
if (body.getName() != null && !body.getName().trim().isEmpty()) {
131+
log.debug("Updating name for datasource {}: {} -> {}", id, existingDs.getName(), body.getName().trim());
132+
existingDs.setName(body.getName().trim());
133+
}
134+
135+
if (body.getDescription() != null) {
136+
existingDs.setDescription(body.getDescription());
137+
}
138+
139+
// 3. 处理config配置 - 只有在传入config时才更新
140+
if (body.getConfig() != null) {
141+
if (body.getConfig().isEmpty()) {
142+
// 如果传入空的config,设置为空JSON对象
143+
existingDs.setConfig("{}");
144+
} else {
145+
try {
146+
String configJson = objectMapper.writeValueAsString(body.getConfig());
147+
existingDs.setConfig(configJson);
148+
} catch (Exception e) {
149+
log.error("Failed to serialize config to JSON for datasource {}: {}", id, e.getMessage());
150+
return ResponseEntity.badRequest().build();
151+
}
152+
}
153+
}
154+
// 如果没有传入config,保持原有配置不变
155+
156+
// 4. 更新数据源
157+
log.debug("Saving updated data source: {}", id);
158+
DataSource updatedDs = dataSourceService.update(existingDs);
159+
log.info("Successfully updated data source: {}", id);
160+
return ResponseEntity.ok(toResponse(updatedDs));
73161
}
74162

75163
@Override
76164
public ResponseEntity<ConnectionTestResult> datasourcesIdTestPost(String id) {
165+
//TODO
77166
ConnectionTestResult r = new ConnectionTestResult();
78167
r.setSuccess(true);
79168
r.setMessage("OK");
@@ -85,12 +174,54 @@ public ResponseEntity<ConnectionTestResult> datasourcesIdTestPost(String id) {
85174

86175
@Override
87176
public ResponseEntity<DataSourceResponse> datasourcesPost(CreateDataSourceRequest body) {
177+
log.debug("Creating new data source: {}", body.getName());
178+
179+
// 1. 验证必填字段
180+
if (body.getName() == null || body.getName().trim().isEmpty()) {
181+
log.warn("Data source name is required");
182+
return ResponseEntity.badRequest().build();
183+
}
184+
185+
if (body.getType() == null) {
186+
log.warn("Data source type is required");
187+
return ResponseEntity.badRequest().build();
188+
}
189+
190+
// 2. 创建数据源对象
88191
DataSource ds = new DataSource();
89-
ds.setName(body.getName());
192+
ds.setName(body.getName().trim());
90193
ds.setDescription(body.getDescription());
91-
ds.setType(com.dataengine.collection.domain.model.DataSourceType.valueOf(body.getType().getValue()));
92-
ds.setConfig("{}");
93-
ds = dataSourceService.create(ds);
94-
return ResponseEntity.status(201).body(toResponse(ds));
194+
195+
// 3. 处理数据源类型
196+
try {
197+
ds.setType(com.dataengine.collection.domain.model.DataSourceType.valueOf(body.getType().getValue()));
198+
} catch (IllegalArgumentException e) {
199+
log.error("Invalid data source type: {}", body.getType().getValue());
200+
return ResponseEntity.badRequest().build();
201+
}
202+
203+
// 4. 处理config配置
204+
if (body.getConfig() != null && !body.getConfig().isEmpty()) {
205+
try {
206+
String configJson = objectMapper.writeValueAsString(body.getConfig());
207+
ds.setConfig(configJson);
208+
} catch (Exception e) {
209+
log.error("Failed to serialize config to JSON: {}", e.getMessage());
210+
return ResponseEntity.badRequest().build();
211+
}
212+
} else {
213+
ds.setConfig("{}");
214+
}
215+
216+
// 5. 创建数据源
217+
try {
218+
log.debug("Saving new data source: {}", ds.getName());
219+
DataSource createdDs = dataSourceService.create(ds);
220+
log.info("Successfully created data source: {} with id: {}", createdDs.getName(), createdDs.getId());
221+
return ResponseEntity.status(201).body(toResponse(createdDs));
222+
} catch (Exception e) {
223+
log.error("Failed to create data source: {}", e.getMessage(), e);
224+
return ResponseEntity.internalServerError().build();
225+
}
95226
}
96227
}

backend/services/data-collection-service/src/main/resources/config/application-datacollection.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ dataengine:
22
data-collection:
33
# DataX配置
44
datax:
5-
home-path: ${DATAX_HOME:./runtime/datax}
5+
home-path: ${DATAX_HOME:D:/datax}
66
python-path: ${DATAX_PYTHON_PATH:python3}
77
job-config-path: ${DATAX_JOB_PATH:./data/temp/datax/jobs}
88
log-path: ${DATAX_LOG_PATH:./logs/datax}

backend/services/data-collection-service/src/main/resources/mappers/DataSourceMapper.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
33
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
44

55
<mapper namespace="com.dataengine.collection.infrastructure.persistence.mapper.DataSourceMapper">
@@ -8,10 +8,10 @@
88
<resultMap id="DataSourceResultMap" type="com.dataengine.collection.domain.model.DataSource">
99
<id property="id" column="id"/>
1010
<result property="name" column="name"/>
11-
<result property="type" column="type" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
11+
<result property="type" column="type"/>
1212
<result property="description" column="description"/>
1313
<result property="config" column="config"/>
14-
<result property="status" column="status" typeHandler="org.apache.ibatis.type.EnumTypeHandler"/>
14+
<result property="status" column="status"/>
1515
<result property="lastTestAt" column="last_test_at"/>
1616
<result property="lastTestResult" column="last_test_result"/>
1717
<result property="createdAt" column="created_at"/>
@@ -39,7 +39,7 @@
3939

4040
<!-- Update -->
4141
<update id="update" parameterType="com.dataengine.collection.domain.model.DataSource">
42-
UPDATE t_dc_data_sources
42+
UPDATE t_dc_data_sources
4343
SET name = #{name},
4444
description = #{description},
4545
config = #{config},
@@ -126,18 +126,18 @@
126126

127127
<!-- Update Status -->
128128
<update id="updateStatus">
129-
UPDATE t_dc_data_sources
129+
UPDATE t_dc_data_sources
130130
SET status = #{status}, updated_at = NOW()
131131
WHERE id = #{id}
132132
</update>
133133

134134
<!-- Update Test Result -->
135135
<update id="updateTestResult">
136-
UPDATE t_dc_data_sources
137-
SET last_test_at = #{lastTestAt},
138-
last_test_result = #{lastTestResult},
136+
UPDATE t_dc_data_sources
137+
SET last_test_at = #{lastTestAt},
138+
last_test_result = #{lastTestResult},
139139
updated_at = NOW()
140140
WHERE id = #{id}
141141
</update>
142142

143-
</mapper>
143+
</mapper>

0 commit comments

Comments
 (0)