job_jdbc_datasource.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from typing import Optional
  2. from fastapi import APIRouter, Depends
  3. from sqlalchemy.orm import Session
  4. import app.crud as crud
  5. from utils.sx_time import sxtimeit
  6. from utils.sx_web import web_try
  7. from fastapi_pagination import Page, add_pagination, paginate, Params
  8. from app.common.security.auth import verify_super_admin, verify_special, verify_users
  9. from app import schemas, get_db
  10. from configs.settings import DefaultOption, config
  11. DATABASE_NAME = config.get('HIVE', 'DATABASE_NAME')
  12. router = APIRouter(
  13. prefix="/jpt/datasource",
  14. tags=["datasource-数据源管理"],
  15. )
  16. @router.post("/test")
  17. @web_try()
  18. @sxtimeit
  19. def test_datasource_connection(ds: schemas.JobJdbcDatasourceCreate,\
  20. token_data: schemas.TokenData = Depends(verify_super_admin),\
  21. db: Session = Depends(get_db)):
  22. return crud.test_datasource_connection(db, ds)
  23. @router.post("/preview")
  24. @web_try()
  25. @sxtimeit
  26. 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)):
  27. return crud.get_preview_data(db, ds_id, table_name, limit)
  28. @router.post("/table_names", description="获取所有表名")
  29. @web_try()
  30. @sxtimeit
  31. def get_table_names(ds_id: int, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
  32. return crud.get_table_names(db, ds_id)
  33. @router.post("/table_schema", description="获取表结构信息")
  34. @web_try()
  35. @sxtimeit
  36. def get_table_schema(ds_id: int, table_name: str, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
  37. return crud.get_table_schema(db, ds_id, table_name)
  38. @router.post("/")
  39. @web_try()
  40. @sxtimeit
  41. def create_datasource(ds: schemas.JobJdbcDatasourceCreate, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
  42. return crud.create_job_jdbc_datasource(db, ds)
  43. @router.get("/")
  44. @web_try()
  45. @sxtimeit
  46. def get_datasources(datasource_type: Optional[str] = None, params: Params=Depends(), token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
  47. return paginate(crud.get_job_jdbc_datasources(db, datasource_type), params)
  48. @router.get("/info")
  49. @web_try()
  50. @sxtimeit
  51. def get_datasources_info(ds_id: int, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
  52. return crud.get_job_jdbc_datasources_info(db, ds_id)
  53. @router.put("/{ds_id}")
  54. @web_try()
  55. @sxtimeit
  56. def update_datasource(ds_id: int, ds: schemas.JobJdbcDatasourceUpdate, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
  57. return crud.update_job_jdbc_datasources(db, ds_id, ds)
  58. @router.delete("/{ds_id}")
  59. @web_try()
  60. @sxtimeit
  61. def delete_job_jdbc_datasource(ds_id: int, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
  62. return crud.delete_job_jdbc_datasource(db, ds_id)
  63. @router.post("/import_datalake")
  64. @web_try()
  65. @sxtimeit
  66. def import_datalake(item: schemas.ImportDataLake, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
  67. return crud.import_datalake(db, item)
  68. @router.put("/update_datalake/{dl_id}")
  69. @web_try()
  70. @sxtimeit
  71. def update_datalake(dl_id: int,item: schemas.ImportDataLake, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
  72. return crud.update_datalake(db, dl_id, item)
  73. @router.delete("/delete_datalake/{dl_id}")
  74. @web_try()
  75. @sxtimeit
  76. def delete_datalake(dl_id: int, token_data: schemas.TokenData = Depends(verify_super_admin), db: Session = Depends(get_db)):
  77. return crud.delete_datalake(db, dl_id)
  78. @router.post("/share_ailab")
  79. @web_try()
  80. @sxtimeit
  81. def share_ailab(item: schemas.ShareAilab, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
  82. return crud.share_ailab(db, item)
  83. @router.post("/create_table")
  84. @web_try()
  85. @sxtimeit
  86. def create_table(item: schemas.CreateAilab, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  87. return crud.create_table(db, item)
  88. @router.get("/ailab_source")
  89. @web_try()
  90. @sxtimeit
  91. def get_ailab_source(token_data: schemas.TokenData = Depends(verify_users), ):
  92. return [{
  93. 'database_name': DATABASE_NAME,
  94. 'datasource': "hive",
  95. 'datasource_name': DATABASE_NAME,
  96. 'id': -1
  97. }]
  98. @router.get("/ailab_table")
  99. @web_try()
  100. @sxtimeit
  101. def get_ailab_table(token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  102. return crud.get_ailab_table(db, token_data.project_id)
  103. @router.get("/ailab_table_schema")
  104. @web_try()
  105. @sxtimeit
  106. def get_ailab_table_schema(table_name: str, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  107. return crud.get_ailab_table_schema(db, table_name)
  108. @router.get("/preview_ailab_table")
  109. @web_try()
  110. @sxtimeit
  111. def get_preview_ailab_table(table_name: str, token_data: schemas.TokenData = Depends(verify_users), db: Session = Depends(get_db)):
  112. return crud.get_preview_ailab_table(db, table_name)
  113. @router.get("/lake_table")
  114. @web_try()
  115. @sxtimeit
  116. def get_lake_table(token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
  117. return crud.get_lake_table(db, token_data.project_id)
  118. @router.get("/lake_table_info")
  119. @web_try()
  120. @sxtimeit
  121. def get_lake_table_info(dl_id: int, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
  122. return crud.get_lake_table_info(db, dl_id)
  123. @router.get("/lake_table_schema")
  124. @web_try()
  125. @sxtimeit
  126. def get_lake_table_schema(db_name: str, table_name: str, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
  127. return crud.get_lake_table_schema(db, db_name, table_name)
  128. @router.get("/preview_lake_table")
  129. @web_try()
  130. @sxtimeit
  131. def get_preview_lake_table(db_name: str, table_name: str, token_data: schemas.TokenData = Depends(verify_special), db: Session = Depends(get_db)):
  132. return crud.get_preview_lake_table(db, db_name, table_name)
  133. @router.get("/table_location")
  134. @web_try()
  135. @sxtimeit
  136. 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)):
  137. res = None
  138. if ds_id is None:
  139. res = crud.get_table_info(db, db_name, table_name)
  140. else:
  141. res = crud.get_job_jdbc_datasource_table_location(db, db_name, table_name, ds_id)
  142. location = ''
  143. for line_list in res[0]:
  144. if line_list[0].find('Location')>=0:
  145. location = line_list[1]
  146. break
  147. return {'location': location}
  148. add_pagination(router)