Complete Q5: Add mithal.space monitoring script, dashboard, and Flask API integration

هذا الالتزام موجود في:
2026-07-26 18:05:58 +03:00
الأصل 3033da3d42
التزام 2e9f3135f7
5 ملفات معدلة مع 297 إضافات و63 حذوفات

114
app.py
عرض الملف

@@ -1,68 +1,56 @@
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
import time
from flask import Flask, jsonify, render_template_string, send_file
import os
import json
import subprocess
app = FastAPI()
app = Flask(__name__)
start_time = time.time()
request_count = 0
METRICS_FILE = os.path.join(os.path.dirname(__file__), "q5-mithal-monitoring", "metrics.json")
@app.get("/")
def read_root():
global request_count
request_count += 1
return {"message": "Welcome to Ghaymah SRE Service"}
@app.route('/')
def home():
return jsonify({
"status": "healthy",
"service": "Ghaymah SRE Engine",
"endpoints": [
"/health",
"/dashboard",
"/mithal-dashboard",
"/api/mithal-metrics"
]
}), 200
@app.get("/health")
def health_check():
global request_count
request_count += 1
return {
"status": "ok",
"uptime_seconds": round(time.time() - start_time, 2),
"total_requests": request_count
}
@app.route('/health')
def health():
return jsonify({"status": "UP", "database": "connected"}), 200
@app.get("/dashboard", response_class=HTMLResponse)
def get_dashboard():
global request_count
request_count += 1
uptime = round(time.time() - start_time, 2)
return f"""
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
<meta charset="UTF-8">
<title>Ghaymah SRE Dashboard</title>
<style>
body {{ font-family: system-ui, sans-serif; background: #0f172a; color: #fff; padding: 40px; text-align: center; }}
.container {{ max-width: 800px; margin: 0 auto; }}
.grid {{ display: flex; gap: 20px; margin-top: 30px; }}
.card {{ background: #1e293b; padding: 25px; border-radius: 12px; flex: 1; border: 1px solid #334155; }}
.status-up {{ color: #22c55e; font-weight: bold; }}
h1 {{ font-size: 2.5rem; color: #38bdf8; }}
</style>
<script>setTimeout(() => {{ window.location.reload(); }}, 5000);</script>
</head>
<body>
<div class="container">
<h2>🚀 Ghaymah SRE Live Monitoring Dashboard</h2>
<div class="grid">
<div class="card">
<h3>الحالة (Status)</h3>
<h1 class="status-up">UP (200 OK)</h1>
</div>
<div class="card">
<h3>مدة التشغيل (Uptime)</h3>
<h1>{uptime}s</h1>
</div>
<div class="card">
<h3>عدد الطلبات (Total Requests)</h3>
<h1>{request_count}</h1>
</div>
</div>
</div>
</body>
</html>
"""
@app.route('/dashboard')
def dashboard():
dashboard_path = os.path.join(os.path.dirname(__file__), 'dashboard.html')
if os.path.exists(dashboard_path):
return send_file(dashboard_path)
return "Dashboard HTML not found", 404
@app.route('/mithal-dashboard')
def mithal_dashboard():
# Trigger a fresh check
try:
subprocess.run(["python3", "q5-mithal-monitoring/monitor.py"], timeout=10)
except Exception:
pass
dashboard_path = os.path.join(os.path.dirname(__file__), 'q5-mithal-monitoring', 'dashboard.html')
if os.path.exists(dashboard_path):
return send_file(dashboard_path)
return "Mithal Dashboard HTML not found", 404
@app.route('/api/mithal-metrics')
def mithal_metrics():
if os.path.exists(METRICS_FILE):
with open(METRICS_FILE, "r") as f:
data = json.load(f)
return jsonify(data)
return jsonify([])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)