prepare_historical_batch.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. # SQLite kann NOT NULL bei einer bestehenden Spalte nicht einfach entfernen.
  7. # Für die CDR-basierte Historie ist call_id fachlich nicht erforderlich.
  8. # Wir bauen transcripts sauber neu auf Basis des bestehenden Schemas um.
  9. cols = [r["name"] for r in con.execute("PRAGMA table_info(transcripts)")]
  10. if "call_id" in cols:
  11. con.execute("PRAGMA foreign_keys=OFF")
  12. con.executescript("""
  13. CREATE TABLE transcripts_new (
  14. id INTEGER PRIMARY KEY AUTOINCREMENT,
  15. call_id INTEGER,
  16. cdr_row_id INTEGER,
  17. rec_id INTEGER,
  18. model TEXT NOT NULL,
  19. language TEXT,
  20. audio_codec TEXT,
  21. audio_channels INTEGER,
  22. audio_sample_rate INTEGER,
  23. audio_duration REAL,
  24. transcript_json TEXT NOT NULL,
  25. status TEXT NOT NULL DEFAULT 'completed',
  26. created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
  27. FOREIGN KEY(call_id)
  28. REFERENCES calls(id)
  29. ON DELETE CASCADE
  30. );
  31. INSERT INTO transcripts_new (
  32. id, call_id, cdr_row_id, rec_id,
  33. model, language,
  34. audio_codec, audio_channels,
  35. audio_sample_rate, audio_duration,
  36. transcript_json, status, created_at
  37. )
  38. SELECT
  39. id, NULL, cdr_row_id, rec_id,
  40. model, language,
  41. audio_codec, audio_channels,
  42. audio_sample_rate, audio_duration,
  43. transcript_json, status, created_at
  44. FROM transcripts;
  45. DROP TABLE transcripts;
  46. ALTER TABLE transcripts_new RENAME TO transcripts;
  47. CREATE INDEX IF NOT EXISTS idx_transcripts_cdr_row_id
  48. ON transcripts(cdr_row_id);
  49. CREATE INDEX IF NOT EXISTS idx_transcripts_rec_id
  50. ON transcripts(rec_id);
  51. """)
  52. con.execute("PRAGMA foreign_keys=ON")
  53. con.commit()
  54. # Bestand exakt feststellen.
  55. summary = con.execute("""
  56. SELECT
  57. COUNT(*) AS cdr_rows,
  58. COUNT(DISTINCT main_call_history_id) AS call_histories,
  59. COUNT(DISTINCT COALESCE(src_rec_id, dst_rec_id)) AS recordings,
  60. MIN(start_time) AS first_call,
  61. MAX(start_time) AS last_call
  62. FROM cdr_calls
  63. WHERE src_rec_id IS NOT NULL
  64. OR dst_rec_id IS NOT NULL
  65. """).fetchone()
  66. processed = con.execute("""
  67. SELECT COUNT(DISTINCT rec_id)
  68. FROM transcripts
  69. WHERE rec_id IS NOT NULL
  70. """).fetchone()[0]
  71. analysed = con.execute("""
  72. SELECT COUNT(DISTINCT t.rec_id)
  73. FROM transcripts t
  74. JOIN analyses a ON a.transcript_id = t.id
  75. WHERE t.rec_id IS NOT NULL
  76. """).fetchone()[0]
  77. print()
  78. print("========================================")
  79. print("HISTORISCHER RECORDING-BESTAND")
  80. print("========================================")
  81. print(f"CDR-Zeilen mit Recording : {summary['cdr_rows']}")
  82. print(f"Eindeutige Call-History : {summary['call_histories']}")
  83. print(f"Eindeutige Recordings : {summary['recordings']}")
  84. print(f"Bereits transkribiert : {processed}")
  85. print(f"Bereits analysiert : {analysed}")
  86. print(f"Noch zu verarbeiten : {summary['recordings'] - analysed}")
  87. print(f"Ältester Call : {summary['first_call']}")
  88. print(f"Neuester Call : {summary['last_call']}")
  89. con.close()