jm_homework.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import time
  2. from typing import List
  3. from app import models, schemas
  4. from sqlalchemy.orm import Session
  5. from sqlalchemy import func
  6. from app.crud.constant import find_and_update
  7. from configs.globals import g
  8. from app.crud.jm_homework_datasource_relation import get_jm_relations
  9. def create_jm_homework(db: Session, item: models.JmHomework):
  10. db.add(item)
  11. db.commit()
  12. db.refresh(item)
  13. return item
  14. def get_jm_homeworks(db: Session, project_id: str):
  15. res: List[models.JmHomework] = db.query(models.JmHomework)\
  16. .filter(models.JmHomework.project_id == g.project_id)\
  17. .filter(models.JmHomework.status == 1)\
  18. .order_by(models.JmHomework.create_time.desc()).all()
  19. return res
  20. def get_jm_homework_info(db: Session, homework_id: int):
  21. item = db.query(models.JmHomework)\
  22. .filter(models.JmHomework.id == homework_id).first()
  23. if item.type == 'Dag':
  24. relations = get_jm_relations(db,homework_id)
  25. item.__dict__.update({"hd_relation":relations})
  26. return item
  27. def get_jm_homework_by_name(db: Session, name: str):
  28. db_item = db.query(models.JmHomework).filter(models.JmHomework.name == func.binary(name))\
  29. .filter(models.JmHomework.project_id == g.project_id)\
  30. .filter(models.JmHomework.status != 0).first()
  31. return db_item
  32. def update_jm_homework(db: Session, id: int, update_item: models.JmHomework):
  33. db.commit()
  34. db.flush()
  35. db.refresh(update_item)
  36. return update_item
  37. def delete_jm_homework(db: Session, id: int):
  38. db_item = db.query(models.JmHomework).filter(models.JmHomework.id == id).first()
  39. if not db_item:
  40. raise Exception('未找到该作业')
  41. db_item.status = 0
  42. db.commit()
  43. db.flush()
  44. db.refresh(db_item)
  45. return db_item
  46. def get_jm_homeworks_by_ids(db: Session, ids: List[int]):
  47. res: List[models.JmHomework] = db.query(models.JmHomework)\
  48. .filter(models.JmHomework.id.in_(ids)).all()
  49. return res
  50. def get_jm_homework_by_dag_url(db: Session, dag_url: str):
  51. res: List[models.JmHomework] = db.query(models.JmHomework)\
  52. .filter(models.JmHomework.dag_url == dag_url)\
  53. .filter(models.JmHomework.status == 1).all()
  54. return res