repair_and_check_batch.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import sqlite3
  2. from pathlib import Path
  3. DB = Path("data/telephony.sqlite3")
  4. con = sqlite3.connect(DB)
  5. con.row_factory = sqlite3.Row
  6. # 1. Bereits vorhandene Transkripte anhand rec_id mit cdr_calls verbinden.
  7. con.execute("""
  8. UPDATE transcripts
  9. SET cdr_row_id = (
  10. SELECT c.id
  11. FROM cdr_calls c
  12. WHERE c.src_rec_id = transcripts.rec_id
  13. OR c.dst_rec_id = transcripts.rec_id
  14. ORDER BY c.id
  15. LIMIT 1
  16. )
  17. WHERE cdr_row_id IS NULL
  18. AND rec_id IS NOT NULL
  19. """)
  20. # 2. Analysen ebenfalls mit dem CDR verbinden.
  21. con.execute("""
  22. UPDATE analyses
  23. SET cdr_row_id = (
  24. SELECT t.cdr_row_id
  25. FROM transcripts t
  26. WHERE t.id = analyses.transcript_id
  27. )
  28. WHERE cdr_row_id IS NULL
  29. """)
  30. con.commit()
  31. print("=== STATUS ===")
  32. r = con.execute("""
  33. SELECT
  34. COUNT(DISTINCT c.id) AS recordings,
  35. COUNT(DISTINCT t.rec_id) AS transcripts,
  36. COUNT(DISTINCT a.cdr_row_id) AS analyses
  37. FROM cdr_calls c
  38. LEFT JOIN transcripts t ON t.cdr_row_id = c.id
  39. LEFT JOIN analyses a ON a.cdr_row_id = c.id
  40. WHERE c.src_rec_id IS NOT NULL
  41. OR c.dst_rec_id IS NOT NULL
  42. """).fetchone()
  43. print(dict(r))
  44. print()
  45. print("=== BEREITS VERARBEITET ===")
  46. for r in con.execute("""
  47. SELECT
  48. c.id AS cdr_row_id,
  49. COALESCE(c.src_rec_id, c.dst_rec_id) AS rec_id,
  50. c.start_time,
  51. t.id AS transcript_id,
  52. a.id AS analysis_id
  53. FROM cdr_calls c
  54. LEFT JOIN transcripts t ON t.cdr_row_id = c.id
  55. LEFT JOIN analyses a ON a.cdr_row_id = c.id
  56. WHERE c.src_rec_id IS NOT NULL
  57. OR c.dst_rec_id IS NOT NULL
  58. ORDER BY c.start_time
  59. """):
  60. if r["transcript_id"] or r["analysis_id"]:
  61. print(dict(r))
  62. con.close()