relation.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import time
  2. from typing import List
  3. from app import models, schemas
  4. from sqlalchemy.orm import Session
  5. def create_relation(db: Session, se_id: int, type: str, af_id: int):
  6. db_item = models.Relation(**{"se_id": se_id,
  7. "type": type,
  8. "af_id": af_id})
  9. db.add(db_item)
  10. db.commit()
  11. db.refresh(db_item)
  12. return db_item
  13. def create_debug_relation(db: Session, dag_uuid: str, type: str, af_id: int):
  14. db_item = models.Relation(**{"dag_uuid": dag_uuid,
  15. "type": type,
  16. "af_id": af_id})
  17. db.add(db_item)
  18. db.commit()
  19. db.refresh(db_item)
  20. return db_item
  21. def get_af_id(db: Session, se_id: int, type: str):
  22. res: models.Relation = db.query(models.Relation)\
  23. .filter(models.Relation.se_id == se_id)\
  24. .filter(models.Relation.type == type).first()
  25. return res
  26. def get_af_ids(db: Session, se_ids: List[int], type: str):
  27. res: List[models.Relation] = db.query(models.Relation)\
  28. .filter(models.Relation.se_id.in_(se_ids))\
  29. .filter(models.Relation.type == type).all()
  30. return res
  31. def get_dag_af_id(db: Session, dag_uuid: int, type: str):
  32. res: models.Relation = db.query(models.Relation)\
  33. .filter(models.Relation.dag_uuid == dag_uuid)\
  34. .filter(models.Relation.type == type).first()
  35. return res
  36. def delete_relation(db: Session, se_id: int, type: str):
  37. res: models.Relation = db.query(models.Relation)\
  38. .filter(models.Relation.se_id == se_id)\
  39. .filter(models.Relation.type == type).delete()
  40. return res