123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- from typing import Optional
- from fastapi import APIRouter, Depends
- from sqlalchemy.orm import Session
- import app.crud as crud
- from utils.sx_time import sxtimeit
- from utils.sx_web import web_try
- from fastapi_pagination import Page, add_pagination, paginate, Params
- from app.common.security.auth import verify_super_admin, verify_special, verify_users
- from app import schemas, get_db
- from configs.settings import DefaultOption, config
- DATABASE_NAME = config.get('HIVE', 'DATABASE_NAME')
- router = APIRouter(
- prefix="/jpt/datasource",
- tags=["datasource-数据源管理"],
- )
- @router.post("/test")
- @web_try()
- @sxtimeit
- def test_datasource_connection(ds: schemas.JobJdbcDatasourceCreate,\
- token_data: schemas.TokenData = Depends(verify_super_admin),\
- db: Session = Depends(get_db)):
- return crud.test_datasource_connection(db, ds)
- @router.post("/preview")
- @web_try()
- @sxtimeit
- def get_preview_data(ds_id: int, table_name: str, limit: int = 100, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return crud.get_preview_data(db, ds_id, table_name, limit)
- @router.post("/table_names", description="获取所有表名")
- @web_try()
- @sxtimeit
- def get_table_names(ds_id: int, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return crud.get_table_names(db, ds_id)
- @router.post("/table_schema", description="获取表结构信息")
- @web_try()
- @sxtimeit
- def get_table_schema(ds_id: int, table_name: str, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return crud.get_table_schema(db, ds_id, table_name)
- @router.post("/")
- @web_try()
- @sxtimeit
- def create_datasource(ds: schemas.JobJdbcDatasourceCreate, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
- return crud.create_job_jdbc_datasource(db, ds)
- @router.get("/")
- @web_try()
- @sxtimeit
- def get_datasources(datasource_type: Optional[str] = None, params: Params=Depends(), token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return paginate(crud.get_job_jdbc_datasources(db, datasource_type), params)
- @router.get("/info")
- @web_try()
- @sxtimeit
- def get_datasources_info(ds_id: int, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return crud.get_job_jdbc_datasources_info(db, ds_id)
- @router.put("/{ds_id}")
- @web_try()
- @sxtimeit
- def update_datasource(ds_id: int, ds: schemas.JobJdbcDatasourceUpdate, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
- return crud.update_job_jdbc_datasources(db, ds_id, ds)
- @router.delete("/{ds_id}")
- @web_try()
- @sxtimeit
- def delete_job_jdbc_datasource(ds_id: int, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
- return crud.delete_job_jdbc_datasource(db, ds_id)
- @router.post("/import_datalake")
- @web_try()
- @sxtimeit
- def import_datalake(item: schemas.ImportDataLake, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
- return crud.import_datalake(db, item)
- @router.put("/update_datalake/{dl_id}")
- @web_try()
- @sxtimeit
- def update_datalake(dl_id: int,item: schemas.ImportDataLake, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
- return crud.update_datalake(db, dl_id, item)
- @router.delete("/delete_datalake/{dl_id}")
- @web_try()
- @sxtimeit
- def delete_datalake(dl_id: int, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
- return crud.delete_datalake(db, dl_id)
- @router.post("/share_ailab")
- @web_try()
- @sxtimeit
- def share_ailab(item: schemas.ShareAilab, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return crud.share_ailab(db, item)
- @router.post("/create_table")
- @web_try()
- @sxtimeit
- def create_table(item: schemas.CreateAilab, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
- return crud.create_table(db, item)
- @router.get("/ailab_source")
- @web_try()
- @sxtimeit
- def get_ailab_source(token_data: schemas.TokenData = Depends(verify_users), ):
- return [{
- 'database_name': DATABASE_NAME,
- 'datasource': "hive",
- 'datasource_name': DATABASE_NAME,
- 'id': -1
- }]
- @router.get("/ailab_table")
- @web_try()
- @sxtimeit
- def get_ailab_table(token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
- return crud.get_ailab_table(db, token_data.project_id)
- @router.get("/ailab_table_schema")
- @web_try()
- @sxtimeit
- def get_ailab_table_schema(table_name: str, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
- return crud.get_ailab_table_schema(db, table_name)
- @router.get("/preview_ailab_table")
- @web_try()
- @sxtimeit
- def get_preview_ailab_table(table_name: str, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
- return crud.get_preview_ailab_table(db, table_name)
- @router.get("/lake_table")
- @web_try()
- @sxtimeit
- def get_lake_table(token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return crud.get_lake_table(db, token_data.project_id)
- @router.get("/lake_table_info")
- @web_try()
- @sxtimeit
- def get_lake_table_info(dl_id: int, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return crud.get_lake_table_info(db, dl_id)
- @router.get("/lake_table_schema")
- @web_try()
- @sxtimeit
- def get_lake_table_schema(db_name: str, table_name: str, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return crud.get_lake_table_schema(db, db_name, table_name)
- @router.get("/preview_lake_table")
- @web_try()
- @sxtimeit
- def get_preview_lake_table(db_name: str, table_name: str, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- return crud.get_preview_lake_table(db, db_name, table_name)
- @router.get("/table_location")
- @web_try()
- @sxtimeit
- def get_table_location(db_name: str, table_name: str, ds_id: Optional[int] = None, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
- res = None
- if ds_id is None:
- res = crud.get_table_info(db, db_name, table_name)
- else:
- res = crud.get_job_jdbc_datasource_table_location(db, db_name, table_name, ds_id)
- location = ''
- for line_list in res[0]:
- if line_list[0].find('Location')>=0:
- location = line_list[1]
- break
- return {'location': location}
- add_pagination(router)
|