shrijayan commited on
Commit
7e70c9c
·
verified ·
1 Parent(s): c4c35c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -3
app.py CHANGED
@@ -9,13 +9,18 @@ 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:
16
  return yaml.safe_load(f)["endpoints"]
17
 
18
  def load_data():
 
19
  try:
20
  with open(DATA_FILE, "r") as f:
21
  return json.load(f)
@@ -23,10 +28,19 @@ def load_data():
23
  return {}
24
 
25
  def save_data(data):
26
- with open(DATA_FILE, "w") as f:
27
- json.dump(data, f)
 
 
 
 
 
 
 
 
28
 
29
  def check_status(endpoint):
 
30
  try:
31
  start = time.time()
32
  response = requests.get(endpoint["url"], timeout=10)
@@ -39,6 +53,7 @@ def check_status(endpoint):
39
  return {"status": "DOWN", "response_time": None, "error": str(e)}
40
 
41
  def update_dashboard():
 
42
  endpoints = load_config()
43
  data = load_data()
44
  current_time = datetime.now().isoformat()
@@ -57,6 +72,7 @@ def update_dashboard():
57
  return format_dashboard(data)
58
 
59
  def format_dashboard(data):
 
60
  output = []
61
  for name, history in data.items():
62
  last_check = history[-1] if history else {}
@@ -70,6 +86,7 @@ def format_dashboard(data):
70
  return "\n".join(output)
71
 
72
  def start_background_updates():
 
73
  def worker():
74
  while True:
75
  update_dashboard()
@@ -78,6 +95,7 @@ def start_background_updates():
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()
@@ -88,5 +106,6 @@ with gr.Blocks() as demo:
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)))
 
9
 
10
  # Configuration
11
  CONFIG_FILE = "config.yaml"
12
+ DATA_FILE = "/data/status.json" # Persistent storage path
13
+
14
+ # Ensure the /data directory exists
15
+ os.makedirs(os.path.dirname(DATA_FILE), exist_ok=True)
16
 
17
  def load_config():
18
+ """Load endpoints from config.yaml."""
19
  with open(CONFIG_FILE, "r") as f:
20
  return yaml.safe_load(f)["endpoints"]
21
 
22
  def load_data():
23
+ """Load status data from persistent storage."""
24
  try:
25
  with open(DATA_FILE, "r") as f:
26
  return json.load(f)
 
28
  return {}
29
 
30
  def save_data(data):
31
+ """Save status data to persistent storage."""
32
+ try:
33
+ with open(DATA_FILE, "w") as f:
34
+ json.dump(data, f)
35
+ except PermissionError:
36
+ # Fallback to temporary storage if /data isn't writable
37
+ temp_file = "/tmp/status.json"
38
+ with open(temp_file, "w") as f:
39
+ json.dump(data, f)
40
+ return temp_file
41
 
42
  def check_status(endpoint):
43
+ """Check the status of a single endpoint."""
44
  try:
45
  start = time.time()
46
  response = requests.get(endpoint["url"], timeout=10)
 
53
  return {"status": "DOWN", "response_time": None, "error": str(e)}
54
 
55
  def update_dashboard():
56
+ """Update the dashboard with latest status."""
57
  endpoints = load_config()
58
  data = load_data()
59
  current_time = datetime.now().isoformat()
 
72
  return format_dashboard(data)
73
 
74
  def format_dashboard(data):
75
+ """Format the dashboard output."""
76
  output = []
77
  for name, history in data.items():
78
  last_check = history[-1] if history else {}
 
86
  return "\n".join(output)
87
 
88
  def start_background_updates():
89
+ """Start a background thread to periodically update the dashboard."""
90
  def worker():
91
  while True:
92
  update_dashboard()
 
95
  thread = threading.Thread(target=worker, daemon=True)
96
  thread.start()
97
 
98
+ # Gradio Interface
99
  with gr.Blocks() as demo:
100
  gr.Markdown("# Website Status Monitor")
101
  status_output = gr.Markdown()
 
106
  outputs=status_output
107
  )
108
 
109
+ # Launch the app
110
  if __name__ == "__main__":
111
  demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))