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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -14
app.py CHANGED
@@ -9,18 +9,16 @@ import time
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,19 +26,15 @@ def load_data():
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,7 +47,6 @@ def check_status(endpoint):
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,7 +65,6 @@ def update_dashboard():
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,7 +78,6 @@ def format_dashboard(data):
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,17 +86,19 @@ def start_background_updates():
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()
102
 
103
- # Initial load and start background updates
104
  demo.load(
105
- fn=lambda: [update_dashboard(), start_background_updates()],
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)))
 
9
 
10
  # Configuration
11
  CONFIG_FILE = "config.yaml"
12
+ DATA_FILE = "/data/status.json"
13
 
14
  # Ensure the /data directory exists
15
  os.makedirs(os.path.dirname(DATA_FILE), exist_ok=True)
16
 
17
  def load_config():
 
18
  with open(CONFIG_FILE, "r") as f:
19
  return yaml.safe_load(f)["endpoints"]
20
 
21
  def load_data():
 
22
  try:
23
  with open(DATA_FILE, "r") as f:
24
  return json.load(f)
 
26
  return {}
27
 
28
  def save_data(data):
 
29
  try:
30
  with open(DATA_FILE, "w") as f:
31
  json.dump(data, f)
32
  except PermissionError:
 
33
  temp_file = "/tmp/status.json"
34
  with open(temp_file, "w") as f:
35
  json.dump(data, f)
 
36
 
37
  def check_status(endpoint):
 
38
  try:
39
  start = time.time()
40
  response = requests.get(endpoint["url"], timeout=10)
 
47
  return {"status": "DOWN", "response_time": None, "error": str(e)}
48
 
49
  def update_dashboard():
 
50
  endpoints = load_config()
51
  data = load_data()
52
  current_time = datetime.now().isoformat()
 
65
  return format_dashboard(data)
66
 
67
  def format_dashboard(data):
 
68
  output = []
69
  for name, history in data.items():
70
  last_check = history[-1] if history else {}
 
78
  return "\n".join(output)
79
 
80
  def start_background_updates():
 
81
  def worker():
82
  while True:
83
  update_dashboard()
 
86
  thread = threading.Thread(target=worker, daemon=True)
87
  thread.start()
88
 
89
+ # Start background updates when app launches
90
+ start_background_updates()
91
+
92
+ # Create Gradio interface
93
  with gr.Blocks() as demo:
94
  gr.Markdown("# Website Status Monitor")
95
  status_output = gr.Markdown()
96
 
97
+ # Initial load
98
  demo.load(
99
+ fn=update_dashboard,
100
  outputs=status_output
101
  )
102
 
 
103
  if __name__ == "__main__":
104
  demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))