job_jdbc_datasource.py 5.6 KB

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