Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,12 @@ 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"
|
| 11 |
|
| 12 |
def load_config():
|
| 13 |
with open(CONFIG_FILE, "r") as f:
|
|
@@ -26,10 +28,11 @@ def save_data(data):
|
|
| 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":
|
| 33 |
"error": None
|
| 34 |
}
|
| 35 |
except Exception as e:
|
|
@@ -61,15 +64,29 @@ def format_dashboard(data):
|
|
| 61 |
output.append(
|
| 62 |
f"**{name}**\n"
|
| 63 |
f"Status: {last_check.get('status', 'UNKNOWN')}\n"
|
| 64 |
-
f"Last Response
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
|
|
|
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
from datetime import datetime
|
| 7 |
+
import threading
|
| 8 |
+
import time
|
| 9 |
|
| 10 |
# Configuration
|
| 11 |
CONFIG_FILE = "config.yaml"
|
| 12 |
+
DATA_FILE = "/data/status.json"
|
| 13 |
|
| 14 |
def load_config():
|
| 15 |
with open(CONFIG_FILE, "r") as f:
|
|
|
|
| 28 |
|
| 29 |
def check_status(endpoint):
|
| 30 |
try:
|
| 31 |
+
start = time.time()
|
| 32 |
response = requests.get(endpoint["url"], timeout=10)
|
| 33 |
return {
|
| 34 |
"status": "UP" if response.ok else "DOWN",
|
| 35 |
+
"response_time": time.time() - start,
|
| 36 |
"error": None
|
| 37 |
}
|
| 38 |
except Exception as e:
|
|
|
|
| 64 |
output.append(
|
| 65 |
f"**{name}**\n"
|
| 66 |
f"Status: {last_check.get('status', 'UNKNOWN')}\n"
|
| 67 |
+
f"Last Response: {last_check.get('response_time', 'N/A'):.2f}s\n"
|
| 68 |
f"Uptime: {uptime:.1f}%\n"
|
| 69 |
)
|
| 70 |
return "\n".join(output)
|
| 71 |
|
| 72 |
+
def start_background_updates():
|
| 73 |
+
def worker():
|
| 74 |
+
while True:
|
| 75 |
+
update_dashboard()
|
| 76 |
+
time.sleep(60)
|
| 77 |
+
|
| 78 |
+
thread = threading.Thread(target=worker, daemon=True)
|
| 79 |
+
thread.start()
|
| 80 |
+
|
| 81 |
with gr.Blocks() as demo:
|
| 82 |
gr.Markdown("# Website Status Monitor")
|
| 83 |
status_output = gr.Markdown()
|
| 84 |
+
|
| 85 |
+
# Initial load and start background updates
|
| 86 |
+
demo.load(
|
| 87 |
+
fn=lambda: [update_dashboard(), start_background_updates()],
|
| 88 |
+
outputs=status_output
|
| 89 |
+
)
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
|