Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import yaml
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# Configuration
|
| 9 |
+
CONFIG_FILE = "config.yaml"
|
| 10 |
+
DATA_FILE = "/data/status.json" # Hugging Face persistent storage
|
| 11 |
+
|
| 12 |
+
def load_config():
|
| 13 |
+
with open(CONFIG_FILE, "r") as f:
|
| 14 |
+
return yaml.safe_load(f)["endpoints"]
|
| 15 |
+
|
| 16 |
+
def load_data():
|
| 17 |
+
try:
|
| 18 |
+
with open(DATA_FILE, "r") as f:
|
| 19 |
+
return json.load(f)
|
| 20 |
+
except (FileNotFoundError, json.JSONDecodeError):
|
| 21 |
+
return {}
|
| 22 |
+
|
| 23 |
+
def save_data(data):
|
| 24 |
+
with open(DATA_FILE, "w") as f:
|
| 25 |
+
json.dump(data, f)
|
| 26 |
+
|
| 27 |
+
def check_status(endpoint):
|
| 28 |
+
try:
|
| 29 |
+
response = requests.get(endpoint["url"], timeout=10)
|
| 30 |
+
return {
|
| 31 |
+
"status": "UP" if response.ok else "DOWN",
|
| 32 |
+
"response_time": response.elapsed.total_seconds(),
|
| 33 |
+
"error": None
|
| 34 |
+
}
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return {"status": "DOWN", "response_time": None, "error": str(e)}
|
| 37 |
+
|
| 38 |
+
def update_dashboard():
|
| 39 |
+
endpoints = load_config()
|
| 40 |
+
data = load_data()
|
| 41 |
+
current_time = datetime.now().isoformat()
|
| 42 |
+
|
| 43 |
+
for endpoint in endpoints:
|
| 44 |
+
result = check_status(endpoint)
|
| 45 |
+
key = endpoint["name"]
|
| 46 |
+
if key not in data:
|
| 47 |
+
data[key] = []
|
| 48 |
+
data[key].append({
|
| 49 |
+
"timestamp": current_time,
|
| 50 |
+
**result
|
| 51 |
+
})
|
| 52 |
+
|
| 53 |
+
save_data(data)
|
| 54 |
+
return format_dashboard(data)
|
| 55 |
+
|
| 56 |
+
def format_dashboard(data):
|
| 57 |
+
output = []
|
| 58 |
+
for name, history in data.items():
|
| 59 |
+
last_check = history[-1] if history else {}
|
| 60 |
+
uptime = sum(1 for h in history if h["status"] == "UP") / len(history) * 100 if history else 0
|
| 61 |
+
output.append(
|
| 62 |
+
f"**{name}**\n"
|
| 63 |
+
f"Status: {last_check.get('status', 'UNKNOWN')}\n"
|
| 64 |
+
f"Last Response Time: {last_check.get('response_time', 'N/A')}s\n"
|
| 65 |
+
f"Uptime: {uptime:.1f}%\n"
|
| 66 |
+
)
|
| 67 |
+
return "\n".join(output)
|
| 68 |
+
|
| 69 |
+
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("# Website Status Monitor")
|
| 71 |
+
status_output = gr.Markdown()
|
| 72 |
+
demo.load(fn=update_dashboard, outputs=status_output, every=60)
|
| 73 |
+
|
| 74 |
+
if __name__ == "__main__":
|
| 75 |
+
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
|