use_decode_video_model.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. DecodeVideo 模型使用示例
  5. 演示如何使用 models 模块进行数据库操作
  6. """
  7. import json
  8. from contextlib import contextmanager
  9. from src.models import get_db, DecodeVideo, DecodeStatus, init_db
  10. @contextmanager
  11. def db_session():
  12. """数据库会话上下文管理器"""
  13. db = next(get_db())
  14. try:
  15. yield db
  16. db.commit()
  17. except Exception as e:
  18. db.rollback()
  19. print(f"数据库操作失败: {e}")
  20. raise
  21. finally:
  22. db.close()
  23. def example_create():
  24. """示例:创建新记录"""
  25. print("\n=== 示例1: 创建新记录 ===")
  26. with db_session() as db:
  27. video = DecodeVideo.create(
  28. task_id=10001,
  29. video_id="58840748",
  30. status=DecodeStatus.PENDING
  31. )
  32. db.add(video)
  33. print(f"✓ 创建记录成功: {video}")
  34. def example_query():
  35. """示例:查询记录"""
  36. print("\n=== 示例2: 查询记录 ===")
  37. with db_session() as db:
  38. # 根据 task_id 查询
  39. video = db.query(DecodeVideo).filter_by(task_id=10001).first()
  40. if video:
  41. print(f"✓ 查询到记录: {video}")
  42. print(f" 详细信息: {json.dumps(video.to_dict(), indent=2, ensure_ascii=False)}")
  43. else:
  44. print("✗ 未找到记录")
  45. def example_update_status():
  46. """示例:更新状态"""
  47. print("\n=== 示例3: 更新状态 ===")
  48. with db_session() as db:
  49. video = db.query(DecodeVideo).filter_by(task_id=10001).first()
  50. if video:
  51. old_status = video.status
  52. video.update_status(DecodeStatus.EXECUTING)
  53. print(f"✓ 状态更新: {DecodeStatus.get_description(old_status)} -> {DecodeStatus.get_description(video.status)}")
  54. def example_update_result():
  55. """示例:更新解码结果"""
  56. print("\n=== 示例4: 更新解码结果 ===")
  57. with db_session() as db:
  58. video = db.query(DecodeVideo).filter_by(task_id=10001).first()
  59. if video:
  60. # 模拟解码结果
  61. result_data = {
  62. "video_id": "58840748",
  63. "title": "示例视频",
  64. "status": "success"
  65. }
  66. result_json = json.dumps(result_data, ensure_ascii=False)
  67. video.update_result(result_json)
  68. print(f"✓ 结果更新成功")
  69. print(f" 结果长度: {len(result_json)} 字符")
  70. def example_query_by_status():
  71. """示例:根据状态查询"""
  72. print("\n=== 示例5: 根据状态查询 ===")
  73. with db_session() as db:
  74. # 查询所有待执行的记录
  75. pending_videos = db.query(DecodeVideo).filter_by(
  76. status=DecodeStatus.PENDING
  77. ).all()
  78. print(f"✓ 待执行任务数: {len(pending_videos)}")
  79. # 查询所有执行成功的记录
  80. success_videos = db.query(DecodeVideo).filter_by(
  81. status=DecodeStatus.SUCCESS
  82. ).all()
  83. print(f"✓ 执行成功任务数: {len(success_videos)}")
  84. def example_update_failed():
  85. """示例:更新为失败状态"""
  86. print("\n=== 示例6: 更新为失败状态 ===")
  87. with db_session() as db:
  88. video = db.query(DecodeVideo).filter_by(task_id=10001).first()
  89. if video:
  90. video.update_status(DecodeStatus.FAILED, error_reason="处理超时")
  91. print(f"✓ 更新为失败状态")
  92. print(f" 失败原因: {video.error_reason}")
  93. def example_list_all():
  94. """示例:列出所有记录"""
  95. print("\n=== 示例7: 列出所有记录 ===")
  96. with db_session() as db:
  97. all_videos = db.query(DecodeVideo).all()
  98. print(f"✓ 总记录数: {len(all_videos)}")
  99. for video in all_videos[:5]: # 只显示前5条
  100. print(f" - {video}")
  101. def main():
  102. """主函数"""
  103. print("=" * 50)
  104. print("DecodeVideo 模型使用示例")
  105. print("=" * 50)
  106. try:
  107. # 初始化数据库(如果表不存在则创建)
  108. print("\n初始化数据库...")
  109. init_db()
  110. print("✓ 数据库初始化完成")
  111. # 运行示例
  112. example_create()
  113. example_query()
  114. example_update_status()
  115. example_update_result()
  116. example_query_by_status()
  117. example_update_failed()
  118. example_list_all()
  119. print("\n" + "=" * 50)
  120. print("所有示例执行完成!")
  121. print("=" * 50)
  122. except Exception as e:
  123. print(f"\n✗ 执行出错: {e}")
  124. import traceback
  125. traceback.print_exc()
  126. if __name__ == "__main__":
  127. main()