job_log.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 get_page, page_help, schemas
  6. from app.common.decorators import verify_all
  7. import app.crud as crud
  8. from app.utils.send_util import get_job_run_status, get_task_log
  9. from constants.constants import RUN_STATUS
  10. from utils.sx_time import sxtimeit
  11. from utils.sx_web import web_try
  12. from fastapi_pagination import Page, add_pagination, paginate, Params
  13. from app import get_db
  14. router = APIRouter(
  15. prefix="/jpt/joblog",
  16. tags=["joblog-日志管理"],
  17. )
  18. @router.get("/", dependencies=[Depends(verify_all)])
  19. @web_try()
  20. @sxtimeit
  21. def get_job_logs(job_id: Optional[int] = None, params: Params=Depends(get_page), db: Session = Depends(get_db)):
  22. job_infos = []
  23. # 是否有任务筛选
  24. if job_id is None:
  25. job_infos = crud.get_job_infos(db)
  26. else:
  27. job_infos = [crud.get_job_info(db,job_id)]
  28. id_to_job = {job.id:job for job in job_infos}
  29. relations = crud.get_af_ids(db, id_to_job.keys(), 'datax')
  30. af_to_datax = {relation.af_id:relation.se_id for relation in relations}
  31. # 获取运行记录
  32. af_job_runs = crud.get_airflow_runs_by_af_job_ids(db, af_to_datax.keys(),(params['page'] - 1) * params['size'],params['page'] * params['size'])
  33. total = crud.count_airflow_runs_by_job_ids(db, af_to_datax.keys())
  34. res = []
  35. # 循环获取日志
  36. for af_job_run in af_job_runs:
  37. job_id = af_to_datax[int(af_job_run.job_id)]
  38. # 获取af_job
  39. job_log = None
  40. if len(af_job_run.details['tasks']) > 0:
  41. job_log = list(af_job_run.details['tasks'].values())[0]
  42. else:
  43. af_job = crud.get_airflow_job_once(db, af_job_run.job_id)
  44. task = list(af_job.tasks)[0] if len(list(af_job.tasks))>0 else None
  45. log_res = get_task_log(af_job.id, af_job_run.af_run_id, task['id'])
  46. job_log = log_res['data'] if 'data' in log_res.keys() else None
  47. log = {
  48. "id": af_job_run.id,
  49. "job_id": job_id,
  50. "job_desc": id_to_job[job_id].job_desc,
  51. "af_job_id": int(af_job_run.job_id),
  52. "run_id": af_job_run.id,
  53. "af_run_id": af_job_run.af_run_id,
  54. "start_time": job_log['start_time'] if job_log and 'start_time' in job_log.keys() else None,
  55. "result": RUN_STATUS[job_log['status']] if job_log and 'status' in job_log.keys() else 0,
  56. }
  57. res.append(log)
  58. return page_help(res,params['page'],params['size'],total)
  59. @router.get("/getOnce", dependencies=[Depends(verify_all)])
  60. @web_try()
  61. @sxtimeit
  62. def get_job_logs_once(run_id: int, db: Session = Depends(get_db)):
  63. # 获取af_run
  64. af_job_run = crud.get_airflow_run_once(db, run_id)
  65. # 获取af_job
  66. af_job = crud.get_airflow_job_once(db, af_job_run.job_id)
  67. # 取出其中的task
  68. task = list(af_job.tasks)[0] if len(list(af_job.tasks))>0 else None
  69. log_res = get_task_log(af_job.id, af_job_run.af_run_id, task['id'])
  70. job_log = log_res['data'] if 'data' in log_res.keys() else None
  71. log = {
  72. "log": job_log['log'] if job_log and 'log' in job_log.keys() else None,
  73. "status": RUN_STATUS[job_log['status']] if job_log and 'status' in job_log.keys() else None
  74. }
  75. return log
  76. @router.get("/logs_status/{ids}", dependencies=[Depends(verify_all)])
  77. @web_try()
  78. @sxtimeit
  79. def get_job_log_status(ids: str):
  80. run_ids = ids.split(',')
  81. id_to_status = {}
  82. for run_id in run_ids:
  83. res = get_job_run_status(run_id)
  84. id_to_status.update({run_id:res['data']['status']})
  85. return id_to_status