Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,9 @@ import redis
|
|
| 10 |
import threading
|
| 11 |
import asyncio
|
| 12 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# config
|
| 15 |
# nest_asyncio.apply()
|
|
@@ -101,27 +104,62 @@ def run_alert_listener():
|
|
| 101 |
alert_thread = threading.Thread(target=run_alert_listener)
|
| 102 |
alert_thread.start()
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
# gradio interface
|
| 105 |
# Gradio interface
|
| 106 |
def get_latest_alert():
|
| 107 |
global latest_alert, alert_count
|
| 108 |
return latest_alert, f"Total Alerts: {alert_count}"
|
| 109 |
|
| 110 |
-
with gr.Blocks() as app:
|
| 111 |
-
gr.Markdown("# Alert Dashboard
|
| 112 |
|
| 113 |
with gr.Row():
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
-
refresh_button
|
| 118 |
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
|
|
|
| 122 |
|
| 123 |
-
|
| 124 |
-
app.load(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box], every=5)
|
| 125 |
|
| 126 |
# Launch the app
|
| 127 |
# if __name__ == "__main__":
|
|
|
|
| 10 |
import threading
|
| 11 |
import asyncio
|
| 12 |
import gradio as gr
|
| 13 |
+
import pandas as pd
|
| 14 |
+
import plotly.express as px
|
| 15 |
+
from datetime import datetime, timedelta
|
| 16 |
|
| 17 |
# config
|
| 18 |
# nest_asyncio.apply()
|
|
|
|
| 104 |
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 |
# gradio interface
|
| 138 |
# Gradio interface
|
| 139 |
def get_latest_alert():
|
| 140 |
global latest_alert, alert_count
|
| 141 |
return latest_alert, f"Total Alerts: {alert_count}"
|
| 142 |
|
| 143 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 144 |
+
gr.Markdown("# 🚨 Alert Dashboard 🚨")
|
| 145 |
|
| 146 |
with gr.Row():
|
| 147 |
+
with gr.Column():
|
| 148 |
+
latest_alert_box = gr.Textbox(label="Latest Alert", lines=3, interactive=False)
|
| 149 |
+
alert_count_box = gr.Textbox(label="Alert Count", interactive=False)
|
| 150 |
+
refresh_button = gr.Button("Refresh", variant="primary")
|
| 151 |
+
|
| 152 |
+
with gr.Row():
|
| 153 |
+
alert_plot = gr.Plot(label="Alerts Over Time")
|
| 154 |
|
| 155 |
+
refresh_button.click(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box, alert_plot])
|
| 156 |
|
| 157 |
+
app.load(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box, alert_plot])
|
| 158 |
|
| 159 |
+
# Auto-refresh every 30 seconds
|
| 160 |
+
app.load(get_latest_alert, inputs=None, outputs=[latest_alert_box, alert_count_box, alert_plot], every=30)
|
| 161 |
|
| 162 |
+
# Launch the app
|
|
|
|
| 163 |
|
| 164 |
# Launch the app
|
| 165 |
# if __name__ == "__main__":
|