shrijayan commited on
Commit
9eea4a2
·
verified ·
1 Parent(s): 9fa21fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -210,57 +210,61 @@ threading.Thread(target=background_updates, daemon=True).start()
210
  with gr.Blocks(title="Website Status Monitor") as demo:
211
  gr.Markdown("# 🚦 Website Status Monitor")
212
 
213
- # Placeholder for dynamic components
214
- status_components = []
215
-
216
  def update_ui():
217
  data = monitor.load_data()
218
- print("Loaded data:", data)
219
  metrics = monitor.get_metrics(data)
220
- print("Metrics:", metrics)
221
  plots = monitor.create_plots(metrics)
222
- print("Plots created.")
223
 
224
- elements = []
 
 
225
  for name, metric in metrics.items():
226
- with gr.Row():
 
227
  with gr.Column(scale=1):
228
  status_color = "#4CAF50" if metric['current_status'] == 'UP' else "#F44336"
229
- status_markdown = gr.Markdown(f"### {name}\n"
230
- f"**Status:** <span style='color: {status_color}'>"
231
- f"{metric['current_status']}</span>\n"
232
- f"**Last Response:** {np.mean(metric['response_times'][-5:]):.2f}s\n"
233
- f"**24h Uptime:** {metric['uptime']*100:.1f}%")
234
- elements.append(status_markdown)
235
 
 
236
  with gr.Column(scale=2):
237
  response_plot = gr.Plot(plots[f"{name}_response"], label="Response Times")
238
- elements.append(response_plot)
239
 
240
  with gr.Column(scale=1):
241
  uptime_plot = gr.Plot(plots[f"{name}_uptime"], label="Uptime")
242
- elements.append(uptime_plot)
243
 
 
244
  with gr.Accordion("Incident History", open=False):
245
  if not metric['incidents']:
246
- incident_markdown = gr.Markdown("No incidents in past 24 hours")
247
- elements.append(incident_markdown)
248
  else:
249
  for incident in metric['incidents'][-5:]:
250
  duration = incident['duration'] or "Ongoing"
251
- incident_markdown = gr.Markdown(f"**Start:** {incident['start']}\n"
252
- f"**End:** {incident['end'] or 'Still down'}\n"
253
- f"**Duration:** {duration}s\n")
254
- elements.append(incident_markdown)
 
255
 
256
- return elements
257
 
258
- # Initialize the UI with placeholders
259
- initial_elements = update_ui()
260
- status_components.extend(initial_elements)
261
 
262
- # Load the UI with the specified components
263
- demo.load(update_ui, outputs=status_components)
 
 
 
 
 
264
 
265
  if __name__ == "__main__":
266
  demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))
 
210
  with gr.Blocks(title="Website Status Monitor") as demo:
211
  gr.Markdown("# 🚦 Website Status Monitor")
212
 
213
+ # Predefine UI containers to hold dynamic content
214
+ dashboard = gr.Column(visible=True) # Container for all status components
215
+
216
  def update_ui():
217
  data = monitor.load_data()
 
218
  metrics = monitor.get_metrics(data)
 
219
  plots = monitor.create_plots(metrics)
 
220
 
221
+ # Clear previous content
222
+ children = []
223
+
224
  for name, metric in metrics.items():
225
+ with gr.Row().style(equal_height=True):
226
+ # Status Card
227
  with gr.Column(scale=1):
228
  status_color = "#4CAF50" if metric['current_status'] == 'UP' else "#F44336"
229
+ status_card = gr.Markdown(
230
+ f"### {name}\n"
231
+ f"**Status:** <span style='color: {status_color}'>{metric['current_status']}</span>\n"
232
+ f"**Last Response:** {np.mean(metric['response_times'][-5:] or 0:.2f}s\n"
233
+ f"**24h Uptime:** {metric['uptime']*100:.1f}%"
234
+ )
235
 
236
+ # Plots
237
  with gr.Column(scale=2):
238
  response_plot = gr.Plot(plots[f"{name}_response"], label="Response Times")
 
239
 
240
  with gr.Column(scale=1):
241
  uptime_plot = gr.Plot(plots[f"{name}_uptime"], label="Uptime")
 
242
 
243
+ # Incident History
244
  with gr.Accordion("Incident History", open=False):
245
  if not metric['incidents']:
246
+ gr.Markdown("No incidents in past 24 hours")
 
247
  else:
248
  for incident in metric['incidents'][-5:]:
249
  duration = incident['duration'] or "Ongoing"
250
+ gr.Markdown(
251
+ f"**Start:** {incident['start']}\n"
252
+ f"**End:** {incident['end'] or 'Still down'}\n"
253
+ f"**Duration:** {duration}s"
254
+ )
255
 
256
+ return dashboard.update(children=children) # Update the container
257
 
258
+ # Initialize UI with empty dashboard
259
+ dashboard.render()
 
260
 
261
+ # Refresh UI every 5 seconds
262
+ demo.load(
263
+ fn=update_ui,
264
+ inputs=None,
265
+ outputs=[dashboard],
266
+ every=5 # Refresh interval in seconds
267
+ )
268
 
269
  if __name__ == "__main__":
270
  demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", 7860)))