mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-08 01:06:59 +08:00
feat: ds tips
This commit is contained in:
@@ -38,9 +38,9 @@ async def get_datasource(session: SessionDep, id: int):
|
||||
|
||||
|
||||
@router.post("/check")
|
||||
async def check(session: SessionDep, ds: CoreDatasource):
|
||||
async def check(session: SessionDep, trans: Trans, ds: CoreDatasource):
|
||||
def inner():
|
||||
return check_status(session, ds, True)
|
||||
return check_status(session, trans, ds, True)
|
||||
|
||||
return await asyncio.to_thread(inner)
|
||||
|
||||
@@ -54,9 +54,9 @@ async def add(session: SessionDep, trans: Trans, user: CurrentUser, ds: CreateDa
|
||||
|
||||
|
||||
@router.post("/chooseTables/{id}")
|
||||
async def choose_tables(session: SessionDep, id: int, tables: List[CoreTable]):
|
||||
async def choose_tables(session: SessionDep, trans: Trans, id: int, tables: List[CoreTable]):
|
||||
def inner():
|
||||
chooseTables(session, id, tables)
|
||||
chooseTables(session, trans, id, tables)
|
||||
|
||||
await asyncio.to_thread(inner)
|
||||
|
||||
@@ -80,12 +80,18 @@ async def get_tables(session: SessionDep, id: int):
|
||||
|
||||
|
||||
@router.post("/getTablesByConf")
|
||||
async def get_tables_by_conf(session: SessionDep, ds: CoreDatasource):
|
||||
async def get_tables_by_conf(session: SessionDep, trans: Trans, ds: CoreDatasource):
|
||||
try:
|
||||
return getTablesByDs(session, ds)
|
||||
except Exception as e:
|
||||
SQLBotLogUtil.error(f"get table failed: {e}")
|
||||
raise HTTPException(status_code=500, detail=f'Get table Failed: {e.args}')
|
||||
# check ds status
|
||||
def inner():
|
||||
return check_status(session, trans, ds, True)
|
||||
|
||||
status = await asyncio.to_thread(inner)
|
||||
if status:
|
||||
SQLBotLogUtil.error(f"get table failed: {e}")
|
||||
raise HTTPException(status_code=500, detail=f'Get table Failed: {e.args}')
|
||||
|
||||
|
||||
@router.post("/getFields/{id}/{table_name}")
|
||||
@@ -127,13 +133,17 @@ async def edit_field(session: SessionDep, field: CoreField):
|
||||
|
||||
|
||||
@router.post("/previewData/{id}")
|
||||
async def preview_data(session: SessionDep, current_user: CurrentUser, id: int, data: TableObj):
|
||||
async def preview_data(session: SessionDep, trans: Trans, current_user: CurrentUser, id: int, data: TableObj):
|
||||
def inner():
|
||||
try:
|
||||
return preview(session, current_user, id, data)
|
||||
except Exception as e:
|
||||
SQLBotLogUtil.error(f"Preview failed: {e}")
|
||||
raise HTTPException(status_code=500, detail=f'Preview Failed: {e.args}')
|
||||
ds = session.query(CoreDatasource).filter(CoreDatasource.id == id).first()
|
||||
# check ds status
|
||||
status = check_status(session, trans, ds, True)
|
||||
if status:
|
||||
SQLBotLogUtil.error(f"Preview failed: {e}")
|
||||
raise HTTPException(status_code=500, detail=f'Preview Failed: {e.args}')
|
||||
|
||||
return await asyncio.to_thread(inner)
|
||||
|
||||
@@ -195,7 +205,8 @@ async def upload_excel(session: SessionDep, file: UploadFile = File(...)):
|
||||
create_table(conn, tableName, fields)
|
||||
|
||||
data = [
|
||||
{df.columns[i]: None if pd.isna(row[i]) else (int(row[i]) if "int" in str(df.dtypes[i]) else row[i])
|
||||
{df.columns[i]: None if pd.isna(row[i]) else (
|
||||
int(row[i]) if "int" in str(df.dtypes[i]) else row[i])
|
||||
for i in range(len(row))}
|
||||
for row in df.values
|
||||
]
|
||||
|
||||
@@ -31,7 +31,6 @@ def get_datasource_list(session: SessionDep, user: CurrentUser, oid: Optional[in
|
||||
current_oid = oid
|
||||
return session.exec(select(CoreDatasource).where(CoreDatasource.oid == current_oid).order_by(
|
||||
func.convert_to(CoreDatasource.name, 'gbk'))).all()
|
||||
|
||||
|
||||
|
||||
def get_ds(session: SessionDep, id: int):
|
||||
@@ -40,7 +39,7 @@ def get_ds(session: SessionDep, id: int):
|
||||
return datasource
|
||||
|
||||
|
||||
def check_status(session: SessionDep, ds: CoreDatasource, is_raise: bool = False):
|
||||
def check_status(session: SessionDep, trans: Trans, ds: CoreDatasource, is_raise: bool = False):
|
||||
conn = get_engine(ds, 10)
|
||||
try:
|
||||
with conn.connect() as connection:
|
||||
@@ -49,7 +48,7 @@ def check_status(session: SessionDep, ds: CoreDatasource, is_raise: bool = False
|
||||
except Exception as e:
|
||||
SQLBotLogUtil.error(f"Datasource {ds.id} connection failed: {e}")
|
||||
if is_raise:
|
||||
raise HTTPException(status_code=500, detail=f'Connect Failed: {e.args}')
|
||||
raise HTTPException(status_code=500, detail=trans('i18n_ds_invalid') + f': {e.args}')
|
||||
return False
|
||||
|
||||
|
||||
@@ -89,9 +88,9 @@ def create_ds(session: SessionDep, trans: Trans, user: CurrentUser, create_ds: C
|
||||
return ds
|
||||
|
||||
|
||||
def chooseTables(session: SessionDep, id: int, tables: List[CoreTable]):
|
||||
def chooseTables(session: SessionDep, trans: Trans, id: int, tables: List[CoreTable]):
|
||||
ds = session.query(CoreDatasource).filter(CoreDatasource.id == id).first()
|
||||
check_status(session, ds, True)
|
||||
check_status(session, trans, ds, True)
|
||||
sync_table(session, ds, tables)
|
||||
updateNum(session, ds)
|
||||
|
||||
@@ -99,7 +98,7 @@ def chooseTables(session: SessionDep, id: int, tables: List[CoreTable]):
|
||||
def update_ds(session: SessionDep, trans: Trans, user: CurrentUser, ds: CoreDatasource):
|
||||
ds.id = int(ds.id)
|
||||
check_name(session, trans, user, ds)
|
||||
status = check_status(session, ds)
|
||||
status = check_status(session, trans, ds)
|
||||
ds.status = "Success" if status is True else "Fail"
|
||||
record = session.exec(select(CoreDatasource).where(CoreDatasource.id == ds.id)).first()
|
||||
update_data = ds.model_dump(exclude_unset=True)
|
||||
|
||||
@@ -31,5 +31,6 @@
|
||||
"validate_error": "Validation failed [{msg}]",
|
||||
"delete_default_error": "Cannot delete default model [{key}]!",
|
||||
"miss_default": "The default large language model has not been configured"
|
||||
}
|
||||
},
|
||||
"i18n_ds_invalid": "Datasource Invalid"
|
||||
}
|
||||
@@ -26,11 +26,12 @@
|
||||
"only_admin": "仅支持管理员调用!",
|
||||
"no_permission": "无权调用{url}{msg}",
|
||||
"authenticate_invalid": "认证无效【{msg}】"
|
||||
|
||||
|
||||
},
|
||||
"i18n_llm": {
|
||||
"validate_error": "校验失败[{msg}]",
|
||||
"delete_default_error": "无法删除默认模型[{key}]!",
|
||||
"miss_default": "尚未配置默认大语言模型"
|
||||
}
|
||||
},
|
||||
"i18n_ds_invalid": "数据源连接无效"
|
||||
}
|
||||
Reference in New Issue
Block a user