Explorer
/tmp/patch_signal_db.py
← Zurück ↓ Download
import re

# Read the file
with open('/opt/struktur/social-media-radar/signal_db.py', 'r', encoding='utf-8') as f:
    content = f.read()

# Find and replace the get_active_signals function
old_function = '''def get_active_signals(min_score: float = 35.0, limit: int = 20):
    conn = get_conn()
    now = datetime.now(timezone.utc).isoformat()
    rows = conn.execute("""\n        SELECT * FROM signals\n        WHERE radar_score >= ?\n          AND (expires_at = '' OR expires_at > ?)\n          AND status NOT IN ('ignoriert', 'veroeffentlicht')\n        ORDER BY radar_score DESC\n        LIMIT ?\n    \"\"\", (min_score, now, limit)).fetchall()\n    conn.close()\n    return [dict(r) for r in rows]'''

new_function = '''def get_active_signals(min_score: float = 35.0, limit: int = 20, days: int | None = None):
    \"\"\"Get active signals, optionally filtered by published_at days.
    
    Args:
        min_score: Minimum radar score threshold
        limit: Maximum number of results
        days: Optional. If given, only return signals with published_at within 
              this many days. None = show all signals (ALLE view).
    \"\"\"
    conn = get_conn()
    now = datetime.now(timezone.utc)
    
    # Base query
    query = """\n        SELECT * FROM signals\n        WHERE radar_score >= ?\n          AND (expires_at = '' OR expires_at > ?)"""
    params = [min_score, now.isoformat()]
    
    # Add published_at filter only when days parameter is explicitly given
    if days is not None and days > 0:
        cutoff = (now - timedelta(days=days)).isoformat()
        query += """
            AND published_at IS NOT NULL 
            AND published_at != '' 
            AND published_at >= ?
        """
        params.append(cutoff)
    
    # No status filter - ALL signals visible in ALLE view
    query += \"\"\"\n        ORDER BY timestamp DESC\n        LIMIT ?"""
    params.append(limit)
    
    rows = conn.execute(query, params).fetchall()
    conn.close()
    return [dict(r) for r in rows]'''

if old_function in content:
    content = content.replace(old_function, new_function)
    print("✓ Successfully replaced get_active_signals function")
else:
    print("✗ Could not find exact old function text")
    # Show what's around line 416
    lines = content.split('\n')
    for i, line in enumerate(lines[415:430], start=416):
        print(f"{i}: {line}")

# Write back
with open('/opt/struktur/social-media-radar/signal_db.py', 'w', encoding='utf-8') as f:
    f.write(content)

print("✓ File written successfully")