api_request_log.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """API 请求日志 CRUD"""
  2. from datetime import datetime
  3. from typing import List, Optional, Tuple
  4. from sqlalchemy import and_, desc, or_
  5. from sqlalchemy.orm import Session
  6. from app.core.utils import generate_id
  7. from app.models.models import SysApiRequestLog, SysApiRequestLogHis
  8. class ApiRequestLogCRUD:
  9. @staticmethod
  10. def get_multi_with_filters(
  11. db: Session,
  12. http_method: str = None,
  13. status_code: int = None,
  14. keyword: str = None,
  15. path_prefix: str = None,
  16. start_date: str = None,
  17. end_date: str = None,
  18. skip: int = 0,
  19. limit: int = 10,
  20. ) -> Tuple[List[SysApiRequestLog], int]:
  21. q = db.query(SysApiRequestLog).filter(SysApiRequestLog.is_del == 0)
  22. if http_method:
  23. q = q.filter(SysApiRequestLog.http_method == http_method.upper())
  24. if status_code is not None:
  25. q = q.filter(SysApiRequestLog.status_code == status_code)
  26. if path_prefix:
  27. q = q.filter(SysApiRequestLog.path.like(f"%{path_prefix}%"))
  28. if keyword:
  29. kw = f"%{keyword}%"
  30. q = q.filter(
  31. or_(
  32. SysApiRequestLog.path.like(kw),
  33. SysApiRequestLog.username.like(kw),
  34. SysApiRequestLog.user_id.like(kw),
  35. SysApiRequestLog.request_body.like(kw),
  36. SysApiRequestLog.response_body.like(kw),
  37. )
  38. )
  39. if start_date:
  40. try:
  41. start_dt = datetime.strptime(start_date, "%Y-%m-%d")
  42. q = q.filter(SysApiRequestLog.create_at >= start_dt)
  43. except Exception:
  44. pass
  45. if end_date:
  46. try:
  47. end_dt = datetime.strptime(end_date, "%Y-%m-%d")
  48. end_dt = end_dt.replace(hour=23, minute=59, second=59)
  49. q = q.filter(SysApiRequestLog.create_at <= end_dt)
  50. except Exception:
  51. pass
  52. total = q.count()
  53. rows = q.order_by(desc(SysApiRequestLog.create_at)).offset(skip).limit(limit).all()
  54. return rows, total
  55. @staticmethod
  56. def get(db: Session, log_id: str) -> Optional[SysApiRequestLog]:
  57. return db.query(SysApiRequestLog).filter(
  58. and_(SysApiRequestLog.id == log_id, SysApiRequestLog.is_del == 0)
  59. ).first()
  60. @staticmethod
  61. def delete(db: Session, log_id: str) -> bool:
  62. log = db.query(SysApiRequestLog).filter(SysApiRequestLog.id == log_id).first()
  63. if not log:
  64. return False
  65. his = SysApiRequestLogHis(
  66. id=log.id,
  67. http_method=log.http_method,
  68. path=log.path,
  69. query_string=log.query_string,
  70. request_body=log.request_body,
  71. response_body=log.response_body,
  72. status_code=log.status_code,
  73. duration_ms=log.duration_ms,
  74. user_id=log.user_id,
  75. username=log.username,
  76. role=log.role,
  77. ip_address=log.ip_address,
  78. user_agent=log.user_agent,
  79. create_at=log.create_at,
  80. create_by=log.create_by,
  81. create_name=log.create_name,
  82. update_at=log.update_at,
  83. update_by=log.update_by,
  84. his_create_at=datetime.now(),
  85. )
  86. db.add(his)
  87. log.is_del = 1
  88. db.commit()
  89. return True
  90. api_request_log_crud = ApiRequestLogCRUD()