process_all_recordings.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python3
  2. import asyncio
  3. import json
  4. import sqlite3
  5. import subprocess
  6. import sys
  7. from pathlib import Path
  8. DB = Path("data/telephony.sqlite3")
  9. WORKER = Path("tools/process_cdr_call.py")
  10. def get_jobs():
  11. con = sqlite3.connect(DB)
  12. con.row_factory = sqlite3.Row
  13. rows = con.execute("""
  14. SELECT
  15. c.id,
  16. c.start_time,
  17. c.source_caller_id,
  18. c.direction,
  19. COALESCE(c.src_rec_id, c.dst_rec_id) AS rec_id,
  20. t.id AS transcript_id,
  21. a.id AS analysis_id
  22. FROM cdr_calls c
  23. LEFT JOIN transcripts t
  24. ON t.cdr_row_id = c.id
  25. LEFT JOIN analyses a
  26. ON a.transcript_id = t.id
  27. WHERE c.src_rec_id IS NOT NULL
  28. OR c.dst_rec_id IS NOT NULL
  29. ORDER BY c.start_time ASC
  30. """).fetchall()
  31. con.close()
  32. return rows
  33. async def main():
  34. rows = get_jobs()
  35. total = len(rows)
  36. done = 0
  37. skipped = 0
  38. failed = 0
  39. print(f"Recordings insgesamt: {total}")
  40. print()
  41. for n, row in enumerate(rows, 1):
  42. rec_id = row["rec_id"]
  43. if row["transcript_id"] and row["analysis_id"]:
  44. skipped += 1
  45. print(
  46. f"[{n}/{total}] SKIP "
  47. f"recId={rec_id} "
  48. f"analysis={row['analysis_id']}"
  49. )
  50. continue
  51. print()
  52. print("=" * 60)
  53. print(
  54. f"[{n}/{total}] VERARBEITE "
  55. f"recId={rec_id} | "
  56. f"{row['start_time']} | "
  57. f"{row['source_caller_id']}"
  58. )
  59. print("=" * 60)
  60. try:
  61. process = await asyncio.create_subprocess_exec(
  62. sys.executable,
  63. str(WORKER),
  64. str(row["id"]),
  65. stdout=asyncio.subprocess.PIPE,
  66. stderr=asyncio.subprocess.STDOUT,
  67. )
  68. while True:
  69. line = await process.stdout.readline()
  70. if not line:
  71. break
  72. print(
  73. line.decode(
  74. "utf-8",
  75. errors="replace"
  76. ).rstrip()
  77. )
  78. rc = await process.wait()
  79. if rc == 0:
  80. done += 1
  81. else:
  82. failed += 1
  83. print(
  84. f"FEHLER: Worker beendet mit Exit-Code {rc}"
  85. )
  86. except Exception as exc:
  87. failed += 1
  88. print(
  89. f"FEHLER bei recId={rec_id}: {exc}"
  90. )
  91. print()
  92. print("=" * 60)
  93. print("VERARBEITUNG ABGESCHLOSSEN")
  94. print("=" * 60)
  95. print(f"Gesamt: {total}")
  96. print(f"Neu verarbeitet: {done}")
  97. print(f"Übersprungen: {skipped}")
  98. print(f"Fehler: {failed}")
  99. if __name__ == "__main__":
  100. asyncio.run(main())