Explorer
/tmp/patch_aa022_observability.py
← Zurück ↓ Download
from pathlib import Path
p=Path('/opt/struktur/social-media-radar/aa045_schema.py')
s=p.read_text()
old='''    c.execute("""
        CREATE TABLE IF NOT EXISTS source_runs (
            id INTEGER PRIMARY KEY AUTOINCREMENT, source_name TEXT NOT NULL,
            run_at TEXT NOT NULL, last_success TEXT, last_error TEXT,
            items_seen INTEGER DEFAULT 0, items_new INTEGER DEFAULT 0,
            items_duplicate INTEGER DEFAULT 0, items_clustered INTEGER DEFAULT 0,
            items_rejected INTEGER DEFAULT 0, runtime_seconds REAL DEFAULT 0,
            quota_usage TEXT DEFAULT ''
        )
    """)
'''
new='''    c.execute("""
        CREATE TABLE IF NOT EXISTS source_runs (
            id INTEGER PRIMARY KEY AUTOINCREMENT, source_name TEXT NOT NULL,
            run_at TEXT NOT NULL, last_success TEXT, last_error TEXT,
            items_seen INTEGER DEFAULT 0, items_parsed INTEGER DEFAULT 0,
            items_canonicalized INTEGER DEFAULT 0, items_new INTEGER DEFAULT 0,
            items_duplicate INTEGER DEFAULT 0, items_clustered INTEGER DEFAULT 0,
            items_rejected INTEGER DEFAULT 0, runtime_seconds REAL DEFAULT 0,
            quota_usage TEXT DEFAULT ''
        )
    """)
    run_cols = {row[1] for row in c.execute("PRAGMA table_info(source_runs)").fetchall()}
    for col in ("items_parsed", "items_canonicalized"):
        if col not in run_cols:
            c.execute(f"ALTER TABLE source_runs ADD COLUMN {col} INTEGER DEFAULT 0")
'''
assert old in s
p.write_text(s.replace(old,new,1))

p=Path('/opt/struktur/social-media-radar/signal_db.py')
s=p.read_text()
s=s.replace('''def record_source_run(source_name: str, t0_monotonic: float, items_seen: int,
                      items_new: int, items_duplicate: int, clustered: int = 0,
                      rejected: int = 0, error=None, quota_usage: str = ""):
''','''def record_source_run(source_name: str, t0_monotonic: float, items_seen: int,
                      items_new: int, items_duplicate: int, clustered: int = 0,
                      rejected: int = 0, error=None, quota_usage: str = "",
                      items_parsed: int = 0, items_canonicalized: int = 0):
''',1)
s=s.replace('''                items_seen, items_new, items_duplicate, items_clustered,
                items_rejected, runtime_seconds, quota_usage
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''','''                items_seen, items_parsed, items_canonicalized, items_new,
                items_duplicate, items_clustered, items_rejected, runtime_seconds, quota_usage
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''',1)
s=s.replace('''            items_seen, items_new, items_duplicate, clustered,
            rejected, round(_t.monotonic() - t0_monotonic, 2), quota_usage,
''','''            items_seen, items_parsed, items_canonicalized, items_new,
            items_duplicate, clustered, rejected,
            round(_t.monotonic() - t0_monotonic, 2), quota_usage,
''',1)
p.write_text(s)