Skip to content

Commit c6d6998

Browse files
committed
feat: ds tips
1 parent d149ac5 commit c6d6998

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

backend/apps/datasource/api/datasource.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ async def get_datasource(session: SessionDep, id: int):
3838

3939

4040
@router.post("/check")
41-
async def check(session: SessionDep, ds: CoreDatasource):
41+
async def check(session: SessionDep, trans: Trans, ds: CoreDatasource):
4242
def inner():
43-
return check_status(session, ds, True)
43+
return check_status(session, trans, ds, True)
4444

4545
return await asyncio.to_thread(inner)
4646

@@ -54,9 +54,9 @@ def inner():
5454

5555

5656
@router.post("/chooseTables/{id}")
57-
async def choose_tables(session: SessionDep, id: int, tables: List[CoreTable]):
57+
async def choose_tables(session: SessionDep, trans: Trans, id: int, tables: List[CoreTable]):
5858
def inner():
59-
chooseTables(session, id, tables)
59+
chooseTables(session, trans, id, tables)
6060

6161
await asyncio.to_thread(inner)
6262

@@ -80,12 +80,18 @@ async def get_tables(session: SessionDep, id: int):
8080

8181

8282
@router.post("/getTablesByConf")
83-
async def get_tables_by_conf(session: SessionDep, ds: CoreDatasource):
83+
async def get_tables_by_conf(session: SessionDep, trans: Trans, ds: CoreDatasource):
8484
try:
8585
return getTablesByDs(session, ds)
8686
except Exception as e:
87-
SQLBotLogUtil.error(f"get table failed: {e}")
88-
raise HTTPException(status_code=500, detail=f'Get table Failed: {e.args}')
87+
# check ds status
88+
def inner():
89+
return check_status(session, trans, ds, True)
90+
91+
status = await asyncio.to_thread(inner)
92+
if status:
93+
SQLBotLogUtil.error(f"get table failed: {e}")
94+
raise HTTPException(status_code=500, detail=f'Get table Failed: {e.args}')
8995

9096

9197
@router.post("/getFields/{id}/{table_name}")
@@ -127,13 +133,17 @@ async def edit_field(session: SessionDep, field: CoreField):
127133

128134

129135
@router.post("/previewData/{id}")
130-
async def preview_data(session: SessionDep, current_user: CurrentUser, id: int, data: TableObj):
136+
async def preview_data(session: SessionDep, trans: Trans, current_user: CurrentUser, id: int, data: TableObj):
131137
def inner():
132138
try:
133139
return preview(session, current_user, id, data)
134140
except Exception as e:
135-
SQLBotLogUtil.error(f"Preview failed: {e}")
136-
raise HTTPException(status_code=500, detail=f'Preview Failed: {e.args}')
141+
ds = session.query(CoreDatasource).filter(CoreDatasource.id == id).first()
142+
# check ds status
143+
status = check_status(session, trans, ds, True)
144+
if status:
145+
SQLBotLogUtil.error(f"Preview failed: {e}")
146+
raise HTTPException(status_code=500, detail=f'Preview Failed: {e.args}')
137147

138148
return await asyncio.to_thread(inner)
139149

@@ -195,7 +205,8 @@ def inner():
195205
create_table(conn, tableName, fields)
196206

197207
data = [
198-
{df.columns[i]: None if pd.isna(row[i]) else (int(row[i]) if "int" in str(df.dtypes[i]) else row[i])
208+
{df.columns[i]: None if pd.isna(row[i]) else (
209+
int(row[i]) if "int" in str(df.dtypes[i]) else row[i])
199210
for i in range(len(row))}
200211
for row in df.values
201212
]

backend/apps/datasource/crud/datasource.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ def get_datasource_list(session: SessionDep, user: CurrentUser, oid: Optional[in
3131
current_oid = oid
3232
return session.exec(select(CoreDatasource).where(CoreDatasource.oid == current_oid).order_by(
3333
func.convert_to(CoreDatasource.name, 'gbk'))).all()
34-
3534

3635

3736
def get_ds(session: SessionDep, id: int):
@@ -40,7 +39,7 @@ def get_ds(session: SessionDep, id: int):
4039
return datasource
4140

4241

43-
def check_status(session: SessionDep, ds: CoreDatasource, is_raise: bool = False):
42+
def check_status(session: SessionDep, trans: Trans, ds: CoreDatasource, is_raise: bool = False):
4443
conn = get_engine(ds, 10)
4544
try:
4645
with conn.connect() as connection:
@@ -49,7 +48,7 @@ def check_status(session: SessionDep, ds: CoreDatasource, is_raise: bool = False
4948
except Exception as e:
5049
SQLBotLogUtil.error(f"Datasource {ds.id} connection failed: {e}")
5150
if is_raise:
52-
raise HTTPException(status_code=500, detail=f'Connect Failed: {e.args}')
51+
raise HTTPException(status_code=500, detail=trans('i18n_ds_invalid') + f': {e.args}')
5352
return False
5453

5554

@@ -89,17 +88,17 @@ def create_ds(session: SessionDep, trans: Trans, user: CurrentUser, create_ds: C
8988
return ds
9089

9190

92-
def chooseTables(session: SessionDep, id: int, tables: List[CoreTable]):
91+
def chooseTables(session: SessionDep, trans: Trans, id: int, tables: List[CoreTable]):
9392
ds = session.query(CoreDatasource).filter(CoreDatasource.id == id).first()
94-
check_status(session, ds, True)
93+
check_status(session, trans, ds, True)
9594
sync_table(session, ds, tables)
9695
updateNum(session, ds)
9796

9897

9998
def update_ds(session: SessionDep, trans: Trans, user: CurrentUser, ds: CoreDatasource):
10099
ds.id = int(ds.id)
101100
check_name(session, trans, user, ds)
102-
status = check_status(session, ds)
101+
status = check_status(session, trans, ds)
103102
ds.status = "Success" if status is True else "Fail"
104103
record = session.exec(select(CoreDatasource).where(CoreDatasource.id == ds.id)).first()
105104
update_data = ds.model_dump(exclude_unset=True)

backend/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@
3131
"validate_error": "Validation failed [{msg}]",
3232
"delete_default_error": "Cannot delete default model [{key}]!",
3333
"miss_default": "The default large language model has not been configured"
34-
}
34+
},
35+
"i18n_ds_invalid": "Datasource Invalid"
3536
}

backend/locales/zh-CN.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
"only_admin": "仅支持管理员调用!",
2727
"no_permission": "无权调用{url}{msg}",
2828
"authenticate_invalid": "认证无效【{msg}】"
29-
29+
3030
},
3131
"i18n_llm": {
3232
"validate_error": "校验失败[{msg}]",
3333
"delete_default_error": "无法删除默认模型[{key}]!",
3434
"miss_default": "尚未配置默认大语言模型"
35-
}
35+
},
36+
"i18n_ds_invalid": "数据源连接无效"
3637
}

0 commit comments

Comments
 (0)