Create app.py
Browse filesInitial MVP app.py
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import sqlite3
|
| 3 |
+
import time
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
DB_PATH = "reliability.db"
|
| 7 |
+
|
| 8 |
+
# --- Setup database (first run only) ---
|
| 9 |
+
def init_db():
|
| 10 |
+
conn = sqlite3.connect(DB_PATH)
|
| 11 |
+
c = conn.cursor()
|
| 12 |
+
c.execute("""
|
| 13 |
+
CREATE TABLE IF NOT EXISTS telemetry (
|
| 14 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 15 |
+
timestamp TEXT,
|
| 16 |
+
component TEXT,
|
| 17 |
+
latency REAL,
|
| 18 |
+
error_rate REAL
|
| 19 |
+
)
|
| 20 |
+
""")
|
| 21 |
+
c.execute("""
|
| 22 |
+
CREATE TABLE IF NOT EXISTS alerts (
|
| 23 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 24 |
+
event_id INTEGER,
|
| 25 |
+
alert_type TEXT,
|
| 26 |
+
threshold REAL,
|
| 27 |
+
timestamp TEXT
|
| 28 |
+
)
|
| 29 |
+
""")
|
| 30 |
+
conn.commit()
|
| 31 |
+
conn.close()
|
| 32 |
+
|
| 33 |
+
init_db()
|
| 34 |
+
|
| 35 |
+
# --- Core functions ---
|
| 36 |
+
def log_event(component, latency, error_rate):
|
| 37 |
+
conn = sqlite3.connect(DB_PATH)
|
| 38 |
+
c = conn.cursor()
|
| 39 |
+
c.execute("INSERT INTO telemetry (timestamp, component, latency, error_rate) VALUES (?, ?, ?, ?)",
|
| 40 |
+
(datetime.now().isoformat(), component, latency, error_rate))
|
| 41 |
+
conn.commit()
|
| 42 |
+
conn.close()
|
| 43 |
+
return detect_anomaly()
|
| 44 |
+
|
| 45 |
+
def detect_anomaly(threshold_latency=200, threshold_error=0.3):
|
| 46 |
+
conn = sqlite3.connect(DB_PATH)
|
| 47 |
+
c = conn.cursor()
|
| 48 |
+
c.execute("SELECT * FROM telemetry ORDER BY id DESC LIMIT 1")
|
| 49 |
+
row = c.fetchone()
|
| 50 |
+
conn.close()
|
| 51 |
+
if row:
|
| 52 |
+
id, ts, component, latency, error_rate = row
|
| 53 |
+
if latency > threshold_latency or error_rate > threshold_error:
|
| 54 |
+
alert_msg = f"⚠️ Anomaly detected in {component} — latency {latency}ms, error rate {error_rate}"
|
| 55 |
+
save_alert(id, "anomaly", max(latency, error_rate))
|
| 56 |
+
return alert_msg
|
| 57 |
+
return "✅ No anomaly detected."
|
| 58 |
+
|
| 59 |
+
def save_alert(event_id, alert_type, threshold):
|
| 60 |
+
conn = sqlite3.connect(DB_PATH)
|
| 61 |
+
c = conn.cursor()
|
| 62 |
+
c.execute("INSERT INTO alerts (event_id, alert_type, threshold, timestamp) VALUES (?, ?, ?, ?)",
|
| 63 |
+
(event_id, alert_type, threshold, datetime.now().isoformat()))
|
| 64 |
+
conn.commit()
|
| 65 |
+
conn.close()
|
| 66 |
+
|
| 67 |
+
def show_recent_alerts():
|
| 68 |
+
conn = sqlite3.connect(DB_PATH)
|
| 69 |
+
c = conn.cursor()
|
| 70 |
+
c.execute("SELECT * FROM alerts ORDER BY id DESC LIMIT 10")
|
| 71 |
+
rows = c.fetchall()
|
| 72 |
+
conn.close()
|
| 73 |
+
if not rows:
|
| 74 |
+
return "No alerts yet."
|
| 75 |
+
return "\n".join([f"[{r[4]}] {r[2]} (threshold: {r[3]})" for r in rows])
|
| 76 |
+
|
| 77 |
+
# --- Gradio UI ---
|
| 78 |
+
with gr.Blocks() as demo:
|
| 79 |
+
gr.Markdown("# 🧠 Agentic Reliability Framework MVP")
|
| 80 |
+
gr.Markdown("Simulate telemetry events and detect anomalies automatically.")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
component = gr.Textbox(label="Component", value="api-service")
|
| 84 |
+
latency = gr.Number(label="Latency (ms)", value=150)
|
| 85 |
+
error_rate = gr.Number(label="Error rate", value=0.05)
|
| 86 |
+
btn = gr.Button("Submit Event")
|
| 87 |
+
output = gr.Textbox(label="Detection Output")
|
| 88 |
+
|
| 89 |
+
btn.click(fn=log_event, inputs=[component, latency, error_rate], outputs=output)
|
| 90 |
+
|
| 91 |
+
gr.Markdown("### Recent Alerts")
|
| 92 |
+
alert_box = gr.Textbox(label="", interactive=False)
|
| 93 |
+
refresh_btn = gr.Button("Refresh Alerts")
|
| 94 |
+
refresh_btn.click(fn=show_recent_alerts, outputs=alert_box)
|
| 95 |
+
|
| 96 |
+
demo.launch()
|