data_management.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from asyncio import current_task
  2. from re import A, I
  3. import time
  4. from typing import Optional
  5. from fastapi import APIRouter
  6. from fastapi import Depends
  7. from sqlalchemy.orm import Session
  8. from app import schemas
  9. from app.common.decorators import verify_all
  10. import app.crud as crud
  11. from app.services.dag import get_tmp_table_name
  12. from app.utils.send_util import data_transfer_run, get_data_transfer_run_status
  13. from constants.constants import RUN_STATUS
  14. from utils.sx_time import sxtimeit
  15. from utils.sx_web import web_try
  16. from app.common.hive import hiveDs
  17. from configs.globals import g
  18. from app import get_db
  19. from configs.settings import DefaultOption, config
  20. database_name = config.get('HIVE', 'DATABASE_NAME')
  21. router = APIRouter(
  22. prefix="/jpt/datamanagement",
  23. tags=["datamanagement-数据管理"],
  24. )
  25. @router.post("/", dependencies=[Depends(verify_all)])
  26. @web_try()
  27. @sxtimeit
  28. def create_data_management(item: schemas.DataManagementCreate, db: Session = Depends(get_db)):
  29. current_time = int(time.time())
  30. table_name = f'project{g.project_id}_user{g.user_id}_{item.name}_{current_time}'
  31. table_name = table_name.lower()
  32. tmp_table_name = get_tmp_table_name(item.dag_uuid, item.node_id, str(item.out_pin), db)
  33. af_run_res = data_transfer_run(database_name+'.'+tmp_table_name, database_name+'.'+table_name)
  34. af_run = af_run_res['data'] if 'data' in af_run_res.keys() else None
  35. af_run_id = af_run['af_run_id'] if af_run and 'af_run_id' in af_run.keys() else None
  36. if af_run_id:
  37. item.name = item.name + '_' + str(current_time)
  38. res = crud.create_data_management(db, item, table_name, af_run_id)
  39. return res
  40. else:
  41. raise Exception('中间结果转存失败')
  42. @router.get("/", dependencies=[Depends(verify_all)])
  43. @web_try()
  44. @sxtimeit
  45. def get_data_managements(db: Session = Depends(get_db)):
  46. res = crud.get_data_managements(db, g.user_id, g.project_id)
  47. data_management_list = []
  48. for item in res:
  49. item.table_name = f'{database_name}.{item.table_name}'
  50. data_management_list.append(item)
  51. return data_management_list
  52. @router.get("/info", dependencies=[Depends(verify_all)])
  53. @web_try()
  54. @sxtimeit
  55. def get_data_management_info(id: int, db: Session = Depends(get_db)):
  56. item = crud.get_data_management_info(db, id)
  57. if item.status == 1:
  58. transfer_run_res = get_data_transfer_run_status(item.af_run_id)
  59. transfer_run = transfer_run_res['data'] if 'data' in transfer_run_res.keys() else None
  60. transfer_run_status = transfer_run['status'] if transfer_run and 'status' in transfer_run.keys() else None
  61. if transfer_run_status:
  62. item = crud.update_data_management_status(db, item.id, RUN_STATUS[transfer_run_status])
  63. location = ''
  64. owner = ''
  65. if item.status == 2:
  66. res = hiveDs.get_table_info(item.table_name)
  67. for line_list in res[0]:
  68. if line_list[0].find('Location')>=0:
  69. location = line_list[1]
  70. if line_list[0].find('Owner')>=0:
  71. owner = line_list[1]
  72. share_status = crud.check_share(db, item.table_name)
  73. item_dict = item.to_dict()
  74. item_dict.update({
  75. 'table_name': f'{database_name}.{item.table_name}',
  76. 'owner': owner,
  77. 'location': location,
  78. 'share_status': share_status
  79. })
  80. return item_dict
  81. @router.delete("/", dependencies=[Depends(verify_all)])
  82. @web_try()
  83. @sxtimeit
  84. def delete_data_management(data_management_id: int, db: Session = Depends(get_db)):
  85. data_management = crud.delete_data_management(db, data_management_id)
  86. return data_management
  87. @router.get("/table_content", dependencies=[Depends(verify_all)])
  88. @web_try()
  89. @sxtimeit
  90. def get_data_management_content(table_name: str, page: Optional[int] = 1, size: Optional[int] = 100, db: Session = Depends(get_db)):
  91. table_name = table_name.split('.')[-1]
  92. result = hiveDs.get_preview_data(table_name,size=size,start=(page-1)*size)
  93. data_num = hiveDs.get_data_num(table_name)
  94. result.update({'total':data_num})
  95. return result
  96. @router.get("/table_schema", dependencies=[Depends(verify_all)])
  97. @web_try()
  98. @sxtimeit
  99. def get_data_management_schema(table_name: str, db: Session = Depends(get_db)):
  100. table_name = table_name.split('.')[-1]
  101. result = hiveDs.get_table_schema(table_name)
  102. return result