ankush-003 commited on
Commit
d7d2462
β€’
1 Parent(s): 4a789b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -31
app.py CHANGED
@@ -105,34 +105,11 @@ alert_thread = threading.Thread(target=run_alert_listener)
105
  alert_thread.start()
106
 
107
  # dashboard
108
- def get_alert_data():
109
- global alert_collection
110
- pipeline = [
111
- {
112
- "$group": {
113
- "_id": {"$dateToString": {"format": "%Y-%m-%d", "date": {"$toDate": "$CreatedAt"}}},
114
- "count": {"$sum": 1}
115
- }
116
- },
117
- {"$sort": {"_id": 1}}
118
- ]
119
- result = list(alert_collection.aggregate(pipeline))
120
- df = pd.DataFrame(result)
121
- df.columns = ['Date', 'Count']
122
- df['Date'] = pd.to_datetime(df['Date'])
123
- return df
124
-
125
- # Function to create a plot of alerts over time
126
- def create_alert_plot():
127
- df = get_alert_data()
128
- fig = px.line(df, x='Date', y='Count', title='Alerts Over Time')
129
- fig.update_layout(xaxis_title='Date', yaxis_title='Number of Alerts')
130
- return fig
131
 
132
  # Function to get the latest alert and count
133
  def get_latest_alert():
134
  global latest_alert, alert_count
135
- return latest_alert, f"Total Alerts: {alert_count}", create_alert_plot()
136
 
137
  with gr.Blocks(theme=gr.themes.Soft()) as app:
138
  gr.Markdown("# 🚨 Alert Dashboard 🚨")
@@ -140,18 +117,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
140
  with gr.Row():
141
  with gr.Column():
142
  latest_alert_box = gr.Textbox(label="Latest Alert", lines=3, interactive=False)
143
- alert_count_box = gr.Textbox(label="Alert Count", interactive=False)
144
  refresh_button = gr.Button("Refresh", variant="primary")
145
-
146
- with gr.Row():
147
- alert_plot = gr.Plot(label="Alerts Over Time")
148
 
149
- refresh_button.click(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box, alert_plot])
150
 
151
- app.load(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box, alert_plot])
152
 
153
  # Auto-refresh every 30 seconds
154
- app.load(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box, alert_plot], every=30)
155
 
156
  # Launch the app
157
 
 
105
  alert_thread.start()
106
 
107
  # dashboard
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  # Function to get the latest alert and count
110
  def get_latest_alert():
111
  global latest_alert, alert_count
112
+ return latest_alert, alert_count
113
 
114
  with gr.Blocks(theme=gr.themes.Soft()) as app:
115
  gr.Markdown("# 🚨 Alert Dashboard 🚨")
 
117
  with gr.Row():
118
  with gr.Column():
119
  latest_alert_box = gr.Textbox(label="Latest Alert", lines=3, interactive=False)
120
+ alert_count_box = gr.Number(label="Alert Count", interactive=False)
121
  refresh_button = gr.Button("Refresh", variant="primary")
 
 
 
122
 
123
+ refresh_button.click(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box])
124
 
125
+ app.load(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box])
126
 
127
  # Auto-refresh every 30 seconds
128
+ app.load(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box], every=30)
129
 
130
  # Launch the app
131