11import datetime
2+ import asyncio
23
34from fastapi import FastAPI , APIRouter , HTTPException , status , Header , Depends ,Body
45from sqlalchemy import func
@@ -93,9 +94,25 @@ async def create_datasource(datasource: DataSourceCreate, db: Session = Depends(
9394 if datasource .source_type not in [item .value for item in DataSourceTypeEnum ]:
9495 return response_fail (msg = "不支持的数据源类型" )
9596 # user_id = 54
97+
98+ # 处理分支信息:修正前端传递的分支信息
99+ # 前端可能将用户填写的分支名错误地放在了 csg_hub_dataset_name 中
100+ if datasource .extra_config is None :
101+ datasource .extra_config = {}
102+
103+ current_branch = datasource .extra_config .get ("csg_hub_dataset_default_branch" , "" )
104+ dataset_name = datasource .extra_config .get ("csg_hub_dataset_name" , "" )
105+ dataset_id = datasource .extra_config .get ("csg_hub_dataset_id" , "" )
106+
107+ # 如果用户选择了数据流向,且分支是 main,但 dataset_name 有值,使用 dataset_name 作为分支
108+ if dataset_id and current_branch == "main" and dataset_name and dataset_name != "main" and dataset_name .strip ():
109+ datasource .extra_config ["csg_hub_dataset_default_branch" ] = dataset_name
96110
97111 connector = get_datasource_connector (datasource )
98- if not connector .test_connection ():
112+ # Run the synchronized test_connection method in the thread pool
113+ loop = asyncio .get_event_loop ()
114+ test_result = await loop .run_in_executor (None , connector .test_connection )
115+ if not test_result .get ('success' , False ):
99116 datasource .source_status = DataSourceStatusEnum .INACTIVE .value
100117 else :
101118 if datasource .is_run :
@@ -104,7 +121,7 @@ async def create_datasource(datasource: DataSourceCreate, db: Session = Depends(
104121 datasource .source_status = DataSourceStatusEnum .WAITING .value
105122 if not user_id :
106123 return response_fail (msg = "用户ID不能为空" )
107- data_source_id = create_data_source (connector . test_connection () , db , datasource , int (user_id ), user_name ,
124+ data_source_id = create_data_source (test_result , db , datasource , int (user_id ), user_name ,
108125 user_token )
109126 return response_success (data = data_source_id )
110127 except Exception as e :
@@ -145,7 +162,10 @@ async def test_datasource_connection(datasource: DataSourceCreate):
145162
146163 try :
147164 connector = get_datasource_connector (datasource )
148- return response_success (data = connector .test_connection ())
165+ # Run the synchronized test_connection method in the thread pool to avoid blocking the event loop
166+ loop = asyncio .get_event_loop ()
167+ result = await loop .run_in_executor (None , connector .test_connection )
168+ return response_success (data = result )
149169 except Exception as e :
150170 logger .error (f"test_datasource_connection: { str (e )} " )
151171 return response_fail (msg = f"测试连接失败:{ str (e )} " )
@@ -154,6 +174,16 @@ async def test_datasource_connection(datasource: DataSourceCreate):
154174@router .put ("/datasource/edit/{datasource_id}" , response_model = dict )
155175async def update_datasource (datasource_id : int , datasource : DataSourceUpdate , db : Session = Depends (get_sync_session )):
156176 try :
177+ # 处理分支信息:修正前端传递的分支信息(与创建接口相同的逻辑)
178+ if datasource .extra_config is not None :
179+ current_branch = datasource .extra_config .get ("csg_hub_dataset_default_branch" , "" )
180+ dataset_name = datasource .extra_config .get ("csg_hub_dataset_name" , "" )
181+ dataset_id = datasource .extra_config .get ("csg_hub_dataset_id" , "" )
182+
183+ # 如果用户选择了数据流向,且分支是 main,但 dataset_name 有值,使用 dataset_name 作为分支
184+ if dataset_id and current_branch == "main" and dataset_name and dataset_name != "main" and dataset_name .strip ():
185+ datasource .extra_config ["csg_hub_dataset_default_branch" ] = dataset_name
186+
157187 data_source = update_data_source (db , datasource_id , datasource )
158188 if not data_source :
159189 return response_fail (msg = "更新失败" )
@@ -213,12 +243,14 @@ async def get_datasource_tables(datasource: DataSourceCreate):
213243 try :
214244 # if datasource.source_type == DataSourceTypeEnum.MONGODB.value:
215245
216-
217246 connector = get_datasource_connector (datasource )
218- if not connector .test_connection ():
247+ # test_the_connection_in_the_thread_pool
248+ loop = asyncio .get_event_loop ()
249+ test_result = await loop .run_in_executor (None , connector .test_connection )
250+ if not test_result .get ('success' , False ):
219251 return response_fail (msg = "数据源连接失败" )
220252
221- tables = connector .get_tables ( )
253+ tables = await loop . run_in_executor ( None , connector .get_tables )
222254 return response_success (data = tables )
223255 except Exception as e :
224256 logger .error (f"获取表列表失败: { str (e )} " )
@@ -233,17 +265,16 @@ async def get_datasource_table_columns(datasource: DataSourceCreate, table_name:
233265 return response_fail (msg = "MongoDB不支持获取表和字段列表" )
234266
235267 connector = get_datasource_connector (datasource )
236- if not connector .test_connection ():
268+ loop = asyncio .get_event_loop ()
269+ test_result = await loop .run_in_executor (None , connector .test_connection )
270+ if not test_result .get ('success' , False ):
237271 return response_fail (msg = "数据源连接失败" )
238272
239- columns = connector .get_table_columns ( table_name )
273+ columns = await loop . run_in_executor ( None , connector .get_table_columns , table_name )
240274 return response_success (data = columns )
241275 except Exception as e :
242276 logger .error (f"获取表字段失败: { str (e )} " )
243277 return response_fail (msg = f"获取表字段失败: { str (e )} " )
244- except Exception as e :
245- logger .error (f"获取表字段失败: { str (e )} " )
246- return response_fail (msg = f"获取字段失败: { str (e )} " )
247278
248279
249280@router .get ("/datasource/info" , response_model = dict )
@@ -266,8 +297,10 @@ async def get_datasource_tables_and_columns(datasource: DataSourceCreate):
266297 return response_fail (msg = "MongoDB不支持获取表和字段列表" )
267298
268299 connector = get_datasource_connector (datasource )
269- if not connector .test_connection ():
270- return response_fail (msg = "数据源连接失败" )
300+ test_result = connector .test_connection ()
301+ if not test_result or not test_result .get ("success" , False ):
302+ error_msg = test_result .get ("message" , "Connection failed" ) if test_result else "Connection test returned None"
303+ return response_fail (msg = f"数据源连接失败: { error_msg } " )
271304
272305 tables_and_columns = connector .get_tables_and_columns ()
273306 return response_success (data = tables_and_columns )
0 commit comments